summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrr- <rr-@sakuya.pl>2016-04-18 19:42:24 +0200
committerrr- <rr-@sakuya.pl>2016-04-18 19:42:24 +0200
commit1c064778c66e772a93d5d1b202f01f07eaeb3a21 (patch)
tree56532797554961ca3d4705ed5a665a03889f8c0d
parente8a9c4ad513815502ceea911d2154619eb36aa3b (diff)
server/tests: adapt freezegun
The reason why this is added to the project is because it has turned out mocking the time is not as trivial as I originally anticipated (specifically, there are some problems with SQLite).
-rw-r--r--server/requirements.txt1
-rw-r--r--server/szurubooru/tests/api/test_tag_creating.py20
-rw-r--r--server/szurubooru/tests/api/test_tag_updating.py18
-rw-r--r--server/szurubooru/tests/api/test_user_creating.py18
-rw-r--r--server/szurubooru/tests/conftest.py15
-rw-r--r--server/szurubooru/tests/util/test_misc.py4
6 files changed, 40 insertions, 36 deletions
diff --git a/server/requirements.txt b/server/requirements.txt
index c59cf69..c521c28 100644
--- a/server/requirements.txt
+++ b/server/requirements.txt
@@ -5,3 +5,4 @@ psycopg2>=2.6.1
SQLAlchemy>=1.0.12
pytest>=2.9.1
pytest-cov>=2.2.1
+freezegun>=0.3.6
diff --git a/server/szurubooru/tests/api/test_tag_creating.py b/server/szurubooru/tests/api/test_tag_creating.py
index d8c72db..ec77322 100644
--- a/server/szurubooru/tests/api/test_tag_creating.py
+++ b/server/szurubooru/tests/api/test_tag_creating.py
@@ -38,16 +38,16 @@ def test_ctx(
return ret
def test_creating_simple_tags(test_ctx, fake_datetime):
- fake_datetime(datetime.datetime(1997, 12, 1))
- result = test_ctx.api.post(
- test_ctx.context_factory(
- input={
- 'names': ['tag1', 'tag2'],
- 'category': 'meta',
- 'suggestions': [],
- 'implications': [],
- },
- user=test_ctx.user_factory(rank='regular_user')))
+ with fake_datetime('1997-12-01'):
+ result = test_ctx.api.post(
+ test_ctx.context_factory(
+ input={
+ 'names': ['tag1', 'tag2'],
+ 'category': 'meta',
+ 'suggestions': [],
+ 'implications': [],
+ },
+ user=test_ctx.user_factory(rank='regular_user')))
assert result == {
'tag': {
'names': ['tag1', 'tag2'],
diff --git a/server/szurubooru/tests/api/test_tag_updating.py b/server/szurubooru/tests/api/test_tag_updating.py
index c68b853..d7262ab 100644
--- a/server/szurubooru/tests/api/test_tag_updating.py
+++ b/server/szurubooru/tests/api/test_tag_updating.py
@@ -43,18 +43,18 @@ def test_ctx(
return ret
def test_simple_updating(test_ctx, fake_datetime):
- fake_datetime(datetime.datetime(1997, 12, 1))
tag = test_ctx.tag_factory(names=['tag1', 'tag2'], category='meta')
test_ctx.session.add(tag)
test_ctx.session.commit()
- result = test_ctx.api.put(
- test_ctx.context_factory(
- input={
- 'names': ['tag3'],
- 'category': 'character',
- },
- user=test_ctx.user_factory(rank='regular_user')),
- 'tag1')
+ with fake_datetime('1997-12-01'):
+ result = test_ctx.api.put(
+ test_ctx.context_factory(
+ input={
+ 'names': ['tag3'],
+ 'category': 'character',
+ },
+ user=test_ctx.user_factory(rank='regular_user')),
+ 'tag1')
assert result == {
'tag': {
'names': ['tag3'],
diff --git a/server/szurubooru/tests/api/test_user_creating.py b/server/szurubooru/tests/api/test_user_creating.py
index 85372be..76cdb37 100644
--- a/server/szurubooru/tests/api/test_user_creating.py
+++ b/server/szurubooru/tests/api/test_user_creating.py
@@ -32,15 +32,15 @@ def test_ctx(
return ret
def test_creating_user(test_ctx, fake_datetime):
- fake_datetime(datetime.datetime(1969, 2, 12))
- result = test_ctx.api.post(
- test_ctx.context_factory(
- input={
- 'name': 'chewie1',
- 'email': 'asd@asd.asd',
- 'password': 'oks',
- },
- user=test_ctx.user_factory(rank='regular_user')))
+ with fake_datetime('1969-02-12'):
+ result = test_ctx.api.post(
+ test_ctx.context_factory(
+ input={
+ 'name': 'chewie1',
+ 'email': 'asd@asd.asd',
+ 'password': 'oks',
+ },
+ user=test_ctx.user_factory(rank='regular_user')))
assert result == {
'user': {
'avatarStyle': 'gravatar',
diff --git a/server/szurubooru/tests/conftest.py b/server/szurubooru/tests/conftest.py
index 239f5b6..48f9a02 100644
--- a/server/szurubooru/tests/conftest.py
+++ b/server/szurubooru/tests/conftest.py
@@ -1,6 +1,7 @@
import datetime
import uuid
import pytest
+import freezegun
import sqlalchemy
from szurubooru import api, config, db
from szurubooru.util import misc
@@ -9,13 +10,15 @@ def get_unique_name():
return str(uuid.uuid4())
@pytest.fixture
-def fake_datetime(monkeypatch):
+def fake_datetime():
def injector(now):
- class mydatetime(datetime.datetime):
- @staticmethod
- def now(tz=None):
- return now
- monkeypatch.setattr(datetime, 'datetime', mydatetime)
+ class scope():
+ def __enter__(self):
+ self.freezer = freezegun.freeze_time(now)
+ self.freezer.start()
+ def __exit__(self, type, value, trackback):
+ self.freezer.stop()
+ return scope()
return injector
@pytest.fixture
diff --git a/server/szurubooru/tests/util/test_misc.py b/server/szurubooru/tests/util/test_misc.py
index 355772e..dbea305 100644
--- a/server/szurubooru/tests/util/test_misc.py
+++ b/server/szurubooru/tests/util/test_misc.py
@@ -21,8 +21,8 @@ def test_parsing_empty_date_time():
('1999-02-06', (dt(1999, 2, 6, 0, 0, 0), dt(1999, 2, 6, 23, 59, 59))),
])
def test_parsing_date_time(fake_datetime, input, output):
- fake_datetime(datetime(1997, 1, 2, 3, 4, 5))
- assert misc.parse_time_range(input) == output
+ with fake_datetime('1997-01-02 03:04:05'):
+ assert misc.parse_time_range(input) == output
@pytest.mark.parametrize('input,output', [
([], []),