[Piglit] [PATCH 2/4] framework/results.py: Fix Subtests class

Dylan Baker baker.dylan.c at gmail.com
Wed Sep 23 17:55:57 PDT 2015


Sub-classing built-in types in python is an error-prone leap, and many
corner cases came out to bit us in this instance. By wrapping a dict
inside of another class we can get the desired results, it means more
code, but it also means less corner cases.

Also change the equality test of two unit tests, since Subtests isn't a
dictionary anymore assert_dict_equal wont work anymore.

cc: Mark Janes <mark.a.janes at intel.com>
Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---

Mark, this appears to clear up most of our jenkins changes from my
series.

 framework/results.py                 | 32 +++++++++++++++++++++++++++-----
 framework/tests/piglit_test_tests.py |  3 +--
 framework/tests/results_tests.py     |  2 +-
 3 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/framework/results.py b/framework/results.py
index 61841b7..d358cca 100644
--- a/framework/results.py
+++ b/framework/results.py
@@ -34,9 +34,31 @@ __all__ = [
 ]
 
 
-class Subtests(dict):
+class Subtests(collections.MutableMapping):
+    """A dict-like object that stores Statuses as values."""
+    def __init__(self, dict_=None):
+        self.__container = {}
+
+        if dict_ is not None:
+            self.update(dict_)
+
     def __setitem__(self, name, value):
-        super(Subtests, self).__setitem__(name, status.status_lookup(value))
+        self.__container[name] = status.status_lookup(value)
+
+    def __getitem__(self, name):
+        return self.__container[name]
+
+    def __delitem__(self, name):
+        del self.__container[name]
+
+    def __iter__(self):
+        return iter(self.__container)
+
+    def __len__(self):
+        return len(self.__container)
+
+    def __repr__(self):
+        return repr(self.__container)
 
     def to_json(self):
         res = dict(self)
@@ -45,10 +67,10 @@ class Subtests(dict):
 
     @classmethod
     def from_dict(cls, dict_):
-        res = cls(dict_)
+        if '__type__' in dict_:
+            del dict_['__type__']
 
-        if '__type__' in res:
-            del res['__type__']
+        res = cls(dict_)
 
         return res
 
diff --git a/framework/tests/piglit_test_tests.py b/framework/tests/piglit_test_tests.py
index db4c8b0..c7c4c8f 100644
--- a/framework/tests/piglit_test_tests.py
+++ b/framework/tests/piglit_test_tests.py
@@ -72,8 +72,7 @@ def test_piglitest_no_clobber():
     test.result.returncode = 0
     test.interpret_result()
 
-    nt.assert_dict_equal(test.result.subtests,
-                         {'test1': 'pass', 'test2': 'pass'})
+    nt.eq_(test.result.subtests, {'test1': 'pass', 'test2': 'pass'})
 
 
 def test_piglittest_command_getter_serial():
diff --git a/framework/tests/results_tests.py b/framework/tests/results_tests.py
index ab5f0d3..a28d78b 100644
--- a/framework/tests/results_tests.py
+++ b/framework/tests/results_tests.py
@@ -68,7 +68,7 @@ def test_Subtests_to_json():
     test['foo'] = status.PASS
     test['bar'] = status.CRASH
 
-    nt.assert_dict_equal(baseline, test.to_json())
+    nt.eq_(baseline, test.to_json())
 
 
 def test_Subtests_from_dict():
-- 
2.5.3



More information about the Piglit mailing list