[Piglit] [PATCH 02/26] framework/results.py: Add a to_json method to the TestrunResults

Dylan Baker baker.dylan.c at gmail.com
Fri Sep 11 15:55:28 PDT 2015


The piglit_encoder knows how to convert an object with a to_json method
into it's json representation. By using this approach we can add special
hints to the json for converting it back into it's python object
representation, which can also be removed easily by using a from*
method.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/backends/json.py       |  5 +---
 framework/results.py             | 13 ++++++++++
 framework/tests/results_tests.py | 53 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 4 deletions(-)

diff --git a/framework/backends/json.py b/framework/backends/json.py
index e88c9c8..fc816f7 100644
--- a/framework/backends/json.py
+++ b/framework/backends/json.py
@@ -326,10 +326,7 @@ def _update_results(results, filepath):
 def _write(results, file_):
     """WRite the values of the results out to a file."""
     with write_compressed(file_) as f:
-        json.dump({k: v for k, v in results.__dict__.iteritems()},
-                  f,
-                  default=piglit_encoder,
-                  indent=INDENT)
+        json.dump(results, f, default=piglit_encoder, indent=INDENT)
 
 
 def _update_zero_to_one(result):
diff --git a/framework/results.py b/framework/results.py
index 7bfc2fd..dd7fdc0 100644
--- a/framework/results.py
+++ b/framework/results.py
@@ -24,6 +24,7 @@
 from __future__ import print_function, absolute_import
 
 import collections
+import copy
 
 from framework import status, exceptions, grouptools
 
@@ -197,6 +198,12 @@ class Totals(dict):
                 return True
         return False
 
+    def to_json(self):
+        """Convert totals to a json object."""
+        result = copy.copy(self)
+        result['__type__'] = 'Totals'
+        return result
+
 
 class TestrunResult(object):
     """The result of a single piglit run."""
@@ -233,3 +240,9 @@ class TestrunResult(object):
                     self.totals[name][res] += 1
                 self.totals['root'][res] += 1
 
+    def to_json(self):
+        if not self.totals:
+            self.calculate_group_totals()
+        rep = copy.copy(self.__dict__)
+        rep['__type__'] = 'TestrunResult'
+        return rep
diff --git a/framework/tests/results_tests.py b/framework/tests/results_tests.py
index d2c4206..9a4b358 100644
--- a/framework/tests/results_tests.py
+++ b/framework/tests/results_tests.py
@@ -445,3 +445,56 @@ def test_totals_true():
         test = results.Totals()
         test[key] += 1
         nt.ok_(bool(test), msg='Returns false with status {}'.format(key))
+
+
+class TestTestrunResultToJson(object):
+    """results.TestrunResult.to_json: returns expected values"""
+    @classmethod
+    def setup_class(cls):
+        test = results.TestrunResult()
+        test.name = 'name'
+        test.uname = 'this is uname'
+        test.options = {'some': 'option'}
+        test.glxinfo = 'glxinfo'
+        test.wglinfo = 'wglinfo'
+        test.lspci = 'this is lspci'
+        test.time_elapsed = 1.23
+        test.tests = {'a test': results.TestResult('pass')}
+
+        cls.test = test.to_json()
+
+    def test_name(self):
+        """results.TestrunResult.to_json: name is properly encoded"""
+        nt.eq_(self.test['name'], 'name')
+
+    def test_uname(self):
+        """results.TestrunResult.to_json: uname is properly encoded"""
+        nt.eq_(self.test['uname'], 'this is uname')
+
+    def test_options(self):
+        """results.TestrunResult.to_json: options is properly encoded"""
+        nt.assert_dict_equal(self.test['options'], {'some': 'option'})
+
+    def test_glxinfo(self):
+        """results.TestrunResult.to_json: glxinfo is properly encoded"""
+        nt.eq_(self.test['glxinfo'], 'glxinfo')
+
+    def test_wglinfo(self):
+        """results.TestrunResult.to_json: wglinfo is properly encoded"""
+        nt.eq_(self.test['wglinfo'], 'wglinfo')
+
+    def test_lspci(self):
+        """results.TestrunResult.to_json: lspci is properly encoded"""
+        nt.eq_(self.test['lspci'], 'this is lspci')
+
+    def test_time(self):
+        """results.TestrunResult.to_json: time_elapsed is properly encoded"""
+        nt.eq_(self.test['time_elapsed'], 1.23)
+
+    def test_tests(self):
+        """results.TestrunResult.to_json: tests is properly encoded"""
+        nt.eq_(self.test['tests']['a test'].result, 'pass')
+
+    def test_type(self):
+        """results.TestrunResult.to_json: __type__ is added"""
+        nt.eq_(self.test['__type__'], 'TestrunResult')
-- 
2.5.1



More information about the Piglit mailing list