Fixing tests

This commit is contained in:
Cyber MacGeddon 2025-09-03 15:52:29 +01:00
parent bb55581f13
commit 0bf2fc9ff8

View file

@ -20,8 +20,9 @@ from trustgraph.storage.knowledge.store import Processor as KgStore
class TestEndToEndConfigurationFlow: class TestEndToEndConfigurationFlow:
"""Test complete configuration flow from environment to processors.""" """Test complete configuration flow from environment to processors."""
@patch('trustgraph.direct.cassandra.TrustGraph') @pytest.mark.asyncio
def test_triples_writer_env_to_connection(self, mock_trust_graph): @patch('trustgraph.direct.cassandra.Cluster')
async def test_triples_writer_env_to_connection(self, mock_cluster):
"""Test complete flow from environment variables to TrustGraph connection.""" """Test complete flow from environment variables to TrustGraph connection."""
env_vars = { env_vars = {
'CASSANDRA_HOST': 'integration-host1,integration-host2,integration-host3', 'CASSANDRA_HOST': 'integration-host1,integration-host2,integration-host3',
@ -29,11 +30,13 @@ class TestEndToEndConfigurationFlow:
'CASSANDRA_PASSWORD': 'integration-pass' 'CASSANDRA_PASSWORD': 'integration-pass'
} }
mock_tg_instance = MagicMock() mock_cluster_instance = MagicMock()
mock_trust_graph.return_value = mock_tg_instance 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): with patch.dict(os.environ, env_vars, clear=True):
processor = TriplesWriter() processor = TriplesWriter(taskgroup=MagicMock())
# Create a mock message to trigger TrustGraph creation # Create a mock message to trigger TrustGraph creation
mock_message = MagicMock() mock_message = MagicMock()
@ -42,16 +45,13 @@ class TestEndToEndConfigurationFlow:
mock_message.triples = [] mock_message.triples = []
# This should create TrustGraph with environment config # This should create TrustGraph with environment config
processor.store_triples(mock_message) await processor.store_triples(mock_message)
# Verify TrustGraph was created with correct environment config # Verify Cluster was created with correct hosts
mock_trust_graph.assert_called_once_with( mock_cluster.assert_called_once()
hosts=['integration-host1', 'integration-host2', 'integration-host3'], call_args = mock_cluster.call_args
keyspace='test_user', assert call_args.args[0] == ['integration-host1', 'integration-host2', 'integration-host3']
table='test_collection', assert 'auth_provider' in call_args.kwargs # Should have auth since credentials provided
username='integration-user',
password='integration-pass'
)
@patch('trustgraph.storage.objects.cassandra.write.Cluster') @patch('trustgraph.storage.objects.cassandra.write.Cluster')
@patch('trustgraph.storage.objects.cassandra.write.PlainTextAuthProvider') @patch('trustgraph.storage.objects.cassandra.write.PlainTextAuthProvider')
@ -71,7 +71,7 @@ class TestEndToEndConfigurationFlow:
mock_cluster.return_value = mock_cluster_instance mock_cluster.return_value = mock_cluster_instance
with patch.dict(os.environ, env_vars, clear=True): with patch.dict(os.environ, env_vars, clear=True):
processor = ObjectsWriter() processor = ObjectsWriter(taskgroup=MagicMock())
# Trigger Cassandra connection # Trigger Cassandra connection
processor.connect_cassandra() processor.connect_cassandra()
@ -88,8 +88,9 @@ class TestEndToEndConfigurationFlow:
assert call_args.kwargs['contact_points'] == ['obj-host1', 'obj-host2'] assert call_args.kwargs['contact_points'] == ['obj-host1', 'obj-host2']
assert call_args.kwargs['auth_provider'] == mock_auth_instance assert call_args.kwargs['auth_provider'] == mock_auth_instance
@pytest.mark.asyncio
@patch('trustgraph.tables.knowledge.KnowledgeTableStore') @patch('trustgraph.tables.knowledge.KnowledgeTableStore')
def test_kg_store_env_to_table_store(self, mock_table_store): async def test_kg_store_env_to_table_store(self, mock_table_store):
"""Test complete flow from environment variables to KnowledgeTableStore.""" """Test complete flow from environment variables to KnowledgeTableStore."""
env_vars = { env_vars = {
'CASSANDRA_HOST': 'kg-host1,kg-host2,kg-host3,kg-host4', 'CASSANDRA_HOST': 'kg-host1,kg-host2,kg-host3,kg-host4',
@ -101,7 +102,7 @@ class TestEndToEndConfigurationFlow:
mock_table_store.return_value = mock_store_instance mock_table_store.return_value = mock_store_instance
with patch.dict(os.environ, env_vars, clear=True): with patch.dict(os.environ, env_vars, clear=True):
processor = KgStore() processor = KgStore(taskgroup=MagicMock())
# Verify KnowledgeTableStore was created with env config # Verify KnowledgeTableStore was created with env config
mock_table_store.assert_called_once_with( mock_table_store.assert_called_once_with(
@ -115,8 +116,9 @@ class TestEndToEndConfigurationFlow:
class TestConfigurationPriorityEndToEnd: class TestConfigurationPriorityEndToEnd:
"""Test configuration priority chains end-to-end.""" """Test configuration priority chains end-to-end."""
@pytest.mark.asyncio
@patch('trustgraph.direct.cassandra.TrustGraph') @patch('trustgraph.direct.cassandra.TrustGraph')
def test_cli_override_env_end_to_end(self, mock_trust_graph): async def test_cli_override_env_end_to_end(self, mock_trust_graph):
"""Test that CLI parameters override environment variables end-to-end.""" """Test that CLI parameters override environment variables end-to-end."""
env_vars = { env_vars = {
'CASSANDRA_HOST': 'env-host', 'CASSANDRA_HOST': 'env-host',
@ -130,6 +132,7 @@ class TestConfigurationPriorityEndToEnd:
with patch.dict(os.environ, env_vars, clear=True): with patch.dict(os.environ, env_vars, clear=True):
# CLI parameters should override environment # CLI parameters should override environment
processor = TriplesWriter( processor = TriplesWriter(
taskgroup=MagicMock(),
cassandra_host='cli-host1,cli-host2', cassandra_host='cli-host1,cli-host2',
cassandra_username='cli-user', cassandra_username='cli-user',
cassandra_password='cli-pass' cassandra_password='cli-pass'
@ -141,7 +144,7 @@ class TestConfigurationPriorityEndToEnd:
mock_message.metadata.collection = 'test_collection' mock_message.metadata.collection = 'test_collection'
mock_message.triples = [] mock_message.triples = []
processor.store_triples(mock_message) await processor.store_triples(mock_message)
# Should use CLI parameters, not environment # Should use CLI parameters, not environment
mock_trust_graph.assert_called_once_with( mock_trust_graph.assert_called_once_with(
@ -152,8 +155,9 @@ class TestConfigurationPriorityEndToEnd:
password='cli-pass' # From CLI password='cli-pass' # From CLI
) )
@pytest.mark.asyncio
@patch('trustgraph.tables.knowledge.KnowledgeTableStore') @patch('trustgraph.tables.knowledge.KnowledgeTableStore')
def test_partial_cli_with_env_fallback_end_to_end(self, mock_table_store): 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.""" """Test partial CLI parameters with environment fallback end-to-end."""
env_vars = { env_vars = {
'CASSANDRA_HOST': 'fallback-host1,fallback-host2', 'CASSANDRA_HOST': 'fallback-host1,fallback-host2',
@ -167,6 +171,7 @@ class TestConfigurationPriorityEndToEnd:
with patch.dict(os.environ, env_vars, clear=True): with patch.dict(os.environ, env_vars, clear=True):
# Only provide host via parameter, rest should fall back to env # Only provide host via parameter, rest should fall back to env
processor = KgStore( processor = KgStore(
taskgroup=MagicMock(),
cassandra_host='partial-host' cassandra_host='partial-host'
# username and password not provided - should use env # username and password not provided - should use env
) )
@ -179,14 +184,15 @@ class TestConfigurationPriorityEndToEnd:
keyspace='knowledge' keyspace='knowledge'
) )
@pytest.mark.asyncio
@patch('trustgraph.direct.cassandra.TrustGraph') @patch('trustgraph.direct.cassandra.TrustGraph')
def test_no_config_defaults_end_to_end(self, mock_trust_graph): async def test_no_config_defaults_end_to_end(self, mock_trust_graph):
"""Test that defaults are used when no configuration provided end-to-end.""" """Test that defaults are used when no configuration provided end-to-end."""
mock_tg_instance = MagicMock() mock_tg_instance = MagicMock()
mock_trust_graph.return_value = mock_tg_instance mock_trust_graph.return_value = mock_tg_instance
with patch.dict(os.environ, {}, clear=True): with patch.dict(os.environ, {}, clear=True):
processor = TriplesQuery() processor = TriplesQuery(taskgroup=MagicMock())
# Mock query to trigger TrustGraph creation # Mock query to trigger TrustGraph creation
mock_query = MagicMock() mock_query = MagicMock()
@ -200,7 +206,7 @@ class TestConfigurationPriorityEndToEnd:
# Mock the get_all method to return empty list # Mock the get_all method to return empty list
mock_tg_instance.get_all.return_value = [] mock_tg_instance.get_all.return_value = []
processor.query_triples(mock_query) await processor.query_triples(mock_query)
# Should use defaults # Should use defaults
mock_trust_graph.assert_called_once_with( mock_trust_graph.assert_called_once_with(
@ -214,14 +220,16 @@ class TestConfigurationPriorityEndToEnd:
class TestBackwardCompatibilityEndToEnd: class TestBackwardCompatibilityEndToEnd:
"""Test backward compatibility with old parameter names end-to-end.""" """Test backward compatibility with old parameter names end-to-end."""
@pytest.mark.asyncio
@patch('trustgraph.direct.cassandra.TrustGraph') @patch('trustgraph.direct.cassandra.TrustGraph')
def test_old_graph_params_still_work_end_to_end(self, mock_trust_graph): async def test_old_graph_params_still_work_end_to_end(self, mock_trust_graph):
"""Test that old graph_* parameters still work end-to-end.""" """Test that old graph_* parameters still work end-to-end."""
mock_tg_instance = MagicMock() mock_tg_instance = MagicMock()
mock_trust_graph.return_value = mock_tg_instance mock_trust_graph.return_value = mock_tg_instance
# Use old parameter names # Use old parameter names
processor = TriplesWriter( processor = TriplesWriter(
taskgroup=MagicMock(),
graph_host='legacy-host', graph_host='legacy-host',
graph_username='legacy-user', graph_username='legacy-user',
graph_password='legacy-pass' graph_password='legacy-pass'
@ -233,7 +241,7 @@ class TestBackwardCompatibilityEndToEnd:
mock_message.metadata.collection = 'legacy_collection' mock_message.metadata.collection = 'legacy_collection'
mock_message.triples = [] mock_message.triples = []
processor.store_triples(mock_message) await processor.store_triples(mock_message)
# Should work with legacy parameters # Should work with legacy parameters
mock_trust_graph.assert_called_once_with( mock_trust_graph.assert_called_once_with(
@ -252,6 +260,7 @@ class TestBackwardCompatibilityEndToEnd:
# Use old cassandra_user parameter # Use old cassandra_user parameter
processor = KgStore( processor = KgStore(
taskgroup=MagicMock(),
cassandra_host='legacy-kg-host', cassandra_host='legacy-kg-host',
cassandra_user='legacy-kg-user', # Old parameter name cassandra_user='legacy-kg-user', # Old parameter name
cassandra_password='legacy-kg-pass' cassandra_password='legacy-kg-pass'
@ -265,14 +274,16 @@ class TestBackwardCompatibilityEndToEnd:
keyspace='knowledge' keyspace='knowledge'
) )
@pytest.mark.asyncio
@patch('trustgraph.direct.cassandra.TrustGraph') @patch('trustgraph.direct.cassandra.TrustGraph')
def test_new_params_override_old_params_end_to_end(self, mock_trust_graph): async def test_new_params_override_old_params_end_to_end(self, mock_trust_graph):
"""Test that new parameters override old ones when both are present end-to-end.""" """Test that new parameters override old ones when both are present end-to-end."""
mock_tg_instance = MagicMock() mock_tg_instance = MagicMock()
mock_trust_graph.return_value = mock_tg_instance mock_trust_graph.return_value = mock_tg_instance
# Provide both old and new parameters # Provide both old and new parameters
processor = TriplesWriter( processor = TriplesWriter(
taskgroup=MagicMock(),
cassandra_host='new-host', cassandra_host='new-host',
graph_host='old-host', # Should be ignored graph_host='old-host', # Should be ignored
cassandra_username='new-user', cassandra_username='new-user',
@ -287,7 +298,7 @@ class TestBackwardCompatibilityEndToEnd:
mock_message.metadata.collection = 'precedence_collection' mock_message.metadata.collection = 'precedence_collection'
mock_message.triples = [] mock_message.triples = []
processor.store_triples(mock_message) await processor.store_triples(mock_message)
# Should use new parameters, not old ones # Should use new parameters, not old ones
mock_trust_graph.assert_called_once_with( mock_trust_graph.assert_called_once_with(
@ -315,7 +326,7 @@ class TestMultipleHostsHandling:
mock_cluster.return_value = mock_cluster_instance mock_cluster.return_value = mock_cluster_instance
with patch.dict(os.environ, env_vars, clear=True): with patch.dict(os.environ, env_vars, clear=True):
processor = ObjectsWriter() processor = ObjectsWriter(taskgroup=MagicMock())
processor.connect_cassandra() processor.connect_cassandra()
# Verify all hosts were passed to Cluster # Verify all hosts were passed to Cluster
@ -323,13 +334,14 @@ class TestMultipleHostsHandling:
call_args = mock_cluster.call_args call_args = mock_cluster.call_args
assert call_args.kwargs['contact_points'] == ['host1', 'host2', 'host3', 'host4', 'host5'] assert call_args.kwargs['contact_points'] == ['host1', 'host2', 'host3', 'host4', 'host5']
@pytest.mark.asyncio
@patch('trustgraph.direct.cassandra.TrustGraph') @patch('trustgraph.direct.cassandra.TrustGraph')
def test_single_host_converted_to_list(self, mock_trust_graph): async def test_single_host_converted_to_list(self, mock_trust_graph):
"""Test that single host is converted to list for TrustGraph.""" """Test that single host is converted to list for TrustGraph."""
mock_tg_instance = MagicMock() mock_tg_instance = MagicMock()
mock_trust_graph.return_value = mock_tg_instance mock_trust_graph.return_value = mock_tg_instance
processor = TriplesWriter(cassandra_host='single-host') processor = TriplesWriter(taskgroup=MagicMock(), cassandra_host='single-host')
# Trigger TrustGraph creation # Trigger TrustGraph creation
mock_message = MagicMock() mock_message = MagicMock()
@ -337,7 +349,7 @@ class TestMultipleHostsHandling:
mock_message.metadata.collection = 'single_collection' mock_message.metadata.collection = 'single_collection'
mock_message.triples = [] mock_message.triples = []
processor.store_triples(mock_message) await processor.store_triples(mock_message)
# Single host should be converted to list # Single host should be converted to list
mock_trust_graph.assert_called_once_with( mock_trust_graph.assert_called_once_with(
@ -380,7 +392,7 @@ class TestAuthenticationFlow:
mock_cluster.return_value = mock_cluster_instance mock_cluster.return_value = mock_cluster_instance
with patch.dict(os.environ, env_vars, clear=True): with patch.dict(os.environ, env_vars, clear=True):
processor = ObjectsWriter() processor = ObjectsWriter(taskgroup=MagicMock())
processor.connect_cassandra() processor.connect_cassandra()
# Auth provider should be created # Auth provider should be created
@ -407,7 +419,7 @@ class TestAuthenticationFlow:
mock_cluster.return_value = mock_cluster_instance mock_cluster.return_value = mock_cluster_instance
with patch.dict(os.environ, env_vars, clear=True): with patch.dict(os.environ, env_vars, clear=True):
processor = ObjectsWriter() processor = ObjectsWriter(taskgroup=MagicMock())
processor.connect_cassandra() processor.connect_cassandra()
# Auth provider should not be created # Auth provider should not be created
@ -422,6 +434,7 @@ class TestAuthenticationFlow:
def test_no_authentication_when_only_username_provided(self, mock_auth_provider, mock_cluster): def test_no_authentication_when_only_username_provided(self, mock_auth_provider, mock_cluster):
"""Test that authentication is not used when only username is provided.""" """Test that authentication is not used when only username is provided."""
processor = ObjectsWriter( processor = ObjectsWriter(
taskgroup=MagicMock(),
cassandra_host='partial-auth-host', cassandra_host='partial-auth-host',
cassandra_username='partial-user' cassandra_username='partial-user'
# No password # No password