def test_ref_type(self): """CypherQuerySource.ref_type returns source type (cypher or tabular).""" s1 = CypherQuerySource("queries.cql", "cypher", 10) self.assertEqual(s1.ref_type, "cypher") s2 = CypherQuerySource(self.demo_table, "tabular", 2) self.assertEqual(s2.ref_type, "tabular")
def test_repr(self): """__repr__ should be as expected.""" s1 = CypherQuerySource("queries.cql", "cypher", 10) self.assertEqual(str(s1), "ref_type: cypher\nindex: 10\nref: queries.cql") s2 = CypherQuerySource(self.demo_table, "tabular", 2) self.assertEqual( str(s2), "ref_type: tabular\nindex: 2\nref:" " start end cond\n0 state1 state2 low\n" "1 state2 state3 high", )
def test_source(self): """CypherQuery.source should return CypherQuerySource object.""" q = CypherQuery( "MATCH (n) RETURN n LIMIT 10;", source=CypherQuerySource("queries.cql", "cypher", 10), ) self.assertIsInstance(q.source, CypherQuerySource)
def _parse_queries(self): """Identify individual Cypher queries. Uses semicolons to identify the boundaries of queries within file text. If no semicolon is found it will be assumed that the file contains only one query and that the terminating semicolon was omitted. Returns: dict: Parsed file contents in the form of a dictionary with a structure of {params:<dict>, queries:<list of str>} """ dat = self._extract_parameters() queries = dat[1] # only include non-empty strings in results (prevents whitespace at # end of file getting an element on its own). query_string_list = [ q.lstrip() + ";" for q in queries.split(";") if q.replace(" ", "") ] query_list = [] for i, statement in enumerate(query_string_list): query_list.append( CypherQuery( statement, params=self._match_params_to_statement(statement, dat[0]), source=CypherQuerySource(self.filename, "cypher", i), )) if len(query_list) == 0: warnings.warn("No queries found in " + self.filename, UserWarning) return query_list
def test_invalid_source_ref_type_throws_error(self): """CypherQuerySource throws value error if invalid ref_type given""" with self.assertRaises(ValueError): CypherQuerySource("queries.cql", "not_cypher_or_tabular", 10)
def test_ref(self): """CypherQuerySource.file_name returns source file name.""" s = CypherQuerySource("queries.cql", "cypher", 10) self.assertEqual(s.ref, "queries.cql")
def _row_to_cypher_query(self, row_index, row): statement = self._row_to_query_statement_string(row) source = CypherQuerySource(self.df, "tabular", row_index) return CypherQuery(statement, params=None, source=source)