Skip to content

Commit

Permalink
Updated the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-au-922 committed Dec 2, 2023
1 parent 53e4a08 commit b4d0c70
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
Binary file removed .coverage_consumer
Binary file not shown.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test_consumer:
export RABBITMQ_USERNAME=$(RABBITMQ_USERNAME) && \
export RABBITMQ_PASSWORD=$(RABBITMQ_PASSWORD) && \
export QUEUE_NAME=$(QUEUE_NAME) && \
COVERAGE_FILE=.coverage_consumer coverage run -m pytest -vxs consumer/tests/test_adapters/test_fetch_filenames/test_rabbitmq
COVERAGE_FILE=.coverage_consumer coverage run -m pytest -vxs consumer/tests
coverage_report:
coverage combine .coverage_producer .coverage_consumer && \
coverage report -m --omit="*/tests/*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from src.entities import IOTRecord
import psycopg2
from .utils import random_iot_records, MockedPostgresConnection
from pytest import MonkeyPatch
from pytest import MonkeyPatch, LogCaptureFixture


@pytest.mark.smoke
Expand All @@ -14,18 +14,19 @@ def test_upsert_single_failed_conn(
raw_postgres_psycopg2_conn_config: psycopg2.extensions.connection,
iot_record: IOTRecord,
monkeypatch: MonkeyPatch,
caplog: LogCaptureFixture,
):
def mocked_failed_conn(
self,
*args,
**kwargs,
) -> None:
raise Exception("Failed to connect")

monkeypatch.setattr(psycopg2, "connect", mocked_failed_conn)

with pytest.raises(Exception, match="^Failed to connect$"):
with caplog.at_level("ERROR"):
assert not postgres_upsert_iot_records_client.upsert(iot_record)
assert "Failed to connect" in caplog.text

