Skip to content

Commit

Permalink
Merge pull request #404 from robbrad/403-improve-unittest-coverage
Browse files Browse the repository at this point in the history
fix: unit test coverage
  • Loading branch information
OliverCullimore authored Oct 30, 2023
2 parents 18a012b + 62332e9 commit 8f2ee58
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
20 changes: 0 additions & 20 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,20 +0,0 @@
## Needed to temp fix this issue
## https://github.com/allure-framework/allure-python/issues/636
from allure_commons.lifecycle import AllureLifecycle
from allure_commons.model2 import TestResult
from allure_commons import plugin_manager

def custom_write_test_case(self, uuid=None):
test_result = self._pop_item(uuid=uuid, item_type=TestResult)
if test_result:
if test_result.parameters:
adj_parameters = []
for param in test_result.parameters:
if param.name != '_pytest_bdd_example':
# do not include parameters with "_pytest_bdd_example"
adj_parameters.append(param)
test_result.parameters = adj_parameters

plugin_manager.hook.report_result(result=test_result)

AllureLifecycle.write_test_case = custom_write_test_case
57 changes: 54 additions & 3 deletions uk_bin_collection/tests/test_common_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from unittest import mock
import pytest
from uk_bin_collection.common import *
from io import StringIO
from contextlib import redirect_stdout
from unittest.mock import patch, MagicMock
from selenium import webdriver


def test_check_postcode_valid():
Expand Down Expand Up @@ -87,11 +91,15 @@ def test_is_holiday():
result = is_holiday("2022, 12, 25")
assert result is True

def test_is_holiday():
date = "20"
result = is_holiday("2022, 12, 25")
assert result is True

def test_is_not_holiday():
def test_is_holiday_region():
date = "20"
result = is_holiday("2022, 12, 01")
assert result is False
result = is_holiday("2022, 12, 25", Region.Wales)
assert result is True


def test_remove_alpha_characters():
Expand Down Expand Up @@ -144,3 +152,46 @@ def test_get_weekday_dates_in_period_bad():
result = get_weekday_dates_in_period(now, 5, 7)
assert len(result) != 8
assert result[6] != "08/04/20232"

def test_get_next_occurrence_from_day_month_false():
result = get_next_occurrence_from_day_month(datetime(2023,12,1))
assert result == datetime(2023, 12, 1, 0, 0)

def test_get_next_occurrence_from_day_month_true():
result = get_next_occurrence_from_day_month(datetime(2023,9,1))
assert result == pd.Timestamp('2024-09-01 00:00:00')

def test_write_output_json():
council = "test_council"
content = '{"example": "data"}'
write_output_json(council, content)
cwd = os.getcwd()
outputs_path = os.path.join(cwd, "uk_bin_collection", "tests", "outputs", council + ".json")
result1 = os.path.exists(outputs_path)

with open(outputs_path, "r") as f:
read_content = f.read()

if os.path.exists(outputs_path):
os.remove(outputs_path)

assert result1 == True
assert read_content == content

def test_write_output_json_fail(capsys, monkeypatch):
def mock_os_path_exists(path):
return False # Simulate the path not existing

monkeypatch.setattr(os.path, 'exists', mock_os_path_exists)

council = "test_council"
content = '{"example": "data"}'
write_output_json(council, content)

captured = capsys.readouterr()
assert "Exception encountered: Unable to save Output JSON file for the council." in captured.out
assert "Please check you're running developer mode" in captured.out

def test_create_webdriver():
result = create_webdriver()
assert result.name == 'chrome'

0 comments on commit 8f2ee58

Please sign in to comment.