|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import sys |
4 | 5 | import csv |
5 | 6 | import redis |
6 | 7 | import unittest |
@@ -584,6 +585,81 @@ def test13_id_namespaces(self): |
584 | 585 | [1, 'Filipe', 'User', 1, 40, 'Post']] |
585 | 586 | self.assertEqual(query_result.result_set, expected_result) |
586 | 587 |
|
| 588 | + def test14_array_properties_inferred(self): |
| 589 | + """Validate that array properties are correctly inserted.""" |
| 590 | + |
| 591 | + graphname = "arr_graph" |
| 592 | + with open('/tmp/nodes.tmp', mode='w') as csv_file: |
| 593 | + out = csv.writer(csv_file, delimiter='|') |
| 594 | + out.writerow(['str_col', 'arr_col']) |
| 595 | + out.writerow(['str1', """[1, 0.2, 'nested_str', False]"""]) |
| 596 | + out.writerow(['str2', """['prop1', ['nested_1', 'nested_2'], 5]"""]) |
| 597 | + |
| 598 | + runner = CliRunner() |
| 599 | + res = runner.invoke(bulk_insert, ['--nodes', '/tmp/nodes.tmp', |
| 600 | + '--separator', '|', |
| 601 | + graphname], catch_exceptions=False) |
| 602 | + |
| 603 | + self.assertEqual(res.exit_code, 0) |
| 604 | + self.assertIn('2 nodes created', res.output) |
| 605 | + |
| 606 | + graph = Graph(graphname, self.redis_con) |
| 607 | + query_result = graph.query('MATCH (a) RETURN a ORDER BY a.str_col') |
| 608 | + |
| 609 | + node_1 = {'str_col': 'str1', 'arr_col': [1, 0.2, 'nested_str', False]} |
| 610 | + node_2 = {'str_col': 'str2', 'arr_col': ['prop1', ['nested_1', 'nested_2'], 5]} |
| 611 | + self.assertEqual(query_result.result_set[0][0].properties, node_1) |
| 612 | + self.assertEqual(query_result.result_set[1][0].properties, node_2) |
| 613 | + |
| 614 | + def test15_array_properties_schema_enforced(self): |
| 615 | + """Validate that array properties are correctly inserted with an enforced schema.""" |
| 616 | + |
| 617 | + graphname = "arr_graph_with_schema" |
| 618 | + with open('/tmp/nodes.tmp', mode='w') as csv_file: |
| 619 | + out = csv.writer(csv_file, delimiter='|') |
| 620 | + out.writerow(['str_col:STRING', 'arr_col:ARRAY']) |
| 621 | + out.writerow(['str1', """[1, 0.2, 'nested_str', False]"""]) |
| 622 | + out.writerow(['str2', """['prop1', ['nested_1', 'nested_2'], 5]"""]) |
| 623 | + |
| 624 | + runner = CliRunner() |
| 625 | + res = runner.invoke(bulk_insert, ['--nodes', '/tmp/nodes.tmp', |
| 626 | + '--separator', '|', |
| 627 | + '--enforce-schema', |
| 628 | + graphname], catch_exceptions=False) |
| 629 | + |
| 630 | + self.assertEqual(res.exit_code, 0) |
| 631 | + self.assertIn('2 nodes created', res.output) |
| 632 | + |
| 633 | + graph = Graph(graphname, self.redis_con) |
| 634 | + query_result = graph.query('MATCH (a) RETURN a ORDER BY a.str_col') |
| 635 | + |
| 636 | + node_1 = {'str_col': 'str1', 'arr_col': [1, 0.2, 'nested_str', False]} |
| 637 | + node_2 = {'str_col': 'str2', 'arr_col': ['prop1', ['nested_1', 'nested_2'], 5]} |
| 638 | + self.assertEqual(query_result.result_set[0][0].properties, node_1) |
| 639 | + self.assertEqual(query_result.result_set[1][0].properties, node_2) |
| 640 | + |
| 641 | + def test16_error_on_schema_failure(self): |
| 642 | + """Validate that the loader errors on processing non-conformant CSVs with an enforced schema.""" |
| 643 | + |
| 644 | + graphname = "schema_error" |
| 645 | + with open('/tmp/nodes.tmp', mode='w') as csv_file: |
| 646 | + out = csv.writer(csv_file, delimiter='|') |
| 647 | + out.writerow(['str_col:STRING', 'arr_col:ARRAY']) |
| 648 | + out.writerow(['str1', """[1, 0.2, 'nested_str', False]"""]) |
| 649 | + out.writerow(['str2', 'strval']) |
| 650 | + |
| 651 | + try: |
| 652 | + runner = CliRunner() |
| 653 | + runner.invoke(bulk_insert, ['--nodes', '/tmp/nodes.tmp', |
| 654 | + '--separator', '|', |
| 655 | + '--enforce-schema', |
| 656 | + graphname], catch_exceptions=False) |
| 657 | + self.fail() # Should be unreachable |
| 658 | + except Exception as e: |
| 659 | + # Verify that the correct exception is raised. |
| 660 | + self.assertEqual(sys.exc_info()[0].__name__, 'SchemaError') |
| 661 | + self.assertIn("Could not parse 'strval' as an array", e.args) |
| 662 | + |
587 | 663 |
|
588 | 664 | if __name__ == '__main__': |
589 | 665 | unittest.main() |
0 commit comments