Skip to content

Commit

Permalink
Merge pull request #796 from robbrad/795_unit_test_coverage
Browse files Browse the repository at this point in the history
fix: #795 unit test coverage
  • Loading branch information
dp247 authored Sep 2, 2024
2 parents 79693ca + 6738b9f commit e87f91a
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 4 deletions.
32 changes: 32 additions & 0 deletions uk_bin_collection/tests/test_collect_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,35 @@ def test_output_json():
output = agbdc.output_json(bin_data)
assert type(output) == str
assert output == '{\n "bin": ""\n}'

class ConcreteGetBinDataClass(agbdc):
"""Concrete implementation of the abstract class to test abstract methods."""
def parse_data(self, page: str, **kwargs) -> dict:
return {"mock_key": "mock_value"}

@pytest.fixture
def concrete_class_instance():
return ConcreteGetBinDataClass()

def test_get_and_parse_data_no_skip_get_url(concrete_class_instance):
mock_page = "mocked page content"
mock_parsed_data = {"mock_key": "mock_value"}

with mock.patch.object(concrete_class_instance, 'get_data', return_value=mock_page) as mock_get_data, \
mock.patch.object(concrete_class_instance, 'parse_data', return_value=mock_parsed_data) as mock_parse_data:

result = concrete_class_instance.get_and_parse_data("http://example.com")

mock_get_data.assert_called_once_with("http://example.com")
mock_parse_data.assert_called_once_with(mock_page, url="http://example.com")
assert result == mock_parsed_data

def test_get_and_parse_data_skip_get_url(concrete_class_instance):
mock_parsed_data = {"mock_key": "mock_value"}

with mock.patch.object(concrete_class_instance, 'parse_data', return_value=mock_parsed_data) as mock_parse_data:

result = concrete_class_instance.get_and_parse_data("http://example.com", skip_get_url=True)

mock_parse_data.assert_called_once_with("", url="http://example.com", skip_get_url=True)
assert result == mock_parsed_data
101 changes: 97 additions & 4 deletions uk_bin_collection/tests/test_common_functions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from contextlib import redirect_stdout
from io import StringIO
from unittest import mock
from unittest.mock import MagicMock, mock_open, patch

import pytest
from uk_bin_collection.common import *
from io import StringIO
from contextlib import redirect_stdout
from unittest.mock import patch, MagicMock, mock_open
from selenium.common.exceptions import WebDriverException
from uk_bin_collection.common import *
from urllib3.exceptions import MaxRetryError


Expand Down Expand Up @@ -342,3 +343,95 @@ def test_create_webdriver_remote_failure():
# Test the scenario where the remote server is not available
with pytest.raises(MaxRetryError) as exc_info:
create_webdriver("http://invalid-url:4444", False)


def test_create_webdriver_remote_with_session_name():
# Test creating a remote WebDriver with a session name
session_name = "test-session"
web_driver_url = (
"http://localhost:4444/wd/hub" # Use a valid remote WebDriver URL for testing
)

# Mock the Remote WebDriver
with mock.patch("uk_bin_collection.common.webdriver.Remote") as mock_remote:
mock_instance = mock.MagicMock()
mock_instance.name = "chrome"
mock_remote.return_value = mock_instance

# Call the function with the test parameters
result = create_webdriver(web_driver=web_driver_url, session_name=session_name)

# Check if the session name was set in capabilities
args, kwargs = mock_remote.call_args
options = kwargs["options"]
assert options._caps.get("se:name") == session_name
assert result.name == "chrome"


def test_string_with_numbers():
assert has_numbers("abc123") is True
assert has_numbers("1a2b3c") is True
assert has_numbers("123") is True


def test_string_without_numbers():
assert has_numbers("abcdef") is False
assert has_numbers("ABC") is False
assert has_numbers("!@#") is False


def test_empty_string():
assert has_numbers("") is False


def test_string_with_only_numbers():
assert has_numbers("1234567890") is True


def test_string_with_special_characters_and_numbers():
assert has_numbers("!@#123$%^") is True
assert has_numbers("abc!@#123") is True


def test_string_with_whitespace_and_numbers():
assert has_numbers(" 123 ") is True
assert has_numbers("abc 123") is True


@pytest.mark.parametrize(
"today_str, day_name, expected",
[
(
"2024-09-02",
"Monday",
"09/09/2024",
), # Today is Monday, so next Monday is in 7 days
(
"2024-09-02",
"Tuesday",
"03/09/2024",
), # Today is Monday, next Tuesday is tomorrow
(
"2024-09-02",
"Sunday",
"08/09/2024",
), # Today is Monday, next Sunday is in 6 days
(
"2024-09-03",
"Wednesday",
"04/09/2024",
), # Today is Tuesday, next Wednesday is tomorrow
(
"2024-09-03",
"Monday",
"09/09/2024",
), # Today is Tuesday, next Monday is in 6 days
],
)
def test_get_next_day_of_week(today_str, day_name, expected):
mock_today = datetime.strptime(today_str, "%Y-%m-%d")
with patch("datetime.datetime") as mock_datetime:
mock_datetime.now.return_value = mock_today
mock_datetime.strptime = datetime.strptime # to handle strptime within patch
result = get_next_day_of_week(day_name)
assert result == expected
27 changes: 27 additions & 0 deletions uk_bin_collection/tests/test_conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest

# Test the command-line options

def test_headless_mode(pytestconfig):
# Simulate pytest command-line option
headless_mode_value = pytestconfig.getoption("--headless")
assert headless_mode_value == "True" # This should match the default value

def test_local_browser(pytestconfig):
local_browser_value = pytestconfig.getoption("--local_browser")
assert local_browser_value == "False" # This should match the default value

def test_selenium_url(pytestconfig):
selenium_url_value = pytestconfig.getoption("--selenium_url")
assert selenium_url_value == "http://localhost:4444" # This should match the default value

# Test the fixtures

def test_headless_mode_fixture(headless_mode):
assert headless_mode == "True" # This should match the default value

def test_local_browser_fixture(local_browser):
assert local_browser == "False" # This should match the default value

def test_selenium_url_fixture(selenium_url):
assert selenium_url == "http://localhost:4444" # This should match the default value

0 comments on commit e87f91a

Please sign in to comment.