Fixing tests

This commit is contained in:
Cyber MacGeddon 2025-09-03 15:58:30 +01:00
parent 0bf2fc9ff8
commit 04698a35b7
2 changed files with 62 additions and 61 deletions

View file

@ -89,7 +89,7 @@ class TestEndToEndConfigurationFlow:
assert call_args.kwargs['auth_provider'] == mock_auth_instance
@pytest.mark.asyncio
@patch('trustgraph.tables.knowledge.KnowledgeTableStore')
@patch('trustgraph.storage.knowledge.store.KnowledgeTableStore')
async def test_kg_store_env_to_table_store(self, mock_table_store):
"""Test complete flow from environment variables to KnowledgeTableStore."""
env_vars = {
@ -117,8 +117,8 @@ class TestConfigurationPriorityEndToEnd:
"""Test configuration priority chains end-to-end."""
@pytest.mark.asyncio
@patch('trustgraph.direct.cassandra.TrustGraph')
async def test_cli_override_env_end_to_end(self, mock_trust_graph):
@patch('trustgraph.direct.cassandra.Cluster')
async def test_cli_override_env_end_to_end(self, mock_cluster):
"""Test that CLI parameters override environment variables end-to-end."""
env_vars = {
'CASSANDRA_HOST': 'env-host',
@ -126,8 +126,10 @@ class TestConfigurationPriorityEndToEnd:
'CASSANDRA_PASSWORD': 'env-pass'
}
mock_tg_instance = MagicMock()
mock_trust_graph.return_value = mock_tg_instance
mock_cluster_instance = MagicMock()
mock_session = MagicMock()
mock_cluster_instance.connect.return_value = mock_session
mock_cluster.return_value = mock_cluster_instance
with patch.dict(os.environ, env_vars, clear=True):
# CLI parameters should override environment
@ -147,16 +149,13 @@ class TestConfigurationPriorityEndToEnd:
await processor.store_triples(mock_message)
# Should use CLI parameters, not environment
mock_trust_graph.assert_called_once_with(
hosts=['cli-host1', 'cli-host2'], # From CLI
keyspace='test_user',
table='test_collection',
username='cli-user', # From CLI
password='cli-pass' # From CLI
)
mock_cluster.assert_called_once()
call_args = mock_cluster.call_args
assert call_args.args[0] == ['cli-host1', 'cli-host2'] # From CLI
assert 'auth_provider' in call_args.kwargs # Should have auth since credentials provided
@pytest.mark.asyncio
@patch('trustgraph.tables.knowledge.KnowledgeTableStore')
@patch('trustgraph.storage.knowledge.store.KnowledgeTableStore')
async def test_partial_cli_with_env_fallback_end_to_end(self, mock_table_store):
"""Test partial CLI parameters with environment fallback end-to-end."""
env_vars = {
@ -185,11 +184,13 @@ class TestConfigurationPriorityEndToEnd:
)
@pytest.mark.asyncio
@patch('trustgraph.direct.cassandra.TrustGraph')
async def test_no_config_defaults_end_to_end(self, mock_trust_graph):
@patch('trustgraph.direct.cassandra.Cluster')
async def test_no_config_defaults_end_to_end(self, mock_cluster):
"""Test that defaults are used when no configuration provided end-to-end."""
mock_tg_instance = MagicMock()
mock_trust_graph.return_value = mock_tg_instance
mock_cluster_instance = MagicMock()
mock_session = MagicMock()
mock_cluster_instance.connect.return_value = mock_session
mock_cluster.return_value = mock_cluster_instance
with patch.dict(os.environ, {}, clear=True):
processor = TriplesQuery(taskgroup=MagicMock())
@ -204,28 +205,30 @@ class TestConfigurationPriorityEndToEnd:
mock_query.limit = 100
# Mock the get_all method to return empty list
mock_tg_instance = MagicMock()
mock_tg_instance.get_all.return_value = []
processor.tg = mock_tg_instance
await processor.query_triples(mock_query)
# Should use defaults
mock_trust_graph.assert_called_once_with(
hosts=['cassandra'], # Default host
keyspace='default_user',
table='default_collection'
# No username/password (defaults to None)
)
mock_cluster.assert_called_once()
call_args = mock_cluster.call_args
assert call_args.args[0] == ['cassandra'] # Default host
assert 'auth_provider' not in call_args.kwargs # No auth with default config
class TestBackwardCompatibilityEndToEnd:
"""Test backward compatibility with old parameter names end-to-end."""
@pytest.mark.asyncio
@patch('trustgraph.direct.cassandra.TrustGraph')
async def test_old_graph_params_still_work_end_to_end(self, mock_trust_graph):
@patch('trustgraph.direct.cassandra.Cluster')
async def test_old_graph_params_still_work_end_to_end(self, mock_cluster):
"""Test that old graph_* parameters still work end-to-end."""
mock_tg_instance = MagicMock()
mock_trust_graph.return_value = mock_tg_instance
mock_cluster_instance = MagicMock()
mock_session = MagicMock()
mock_cluster_instance.connect.return_value = mock_session
mock_cluster.return_value = mock_cluster_instance
# Use old parameter names
processor = TriplesWriter(
@ -244,15 +247,12 @@ class TestBackwardCompatibilityEndToEnd:
await processor.store_triples(mock_message)
# Should work with legacy parameters
mock_trust_graph.assert_called_once_with(
hosts=['legacy-host'],
keyspace='legacy_user',
table='legacy_collection',
username='legacy-user',
password='legacy-pass'
)
mock_cluster.assert_called_once()
call_args = mock_cluster.call_args
assert call_args.args[0] == ['legacy-host']
assert 'auth_provider' in call_args.kwargs # Should have auth since credentials provided
@patch('trustgraph.tables.knowledge.KnowledgeTableStore')
@patch('trustgraph.storage.knowledge.store.KnowledgeTableStore')
def test_old_cassandra_user_param_still_works_end_to_end(self, mock_table_store):
"""Test that old cassandra_user parameter still works end-to-end."""
mock_store_instance = MagicMock()
@ -275,11 +275,13 @@ class TestBackwardCompatibilityEndToEnd:
)
@pytest.mark.asyncio
@patch('trustgraph.direct.cassandra.TrustGraph')
async def test_new_params_override_old_params_end_to_end(self, mock_trust_graph):
@patch('trustgraph.direct.cassandra.Cluster')
async def test_new_params_override_old_params_end_to_end(self, mock_cluster):
"""Test that new parameters override old ones when both are present end-to-end."""
mock_tg_instance = MagicMock()
mock_trust_graph.return_value = mock_tg_instance
mock_cluster_instance = MagicMock()
mock_session = MagicMock()
mock_cluster_instance.connect.return_value = mock_session
mock_cluster.return_value = mock_cluster_instance
# Provide both old and new parameters
processor = TriplesWriter(
@ -301,13 +303,10 @@ class TestBackwardCompatibilityEndToEnd:
await processor.store_triples(mock_message)
# Should use new parameters, not old ones
mock_trust_graph.assert_called_once_with(
hosts=['new-host'], # New parameter wins
keyspace='precedence_user',
table='precedence_collection',
username='new-user', # New parameter wins
password='new-pass' # New parameter wins
)
mock_cluster.assert_called_once()
call_args = mock_cluster.call_args
assert call_args.args[0] == ['new-host'] # New parameter wins
assert 'auth_provider' in call_args.kwargs # Should have auth since credentials provided
class TestMultipleHostsHandling:
@ -335,11 +334,13 @@ class TestMultipleHostsHandling:
assert call_args.kwargs['contact_points'] == ['host1', 'host2', 'host3', 'host4', 'host5']
@pytest.mark.asyncio
@patch('trustgraph.direct.cassandra.TrustGraph')
async def test_single_host_converted_to_list(self, mock_trust_graph):
@patch('trustgraph.direct.cassandra.Cluster')
async def test_single_host_converted_to_list(self, mock_cluster):
"""Test that single host is converted to list for TrustGraph."""
mock_tg_instance = MagicMock()
mock_trust_graph.return_value = mock_tg_instance
mock_cluster_instance = MagicMock()
mock_session = MagicMock()
mock_cluster_instance.connect.return_value = mock_session
mock_cluster.return_value = mock_cluster_instance
processor = TriplesWriter(taskgroup=MagicMock(), cassandra_host='single-host')
@ -352,11 +353,10 @@ class TestMultipleHostsHandling:
await processor.store_triples(mock_message)
# Single host should be converted to list
mock_trust_graph.assert_called_once_with(
hosts=['single-host'], # Converted to list
keyspace='single_user',
table='single_collection'
)
mock_cluster.assert_called_once()
call_args = mock_cluster.call_args
assert call_args.args[0] == ['single-host'] # Converted to list
assert 'auth_provider' not in call_args.kwargs # No auth since no credentials provided
def test_whitespace_handling_in_host_list(self):
"""Test that whitespace in host lists is handled correctly."""

View file

@ -28,7 +28,7 @@ class TestTriplesWriterConfiguration:
}
with patch.dict(os.environ, env_vars, clear=True):
processor = TriplesWriter()
processor = TriplesWriter(taskgroup=MagicMock())
assert processor.graph_host == ['env-host1', 'env-host2']
assert processor.username == 'env-user'
@ -45,6 +45,7 @@ class TestTriplesWriterConfiguration:
with patch.dict(os.environ, env_vars, clear=True):
processor = TriplesWriter(
taskgroup=MagicMock(),
cassandra_host='param-host1,param-host2',
cassandra_username='param-user',
cassandra_password='param-pass'
@ -71,7 +72,7 @@ class TestTriplesWriterConfiguration:
def test_default_configuration(self, mock_trust_graph):
"""Test default configuration when no params or env vars provided."""
with patch.dict(os.environ, {}, clear=True):
processor = TriplesWriter()
processor = TriplesWriter(taskgroup=MagicMock())
assert processor.graph_host == ['cassandra']
assert processor.username is None
@ -94,7 +95,7 @@ class TestObjectsWriterConfiguration:
mock_cluster.return_value = mock_cluster_instance
with patch.dict(os.environ, env_vars, clear=True):
processor = ObjectsWriter()
processor = ObjectsWriter(taskgroup=MagicMock())
assert processor.graph_host == ['obj-env-host1', 'obj-env-host2']
assert processor.graph_username == 'obj-env-user'
@ -170,7 +171,7 @@ class TestTriplesQueryConfiguration:
}
with patch.dict(os.environ, env_vars, clear=True):
processor = TriplesQuery()
processor = TriplesQuery(taskgroup=MagicMock())
assert processor.graph_host == ['query-env-host1', 'query-env-host2']
assert processor.username == 'query-env-user'
@ -207,7 +208,7 @@ class TestKgStoreConfiguration:
mock_table_store.return_value = mock_store_instance
with patch.dict(os.environ, env_vars, clear=True):
processor = KgStore()
processor = KgStore(taskgroup=MagicMock())
# Verify KnowledgeTableStore was called with resolved config
mock_table_store.assert_called_once_with(
@ -264,7 +265,7 @@ class TestKgStoreConfiguration:
mock_table_store.return_value = mock_store_instance
with patch.dict(os.environ, {}, clear=True):
processor = KgStore()
processor = KgStore(taskgroup=MagicMock())
# Should use defaults
mock_table_store.assert_called_once_with(