Skip to content

Commit

Permalink
Merge pull request #8 from MountainGod2/dev
Browse files Browse the repository at this point in the history
tests: update tests and expand coverage
  • Loading branch information
MountainGod2 authored Aug 12, 2024
2 parents d256bcf + bf4d183 commit aaf58aa
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 254 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ changelog_file = "CHANGELOG.md"
build_command = "pip install poetry && poetry build"

[tool.pytest.ini_options]
addopts = "--strict-markers"
addopts = "--cov=chaturbate_poller --cov-report=xml --cov-fail-under=80"
generate_report_on_test = "True"
asyncio_mode = "strict"

Expand Down
12 changes: 12 additions & 0 deletions tests/test___main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Tests for the __main__ module."""

import asyncio
import logging
import subprocess
import sys
from contextlib import suppress
Expand Down Expand Up @@ -40,3 +41,14 @@ def test_start_polling() -> None:
pytest.raises(ValueError, match="Unauthorized access. Verify the username and token."),
):
asyncio.run(start_polling("username", "token", 10, testbed=False, verbose=False))


def test_start_polling_verbose() -> None:
"""Test the start_polling function with verbose output."""
with ( # noqa: PT012
suppress(KeyboardInterrupt),
pytest.raises(ValueError, match="Unauthorized access. Verify the username and token."),
):
asyncio.run(start_polling("username", "token", 10, testbed=False, verbose=True))

assert logging.getLogger().level == logging.DEBUG
44 changes: 16 additions & 28 deletions tests/test_docker_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,23 @@
import subprocess


def test_docker_image_build(mocker) -> None: # noqa: ANN001
"""Test building the Docker image."""
docker_executable = shutil.which("docker") # Find the full path of the Docker executable
assert docker_executable is not None, "Docker is not installed or not found in PATH"

mock_subprocess = mocker.patch("subprocess.run")
mock_subprocess.return_value.returncode = 0

result = subprocess.run(
[docker_executable, "build", "-t", "chaturbate_poller:latest", "."], check=True
def test_docker_build_and_run(mocker) -> None: # noqa: ANN001
"""Test the full Docker build and run process."""
docker_executable = shutil.which("docker")
assert docker_executable is not None, "Docker is not installed."

# Mock subprocess for Docker build
mocker.patch(
"subprocess.run",
return_value=subprocess.CompletedProcess(args=["docker", "build"], returncode=0),
)
subprocess.run([docker_executable, "build", "-t", "chaturbate_poller:latest", "."], check=True)

assert result.returncode == 0
mock_subprocess.assert_called_once_with(
[docker_executable, "build", "-t", "chaturbate_poller:latest", "."], check=True
# Mock subprocess for Docker run
mocker.patch(
"subprocess.run",
return_value=subprocess.CompletedProcess(args=["docker", "run"], returncode=0),
)
subprocess.run([docker_executable, "run", "chaturbate_poller:latest"], check=True)


def test_docker_image_run(mocker) -> None: # noqa: ANN001
"""Test running the Docker container."""
docker_executable = shutil.which("docker") # Find the full path of the Docker executable
assert docker_executable is not None, "Docker is not installed or not found in PATH"

mock_subprocess = mocker.patch("subprocess.run")
mock_subprocess.return_value.returncode = 0

result = subprocess.run([docker_executable, "run", "chaturbate_poller:latest"], check=True)

assert result.returncode == 0
mock_subprocess.assert_called_once_with(
[docker_executable, "run", "chaturbate_poller:latest"], check=True
)
assert True
137 changes: 62 additions & 75 deletions tests/test_influxdb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,69 @@
from chaturbate_poller.influxdb_client import InfluxDBHandler


class TestInfluxDBHandler:
"""Tests for InfluxDBHandler."""

def test_flatten_dict(self) -> None:
"""Test the dictionary flattening method."""
handler = InfluxDBHandler()
nested_dict = {"level1": {"level2": "value", "level2b": {"level3": "value3"}}}
flattened_dict = handler.flatten_dict(nested_dict)
expected_dict = {"level1.level2": "value", "level1.level2b.level3": "value3"}

assert flattened_dict == expected_dict

@pytest.mark.parametrize(
("input_dict", "expected_dict"),
[
(
{"key": "value", "nested": {"subkey": "subvalue"}},
{"key": "value", "nested.subkey": "subvalue"},
),
(
{
"key": "value",
"nested": {"subkey": "subvalue", "subkey2": {"subsubkey": "value"}},
},
{"key": "value", "nested.subkey": "subvalue", "nested.subkey2.subsubkey": "value"},
),
],
)
def test_flatten_dict_with_params(self, input_dict: dict, expected_dict: dict) -> None:
"""Test flatten_dict method with various inputs."""
handler = InfluxDBHandler()
assert handler.flatten_dict(input_dict) == expected_dict

def test_write_event(self, mocker) -> None: # noqa: ANN001
"""Test writing an event to InfluxDB."""
handler = InfluxDBHandler()
mock_write_api = mocker.patch.object(handler.write_api, "write", autospec=True)
event_data = {"event": "data"}
handler.write_event("test_measurement", event_data)
mock_write_api.assert_called_once()

def test_write_event_failure(self, mocker, caplog) -> None: # noqa: ANN001
"""Test writing an event to InfluxDB failure."""
handler = InfluxDBHandler()
mocker.patch.object(handler.write_api, "write", side_effect=ApiException)
event_data = {"event": "data"}

with caplog.at_level(logging.ERROR), pytest.raises(ApiException):
handler.write_event("test_measurement", event_data)

assert "Failed to write data to InfluxDB" in caplog.text

def test_close_handler(self, mocker) -> None: # noqa: ANN001
"""Test closing the InfluxDB handler."""
handler = InfluxDBHandler()
mock_close = mocker.patch.object(handler.client, "close", autospec=True)
handler.close()
mock_close.assert_called_once()

@mock.patch.dict(
@pytest.fixture(scope="module")
def influxdb_handler() -> InfluxDBHandler:
"""Fixture for InfluxDBHandler."""
return InfluxDBHandler()


def test_flatten_dict(influxdb_handler) -> None: # noqa: ANN001
"""Test flatten_dict method."""
nested_dict = {"level1": {"level2": "value", "level2b": {"level3": "value3"}}}
flattened_dict = influxdb_handler.flatten_dict(nested_dict)
expected_dict = {"level1.level2": "value", "level1.level2b.level3": "value3"}
assert flattened_dict == expected_dict


@pytest.mark.parametrize(
("input_dict", "expected_dict"),
[
(
{"key": "value", "nested": {"subkey": "subvalue"}},
{"key": "value", "nested.subkey": "subvalue"},
),
(
{"key": "value", "nested": {"subkey": "subvalue", "subkey2": {"subsubkey": "value"}}},
{"key": "value", "nested.subkey": "subvalue", "nested.subkey2.subsubkey": "value"},
),
],
)
def test_flatten_dict_with_params(influxdb_handler, input_dict, expected_dict) -> None: # noqa: ANN001
"""Test flatten_dict method with different parameters."""
assert influxdb_handler.flatten_dict(input_dict) == expected_dict


def test_write_event_success(influxdb_handler, mocker) -> None: # noqa: ANN001
"""Test write_event method success."""
mock_write_api = mocker.patch.object(influxdb_handler.write_api, "write", autospec=True)
event_data = {"event": "data"}
influxdb_handler.write_event("test_measurement", event_data)
mock_write_api.assert_called_once()


def test_write_event_failure(influxdb_handler, mocker, caplog) -> None: # noqa: ANN001
"""Test write_event method when write fails."""
mocker.patch.object(influxdb_handler.write_api, "write", side_effect=ApiException)
event_data = {"event": "data"}

with caplog.at_level(logging.ERROR), pytest.raises(ApiException):
influxdb_handler.write_event("test_measurement", event_data)

assert "Failed to write data to InfluxDB" in caplog.text


def test_close_handler(influxdb_handler, mocker) -> None: # noqa: ANN001
"""Test close method."""
mock_close = mocker.patch.object(influxdb_handler.client, "close", autospec=True)
influxdb_handler.close()
mock_close.assert_called_once()


def test_influxdb_handler_init() -> None:
"""Test InfluxDBHandler initialization."""
with mock.patch.dict(
os.environ, {"INFLUXDB_URL": "http://localhost:8086", "INFLUXDB_TOKEN": "test_token"}
)
def test_init_handler(self) -> None:
"""Test initializing the InfluxDB handler."""
):
handler = InfluxDBHandler()
assert handler.client.url == "http://localhost:8086"
assert handler.client.token == "test_token" # noqa: S105

def test_flatten_dict_with_complex_structure(self) -> None:
"""Test flatten_dict method with a more complex nested structure."""
handler = InfluxDBHandler()
complex_dict = {
"level1": {"level2": {"level3": {"level4": "value"}}},
"another_level1": "another_value",
}
flattened_dict = handler.flatten_dict(complex_dict)
expected_dict = {"level1.level2.level3.level4": "value", "another_level1": "another_value"}

assert flattened_dict == expected_dict
Loading

0 comments on commit aaf58aa

Please sign in to comment.