6 Unit tests for the Json Diff Tool library
10 __author__ = "Diego Granados"
11 __copyright__ = "Copyright(c) 2017, Ericsson."
12 __license__ = "New-style BSD"
13 __email__ = "diego.jesus.granados.lopez@ericsson.com"
16 class PathConversionTest(unittest.TestCase):
18 Conversions from jsonpath paths to jsonpatch patches used to remove the element pointed by the path
21 def testArrayElementConversion(self):
22 self.assertEquals('[{"op":"remove","path":"/ietf-yang-library:modules-state/module/56"}]',
23 JsonDiffTool.from_path_to_jsonpatch('/ietf-yang-library:modules-state/module/56'),
24 "Array element conversion failed!")
26 def testMapValueElementConversion(self):
27 self.assertEquals('[{"op":"remove","path":"/ietf-yang-library:modules-state/module/blablah"}]',
28 JsonDiffTool.from_path_to_jsonpatch('/ietf-yang-library:modules-state/module/blablah'),
29 "Array element conversion failed!")
32 class JsonDiffToolTest(unittest.TestCase):
34 Tests for the tool itself, including both command-line and RIDE keyword invokation style
37 def testSimpleDifferenceCountingWithoutFiltering(self):
42 JsonDiffTool.Json_Diff_Check_Keyword('testinput/arrayTwoNames.json',
43 'testinput/arrayTwoNamesCopy.json',
46 'failed! (expected 0 differences)')
48 def testEqualFilesWithScrambledArrayOrder(self):
50 This is moving an array element from one position to other. RFC 6902 describes this as "moving
51 a value", but this jsonpatch implementation constructs a patch using remove + add. Acceptable though
54 JsonDiffTool.Json_Diff_Check_Keyword('testinput/arrayTwoNames.json',
55 'testinput/arrayTwoNamesReversed.json',
58 'failed! (expected 2 differences)')
60 def testEqualFilesWithChangedAttributeOrder(self):
62 Attributes in different order. It's not a difference
65 JsonDiffTool.Json_Diff_Check_Keyword('testinput/setTwoNames.json',
66 'testinput/setTwoNamesReversed.json',
69 'failed! (expected 0 differences)')
71 def testSimpleDifferenceSecondFileWithExtraAttrib(self):
73 JsonDiffTool.Json_Diff_Check_Keyword('testinput/setTwoNames.json',
74 'testinput/setTwoNamesExtraAttrib.json',
77 'failed! (expected 1 differences)')
79 def testSimpleDifferenceCountingWithoutFiltering(self):
81 Example coming from a true daexim export. No prefilters used
83 input_argv = ['-i', 'testinput/mainTestCase/odl_backup_operational_before.json',
84 '-f', 'testinput/mainTestCase/odl_backup_operational_after.json']
85 sys.argv[1:] = input_argv
87 JsonDiffTool.Json_Diff_Check(),
88 "main failed! expected 16 differences, result was: " + str(JsonDiffTool.Json_Diff_Check()))
90 def testSimpleDifferenceCountingUsingSingleMatchingBeforeFilter(self):
92 Using a prefilter for the initial file The prefilter contains one expression only
94 input_argv = ['-i', 'testinput/mainTestCase/odl_backup_operational_before.json',
95 '-f', 'testinput/mainTestCase/odl_backup_operational_after.json',
96 '-ipf', 'testinput/mainTestCase/json_prefilter.conf', '-v']
97 sys.argv[1:] = input_argv
99 JsonDiffTool.Json_Diff_Check(),
100 "main failed! expected 15 differences, result was: " + str(JsonDiffTool.Json_Diff_Check()))
102 def testSimpleDifferenceCountingUsingMatchingBeforeFilterMatchingTwoEntries(self):
104 Using a prefilter for the initial file The prefilter contains two expressions
106 input_argv = ['-i', 'testinput/mainTestCase/odl_backup_operational_before.json',
107 '-f', 'testinput/mainTestCase/odl_backup_operational_after.json',
108 '-ipf', 'testinput/mainTestCase/json_prefilter_two_matches.conf', '-v']
109 sys.argv[1:] = input_argv
110 self.assertEquals(14,
111 JsonDiffTool.Json_Diff_Check(),
112 "main failed! expected 14 differences, result was: " + str(JsonDiffTool.Json_Diff_Check()))
114 def testSimpleDifferenceCountingUsingSingleMatchingBeforeFilter(self):
116 Using a prefilter for both initial and final files
118 input_argv = ['-i', 'testinput/mainTestCase/odl_backup_operational_before.json',
119 '-f', 'testinput/mainTestCase/odl_backup_operational_after.json',
120 '-ipf', 'testinput/mainTestCase/json_prefilter.conf',
121 '-fpf', 'testinput/mainTestCase/json_postfilter.conf',
123 sys.argv[1:] = input_argv
124 self.assertEquals(16,
125 JsonDiffTool.Json_Diff_Check(),
126 "main failed! expected 16 differences, result was: " + str(JsonDiffTool.Json_Diff_Check()))
128 def testUsingANonExistingFile(self):
130 The second file does not exist. Exception expected
132 self.assertRaises(IOError,
133 JsonDiffTool.Json_Diff_Check_Keyword,
134 'testinput/arrayTwoNames.json',
135 'testinput/thisFileDoesNotExist.json',
139 def testNotPassingAMandatoryParameter(self):
141 Both initial and final json files are mandatory
143 input_argv = ['-f', 'testinput/mainTestCase/odl_backup_operational_after.json']
144 # parser = JsonDiffTool.parseArgs(input_argv)
146 with self.assertRaises(SystemExit) as cm:
147 JsonDiffTool.parse_args(input_argv)
149 # 2 for missing argument
150 self.assertEqual(cm.exception.code, 2)
152 def testUsingNotMatchingFilterExpressions(self):
154 Using prefilter files whose expressions match nothing
156 input_argv = ['-i', 'testinput/mainTestCase/odl_backup_operational_before.json',
157 '-f', 'testinput/mainTestCase/odl_backup_operational_after.json',
158 '-ipf', 'testinput/mainTestCase/json_prefilter_zero_matches.conf',
159 '-fpf', 'testinput/mainTestCase/json_prefilter_zero_matches.conf',
161 sys.argv[1:] = input_argv
162 self.assertEquals(16,
163 JsonDiffTool.Json_Diff_Check(),
164 "main failed! expected 16 differences, result was: " + str(
165 JsonDiffTool.Json_Diff_Check()))
168 if __name__ == "__main__":