"""Integration tests module.""" import os import index from neo4j import GraphDatabase from neo4j.exceptions import CypherSyntaxError import config def test_explain_all_queries(): """Test using EXPLAIN in all the queries to validate them.""" # TODO: This would benefit from using a Cypher lexer and parser to avoid test overhead with GraphDatabase.driver( config.NEO4J_URL, auth=(config.NEO4J_CONNECTION_USER, config.NEO4J_CONNECTION_PASSWORD) ) as driver: queries = 'queries/' # Get query directories paths query_directories = [ os.path.relpath(root, queries) for root, _, files in os.walk(queries) if any(file.endswith('.cypher') for file in files) ] # Get query files for dir in query_directories: query_files = index.get_queries_from_folder(dir) for file in query_files: cypher = index.format_query(file) query_list = index.get_query_list_from_cypher(cypher) print(f'Checking query syntax in {dir}/{file} ...') try: with driver.session() as session: for query in query_list: # Run the query with EXPLAIN explain_query = f'EXPLAIN {query}' result = session.run(explain_query) assert result except CypherSyntaxError as cse: raise AssertionError( f'Found errors validating query syntax in {dir}/{file}:\n{cse}' ) from None except Exception as e: print('Error executing query during syntax validation: ', e) else: print('Query syntax checked successfully') pass def test_query(): """Test running a simple query.""" config.NEO4J_SOURCE_NAME = 'test' config.NEO4J_SOURCE_FOLDER_NAME = '' data = index.main() assert data[0]['result'][0]['success'] is True def test_multiple_queries(): """Test running multiple queries with JDBC.""" config.NEO4J_SOURCE_NAME = 'test' config.NEO4J_SOURCE_FOLDER_NAME = 'test-folder' data = index.main() assert data[0]['result'][0]['vendorId'] == 7123 assert data[1]['result'][0]['projectId'] == 4601458 assert data[2]['result'][0]['trackId'] == 34505923 def test_comments(): """Test parsing of comments with JDBC.""" config.NEO4J_SOURCE_NAME = 'test' config.NEO4J_SOURCE_FOLDER_NAME = 'test-folder' data = index.main() assert data[2]['result'][0]['trackId'] == 34505923 assert data[2]['result'][0]['trackType'] == 'music'