Belle II Software  release-08-01-10
json_serialize_test.py
1 #!/usr/bin/env python3
2 
3 
10 
11 """
12 Test serialization of JSON objects.
13 """
14 
15 import unittest
16 import json
17 import json_objects
18 
19 
20 # @cond internal_test
21 
22 
23 class TestJsonSerialize(unittest.TestCase):
24  """
25  Test serialization of JSON objects.
26 
27  This is usually done as follows:
28 
29  1. Create JSON class
30  2. Serialize
31  3. Deserialize
32  4. Check that extracted information is correct
33  """
34 
35  def test_serialize_comparison(self):
36 
37  revs = [
38  json_objects.Revision("run1", "iae223auiae", "git_hash1", ""),
39  json_objects.Revision("run2", "iuiaehuiaen", "git_hash1", ""),
40  json_objects.Revision("arun", "buie223aae", "git_hash1", ""),
41  ]
42 
43  cplot = json_objects.ComparisonPlot("plot_one")
44 
46  compared_revisions=revs,
47  plots=[cplot],
48  title="",
49  rootfile="",
50  package="",
51  )
52 
53  cp = json_objects.ComparisonPackage("pack1", [cfile])
54 
55  comp = json_objects.Comparison(revs, [cp])
56 
57  js = json_objects.dumps(comp)
58  js_decode = json.loads(js)
59 
60  self.assertEqual(1, len(js_decode["packages"]))
61  self.assertEqual("arun_run1_run2", js_decode["label"])
62  self.assertEqual("pack1", js_decode["packages"][0]["name"])
63 
64  def test_serialize_revision_nested(self):
65  rr1 = json_objects.Revision("label1", "date", "git_hash1", "black")
66  rr2 = json_objects.Revision("label2", "date", "git_hash2", "black")
67  rlist = json_objects.Revisions([rr1, rr2])
68 
69  js = json_objects.dumps(rlist)
70  js_decode = json.loads(js)
71 
72  self.assertEqual(2, len(js_decode["revisions"]))
73  self.assertEqual("label1", js_decode["revisions"][0]["label"])
74  self.assertEqual("label2", js_decode["revisions"][1]["label"])
75 
76  def test_comparison_plot_file(self):
78  title="title", package="package", rootfile="rootfile"
79  )
80  ccp2 = json_objects.ComparisonPlotFile("package", "title", "rootfile")
81 
82  self.assertEqual(ccp1.title, "title")
83  self.assertEqual(ccp1.package, "package")
84 
85  self.assertEqual(ccp2.title, "title")
86  self.assertEqual(ccp2.package, "package")
87 
88 
89 if __name__ == "__main__":
90  unittest.main()
91 
92 # @endcond
def dumps(obj)