"""Testing the Neo4jExecutor.""" from unittest.mock import MagicMock import pytest from feed_ingestion.util.neo4j import neo4j_executor def test_executor_finds_query(): """Should fine a query file if it exists.""" under_test = neo4j_executor.Neo4jExecutor('chartmetric_socials') file = under_test.load_query('ingest_accounts') assert file def test_executor_does_not_find_query(): """Should raise FileNotFoundError if file does not exist.""" under_test = neo4j_executor.Neo4jExecutor('chartmetric_socials') with pytest.raises(FileNotFoundError) as error: under_test.load_query('non-existant-query') assert error.value.args[0].endswith('non-existant-query.cypher') @pytest.mark.parametrize('template', ['ingest_aggregate_socials_by_account', 'ingest_aggregate_socials_by_participant']) def test_executor_does_not_parameterize_query(template): """Should not replace placeholders with params.""" date = '2020-01-01' date_limit = '2020-04-01' params = {'start_date': date, 'end_date': date_limit} under_test = neo4j_executor.Neo4jExecutor('chartmetric_socials') template = under_test.load_query(template) assert date not in template assert date_limit not in template result = under_test.parametrize_query(template, params) assert result == template def test_executor_retries_failed_query(monkeypatch): """Test that a failed queries is retried.""" executor = neo4j_executor.Neo4jExecutor('chartmetric_socials') mock_session = MagicMock() mock_session.write_transaction.side_effect = Exception('FAIL') mock_driver = MagicMock() mock_driver.session.return_value.__enter__.return_value = mock_session executor.driver = mock_driver query_name = 'ingest_accounts' params = {} try: executor.execute_query(query_name, params) except Exception as error: assert mock_driver.session.call_count == 4 assert mock_session.close.call_count == 3 assert str(error) == 'FAIL'