Skip to content

Commit

Permalink
Merge pull request #58 from zalando-incubator/pytestification
Browse files Browse the repository at this point in the history
Move tests outside of package (and refactor them a bit)
  • Loading branch information
tortila authored May 17, 2019
2 parents 409f5ab + d96b583 commit 3fde4b7
Show file tree
Hide file tree
Showing 24 changed files with 131 additions and 94 deletions.
6 changes: 3 additions & 3 deletions common.mk
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,12 +14,12 @@ 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/ 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
Expand Down
18 changes: 18 additions & 0 deletions docs/Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------------------------------------
Expand Down
14 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down Expand Up @@ -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"
Expand Down
40 changes: 40 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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")
4 changes: 2 additions & 2 deletions functional-tests/Makefile → tests/functional/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand All @@ -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
2 changes: 1 addition & 1 deletion transformer/test_cli.py → tests/transformer/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path

from .cli import read_config
from transformer.cli import read_config


class TestReadConfig:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
File renamed without changes.
10 changes: 6 additions & 4 deletions transformer/test_python.py → tests/transformer/test_python.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pprint
import string
from typing import List
from unittest.mock import patch

import pytest
from hypothesis import given
Expand Down Expand Up @@ -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",
[
Expand All @@ -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(
Expand Down
File renamed without changes.
Loading

0 comments on commit 3fde4b7

Please sign in to comment.