From 524ca94fcebcae4d418f2e42d1ca88b8b9bed1c7 Mon Sep 17 00:00:00 2001 From: Oliwia Zaremba Date: Tue, 14 May 2019 13:42:45 +0200 Subject: [PATCH 1/6] Change the test structure and adapt Makefile config Signed-off-by: Oliwia Zaremba --- common.mk | 5 +++-- .../functional}/Makefile | 0 .../functional}/README.md | 0 .../functional}/test_examples.py | 2 +- .../functional}/test_version.py | 0 .../plugins/test_contracts.py | 2 +- {transformer => tests}/plugins/test_dummy.py | 0 .../plugins/test_resolve.py | 2 +- .../plugins/test_sanitize_headers.py | 2 +- .../transformer}/test_blacklist.py | 0 .../transformer}/test_cli.py | 2 +- .../transformer}/test_locust.py | 2 +- .../transformer}/test_naming.py | 0 .../transformer}/test_python.py | 0 .../transformer}/test_request.py | 0 .../transformer}/test_scenario.py | 0 .../transformer}/test_task.py | 0 .../transformer}/test_transform.py | 0 transformer/conftest.py | 22 +++++++++++++++++++ 19 files changed, 31 insertions(+), 8 deletions(-) rename {functional-tests => tests/functional}/Makefile (100%) rename {functional-tests => tests/functional}/README.md (100%) rename {functional-tests => tests/functional}/test_examples.py (86%) rename {functional-tests => tests/functional}/test_version.py (100%) rename {transformer => tests}/plugins/test_contracts.py (98%) rename {transformer => tests}/plugins/test_dummy.py (100%) rename {transformer => tests}/plugins/test_resolve.py (97%) rename {transformer => tests}/plugins/test_sanitize_headers.py (96%) rename {transformer => tests/transformer}/test_blacklist.py (100%) rename {transformer => tests/transformer}/test_cli.py (96%) rename {transformer => tests/transformer}/test_locust.py (99%) rename {transformer => tests/transformer}/test_naming.py (100%) rename {transformer => tests/transformer}/test_python.py (100%) rename {transformer => tests/transformer}/test_request.py (100%) rename {transformer => tests/transformer}/test_scenario.py (100%) rename {transformer => tests/transformer}/test_task.py (100%) rename {transformer => tests/transformer}/test_transform.py (100%) create mode 100644 transformer/conftest.py diff --git a/common.mk b/common.mk index aa90b4e..f1abdd5 100644 --- a/common.mk +++ b/common.mk @@ -14,12 +14,13 @@ configure: .make/configure # Runs pytest with coverage reporting. .PHONY: unittest unittest: configure - poetry run pytest --failed-first --cov-config .coveragerc --cov-report xml --cov=. transformer/ + poetry run pytest --failed-first --cov-config .coveragerc --cov-report xml --cov=. tests/transformer/ + poetry run pytest --failed-first --cov-config .coveragerc --cov-report xml --cov=. tests/plugins/ poetry run pytest --failed-first update-version.py .PHONY: functest functest: configure - $(MAKE) -C functional-tests/ + $(MAKE) -C tests/functional/ .PHONY: functest test: unittest functest diff --git a/functional-tests/Makefile b/tests/functional/Makefile similarity index 100% rename from functional-tests/Makefile rename to tests/functional/Makefile diff --git a/functional-tests/README.md b/tests/functional/README.md similarity index 100% rename from functional-tests/README.md rename to tests/functional/README.md diff --git a/functional-tests/test_examples.py b/tests/functional/test_examples.py similarity index 86% rename from functional-tests/test_examples.py rename to tests/functional/test_examples.py index a98310b..88cf14f 100644 --- a/functional-tests/test_examples.py +++ b/tests/functional/test_examples.py @@ -3,7 +3,7 @@ def run_example(example_name: str) -> subprocess.CompletedProcess: - har_path = Path(__file__).parent.parent.joinpath("examples", example_name) + har_path = Path(__file__).parent.parent.parent.joinpath("examples", example_name) return subprocess.run( ["transformer", str(har_path)], timeout=2, check=True, stdout=subprocess.PIPE ) diff --git a/functional-tests/test_version.py b/tests/functional/test_version.py similarity index 100% rename from functional-tests/test_version.py rename to tests/functional/test_version.py diff --git a/transformer/plugins/test_contracts.py b/tests/plugins/test_contracts.py similarity index 98% rename from transformer/plugins/test_contracts.py rename to tests/plugins/test_contracts.py index 15c3a73..fb530be 100644 --- a/transformer/plugins/test_contracts.py +++ b/tests/plugins/test_contracts.py @@ -3,7 +3,7 @@ from hypothesis.strategies import from_type from transformer.plugins import apply, group_by_contract -from .contracts import ( +from transformer.plugins.contracts import ( Contract, plugin, contract, diff --git a/transformer/plugins/test_dummy.py b/tests/plugins/test_dummy.py similarity index 100% rename from transformer/plugins/test_dummy.py rename to tests/plugins/test_dummy.py diff --git a/transformer/plugins/test_resolve.py b/tests/plugins/test_resolve.py similarity index 97% rename from transformer/plugins/test_resolve.py rename to tests/plugins/test_resolve.py index 0d38a38..227b543 100644 --- a/transformer/plugins/test_resolve.py +++ b/tests/plugins/test_resolve.py @@ -10,7 +10,7 @@ from hypothesis._strategies import permutations from transformer.plugins.contracts import plugin, Contract -from .resolve import load_plugins_from_module, resolve, NoPluginError +from transformer.plugins.resolve import load_plugins_from_module, resolve, NoPluginError @pytest.fixture() diff --git a/transformer/plugins/test_sanitize_headers.py b/tests/plugins/test_sanitize_headers.py similarity index 96% rename from transformer/plugins/test_sanitize_headers.py rename to tests/plugins/test_sanitize_headers.py index eed84c2..b2b0086 100644 --- a/transformer/plugins/test_sanitize_headers.py +++ b/tests/plugins/test_sanitize_headers.py @@ -3,7 +3,7 @@ from transformer.request import HttpMethod, Request, CaseInsensitiveDict from transformer.task import Task2 -from .sanitize_headers import plugin +from transformer.plugins.sanitize_headers import plugin def test_its_name_is_resolvable(): diff --git a/transformer/test_blacklist.py b/tests/transformer/test_blacklist.py similarity index 100% rename from transformer/test_blacklist.py rename to tests/transformer/test_blacklist.py diff --git a/transformer/test_cli.py b/tests/transformer/test_cli.py similarity index 96% rename from transformer/test_cli.py rename to tests/transformer/test_cli.py index cef7c3c..4e199c2 100644 --- a/transformer/test_cli.py +++ b/tests/transformer/test_cli.py @@ -1,6 +1,6 @@ from pathlib import Path -from .cli import read_config +from transformer.cli import read_config class TestReadConfig: diff --git a/transformer/test_locust.py b/tests/transformer/test_locust.py similarity index 99% rename from transformer/test_locust.py rename to tests/transformer/test_locust.py index 5c7a861..4947281 100644 --- a/transformer/test_locust.py +++ b/tests/transformer/test_locust.py @@ -11,7 +11,7 @@ from transformer.task import Task, TIMEOUT from transformer.plugins import plugin, Contract from transformer.task import Task2 -from ._version import __version__ +from transformer._version import __version__ class TestLocustfile: diff --git a/transformer/test_naming.py b/tests/transformer/test_naming.py similarity index 100% rename from transformer/test_naming.py rename to tests/transformer/test_naming.py diff --git a/transformer/test_python.py b/tests/transformer/test_python.py similarity index 100% rename from transformer/test_python.py rename to tests/transformer/test_python.py diff --git a/transformer/test_request.py b/tests/transformer/test_request.py similarity index 100% rename from transformer/test_request.py rename to tests/transformer/test_request.py diff --git a/transformer/test_scenario.py b/tests/transformer/test_scenario.py similarity index 100% rename from transformer/test_scenario.py rename to tests/transformer/test_scenario.py diff --git a/transformer/test_task.py b/tests/transformer/test_task.py similarity index 100% rename from transformer/test_task.py rename to tests/transformer/test_task.py diff --git a/transformer/test_transform.py b/tests/transformer/test_transform.py similarity index 100% rename from transformer/test_transform.py rename to tests/transformer/test_transform.py diff --git a/transformer/conftest.py b/transformer/conftest.py new file mode 100644 index 0000000..3980a10 --- /dev/null +++ b/transformer/conftest.py @@ -0,0 +1,22 @@ +import json + +import pytest + + +@pytest.fixture +def dummy_har_dict(): + return { + "log": { + "entries": [ + { + "startedDateTime": "2018-01-01", + "request": {"method": "GET", "url": "https://www.zalando.de"}, + } + ] + } + } + + +@pytest.fixture +def dummy_har_string(): + return json.dumps(dummy_har_dict()) From 4fe24d252e5e761e5fc45f682b8c15ebc31e5be3 Mon Sep 17 00:00:00 2001 From: Oliwia Zaremba Date: Fri, 17 May 2019 15:02:13 +0200 Subject: [PATCH 2/6] Use pytest fixtures and mocks for clean, helper-less tests Signed-off-by: Oliwia Zaremba --- poetry.lock | 14 ++++++- pyproject.toml | 1 + tests/conftest.py | 40 +++++++++++++++++++ tests/plugins/test_dummy.py | 5 +-- tests/transformer/test_blacklist.py | 10 +---- tests/transformer/test_python.py | 10 +++-- tests/transformer/test_scenario.py | 62 ++++++++++++++--------------- tests/transformer/test_task.py | 3 -- tests/transformer/test_transform.py | 17 ++++---- transformer/helpers.py | 19 --------- 10 files changed, 100 insertions(+), 81 deletions(-) create mode 100644 tests/conftest.py delete mode 100644 transformer/helpers.py diff --git a/poetry.lock b/poetry.lock index 632bc41..1047b06 100644 --- a/poetry.lock +++ b/poetry.lock @@ -469,6 +469,17 @@ version = "2.6.1" coverage = ">=4.4" pytest = ">=3.6" +[[package]] +category = "dev" +description = "Thin-wrapper around the mock package for easier use with py.test" +name = "pytest-mock" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +version = "1.10.4" + +[package.dependencies] +pytest = ">=2.7" + [[package]] category = "main" description = "Extensions to the standard Python datetime module" @@ -623,7 +634,7 @@ version = "0.14.1" docs = ["sphinx", "sphinx-autodoc-typehints", "sphinx-issues"] [metadata] -content-hash = "e212964c7be27b51c430985179a42b50be7db57fd5d51df57a4befd06e3535aa" +content-hash = "2026314ed2b4cf02f25d69dbf505ccf4004afc98a273b668c9ce16c5d266297c" python-versions = "^3.6" [metadata.hashes] @@ -675,6 +686,7 @@ pygments = ["5ffada19f6203563680669ee7f53b64dabbeb100eb51b61996085e99c03b284a", pyparsing = ["66c9268862641abcac4a96ba74506e594c884e3f57690a696d21ad8210ed667a", "f6c5ef0d7480ad048c054c37632c67fca55299990fff127850181659eea33fc3"] pytest = ["3f193df1cfe1d1609d4c583838bea3d532b18d6160fd3f55c9447fdca30848ec", "e246cf173c01169b9617fc07264b7b1316e78d7a650055235d6d897bc80d9660"] pytest-cov = ["0ab664b25c6aa9716cbf203b17ddb301932383046082c081b9848a0edf5add33", "230ef817450ab0699c6cc3c9c8f7a829c34674456f2ed8df1fe1d39780f7c87f"] +pytest-mock = ["43ce4e9dd5074993e7c021bb1c22cbb5363e612a2b5a76bc6d956775b10758b7", "5bf5771b1db93beac965a7347dc81c675ec4090cb841e49d9d34637a25c30568"] python-dateutil = ["7e6584c74aeed623791615e26efd690f29817a27c73085b78e4bad02493df2fb", "c89805f6f4d64db21ed966fda138f8a5ed7a4fdbc1a8ee329ce1b74e3c74da9e"] pytz = ["32b0891edff07e28efe91284ed9c31e123d84bea3fd98e1f72be2508f43ef8d9", "d5f05e487007e29e03409f9398d074e158d920d36eb82eaf66fb1136b0c5374c"] pytzdata = ["9626e42fd9df77b16aedbd909d1e5fda839be47966adb7089b990f3452c45dd8", "dddaaf4f1717820a6fdcac94057e03c1a15b3829a44d9eaf19988917977db408"] diff --git a/pyproject.toml b/pyproject.toml index dd47ca9..fd3535a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,6 +45,7 @@ docs = ["sphinx", "sphinx-autodoc-typehints", "sphinx-issues"] locustio = "^0.9.0" pytest = "^3.9" pytest-cov = "*" +pytest-mock = "^1.10" hypothesis = "^4.4" black = {version = "*",allows-prereleases = true} flake8 = "^3.7" diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..e65811d --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,40 @@ +import json +from pytest import fixture + + +@fixture +def mock_open(mocker): + return mocker.patch("builtins.open") + + +@fixture +def dummy_har_dict(): + return { + "log": { + "entries": [ + { + "startedDateTime": "2018-01-01", + "request": {"method": "GET", "url": "https://www.zalando.de"}, + } + ] + } + } + + +@fixture +def dummy_har_string(dummy_har_dict): + return json.dumps(dummy_har_dict) + + +@fixture +def mocked_har(mocker, dummy_har_dict): + j = mocker.patch("transformer.scenario.json.load") + j.return_value = dummy_har_dict + return j + + +@fixture +def mock_paths(mocker): + mocker.patch("transformer.scenario.Path.is_dir").return_value = False + mocker.patch("transformer.scenario.Path.iterdir").return_value = () + mocker.patch("transformer.scenario.Path.open") diff --git a/tests/plugins/test_dummy.py b/tests/plugins/test_dummy.py index ce22701..dccbd23 100644 --- a/tests/plugins/test_dummy.py +++ b/tests/plugins/test_dummy.py @@ -3,12 +3,11 @@ from pathlib import Path import transformer -from transformer.helpers import DUMMY_HAR_STRING -def test_dummy_plugin_works(tmp_path: Path, caplog): +def test_dummy_plugin_works(tmp_path: Path, caplog, dummy_har_string): har_path = tmp_path / "test.har" - har_path.write_text(DUMMY_HAR_STRING) + har_path.write_text(dummy_har_string) caplog.set_level(logging.INFO) diff --git a/tests/transformer/test_blacklist.py b/tests/transformer/test_blacklist.py index 36dd00e..a24d1bc 100644 --- a/tests/transformer/test_blacklist.py +++ b/tests/transformer/test_blacklist.py @@ -4,13 +4,11 @@ import os import logging -from unittest.mock import patch - from transformer.blacklist import on_blacklist, from_file as read_blacklist class TestBlacklist: - @patch("builtins.open") + def test_it_returns_false_and_logs_error_if_the_blacklist_does_not_exist( self, mock_open, caplog ): @@ -21,32 +19,26 @@ def test_it_returns_false_and_logs_error_if_the_blacklist_does_not_exist( assert on_blacklist(blacklist, "whatever") is False assert f"Could not read blacklist file {os.getcwd()}/.urlignore" in caplog.text - @patch("builtins.open") def test_it_returns_false_if_the_blacklist_is_empty(self, mock_open): mock_open.return_value = io.StringIO("") assert on_blacklist(read_blacklist(), "") is False - @patch("builtins.open") def test_it_returns_false_if_url_is_not_on_blacklist(self, mock_open): mock_open.return_value = io.StringIO("www.amazon.com") assert on_blacklist(read_blacklist(), "www.zalando.de") is False - @patch("builtins.open") def test_it_returns_true_if_url_is_on_blacklist(self, mock_open): mock_open.return_value = io.StringIO("www.google.com\nwww.amazon.com") assert on_blacklist(read_blacklist(), "www.amazon.com") is True - @patch("builtins.open") def test_it_returns_true_if_a_partial_match_is_found(self, mock_open): mock_open.return_value = io.StringIO("www.amazon.com") assert on_blacklist(read_blacklist(), "http://www.amazon.com/") is True - @patch("builtins.open") def test_it_ignores_whitespace_only_lines(self, mock_open): mock_open.return_value = io.StringIO(" \n \r\nwww.amazon.com") assert on_blacklist(read_blacklist(), "www.zalando.de") is False - @patch("builtins.open") def test_it_removes_duplicate_entries(self, mock_open): mock_open.return_value = io.StringIO("\nwww.amazon.com" * 3) assert len(read_blacklist()) == 1 diff --git a/tests/transformer/test_python.py b/tests/transformer/test_python.py index 1063619..34ed505 100644 --- a/tests/transformer/test_python.py +++ b/tests/transformer/test_python.py @@ -1,7 +1,6 @@ import pprint import string from typing import List -from unittest.mock import patch import pytest from hypothesis import given @@ -143,6 +142,10 @@ def test_lines_returns_block_lines_without_empty_top_and_bottom(self): X = py.Line.INDENT_UNIT + @pytest.fixture + def indent_unit(self, mocker): + return mocker.patch("transformer.python.Line.INDENT_UNIT", " ") + @pytest.mark.parametrize( "input_block, indent_level, expected", [ @@ -169,13 +172,12 @@ def test_lines_returns_block_lines_without_empty_top_and_bottom(self): ], ) def test_lines_indents_correctly( - self, input_block: str, indent_level: int, expected: str + self, input_block: str, indent_level: int, expected: str, indent_unit ): lines = py.OpaqueBlock(input_block).lines(indent_level) print("lines =") pprint.pprint(lines) - with patch("transformer.python.Line.INDENT_UNIT", " "): - assert "\n".join(str(line) for line in lines) == expected + assert "\n".join(str(line) for line in lines) == expected @given(opaque_blocks, indent_levels, ascii_inline_text(min_size=1)) def test_lines_displays_comment_always_above( diff --git a/tests/transformer/test_scenario.py b/tests/transformer/test_scenario.py index e3b8577..77c66a3 100644 --- a/tests/transformer/test_scenario.py +++ b/tests/transformer/test_scenario.py @@ -3,13 +3,12 @@ import string from pathlib import Path from typing import List -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock import pytest from hypothesis import given from hypothesis.strategies import lists, text, recursive, tuples -from transformer.helpers import DUMMY_HAR_STRING, _DUMMY_HAR_DICT from transformer.scenario import Scenario, SkippableScenarioError, WeightValueError from transformer.task import Task @@ -23,24 +22,21 @@ class TestScenario: - @patch("transformer.scenario.Path.is_dir", MagicMock(return_value=False)) - @patch("transformer.scenario.Path.iterdir", MagicMock(return_value=())) - @patch("transformer.scenario.Path.open") - @patch("transformer.scenario.json.load", MagicMock(return_value=_DUMMY_HAR_DICT)) + @given(paths=lists(paths, unique=True, min_size=2)) - def test_names_are_unique(*_, paths: List[Path]): + def test_names_are_unique(self, paths: List[Path], mocked_har, mock_paths): scenario_names = [Scenario.from_path(path).name for path in paths] assert sorted(set(scenario_names)) == sorted(scenario_names) assert len(paths) == len(scenario_names) - def test_creation_from_scenario_directory_with_weight_file(self, tmp_path: Path): + def test_creation_from_scenario_directory_with_weight_file(self, tmp_path: Path, dummy_har_string): root_path = tmp_path / "some-path" root_path.mkdir() expected_weight = 7 root_path.with_suffix(".weight").write_text(str(expected_weight)) nb_har_files = 2 for i in range(nb_har_files): - root_path.joinpath(f"{i}.har").write_text(DUMMY_HAR_STRING) + root_path.joinpath(f"{i}.har").write_text(dummy_har_string) result = Scenario.from_path(root_path) assert len(result.children) == nb_har_files @@ -54,11 +50,11 @@ def test_on_har_raises_error_with_incorrect_har(self, tmp_path: Path): with pytest.raises(SkippableScenarioError): Scenario.from_path(not_har_path) - def test_on_dir_ignores_some_incorrect_hars(self, tmp_path: Path): + def test_on_dir_ignores_some_incorrect_hars(self, tmp_path: Path, dummy_har_string): not_har_path = tmp_path / "not.har" not_har_path.write_text("not JSON!") har_path = tmp_path / "good.har" - har_path.write_text(DUMMY_HAR_STRING) + har_path.write_text(dummy_har_string) scenario = Scenario.from_path(tmp_path) assert len(scenario.children) == 1 @@ -72,9 +68,9 @@ def test_on_dir_raises_error_with_all_incorrect_hars(self, tmp_path: Path): Scenario.from_path(tmp_path) def test_on_dir_with_dangling_weights_raises_error( - self, tmp_path: Path, caplog + self, tmp_path: Path, dummy_har_string, caplog ): - (tmp_path / "ok.har").write_text(DUMMY_HAR_STRING) + (tmp_path / "ok.har").write_text(dummy_har_string) (tmp_path / "fail.weight").write_text("7") caplog.set_level(logging.INFO) @@ -127,31 +123,31 @@ def test_group_records_global_code_blocks_uniquely(self): "t2b": ["xyz"], } - def test_without_weight_file_has_weight_1(self, tmp_path: Path): + def test_without_weight_file_has_weight_1(self, tmp_path: Path, dummy_har_string): har_path = tmp_path / "test.har" - har_path.write_text(DUMMY_HAR_STRING) + har_path.write_text(dummy_har_string) assert Scenario.from_path(har_path).weight == 1 - def test_with_weight_file_has_corresponding_weight(self, tmp_path: Path): + def test_with_weight_file_has_corresponding_weight(self, tmp_path: Path, dummy_har_string): weight_path = tmp_path / "test.weight" weight_path.write_text("74") har_path = tmp_path / "test.har" - har_path.write_text(DUMMY_HAR_STRING) + har_path.write_text(dummy_har_string) assert Scenario.from_path(har_path).weight == 74 @pytest.mark.parametrize("weight", [0, -2, 2.1, -2.1, "NaN", "abc", " "]) def test_with_invalid_weight_raises_error_and_never_skips( - self, tmp_path: Path, weight + self, tmp_path: Path, dummy_har_string, weight ): legit_har_path = tmp_path / "legit.har" - legit_har_path.write_text(DUMMY_HAR_STRING) + legit_har_path.write_text(dummy_har_string) bad_weight_path = tmp_path / "test.weight" bad_weight_path.write_text(str(weight)) bad_weight_har_path = tmp_path / "test.har" - bad_weight_har_path.write_text(DUMMY_HAR_STRING) + bad_weight_har_path.write_text(dummy_har_string) with pytest.raises(WeightValueError): # If from_path was skipping the bad scenario/weight pair, it @@ -160,7 +156,7 @@ def test_with_invalid_weight_raises_error_and_never_skips( Scenario.from_path(tmp_path) def test_with_many_weight_files_selects_weight_based_on_name( - self, tmp_path: Path + self, tmp_path: Path, dummy_har_string ): expected_weight_path = tmp_path / "test.weight" expected_weight_path.write_text("7") @@ -172,14 +168,14 @@ def test_with_many_weight_files_selects_weight_based_on_name( second_wrong_weight_path.write_text("4") har_path = tmp_path / "test.har" - har_path.write_text(DUMMY_HAR_STRING) + har_path.write_text(dummy_har_string) assert Scenario.from_path(har_path).weight == 7 - def test_uses_full_path_for_scenario_name(self, tmp_path: Path): + def test_uses_full_path_for_scenario_name(self, tmp_path: Path, dummy_har_string): har_basename = "e3ee4a1ef0817cde0a0a78c056e7cb35" har_path = tmp_path / har_basename - har_path.write_text(DUMMY_HAR_STRING) + har_path.write_text(dummy_har_string) scenario = Scenario.from_path(har_path) @@ -199,13 +195,13 @@ def test_uses_full_path_for_scenario_name(self, tmp_path: Path): ), "all components of the parent path must be in the scenario name" def test_uses_full_path_for_parents_and_basename_for_children( - self, tmp_path: Path + self, tmp_path: Path, dummy_har_string ): root_basename = "615010a656a5bb29d1898f163619611f" root = tmp_path / root_basename root.mkdir() for i in range(2): - (root / f"s{i}.har").write_text(DUMMY_HAR_STRING) + (root / f"s{i}.har").write_text(dummy_har_string) root_scenario = Scenario.from_path(root) @@ -227,11 +223,11 @@ def test_uses_full_path_for_parents_and_basename_for_children( }, "child scenarios have short names" def test_raises_error_for_colliding_scenario_names_from_har_files( - self, tmp_path: Path, caplog + self, tmp_path: Path, dummy_har_string, caplog ): - (tmp_path / "good.har").write_text(DUMMY_HAR_STRING) - (tmp_path / "bad.har").write_text(DUMMY_HAR_STRING) - (tmp_path / "bad.json").write_text(DUMMY_HAR_STRING) + (tmp_path / "good.har").write_text(dummy_har_string) + (tmp_path / "bad.har").write_text(dummy_har_string) + (tmp_path / "bad.json").write_text(dummy_har_string) caplog.set_level(logging.ERROR) @@ -243,15 +239,15 @@ def test_raises_error_for_colliding_scenario_names_from_har_files( assert "bad.json" in caplog.text def test_raises_error_for_colliding_scenario_names_from_directory_and_file( - self, tmp_path: Path, caplog + self, tmp_path: Path, dummy_har_string, caplog ): directory = tmp_path / "x" directory.mkdir() # directory needs to contain a HAR file, otherwise Transformer will # not consider it a scenario. - (directory / "a.har").write_text(DUMMY_HAR_STRING) + (directory / "a.har").write_text(dummy_har_string) - (tmp_path / "x.har").write_text(DUMMY_HAR_STRING) + (tmp_path / "x.har").write_text(dummy_har_string) caplog.set_level(logging.ERROR) diff --git a/tests/transformer/test_task.py b/tests/transformer/test_task.py index 77f7a01..f61ae10 100644 --- a/tests/transformer/test_task.py +++ b/tests/transformer/test_task.py @@ -2,7 +2,6 @@ import enum import io from unittest.mock import MagicMock, Mock -from unittest.mock import patch from urllib.parse import urlparse import pytest @@ -37,7 +36,6 @@ def test_it_returns_a_task(self): for t in Task.from_requests([request, second_request]) ) - @patch("builtins.open") def test_it_doesnt_create_a_task_if_the_url_is_on_the_blacklist( self, mock_open ): @@ -48,7 +46,6 @@ def test_it_doesnt_create_a_task_if_the_url_is_on_the_blacklist( task = Task.from_requests([request], blacklist=blacklist.from_file()) assert len(list(task)) == 0 - @patch("builtins.open") def test_it_creates_a_task_if_the_path_not_host_is_on_the_blacklist( self, mock_open ): diff --git a/tests/transformer/test_transform.py b/tests/transformer/test_transform.py index 8a00f93..e1fec14 100644 --- a/tests/transformer/test_transform.py +++ b/tests/transformer/test_transform.py @@ -6,24 +6,23 @@ import transformer import transformer.transform as tt -from transformer.helpers import DUMMY_HAR_STRING from transformer.locust import locustfile_lines from transformer.plugins import plugin, Contract class TestTransform: - def test_it_returns_a_locustfile_program_given_scenario_path(self, tmp_path: Path): + def test_it_returns_a_locustfile_program_given_scenario_path(self, tmp_path: Path, dummy_har_string): har_path = tmp_path / "some.har" - har_path.write_text(DUMMY_HAR_STRING) + har_path.write_text(dummy_har_string) locustfile_contents = str(tt.transform(har_path)) try: compile(locustfile_contents, "locustfile.py", "exec") except Exception as exception: pytest.fail(f"Compiling locustfile failed. [{exception}].") - def test_it_uses_default_plugins(self, tmp_path: Path, monkeypatch): + def test_it_uses_default_plugins(self, tmp_path: Path, dummy_har_string, monkeypatch): har_path = tmp_path / "some.har" - har_path.write_text(DUMMY_HAR_STRING) + har_path.write_text(dummy_har_string) times_plugin_called = 0 @@ -61,9 +60,9 @@ def test_with_no_paths_it_returns_empty_locustfile(self, f): ) assert f([]) == expected_empty_locustfile - def test_dump_and_dumps_have_same_output_for_simple_har(self, tmp_path): + def test_dump_and_dumps_have_same_output_for_simple_har(self, tmp_path, dummy_har_string): har_path = tmp_path / "some.har" - har_path.write_text(DUMMY_HAR_STRING) + har_path.write_text(dummy_har_string) assert transformer.dumps([tmp_path]) == dump_as_str([tmp_path]) @@ -76,10 +75,10 @@ def test_dump_and_dumps_have_same_output_for_simple_har(self, tmp_path): ), ) def test_it_uses_default_plugins( - self, tmp_path: Path, monkeypatch, f, with_default, expected_times_called + self, tmp_path: Path, monkeypatch, f, with_default, expected_times_called, dummy_har_string ): har_path = tmp_path / "some.har" - har_path.write_text(DUMMY_HAR_STRING) + har_path.write_text(dummy_har_string) times_plugin_called = 0 diff --git a/transformer/helpers.py b/transformer/helpers.py deleted file mode 100644 index 023fd63..0000000 --- a/transformer/helpers.py +++ /dev/null @@ -1,19 +0,0 @@ -import json - - -""" -Use this with caution, as it is global and mutable! -See also DUMMY_HAR_STRING. -""" -_DUMMY_HAR_DICT = { - "log": { - "entries": [ - { - "startedDateTime": "2018-01-01", - "request": {"method": "GET", "url": "https://www.zalando.de"}, - } - ] - } -} - -DUMMY_HAR_STRING = json.dumps(_DUMMY_HAR_DICT) From 88d9b154df0c56d8bd7fff0fd28c10a9d0727ac8 Mon Sep 17 00:00:00 2001 From: Oliwia Zaremba Date: Fri, 17 May 2019 15:49:54 +0200 Subject: [PATCH 3/6] Make patch release Signed-off-by: Oliwia Zaremba --- docs/Changelog.rst | 18 ++++++++++++++++++ docs/conf.py | 2 +- pyproject.toml | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/Changelog.rst b/docs/Changelog.rst index 108eead..c537fb6 100644 --- a/docs/Changelog.rst +++ b/docs/Changelog.rst @@ -15,6 +15,24 @@ The format is based on `Keep a Changelog`_, and this project adheres to :local: :depth: 1 +.. _v1.2.4: + +v1.2.4 +====== + +- Release date: 2019-05-17 15:46 + +- Diff__. + +__ https://github.com/zalando-incubator/transformer/compare/v1.2.3...v1.2.4 + +Changed +------- + +No functional changes in Transformer! Moved tests away from the Transformer package +and refactored them to use common fixtures. + + .. _v1.2.3: v1.2.3 diff --git a/docs/conf.py b/docs/conf.py index 5dd15f3..59827e4 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -25,7 +25,7 @@ # The short X.Y version version = "1.2" # The full version, including alpha/beta/rc tags -release = "1.2.3" +release = "1.2.4" # -- General configuration --------------------------------------------------- diff --git a/pyproject.toml b/pyproject.toml index fd3535a..54ea9e1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "har-transformer" -version = "1.2.3" +version = "1.2.4" description = "A tool to convert HAR files into a locustfile." authors = [ "Serhii Cherniavskyi ", From e82bcbdfb2360439c114734f1ee045b53a9531f2 Mon Sep 17 00:00:00 2001 From: Oliwia Zaremba Date: Fri, 17 May 2019 15:53:20 +0200 Subject: [PATCH 4/6] Remove redundant conftest from transformer package Signed-off-by: Oliwia Zaremba --- transformer/conftest.py | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 transformer/conftest.py diff --git a/transformer/conftest.py b/transformer/conftest.py deleted file mode 100644 index 3980a10..0000000 --- a/transformer/conftest.py +++ /dev/null @@ -1,22 +0,0 @@ -import json - -import pytest - - -@pytest.fixture -def dummy_har_dict(): - return { - "log": { - "entries": [ - { - "startedDateTime": "2018-01-01", - "request": {"method": "GET", "url": "https://www.zalando.de"}, - } - ] - } - } - - -@pytest.fixture -def dummy_har_string(): - return json.dumps(dummy_har_dict()) From d8e86b058c9bf7ef73826fe3b14f641fecaf6482 Mon Sep 17 00:00:00 2001 From: Oliwia Zaremba Date: Fri, 17 May 2019 16:13:31 +0200 Subject: [PATCH 5/6] Attempt at fixing Travis build Signed-off-by: Oliwia Zaremba --- common.mk | 2 +- tests/functional/Makefile | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/common.mk b/common.mk index f1abdd5..f65f518 100644 --- a/common.mk +++ b/common.mk @@ -1,6 +1,6 @@ # This file is included in both Makefile.local & Makefile.ci. -SRC := $(shell find transformer/ -name '*.py' ! -name 'test_*' ! -name 'builders_*' ) +SRC := $(shell find transformer/ -name '*.py' ! -name 'test_*' ! -name 'builders_*' ! -name 'conftest.py') DIST := pyproject.toml poetry.lock # Runs "poetry install" if pyproject.toml or poetry.lock have changed. diff --git a/tests/functional/Makefile b/tests/functional/Makefile index 44bd904..8d7444d 100644 --- a/tests/functional/Makefile +++ b/tests/functional/Makefile @@ -16,9 +16,9 @@ venv: $(VENV_DIR)/bin .PHONY: wheel wheel: - cd .. && rm -rf dist/ && poetry build + cd ../.. && rm -rf dist/ && poetry build -WHEEL = $(shell find ../dist/ -name "*.whl" -print) +WHEEL = $(shell find ../../dist/ -name "*.whl" -print) .PHONY: wheel-in-venv wheel-in-venv: venv wheel From d96b5836e1e5449b2de2dbebe87b3970627781cb Mon Sep 17 00:00:00 2001 From: Oliwia Zaremba Date: Fri, 17 May 2019 16:16:12 +0200 Subject: [PATCH 6/6] Don't override coverage report for plugins tests Signed-off-by: Oliwia Zaremba --- common.mk | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common.mk b/common.mk index f65f518..77f901a 100644 --- a/common.mk +++ b/common.mk @@ -14,8 +14,7 @@ configure: .make/configure # Runs pytest with coverage reporting. .PHONY: unittest unittest: configure - poetry run pytest --failed-first --cov-config .coveragerc --cov-report xml --cov=. tests/transformer/ - poetry run pytest --failed-first --cov-config .coveragerc --cov-report xml --cov=. tests/plugins/ + poetry run pytest --failed-first --cov-config .coveragerc --cov-report xml --cov=. tests/transformer/ tests/plugins/ poetry run pytest --failed-first update-version.py .PHONY: functest