-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from khalford/tests
Tests
- Loading branch information
Showing
13 changed files
with
182 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[report] | ||
|
||
exclude_lines = | ||
if __name__ == .__main__.: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.idea | ||
main_version | ||
branch_version | ||
branch_version | ||
src/__pycache__/* | ||
tests/__pycache__/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
services: | ||
self-test: | ||
image: some/test:0.4.7 | ||
image: some/test:0.4.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[pytest] | ||
pythonpath = src | ||
testpaths = tests | ||
python_files = *.py | ||
python_functions = test_* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Tests for comparison.CompareAppVersion""" | ||
from unittest.mock import patch, mock_open | ||
from pathlib import Path | ||
from packaging.version import Version | ||
import pytest | ||
from comparison import CompareAppVersion, VersionNotUpdated | ||
|
||
|
||
@pytest.fixture(name="instance", scope="function") | ||
def instance_fixture(): | ||
"""Provide a fixture instance for the tests""" | ||
return CompareAppVersion() | ||
|
||
|
||
@patch("comparison.CompareAppVersion.compare") | ||
@patch("comparison.CompareAppVersion.get_version") | ||
@patch("comparison.CompareAppVersion.read_files") | ||
def test_run(mock_read, mock_get_version, mock_compare, instance): | ||
"""Test the run method makes correct calls.""" | ||
mock_path1 = Path("mock1") | ||
mock_path2 = Path("mock2") | ||
mock_read.return_value = ("1.0.0", "1.0.1") | ||
mock_get_version.side_effect = [Version("1.0.0"), Version("1.0.0")] | ||
mock_compare.return_value = True | ||
res = instance.run(mock_path1, mock_path2) | ||
mock_read.assert_called_once_with(mock_path1, mock_path2) | ||
mock_get_version.assert_any_call("1.0.0") | ||
mock_get_version.assert_any_call("1.0.1") | ||
mock_compare.assert_called_once_with(Version("1.0.0"), Version("1.0.0")) | ||
assert res | ||
|
||
|
||
@patch("comparison.CompareAppVersion.compare") | ||
@patch("comparison.CompareAppVersion.get_version") | ||
@patch("comparison.CompareAppVersion.read_files") | ||
def test_run_fails(mock_read, _, mock_compare, instance): | ||
"""Test the run method fails.""" | ||
mock_read.return_value = ("mock1", "mock2") | ||
mock_compare.side_effect = VersionNotUpdated() | ||
with pytest.raises(VersionNotUpdated): | ||
instance.run(Path("mock1"), Path("mock2")) | ||
|
||
|
||
def test_read_files(instance): | ||
"""Test the read files method returns a tuple""" | ||
with patch("builtins.open", mock_open(read_data="1.0.0")): | ||
res = instance.read_files(Path("mock1"), Path("mock2")) | ||
assert res == ("1.0.0", "1.0.0") | ||
|
||
|
||
def test_get_version(instance): | ||
"""Test a version object is returned""" | ||
res = instance.get_version("1.0.0") | ||
assert isinstance(res, Version) | ||
|
||
|
||
def test_compare_pass(instance): | ||
"""Test that the compare returns true for a valid comparison""" | ||
res = instance.compare(Version("1.0.0"), Version("1.0.1")) | ||
assert res != VersionNotUpdated | ||
|
||
|
||
def test_compare_fails(instance): | ||
"""Test that the compare returns an error for an invalid comparison""" | ||
res = instance.compare(Version("1.0.1"), Version("1.0.0")) | ||
assert res == VersionNotUpdated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"""Tests for comparison.CompareComposeVersion""" | ||
from unittest.mock import patch, mock_open | ||
from pathlib import Path | ||
import pytest | ||
from packaging.version import Version | ||
from comparison import CompareComposeVersion, VersionNotUpdated | ||
|
||
|
||
@pytest.fixture(name="instance", scope="function") | ||
def instance_fixture(): | ||
"""Provide a fixture instance for the tests""" | ||
return CompareComposeVersion() | ||
|
||
|
||
@patch("comparison.CompareComposeVersion.compare") | ||
@patch("comparison.CompareComposeVersion.get_version") | ||
@patch("comparison.CompareComposeVersion.read_files") | ||
def test_run(mock_read, mock_get_version, mock_compare, instance): | ||
"""Test the run method makes correct calls.""" | ||
mock_path1 = Path("mock1") | ||
mock_path2 = Path("mock2") | ||
mock_read.return_value = ("1.0.0", "1.0.0") | ||
mock_get_version.side_effect = [Version("1.0.0"), Version("1.0.0")] | ||
mock_compare.return_value = True | ||
res = instance.run(mock_path1, mock_path2) | ||
mock_read.assert_called_once_with(mock_path1, mock_path2) | ||
mock_get_version.assert_any_call("1.0.0") | ||
mock_compare.assert_called_once_with(Version("1.0.0"), Version("1.0.0")) | ||
assert res | ||
|
||
|
||
@patch("comparison.CompareComposeVersion.compare") | ||
@patch("comparison.CompareComposeVersion.get_version") | ||
@patch("comparison.CompareComposeVersion.read_files") | ||
def test_run_fails(mock_read, _, mock_compare, instance): | ||
"""Test the run method fails.""" | ||
mock_read.return_value = ("1.0.0", "1.0.1") | ||
mock_compare.side_effect = VersionNotUpdated() | ||
with pytest.raises(VersionNotUpdated): | ||
instance.run(Path("mock1"), Path("mock2")) | ||
|
||
|
||
def test_read_files(instance): | ||
"""Test the read files method returns a tuple""" | ||
with patch("builtins.open", mock_open(read_data="1.0.0")): | ||
res = instance.read_files(Path("mock1"), Path("mock2")) | ||
assert res == ("1.0.0", ["1.0.0"]) | ||
|
||
|
||
def test_get_version(instance): | ||
"""Test a version object is returned""" | ||
res = instance.get_version(["image: some/image:1.0.0\n"]) | ||
assert isinstance(res, Version) | ||
|
||
|
||
def test_compare_pass(instance): | ||
"""Test that the compare returns true for a valid comparison""" | ||
res = instance.compare(Version("1.0.0"), Version("1.0.0")) | ||
assert res != VersionNotUpdated | ||
|
||
|
||
def test_compare_fails(instance): | ||
"""Test that the compare returns an error for an invalid comparison""" | ||
res = instance.compare(Version("1.0.1"), Version("1.0.0")) | ||
assert res == VersionNotUpdated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Tests for main""" | ||
from unittest.mock import patch, mock_open | ||
from pathlib import Path | ||
from main import main | ||
|
||
|
||
@patch("main.CompareComposeVersion") | ||
@patch("main.CompareAppVersion") | ||
@patch("main.os") | ||
def test_main(mock_os, mock_compare_app, mock_compare_compose): | ||
"""Test the main method runs correctly.""" | ||
mock_os.environ.get.side_effect = [Path("app"), Path("compose"), Path("workspace")] | ||
with patch("builtins.open", mock_open(read_data="1.0.0")): | ||
main() | ||
mock_os.environ.get.assert_any_call("INPUT_APP_VERSION_PATH") | ||
mock_os.environ.get.assert_any_call("INPUT_DOCKER_COMPOSE_PATH") | ||
mock_os.environ.get.assert_any_call("GITHUB_WORKSPACE") | ||
mock_branch_path = Path("workspace") / "branch" | ||
mock_main_path = Path("workspace") / "main" | ||
mock_compare_app.return_value.run.assert_called_once_with( | ||
mock_main_path / "app", mock_branch_path / "app" | ||
) | ||
mock_compare_compose.return_value.run.assert_called_once_with( | ||
mock_branch_path / "app", mock_branch_path / "compose" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.4.7 | ||
0.4.8 |