with raw_postgres_psycopg2_conn_config.cursor() as cursor:
cursor.execute(
Expand Down Expand Up @@ -54,18 +55,19 @@ def test_upsert_batch_failed_conn(
raw_postgres_psycopg2_conn_config: psycopg2.extensions.connection,
iot_records: list[IOTRecord],
monkeypatch: MonkeyPatch,
caplog: LogCaptureFixture,
):
def mocked_failed_conn(
self,
*args,
**kwargs,
) -> None:
raise Exception("Failed to connect")

monkeypatch.setattr(psycopg2, "connect", mocked_failed_conn)

with pytest.raises(Exception, match="^Failed to connect$"):
with caplog.at_level("ERROR"):
assert not any(postgres_upsert_iot_records_client.upsert(iot_records))
assert "Failed to connect" in caplog.text

with raw_postgres_psycopg2_conn_config.cursor() as cursor:
stmt = """
Expand Down Expand Up @@ -94,6 +96,7 @@ def mocked_failed_conn(
def test_upsert_single_wrong_credentials(
raw_postgres_psycopg2_conn_config: psycopg2.extensions.connection,
iot_record: IOTRecord,
caplog: LogCaptureFixture,
):
postgres_upsert_iot_records_client = PostgresUpsertIOTRecordsClient(
host=PostgresConfig.HOST,
Expand All @@ -103,8 +106,9 @@ def test_upsert_single_wrong_credentials(
batch_upsert_size=1,
)

with pytest.raises(Exception, match="^.*403.*ACCESS_REFISED.*$"):
with caplog.at_level("ERROR"):
assert not postgres_upsert_iot_records_client.upsert(iot_record)
assert "ERROR" in caplog.text

with raw_postgres_psycopg2_conn_config.cursor() as cursor:
cursor.execute(
Expand All @@ -129,6 +133,7 @@ def test_upsert_single_wrong_credentials(
def test_upsert_single_wrong_host(
raw_postgres_psycopg2_conn_config: psycopg2.extensions.connection,
iot_record: IOTRecord,
caplog: LogCaptureFixture,
):
postgres_upsert_iot_records_client = PostgresUpsertIOTRecordsClient(
host="wrong",
Expand All @@ -138,8 +143,9 @@ def test_upsert_single_wrong_host(
batch_upsert_size=1,
)

with pytest.raises(Exception, match="^.*403.*ACCESS_REFUSED.*$"):
with caplog.at_level("ERROR"):
assert not postgres_upsert_iot_records_client.upsert(iot_record)
assert "ERROR" in caplog.text

with raw_postgres_psycopg2_conn_config.cursor() as cursor:
cursor.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from src.entities import IOTRecord
import psycopg2
from pytest import MonkeyPatch
from pytest import MonkeyPatch, LogCaptureFixture


@pytest.mark.smoke
Expand All @@ -15,13 +15,15 @@ def test_upsert_single_failed(
raw_postgres_psycopg2_conn_config: psycopg2.extensions.connection,
iot_record: IOTRecord,
monkeypatch: MonkeyPatch,
caplog: LogCaptureFixture,
):
monkeypatch.setattr(
psycopg2, "connect", lambda *args, **kwargs: MockedPostgresConnection()
)

with pytest.raises(Exception, match="^Failed to execute!$"):
with caplog.at_level("ERROR"):
assert not postgres_upsert_iot_records_client.upsert(iot_record)
assert "Failed to execute!" in caplog.text

with raw_postgres_psycopg2_conn_config.cursor() as cursor:
cursor.execute(
Expand Down Expand Up @@ -50,13 +52,15 @@ def test_upsert_batch_failed(
raw_postgres_psycopg2_conn_config: psycopg2.extensions.connection,
iot_records: list[IOTRecord],
monkeypatch: MonkeyPatch,
caplog: LogCaptureFixture,
):
monkeypatch.setattr(
psycopg2, "connect", lambda *args, **kwargs: MockedPostgresConnection()
)

with pytest.raises(Exception, match="^Failed to execute!$"):
assert not any(postgres_upsert_iot_records_client.upsert(iot_records))
with caplog.at_level("ERROR"):
assert not all(postgres_upsert_iot_records_client.upsert(iot_records))
assert "Failed to execute!" in caplog.text

with raw_postgres_psycopg2_conn_config.cursor() as cursor:
stmt = """
Expand Down Expand Up @@ -90,6 +94,7 @@ def test_upsert_batch_partial_failed(
raw_postgres_psycopg2_conn_config: psycopg2.extensions.connection,
iot_records: list[IOTRecord],
monkeypatch: MonkeyPatch,
caplog: LogCaptureFixture,
):
new_postgres_upsert_iot_records_client = PostgresUpsertIOTRecordsClient(
host=postgres_upsert_iot_records_client._host,
Expand Down Expand Up @@ -139,12 +144,13 @@ def mocked_partially_failed_upsert(
MockedPostgresCursor, "executemany", mocked_partially_failed_upsert
)

with pytest.raises(Exception, match="^Failed to execute!"):
with caplog.at_level("ERROR"):
upsert_successes = new_postgres_upsert_iot_records_client.upsert(iot_records)

assert not all(upsert_successes)
assert any(upsert_successes)
assert upsert_successes[2] == False
assert "Failed to execute!" in caplog.text

successful_records = [
iot_record
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ def test_publish_single_wrong_credentials(
def test_publish_single_wrong_host(
raw_rabbitmq_pika_conn_config: tuple[pika.BaseConnection, str],
filename: str,
caplog: LogCaptureFixture,
):
rabbitmq_publish_filenames_client = RabbitMQPublishFilenamesClient(
host="wrong",
Expand All @@ -110,9 +109,7 @@ def test_publish_single_wrong_host(
queue=RabbitMQConfig.QUEUE,
)

with caplog.at_level("ERROR"):
assert not rabbitmq_publish_filenames_client.publish(filename)
assert "Name or service not known" in caplog.text
assert not rabbitmq_publish_filenames_client.publish(filename)

pika_conn, queue = raw_rabbitmq_pika_conn_config
channel = pika_conn.channel()
Expand Down

0 comments on commit b4d0c70

Please sign in to comment.