Skip to content

Commit 3c36a82

Browse files
author
NyanKiyoshi
committed
Feature: add backup command to make it easier of making a copy
Closes #29
1 parent f9ebcd0 commit 3c36a82

File tree

8 files changed

+45
-13
lines changed

8 files changed

+45
-13
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ It will generate something [like this](https://pytest-django-queries.readthedocs
148148

149149
## Comparing Results
150150

151-
When running pytest, pass the `--django-backup-queries` (can take a path, optionally)
152-
then you can run `django-queries diff` to generate results looking like this:
151+
You can run `django-queries backup` (can take a path, optionally) after
152+
running your tests then rerun them. After that, you can run `django-queries diff`
153+
to generate results looking like this:
153154

154155
<a href='./docs/_static/diff_results.png'>
155156
<img src='./docs/_static/diff_results.png' alt='screenshot' width='500px' />

docs/diff.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
The Diff Command
44
----------------
55

6-
The plugin can backup the test results for you if you pass the ``--django-backup-queries [BACKUP_PATH]`` parameter to it. It will create a backup to ``.pytest-query.old`` by default if previous results were found.
6+
The plugin can backup the test results for you if you run the ``django-queries backup [BACKUP_PATH]`` command. It will create a backup to ``.pytest-query.old`` by default if previous results were found.
77

88
.. warning::
99

1010
Bear in mind that it will override any existing backup file in the provided or default path.
1111

12-
After running ``pytest --django-backup-queries``, you can run ``django-queries diff`` to show the changes. Make sure you actually had previous results, otherwise it will have nothing to compare.
12+
After running ``pytest`` again, you can run ``django-queries diff`` to show the changes. Make sure you actually had previous results, otherwise it will have nothing to compare.

docs/usage.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Customizing the Save Path
1515
Backing Up Results
1616
++++++++++++++++++
1717

18-
You can pass the ``--django-backup-queries`` parameter to backup previous results to `.pytest-django.old``.
18+
The easiest way is to run the ``django-queries backup`` command which will create a copy of the current results.
19+
20+
Another way is by passing the ``--django-backup-queries`` parameter to backup previous results to `.pytest-django.old``.
1921

2022
Or pass a custom path.
2123

pytest_django_queries/cli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
DEFAULT_RESULT_FILENAME,
1414
)
1515
from pytest_django_queries.tables import entries_to_html, print_entries
16+
from pytest_django_queries.utils import create_backup
1617

1718
HERE = dirname(__file__)
1819
DEFAULT_TEMPLATE_PATH = abspath(pathjoin(HERE, "templates", "default_bootstrap.jinja2"))
@@ -124,5 +125,13 @@ def diff(left_file, right_file):
124125
click.secho(line, fg=fg_color)
125126

126127

128+
@main.command()
129+
@click.argument("target_path", type=str, default=DEFAULT_OLD_RESULT_FILENAME)
130+
def backup(target_path):
131+
source_path = DEFAULT_RESULT_FILENAME
132+
click.echo("{0} -> {1}".format(source_path, target_path))
133+
create_backup(source_path, target_path)
134+
135+
127136
if __name__ == "__main__":
128137
main()

pytest_django_queries/plugin.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import json
2-
import shutil
32
from os.path import isfile
43

54
import pytest
65
from django.test.utils import CaptureQueriesContext
76

87
# Defines the plugin marker name
8+
from pytest_django_queries.utils import create_backup
9+
910
PYTEST_QUERY_COUNT_MARKER = "count_queries"
1011
PYTEST_QUERY_COUNT_FIXTURE_NAME = "count_queries"
1112
DEFAULT_RESULT_FILENAME = ".pytest-queries"
@@ -20,10 +21,6 @@ def _get_session(request):
2021
return request.config.pytest_django_queries_session
2122

2223

23-
def _create_backup(save_path, backup_path):
24-
shutil.copy(save_path, backup_path)
25-
26-
2724
class _Session(object):
2825
def __init__(self, save_path, backup_path):
2926
"""
@@ -43,7 +40,7 @@ def add_entry(self, module_name, test_name, query_count):
4340

4441
def save_json(self):
4542
if self.backup_path and isfile(self.save_path):
46-
_create_backup(self.save_path, self.backup_path)
43+
create_backup(self.save_path, self.backup_path)
4744

4845
with open(self.save_path, "w") as fp:
4946
json.dump(self._data, fp, indent=2)

pytest_django_queries/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import shutil
12
import sys
23

34
import click
@@ -14,3 +15,7 @@ def assert_type(value, expected_type):
1415
"Expected a %s, got %s instead"
1516
% (expected_type.__name__, type(value).__name__)
1617
)
18+
19+
20+
def create_backup(save_path, backup_path):
21+
shutil.copy(save_path, backup_path)

tests/test_cli.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,21 @@ def test_export_to_html_using_invalid_custom_template_should_fail(testdir):
219219
'Error: Invalid value for "--template": '
220220
"The file is not a valid jinja2 template: tag name expected" in result.stdout
221221
)
222+
223+
224+
def test_backup_command_is_making_a_backup(testdir):
225+
results_file = str(testdir.tmpdir.join(".pytest-queries"))
226+
227+
with open(results_file, "w") as fp:
228+
fp.write("hello!")
229+
230+
backup_file = testdir.tmpdir.join("backup.json")
231+
232+
runner = CliRunner()
233+
result = runner.invoke(cli.main, ["backup", "backup.json"])
234+
assert result.exit_code == 0, result.stdout
235+
assert result.stdout == ".pytest-queries -> backup.json\n"
236+
237+
assert backup_file.check()
238+
with open(str(backup_file), "r") as fp:
239+
assert fp.read() == "hello!"

tests/test_plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def test_fixture_is_not_backing_up_if_not_asked_to(testdir):
210210
# and triggers a counting of the query number
211211
testdir.makepyfile(test_file=DUMMY_TEST_QUERY)
212212

213-
with mock.patch("pytest_django_queries.plugin._create_backup") as mocked_backup:
213+
with mock.patch("pytest_django_queries.plugin.create_backup") as mocked_backup:
214214
results = testdir.runpytest("--django-db-bench", results_path)
215215
assert mocked_backup.call_count == 0
216216

@@ -228,7 +228,7 @@ def test_fixture_is_backing_up_old_results_to_default_path_if_no_path_provided(t
228228
# and triggers a counting of the query number
229229
testdir.makepyfile(test_file=DUMMY_TEST_QUERY)
230230

231-
with mock.patch("pytest_django_queries.plugin._create_backup") as mocked_backup:
231+
with mock.patch("pytest_django_queries.plugin.create_backup") as mocked_backup:
232232
from pytest_django_queries.plugin import DEFAULT_OLD_RESULT_FILENAME
233233

234234
results = testdir.runpytest(

0 commit comments

Comments
 (0)