Skip to content

Commit

Permalink
mock datetime in tests to prevents spurious failures
Browse files Browse the repository at this point in the history
  • Loading branch information
saxix committed Dec 11, 2015
1 parent b4dbc45 commit cf2e251
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/adminactions/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import absolute_import, unicode_literals

import datetime
from datetime import datetime

from django.http import HttpResponse
from django.utils import dateformat


def format_date(request):
d = datetime.datetime.now()
d = datetime.now()
return HttpResponse(dateformat.format(d, request.GET.get('fmt', '')))
24 changes: 17 additions & 7 deletions tests/selenium_tests/test_export_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
import pytest # noqa
import django # noqa
import datetime
import mock
from time import sleep
from django.utils import dateformat
from .base import * # noqa
from selenium.webdriver.support.select import Select

FAKE_TIME = datetime.datetime(2020, 12, 25, 17, 5, 55)

pytestmark = pytest.mark.selenium

@pytest.fixture
def now(monkeypatch):

class FixedDateTime:
@classmethod
def now(cls):
return FAKE_TIME

monkeypatch.setattr('adminactions.views.datetime', FixedDateTime)

# @pytest.mark.skipif('django.VERSION[:2]==(1,8)')
def test_export_as_csv(admin_site):
Expand All @@ -33,20 +44,19 @@ def export_csv_page(admin_site):
return browser, administrator


def _test(browser, target, format, sample_num):
def _test(browser, target, format, sample_num, expected_value):
fmt = browser.find_element_by_id(target)
fmt.clear()
fmt.send_keys(format)
sleep(1)
sample = browser.find_elements_by_css_selector("span.sample")[sample_num]
expected_value = dateformat.format(datetime.datetime.now(), format)
# expected_value = dateformat.format(datetime.datetime.now(), format)
assert sample.text == expected_value, "Failed Ajax call on %s" % target


# @pytest.mark.skipif('django.VERSION[:2]==(1,8)')
def test_datetime_format_ajax(export_csv_page):
def test_datetime_format_ajax(export_csv_page, now):
browser, administrator = export_csv_page
_test(browser, "id_datetime_format", 'l, d F Y', 0)
_test(browser, "id_date_format", 'd F Y', 1)
_test(browser, "id_time_format", 'H:i', 2)

_test(browser, "id_datetime_format", 'l, d F Y', 0, 'Friday, 25 December 2020')
_test(browser, "id_date_format", 'd F Y', 1, '25 December 2020')
_test(browser, "id_time_format", 'H:i', 2, '17:05')
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ deps=
d16: django>=1.6,<1.7
d17: django>=1.7,<1.8
d18: django>=1.8,<1.9
d19: django>=1.9c,<2.0
d19: django>=1.9,<1.10


commands =
Expand Down

0 comments on commit cf2e251

Please sign in to comment.