Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwin1111 committed Dec 23, 2024
1 parent 85cb90b commit 3f86eb3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 34 deletions.
3 changes: 0 additions & 3 deletions common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ def validate_db_configs(db_configs: List[Dict[str, Any]]) -> None:
raise click.BadParameter("db_configs cannot be empty")

for idx, config in enumerate(db_configs):
if not isinstance(config, dict):
raise click.BadParameter(f"Configuration at index {idx} must be a dictionary")

# Check required fields
for field, field_type in required_fields.items():
if field not in config:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ services:
command: ["bash", "-c", "producer"]
volumes:
- ./producer:/pgevents/producer
- ./test:/pgevents/test
- ./tests:/pgevents/tests
depends_on:
- database
- rabbitmq
Expand Down
44 changes: 14 additions & 30 deletions tests/test_event_producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,38 +233,31 @@ def test_invalid_table_format():
@pytest.fixture
def valid_db_configs():
return json.dumps([{
'pg_host': 'localhost',
'pg_host': 'database',
'pg_port': 5432,
'pg_database': 'test_db',
'pg_database': 'dummy',
'pg_user': 'postgres',
'pg_password': 'secret',
'pg_password': 'postgres',
'pg_tables': 'public.users',
'pg_replication_slot': 'test_slot'
'pg_replication_slot': 'events'
}])

@pytest.fixture
def mock_multidb_producer():
with mock.patch('producer.main.MultiDBEventProducer') as mock_producer:
yield mock_producer

def test_produce_multiple_dbs_success(valid_db_configs, mock_multidb_producer):
def test_produce_multiple_dbs_success(valid_db_configs):
runner = CliRunner()
result = runner.invoke(produce_multiple_dbs, [
'--db_configs', valid_db_configs,
'--rabbitmq_url', 'amqp://localhost',
'--rabbitmq_exchange', 'test_exchange'
'--rabbitmq_url', 'amqp://admin:password@rabbitmq:5672/?heartbeat=0',
'--rabbitmq_exchange', 'pgevents_exchange'
])

assert result.exit_code == 0
mock_multidb_producer.assert_called_once()
mock_multidb_producer.return_value.start.assert_called_once()

def test_produce_multiple_dbs_invalid_json():
runner = CliRunner()
result = runner.invoke(produce_multiple_dbs, [
'--db_configs', 'invalid-json',
'--rabbitmq_url', 'amqp://localhost',
'--rabbitmq_exchange', 'test_exchange'
'--rabbitmq_url', 'amqp://admin:password@rabbitmq:5672/?heartbeat=0',
'--rabbitmq_exchange', 'pgevents_exchange'
])

assert "db_configs must be a valid JSON string" in str(result.__dict__)
Expand All @@ -276,29 +269,20 @@ def test_produce_multiple_dbs_invalid_config(mock_validate, valid_db_configs):
runner = CliRunner()
result = runner.invoke(produce_multiple_dbs, [
'--db_configs', valid_db_configs,
'--rabbitmq_url', 'amqp://localhost',
'--rabbitmq_exchange', 'test_exchange'
'--rabbitmq_url', 'amqp://admin:password@rabbitmq:5672/?heartbeat=0',
'--rabbitmq_exchange', 'pgevents_exchange'
])

assert "Invalid config" in str(result.__dict__)

def test_produce_multiple_dbs_common_kwargs(valid_db_configs, mock_multidb_producer):
def test_produce_multiple_dbs_common_kwargs(valid_db_configs):
runner = CliRunner()
result = runner.invoke(produce_multiple_dbs, [
'--db_configs', valid_db_configs,
'--pg_output_plugin', 'test_plugin',
'--pg_publication_name', 'test_pub',
'--rabbitmq_url', 'amqp://localhost',
'--rabbitmq_exchange', 'test_exchange'
'--rabbitmq_url', 'amqp://admin:password@rabbitmq:5672/?heartbeat=0',
'--rabbitmq_exchange', 'pgevents_exchange'
])

assert result.exit_code == 0
mock_multidb_producer.assert_called_once_with(
json.loads(valid_db_configs),
qconnector_cls=mock.ANY,
event_cls=mock.ANY,
pg_output_plugin='test_plugin',
pg_publication_name='test_pub',
rabbitmq_url='amqp://localhost',
rabbitmq_exchange='test_exchange'
)

0 comments on commit 3f86eb3

Please sign in to comment.