def test_parse_two_packets(self): xp = XSensParser(None) data = bytes.fromhex( "FA FF 36 53 10 20 02 89 90 10 60 04 00 26 C7 33 40 20 0C 3D 0A 5D 4A BD 28 44 1C 41 1D " "09 AF 40 10 0C 38 B0 80 00 B8 D7 28 00 3C C9 02 28 80 20 0C BA E5 4C 02 BB 9A 3F 83 BA " "D8 04 03 80 30 10 3F 80 00 01 B6 12 C0 01 B6 C5 70 00 B6 0A 40 00 E0 20 04 00 00 00 05 " "F4" "FA FF 36 53 10 20 02 89 90 10 60 04 00 26 C7 33 40 20 0C 3D 0A 5D 4A BD 28 44 1C 41 1D " "09 AF 40 10 0C 38 B0 80 00 B8 D7 28 00 3C C9 02 28 80 20 0C BA E5 4C 02 BB 9A 3F 83 BA " "D8 04 03 80 30 10 3F 80 00 01 B6 12 C0 01 B6 C5 70 00 B6 0A 40 00 E0 20 04 00 00 00 05 " "F4") xp.parse(data) packet1 = xp.get() packet2 = xp.get() errors = object_hierarchy_equals(packet1, self.correct_single_packet) # print("\n".join(errors)) self.assertEqual(len(errors), 0) errors = object_hierarchy_equals(packet2, self.correct_single_packet) # print("\n".join(errors)) self.assertEqual(len(errors), 0)
def test_dump_load(self): for (serializer_name, serializer), \ (extension, extension_serializer), \ is_gzipped,\ is_gzipped_default in product(self.serializers.items(), self.file_extensions.items(), [True, False], [True, False]): with self.subTest(serializer=serializer_name, extension=extension, is_gzipped=is_gzipped, is_gzipped_default=is_gzipped_default): file_path = self.flex_file_temp / f"data.i{extension}{'.gz' if is_gzipped else ''}" if extension == ".unknown": is_actually_gzipped = is_gzipped or is_gzipped_default else: is_actually_gzipped = is_gzipped flex_dump(self.test_data, file_path, default_serializer=serializer, default_is_gzipped=is_gzipped_default) loaded_in_data = flex_load( file_path, default_serializer=serializer, default_is_gzipped=is_gzipped_default) det_serializer, det_is_gzipped, det_is_binary = \ determine_serialization(file_path, default_serializer=serializer, default_is_gzipped=is_gzipped_default) self.assertEqual(det_is_gzipped, is_actually_gzipped) errors = object_hierarchy_equals(loaded_in_data, self.test_data) print(yaml.dump(errors, default_flow_style=False)) self.assertEqual(len(errors), 0) rm(file_path)
def test_equals_lists(self): """ Simple tests that checks two lists with elements of different numerical types. """ l1 = [0, 1., 2., 3, 4, Decimal(5)] l2 = [0, 1, 2., (3j*-1j), Decimal(4), (5+0j)] errors = object_hierarchy_equals(l1, l2) self.assertEqual(len(errors), 0)
def test_node_from_path_integer_keys(self): root = TreeNode() child1 = TreeNode(key="foo", parent=root) child2 = TreeNode(value=333, key=0, parent=child1) child3 = TreeNode(value=333, key=1, parent=child1) child4 = TreeNode(value=333, key=2, parent=child1) correct_tree = {'foo': [333, 333, 333]} errors = object_hierarchy_equals(root.object_hierarchy(), correct_tree) self.assertEqual(len(errors), 0)
def test_equals_dictionary(self): """ Test that checks two trees with dictionaries as the root element is checked correctly. No errors should be found during this test. """ d1 = { "copper": "explain", "truck": 2, "neat": [0, 1, 2., (3j*-1j), Decimal(4)], "unite": { "branch": "educated", "tenuous": 4., "hum": {7, 8, 9}, "decisive": np.array([[7, 8, 9], [4, 5, 6], [1, 2, 3]]) }, "notice": [ "cattle", 2., 5.4j, {"a", "b", "c", "c"}, { "team": "sneeze", "warn": "deadpan", "table": np.array([1, 2, 3, 4]) } ] } d2 = { "copper": "explain", "truck": 2., "neat": [0, 1., 2, Decimal(3), 4], "unite": { "branch": "educated", "tenuous": 4, "hum": {7, 8, 9, 8}, "decisive": np.array([[7., 8., 9.], [4, 5, 6], [1, 2, 3]]) }, "notice": [ "cattle", 2., 5.4j, {"a", "b", "c"}, { "team": "sneeze", "warn": "deadpan", "table": [1, 2, 3, 4] } ] } errors = object_hierarchy_equals(d1, d2) # print("\n".join(errors)) self.assertEqual(len(errors), 0)
def test_single_packet_file_like(self): bytes_io = BytesIO( bytes.fromhex( "FA FF 36 53 10 20 02 89 90 10 60 04 00 26 C7 33 40 20 0C 3D 0A 5D 4A BD 28 44 1C 41 1D " "09 AF 40 10 0C 38 B0 80 00 B8 D7 28 00 3C C9 02 28 80 20 0C BA E5 4C 02 BB 9A 3F 83 BA " "D8 04 03 80 30 10 3F 80 00 01 B6 12 C0 01 B6 C5 70 00 B6 0A 40 00 E0 20 04 00 00 00 05 " "F4")) xp = XSensParser(bytes_io) xp.start() packet = xp.get() errors = object_hierarchy_equals(packet, self.correct_single_packet) # print("\n".join(errors)) self.assertEqual(len(errors), 0)
def test_object_hierarchy(self): root = TreeNode(value=111) child1 = TreeNode(key="foo", parent=root) child2 = TreeNode(value=111, key=0, parent=child1) child3 = TreeNode(value=222, key=1, parent=child1) child4 = TreeNode(key=2, parent=child1) child5 = TreeNode(key="bar", parent=child4) child6 = TreeNode(key="quux", parent=child5) child7 = TreeNode(value=333, key="quuz", parent=child6) correct_tree = {'foo': [111, 222, {'bar': {'quux': {'quuz': 333}}}]} errors = object_hierarchy_equals(root.object_hierarchy(), correct_tree) self.assertEqual(len(errors), 0)
def test_file(self): with open(test_dir_path / "data_files" / "xsens_raw_data.bin", "rb") as f: xp = XSensParser(f) xp.start() tables = xp.get_tables() # Uncomment this line to update the tables in the data file. # File(test_dir_path / "data_files" / "correct_xsens_tables.msgp.gz").dump(tables) correct_tables = flex_load(test_dir_path / "data_files" / "correct_xsens_tables.msgp.gz") errors = object_hierarchy_equals(tables, correct_tables) # print("\n".join(errors)) self.assertEqual(len(errors), 0)
def test_from_path_dict(self): path_value_pairs = [ (PurePosixPath("hello/world"), "!!!"), (PurePosixPath("foo/[0]"), "spam"), (PurePosixPath("/foo/[2]"), "eggs"), (PurePosixPath("foo/[1]"), "ham"), ] root = TreeNode.from_dict(dict(path_value_pairs)) correct_tree = { 'foo': ['spam', 'ham', 'eggs'], 'hello': { 'world': '!!!' } } errors = object_hierarchy_equals(root.object_hierarchy(), correct_tree) self.assertEqual(len(errors), 0)
def test_tables_to_object_hierarchy_find_ndarrays(self): data = flatten_tables({ 0: {'foo': 0}, 2: {'bar': [1, 2]}, 3: {'baz': np.array([3, 4, 5]), 'ham.qux': np.array([6, 7, 8]), 'spam.[0]': [1, 2, 3], 'spam.[1]': [4, 5, 6], 'spam.[2]': [7, 8, 9], 'quux_0_0': np.array([9, 11, 14, 17]), 'quux_1_0': np.array([10, 12, 15, 18]), 'quux_2_0': np.array([11, 13, 16, 19]) }, "eggs": {'ham.quux_0_0': np.array([9, 11, 14, 17]), 'ham.quux_1_0': np.array([10, 12, 15, 18]), 'ham.quux_2_0': np.array([11, 13, 16, 19]) }}) correct_result = { PurePosixPath('eggs/ham/quux'): [ (PurePosixPath('eggs/ham/quux_0_0'), (0, 0)), (PurePosixPath('eggs/ham/quux_1_0'), (1, 0)), (PurePosixPath('eggs/ham/quux_2_0'), (2, 0))], PurePosixPath('quux'): [ (PurePosixPath('quux_0_0'), (0, 0)), (PurePosixPath('quux_1_0'), (1, 0)), (PurePosixPath('quux_2_0'), (2, 0))]} results = {} for path, group in find_ndarrays(data): results[path] = [] for data in group: results[path].append(data) errors = object_hierarchy_equals(results, correct_result) self.assertEqual(len(errors), 0)
def test_process_ndarrays(self): data = flatten_tables({ 0: {'foo': 0}, 2: {'bar': [1, 2]}, 3: {'baz': np.array([3, 4, 5]), 'ham.qux': np.array([6, 7, 8]), 'spam.[0]': [1, 2, 3], 'spam.[1]': [4, 5, 6], 'spam.[2]': [7, 8, 9], 'quux_0_0': np.array([9, 11, 14, 17]), 'quux_1_0': np.array([10, 12, 15, 18]), 'quux_2_0': np.array([11, 13, 16, 19]) }, "eggs": {'ham.quux_0_0': np.array([9, 11, 14, 17]), 'ham.quux_1_0': np.array([10, 12, 15, 18]), 'ham.quux_2_0': np.array([11, 13, 16, 19]) }}) process_ndarrays(data) correct = { PurePosixPath('bar'): [1, 2], PurePosixPath('baz'): np.array([3, 4, 5]), PurePosixPath('eggs/ham/quux'): np.array([[[9.], [10.], [11.]], [[11.], [12.], [13.]], [[14.], [15.], [16.]], [[17.], [18.], [19.]]]), PurePosixPath('foo'): 0, PurePosixPath('ham/qux'): np.array([6, 7, 8]), PurePosixPath('quux'): np.array([[[9.], [10.], [11.]], [[11.], [12.], [13.]], [[14.], [15.], [16.]], [[17.], [18.], [19.]]]), PurePosixPath('spam/[0]'): [1, 2, 3], PurePosixPath('spam/[1]'): [4, 5, 6], PurePosixPath('spam/[2]'): [7, 8, 9]} errors = object_hierarchy_equals(data, correct) self.assertEqual(len(errors), 0)
def test_object_hierarchy_to_tables(self): """ Test to see if the object_hierarchy_to_tables works properly. """ inp = { "foo": 0, "bar": [1, 2], "baz": np.array([3, 4, 5]), "ham": { "qux": np.array([6, 7, 8]), "quux": np.array([[[9], [10], [11]], [[11], [12], [13]], [[14], [15], [16]], [[17], [18], [19]]]), }, "spam": [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] } correct = { 0: {'foo': 0}, 2: {'bar': [1, 2]}, 3: {'baz': np.array([3, 4, 5]), 'ham.qux': np.array([6, 7, 8]), 'spam.[0]': [1, 2, 3], 'spam.[1]': [4, 5, 6], 'spam.[2]': [7, 8, 9]}, 4: {'ham.quux_0_0': np.array([9, 11, 14, 17]), 'ham.quux_1_0': np.array([10, 12, 15, 18]), 'ham.quux_2_0': np.array([11, 13, 16, 19])}} out = object_hierarchy_to_tables(inp) # pprint(out) errors = object_hierarchy_equals(out, correct) self.assertEqual(len(errors), 0)
def test_equals_dictionary_failure(self): """ Test that checks two trees with dictionaries as the root element is checked correctly. """ d1 = { "copper": "limit", "truck": 3, "neat": [0, 0, 2., (3j*1j), Decimal(4)], "unite": { "branch": "educated", "tenuous": 4.2, "hum": {7, 8, 9, 10}, "decisive": np.array([[7, 8, 9.1], [4, 5, 6], [1, 2.4, 3]]) }, "automatic": [ "cattle", 2., 5.4j, {"a", "b", "c"}, { "team": "sneeze", "warn": "deadpan", "table": np.array([1, 2, 3, 4]) } ] } d2 = { "copper": "explain", "truck": 2., "neat": [0, 1., 2, Decimal(3), 4], "unite": { "branch": "educated", "tenuous": 4, "hum": {7, 8, 9, 8}, "decisive": [[7., 8., 9.], [4, 5, 6], [1, 2, 3]] }, "notice": [ "cattle", 2., 5.4j, {"a", "b", "c"}, { "team": "sneeze", "warn": "deadpan", "table": [1, 2, 3, 4] } ] } errors = object_hierarchy_equals(d1, d2) expected_errors = [ "Missing element '/notice' in the first object hierarchy.", "Value mismatch at '/copper'", "Value mismatch at '/truck'", "Value mismatch at '/neat[1]'", "Value mismatch at '/neat[3]'", "Value mismatch at '/unite/tenuous'", "Value mismatch at '/unite/hum'", "Value mismatch at '/unite/decisive[0][2]'", "Value mismatch at '/unite/decisive[2][1]'", "Missing element '/automatic' in the second object hierarchy." ] # print("\n".join(errors)) self.assertEqual(len(errors), len(expected_errors)) for expected_error in expected_errors: with self.subTest(expected_error=expected_error): self.assertIn(expected_error, errors)
def test_equals_scalars_failure(self): """""" errors = object_hierarchy_equals(1, 2) self.assertEqual(len(errors), 1) self.assertEqual(errors[0], "Value mismatch at '/'")