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).
This commit is contained in:
rr- 2016-04-18 19:42:24 +02:00
parent e8a9c4ad51
commit 1c064778c6
6 changed files with 40 additions and 36 deletions

View File

@ -5,3 +5,4 @@ psycopg2>=2.6.1
SQLAlchemy>=1.0.12 SQLAlchemy>=1.0.12
pytest>=2.9.1 pytest>=2.9.1
pytest-cov>=2.2.1 pytest-cov>=2.2.1
freezegun>=0.3.6

View File

@ -38,16 +38,16 @@ def test_ctx(
return ret return ret
def test_creating_simple_tags(test_ctx, fake_datetime): def test_creating_simple_tags(test_ctx, fake_datetime):
fake_datetime(datetime.datetime(1997, 12, 1)) with fake_datetime('1997-12-01'):
result = test_ctx.api.post( result = test_ctx.api.post(
test_ctx.context_factory( test_ctx.context_factory(
input={ input={
'names': ['tag1', 'tag2'], 'names': ['tag1', 'tag2'],
'category': 'meta', 'category': 'meta',
'suggestions': [], 'suggestions': [],
'implications': [], 'implications': [],
}, },
user=test_ctx.user_factory(rank='regular_user'))) user=test_ctx.user_factory(rank='regular_user')))
assert result == { assert result == {
'tag': { 'tag': {
'names': ['tag1', 'tag2'], 'names': ['tag1', 'tag2'],

View File

@ -43,18 +43,18 @@ def test_ctx(
return ret return ret
def test_simple_updating(test_ctx, fake_datetime): 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') tag = test_ctx.tag_factory(names=['tag1', 'tag2'], category='meta')
test_ctx.session.add(tag) test_ctx.session.add(tag)
test_ctx.session.commit() test_ctx.session.commit()
result = test_ctx.api.put( with fake_datetime('1997-12-01'):
test_ctx.context_factory( result = test_ctx.api.put(
input={ test_ctx.context_factory(
'names': ['tag3'], input={
'category': 'character', 'names': ['tag3'],
}, 'category': 'character',
user=test_ctx.user_factory(rank='regular_user')), },
'tag1') user=test_ctx.user_factory(rank='regular_user')),
'tag1')
assert result == { assert result == {
'tag': { 'tag': {
'names': ['tag3'], 'names': ['tag3'],

View File

@ -32,15 +32,15 @@ def test_ctx(
return ret return ret
def test_creating_user(test_ctx, fake_datetime): def test_creating_user(test_ctx, fake_datetime):
fake_datetime(datetime.datetime(1969, 2, 12)) with fake_datetime('1969-02-12'):
result = test_ctx.api.post( result = test_ctx.api.post(
test_ctx.context_factory( test_ctx.context_factory(
input={ input={
'name': 'chewie1', 'name': 'chewie1',
'email': 'asd@asd.asd', 'email': 'asd@asd.asd',
'password': 'oks', 'password': 'oks',
}, },
user=test_ctx.user_factory(rank='regular_user'))) user=test_ctx.user_factory(rank='regular_user')))
assert result == { assert result == {
'user': { 'user': {
'avatarStyle': 'gravatar', 'avatarStyle': 'gravatar',

View File

@ -1,6 +1,7 @@
import datetime import datetime
import uuid import uuid
import pytest import pytest
import freezegun
import sqlalchemy import sqlalchemy
from szurubooru import api, config, db from szurubooru import api, config, db
from szurubooru.util import misc from szurubooru.util import misc
@ -9,13 +10,15 @@ def get_unique_name():
return str(uuid.uuid4()) return str(uuid.uuid4())
@pytest.fixture @pytest.fixture
def fake_datetime(monkeypatch): def fake_datetime():
def injector(now): def injector(now):
class mydatetime(datetime.datetime): class scope():
@staticmethod def __enter__(self):
def now(tz=None): self.freezer = freezegun.freeze_time(now)
return now self.freezer.start()
monkeypatch.setattr(datetime, 'datetime', mydatetime) def __exit__(self, type, value, trackback):
self.freezer.stop()
return scope()
return injector return injector
@pytest.fixture @pytest.fixture

View File

@ -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))), ('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): def test_parsing_date_time(fake_datetime, input, output):
fake_datetime(datetime(1997, 1, 2, 3, 4, 5)) with fake_datetime('1997-01-02 03:04:05'):
assert misc.parse_time_range(input) == output assert misc.parse_time_range(input) == output
@pytest.mark.parametrize('input,output', [ @pytest.mark.parametrize('input,output', [
([], []), ([], []),