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):
23 '[{"op":"remove","path":"/ietf-yang-library:modules-state/module/56"}]',
24 JsonDiffTool.from_path_to_jsonpatch(
25 "/ietf-yang-library:modules-state/module/56"
27 "Array element conversion failed!",
30 def testMapValueElementConversion(self):
32 '[{"op":"remove","path":"/ietf-yang-library:modules-state/module/blablah"}]',
33 JsonDiffTool.from_path_to_jsonpatch(
34 "/ietf-yang-library:modules-state/module/blablah"
36 "Array element conversion failed!",
40 class JsonDiffToolTest(unittest.TestCase):
42 Tests for the tool itself, including both command-line and RIDE keyword invokation style
45 def testSimpleDifferenceCountingWithoutFiltering(self):
51 JsonDiffTool.Json_Diff_Check_Keyword(
52 "testinput/arrayTwoNames.json",
53 "testinput/arrayTwoNamesCopy.json",
57 "failed! (expected 0 differences)",
60 def testEqualFilesWithScrambledArrayOrder(self):
62 This is moving an array element from one position to other. RFC 6902 describes this as "moving
63 a value", but this jsonpatch implementation constructs a patch using remove + add. Acceptable though
67 JsonDiffTool.Json_Diff_Check_Keyword(
68 "testinput/arrayTwoNames.json",
69 "testinput/arrayTwoNamesReversed.json",
73 "failed! (expected 2 differences)",
76 def testEqualFilesWithChangedAttributeOrder(self):
78 Attributes in different order. It's not a difference
82 JsonDiffTool.Json_Diff_Check_Keyword(
83 "testinput/setTwoNames.json",
84 "testinput/setTwoNamesReversed.json",
88 "failed! (expected 0 differences)",
91 def testSimpleDifferenceSecondFileWithExtraAttrib(self):
94 JsonDiffTool.Json_Diff_Check_Keyword(
95 "testinput/setTwoNames.json",
96 "testinput/setTwoNamesExtraAttrib.json",
100 "failed! (expected 1 differences)",
103 def testSimpleDifferenceCountingWithoutFiltering(self):
105 Example coming from a true daexim export. No prefilters used
109 "testinput/mainTestCase/odl_backup_operational_before.json",
111 "testinput/mainTestCase/odl_backup_operational_after.json",
113 sys.argv[1:] = input_argv
116 JsonDiffTool.Json_Diff_Check(),
117 "main failed! expected 16 differences, result was: "
118 + str(JsonDiffTool.Json_Diff_Check()),
121 def testSimpleDifferenceCountingUsingSingleMatchingBeforeFilter(self):
123 Using a prefilter for the initial file The prefilter contains one expression only
127 "testinput/mainTestCase/odl_backup_operational_before.json",
129 "testinput/mainTestCase/odl_backup_operational_after.json",
131 "testinput/mainTestCase/json_prefilter.conf",
134 sys.argv[1:] = input_argv
137 JsonDiffTool.Json_Diff_Check(),
138 "main failed! expected 15 differences, result was: "
139 + str(JsonDiffTool.Json_Diff_Check()),
142 def testSimpleDifferenceCountingUsingMatchingBeforeFilterMatchingTwoEntries(self):
144 Using a prefilter for the initial file The prefilter contains two expressions
148 "testinput/mainTestCase/odl_backup_operational_before.json",
150 "testinput/mainTestCase/odl_backup_operational_after.json",
152 "testinput/mainTestCase/json_prefilter_two_matches.conf",
155 sys.argv[1:] = input_argv
158 JsonDiffTool.Json_Diff_Check(),
159 "main failed! expected 14 differences, result was: "
160 + str(JsonDiffTool.Json_Diff_Check()),
163 def testSimpleDifferenceCountingUsingSingleMatchingBeforeFilter(self):
165 Using a prefilter for both initial and final files
169 "testinput/mainTestCase/odl_backup_operational_before.json",
171 "testinput/mainTestCase/odl_backup_operational_after.json",
173 "testinput/mainTestCase/json_prefilter.conf",
175 "testinput/mainTestCase/json_postfilter.conf",
178 sys.argv[1:] = input_argv
181 JsonDiffTool.Json_Diff_Check(),
182 "main failed! expected 16 differences, result was: "
183 + str(JsonDiffTool.Json_Diff_Check()),
186 def testUsingANonExistingFile(self):
188 The second file does not exist. Exception expected
192 JsonDiffTool.Json_Diff_Check_Keyword,
193 "testinput/arrayTwoNames.json",
194 "testinput/thisFileDoesNotExist.json",
199 def testNotPassingAMandatoryParameter(self):
201 Both initial and final json files are mandatory
203 input_argv = ["-f", "testinput/mainTestCase/odl_backup_operational_after.json"]
204 # parser = JsonDiffTool.parseArgs(input_argv)
206 with self.assertRaises(SystemExit) as cm:
207 JsonDiffTool.parse_args(input_argv)
209 # 2 for missing argument
210 self.assertEqual(cm.exception.code, 2)
212 def testUsingNotMatchingFilterExpressions(self):
214 Using prefilter files whose expressions match nothing
218 "testinput/mainTestCase/odl_backup_operational_before.json",
220 "testinput/mainTestCase/odl_backup_operational_after.json",
222 "testinput/mainTestCase/json_prefilter_zero_matches.conf",
224 "testinput/mainTestCase/json_prefilter_zero_matches.conf",
227 sys.argv[1:] = input_argv
230 JsonDiffTool.Json_Diff_Check(),
231 "main failed! expected 16 differences, result was: "
232 + str(JsonDiffTool.Json_Diff_Check()),
236 if __name__ == "__main__":