Skip to content

Commit

Permalink
Implement tests for the diff tool
Browse files Browse the repository at this point in the history
  • Loading branch information
NyanKiyoshi committed May 23, 2019
1 parent 8d1821a commit ba4f3f7
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 23 deletions.
17 changes: 3 additions & 14 deletions pytest_django_queries/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from pytest_django_queries.diff import DiffGenerator
from pytest_django_queries.entry import flatten_entries
from pytest_django_queries.plugin import DEFAULT_RESULT_FILENAME
from pytest_django_queries.plugin import (
DEFAULT_RESULT_FILENAME, DEFAULT_OLD_RESULT_FILENAME)
from pytest_django_queries.tables import print_entries, print_entries_as_html

HERE = dirname(__file__)
Expand Down Expand Up @@ -73,7 +74,7 @@ def html(input_file, template):

@main.command()
@click.argument(
'left_file', type=JsonFileParamType('r'))
'left_file', type=JsonFileParamType('r'), default=DEFAULT_OLD_RESULT_FILENAME)
@click.argument(
'right_file', type=JsonFileParamType('r'), default=DEFAULT_RESULT_FILENAME)
def diff(left_file, right_file):
Expand All @@ -93,17 +94,5 @@ def diff(left_file, right_file):
click.secho(line, fg=fg_color)


@main.command()
@click.argument(
'left_file', type=JsonFileParamType('r'))
@click.argument(
'right_file', type=JsonFileParamType('r'), default=DEFAULT_RESULT_FILENAME)
def ediff(left_file, right_file):
"""Render the diff as HTML instead of a diff table."""
left = flatten_entries(left_file)
right = flatten_entries(right_file)
raise NotImplementedError


if __name__ == '__main__':
main()
21 changes: 12 additions & 9 deletions pytest_django_queries/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def __init__(self, left=None, right=None):

self.left = left
self.right = right
self.diff = None

def _diff_from_newest(self):
"""
Expand Down Expand Up @@ -99,10 +98,11 @@ def left_count(self):
def right_count(self):
return str(self.right.query_count) if self.right else _NA_CHAR

def to_string(self, lengths):
if self.diff is None:
self.diff = self._diff_from_newest()
@property
def diff(self):
return self._diff_from_newest()

def to_string(self, lengths):
return entry_row(self, lengths=lengths)


Expand Down Expand Up @@ -161,13 +161,16 @@ def _generate_mapping(self):
self._map_side(self.entries_left, 'left')
self._map_side(self.entries_right, 'right')

def iter_module(self, module_entries):
def _iter_module(self, module_entries):
yield self.header_rows
for test_comparison in module_entries.values(): # type: SingleEntryComparison
for _, test_comparison in sorted(module_entries.items()): # type: SingleEntryComparison
yield test_comparison.to_string(lengths=self.longest_props)

def __iter__(self):
for module_name, module_entries in self._mapping.items():
def _iter_modules(self):
for module_name, module_entries in sorted(self._mapping.items()):
yield (
format_underscore_name_to_human(module_name),
self.iter_module(module_entries))
self._iter_module(module_entries))

def __iter__(self):
return self._iter_modules()
1 change: 1 addition & 0 deletions pytest_django_queries/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Defines the plugin marker name
PYTEST_QUERY_COUNT_MARKER = 'count_queries'
DEFAULT_RESULT_FILENAME = '.pytest-queries'
DEFAULT_OLD_RESULT_FILENAME = '.pytest-queries.old'


def _set_session(config, new_session):
Expand Down
25 changes: 25 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
import pytest

from pytest_django_queries.entry import flatten_entries

pytest_plugins = 'pytester'


@pytest.fixture
def valid_comparison_entries():
left = {
"test_module": {
"test_improved_func": {"query-count": 20},
"test_degraded_func": {"query-count": 15},
"test_unchanged_func": {"query-count": 1},
}
}

right = {
"test_module": {
"test_improved_func": {"query-count": 19},
"test_degraded_func": {"query-count": 16},
"test_unchanged_func": {"query-count": 1},
}
}

return left, right
28 changes: 28 additions & 0 deletions tests/test_cli_diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json

from click.testing import CliRunner
from pytest_django_queries import cli


def test_export_to_html_using_custom_template(testdir, valid_comparison_entries):
left, right = valid_comparison_entries
right["another_module"] = {"test_new_test": {"query-count": 1}}
testdir.makefile('json', left=json.dumps(left))
testdir.makefile('json', right=json.dumps(right))

runner = CliRunner()
result = runner.invoke(cli.main, ['diff', 'left.json', 'right.json'])
assert result.exit_code == 0, result.stdout
assert repr(result.stdout) == repr(u"""\
# another module
test name \tleft count \tright count
-------------------\t-----------\t-----------
+ new test \t -\t 1
# module
test name \tleft count \tright count
-------------------\t-----------\t-----------
- degraded func \t 15\t 16
+ improved func \t 20\t 19
unchanged func \t 1\t 1
""")
51 changes: 51 additions & 0 deletions tests/test_diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest

from pytest_django_queries.diff import DiffGenerator
from pytest_django_queries.entry import flatten_entries


@pytest.mark.parametrize('right', (
{},
{"test_module": {}},
{"test_module_123": {}}
))
def test_comparison_deleted_test_triggers_negative_review(right):
left = flatten_entries({"test_module": {"test_deleted_func": {"query-count": 1}}})
right = flatten_entries(right)
module_diffs = list(next(iter(DiffGenerator(left, right)))[1])

# We expect it to:
# 1. start with '-'
# 2. to give a left value of 1
# 3. to give a right value of N/A
assert '- deleted func \t 1\t -' in module_diffs


@pytest.mark.parametrize('left', (
{},
{"test_module": {}},
{"test_module_123": {}}
))
def test_comparison_new_test_triggers_positive_review(left):
left = flatten_entries(left)
right = flatten_entries({"test_module": {"test_added_func": {"query-count": 1}}})
module_diffs = list(next(iter(DiffGenerator(left, right)))[1])

# We expect it to:
# 1. start with '+'
# 2. to give a left value of N/A
# 3. to give a right value of 1
assert '+ added func \t -\t 1' in module_diffs


def test_comparison_diff_is_correct(valid_comparison_entries):
left, right = valid_comparison_entries
left = flatten_entries(left)
right = flatten_entries(right)

module_diffs = list(next(iter(DiffGenerator(left, right)))[1])

assert len(module_diffs) == 4 # 3 tests + header
assert '+ improved func \t 20\t 19' in module_diffs
assert '- degraded func \t 15\t 16' in module_diffs
assert ' unchanged func \t 1\t 1' in module_diffs

0 comments on commit ba4f3f7

Please sign in to comment.