def test_queries_has_correct_number_of_elements(self): """Generator returned by queries() should have correct no. elements.""" cf1 = CypherFile(self.three_query_explicit_cypher_file_name) self.assertEqual(len(cf1.queries), 3) cf2 = CypherFile(self.two_query_param_cypher_file_name) self.assertEqual(len(cf2.queries), 2) cf3 = CypherFile(self.two_query_partial_param_cypher_file_name) self.assertEqual(len(cf3.queries), 2)
def test_queries_is_a_tuple(self): """CypherFile.queries should be a tuple of CypherQuery objects.""" cf1 = CypherFile(self.three_query_explicit_cypher_file_name) self.assertIsInstance(cf1.queries, tuple) cf2 = CypherFile(self.two_query_param_cypher_file_name) self.assertIsInstance(cf2.queries, tuple) cf3 = CypherFile(self.two_query_partial_param_cypher_file_name) self.assertIsInstance(cf3.queries, tuple)
def test_priority_is_retrievable(self): """CypherFile.priority should be retrieved from file parameters. If not specified, priority assumed to be 0. """ cf1 = CypherFile(self.one_query_param_cypher_file_w_priority_name) self.assertEqual(cf1.priority, 2) cf2 = CypherFile(self.three_query_explicit_cypher_file_name) self.assertEqual(cf2.priority, 0) cf3 = CypherFile(self.two_query_partial_param_cypher_file_name) self.assertEqual(cf3.priority, 0)
def test_params_specifications_allowed(self): """Confirm various allowed parameter specifications register.""" f1_name = path.join(self.test_dir, "queries1.cql") with open(f1_name, "w") as f: f.write('{ "priority": 1, "other_param": "some value" }\n' + "MATCH (n) RETURN n;") cf = CypherFile(f1_name) self.assertEqual(cf.priority, 1)
def test_partially_param_statement_file_has_expected_params(self): """Partially parameterised statement file has expected params.""" cf = CypherFile(self.two_query_partial_param_cypher_file_name) self.assertEqual(cf.queries[0].params, {"name1": "Sue"}) self.assertEqual( cf.queries[1].params, {"name2": None}, "No relevant parameters specified in file", )
def test_extant_but_empty_file_gives_warning(self): """If a Cypher file doesn't contain any queries it should warn user.""" empty_fname = os.path.join(self.test_dir, "empty.cql") touch(empty_fname) with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger the warning CypherFile(empty_fname) assert len(w) == 1 assert issubclass(w[-1].category, UserWarning) assert "No queries found in " + empty_fname == str(w[-1].message)
def test_partially_param_statement_file_has_expected_queries(self): """Partially parameterised statement file has expected queries.""" cf = CypherFile(self.two_query_partial_param_cypher_file_name) self.assertEqual( cf.queries[0].statement, 'MERGE (n1:TestNode {role:"influencer", name:$name1})' + "-[:INFLUENCES]-> " # newline replaced with space + '(n2:TestNode {name:"Billy", role:"follower"});', ) self.assertEqual( cf.queries[1].statement, 'MATCH (n:TestNode {role:"influencer"}) ' + 'MERGE (n)-[:INFLUENCES]->(:TestNode {role:"follower", ' + "name:$name2});", )
def test_explicit_statement_file_has_expected_queries(self): """Cypher file with three explicit statements has expected queries.""" cf = CypherFile(self.three_query_explicit_cypher_file_name) self.assertEqual( cf.queries[0].statement, 'MERGE (n1:TestNode {role:"influencer", name:"Sue"})' + "-[:INFLUENCES]-> " + '(n2:TestNode {name:"Billy", role:"follower"});', # newlines replaced with spaces ) self.assertEqual( cf.queries[1].statement, 'MATCH (n:TestNode {role:"influencer"}) ' + 'MERGE (n)-[:INFLUENCES]->(:TestNode {role:"follower", ' + 'name:"Sarah"});', ) self.assertEqual(cf.queries[2].statement, 'MERGE (n:TestNode {role:"loner", name:"Tim"});')
def test_file_with_no_explicit_parameters_works(self): """Okay to have no params specified but require global params.""" cf = CypherFile(self.one_query_req_global_param_cypher_file_name) self.assertEqual(cf.queries[0].params, {"paramval": None})
def test_parameterised_statement_file_has_expected_params(self): """Cypher file with two param statements has expected params.""" cf = CypherFile(self.two_query_param_cypher_file_name) self.assertEqual(cf.queries[0].params, {"name1": "Sue"}) self.assertEqual(cf.queries[1].params, {"name2": "Sarah"})
def test_explicit_statement_file_has_expected_params(self): """Cypher file with three explicit statements has no params.""" cf = CypherFile(self.three_query_explicit_cypher_file_name) self.assertEqual(cf.queries[0].params, {}) self.assertEqual(cf.queries[1].params, {}) self.assertEqual(cf.queries[2].params, {})