Skip to content

Commit

Permalink
a few unit test fixes, plus tox/travis builds
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Mar 10, 2017
1 parent e908e2b commit 71f46e5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 33 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: python
env:
- TOXENV=flake8
- TOXENV=py27
- TOXENV=py34
- TOXENV=py35
- TOXENV=py36
- TOXENV=pypy
install:
- pip install tox
script:
- tox
15 changes: 10 additions & 5 deletions example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
app = Flask(__name__)
moment = Moment(app)


@app.route('/')
def index():
now = datetime.utcnow()
midnight = datetime(now.year, now.month, now.day, 0, 0, 0)
epoch = datetime(1970, 1, 1, 0, 0, 0)
return render_template('index.html', now=now, midnight=midnight, epoch=epoch)
epoch = datetime(1970, 1, 1, 0, 0, 0)
return render_template('index.html', now=now, midnight=midnight,
epoch=epoch)


@app.route('/ajax')
def ajax():
return jsonify({ 'timestamp': moment.create(datetime.utcnow()).format('LLLL') });

return jsonify({'timestamp': moment.create(datetime.utcnow()).format(
'LLLL')})


if __name__ == '__main__':
app.run(debug = True)
app.run(debug=True)
2 changes: 1 addition & 1 deletion flask_moment.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def include_moment(version='2.10.3', local_js=None):
$(document).ready(function() {
flask_moment_render_all();
});
</script>''' % js)
</script>''' % js) # noqa: E501

@staticmethod
def include_jquery(version='2.1.0', local_js=None):
Expand Down
6 changes: 4 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import pytest
from flask import Flask
from jinja2 import Template
from flask_moment import Moment
import datetime


@pytest.fixture(scope='module')
def app():
_app = Flask(__name__)
with _app.app_context():
yield _app


@pytest.fixture(scope='module')
def moment(app):
moment = Moment()
moment.init_app(app)
yield moment


@pytest.fixture(scope='module')
def current_time():
return datetime.datetime(2017, 3, 10, 1, 49, 59, 38650)
return datetime.datetime(2017, 3, 10, 1, 49, 59, 38650)
57 changes: 32 additions & 25 deletions tests/test_flask_moment.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import pytest
from datetime import datetime

from flask import render_template_string
import flask_moment
from flask_moment import _moment, Moment
from jinja2 import Markup

from mock import patch


# Mock Objects

Expand All @@ -17,8 +13,10 @@ class NewDate(datetime):
def utcnow(cls):
return cls(2017, 1, 15, 22, 1, 21, 101361)


_datetime_mock = NewDate


class NewPrivateMoment(_moment):
"""Mock the _moment class for predictable now timestamps"""
def __init__(self, timestamp=None, local=False):
Expand All @@ -27,8 +25,10 @@ def __init__(self, timestamp=None, local=False):
self.timestamp = timestamp
self.local = local


_moment_mock = NewPrivateMoment


class NewPublicMoment(Moment):
"""Mock the Moment class for predictable now timestamps"""
def init_app(self, app):
Expand All @@ -37,6 +37,7 @@ def init_app(self, app):
app.extensions['moment'] = _moment_mock
app.context_processor(self.context_processor)


_Moment = NewPublicMoment


Expand All @@ -48,8 +49,8 @@ def test_init_app(self, app, moment):
assert app.extensions['moment'] == _moment

def test_app_context_processor(self, app, moment):
assert app.template_context_processors[None][1].func_globals['__name__'] == 'flask_moment'

assert app.template_context_processors[None][1].__globals__[
'__name__'] == 'flask_moment'


class TestFlaskMomentIncludes(object):
Expand All @@ -69,10 +70,12 @@ def test_include_moment_with_different_version_directly(self):
assert "2.17.1/moment-with-locales.min.js" in str(include_moment)

def test_include_moment_with_local_js_directly(self):
include_moment = _moment.include_moment(local_js="/path/to/local/moment.js")
include_moment = _moment.include_moment(
local_js="/path/to/local/moment.js")

assert isinstance(include_moment, Markup)
assert "<script src=\"/path/to/local/moment.js\"></script>" in str(include_moment)
assert "<script src=\"/path/to/local/moment.js\"></script>" in str(
include_moment)

def test_include_moment_renders_properly(self, app, moment):
ts = str(render_template_string("{{ moment.include_moment() }}"))
Expand All @@ -84,12 +87,14 @@ def test_include_jquery_default(self):
include_jquery = _moment.include_jquery()

assert isinstance(include_jquery, Markup)
assert all([each in str(include_jquery) for each in ['code.jquery.com', '2.1.0']])
assert all([each in str(include_jquery) for each in [
'code.jquery.com', '2.1.0']])

def test_include_jquery_local(self):
include_jquery = _moment.include_jquery(local_js=True)

assert all([each in str(include_jquery) for each in ['<script', '</script>']])
assert all([each in str(include_jquery) for each in [
'<script', '</script>']])


class TestPrivateMomentClass(object):
Expand All @@ -98,12 +103,12 @@ class TestPrivateMomentClass(object):
def test__moment_default(self):
mom = _moment_mock()
assert mom.timestamp == _datetime_mock.utcnow()
assert mom.local == False
assert mom.local is False

def test__moment_local_true(self):
mom = _moment_mock(local=True)
assert mom.timestamp == _datetime_mock.utcnow()
assert mom.local == True
assert mom.local is True

def test_locale(self):
mom = _moment_mock()
Expand All @@ -117,13 +122,13 @@ def test_lang(self):
l = 'en'
lang = mom.lang(l)
assert isinstance(lang, Markup)
assert 'moment.locale("%s")' % l in str(lang)
assert 'moment.locale("%s")' % l in str(lang)

def test__moment_timestamp_passed(self):
ts = datetime(2017, 1, 15, 22, 47, 6, 479898)
mom = _moment_mock(timestamp=ts)
assert mom.timestamp == ts
assert mom.local == False
assert mom.local is False

def test__timestamp_as_iso_8601_default(self):
mom = _moment_mock()
Expand All @@ -138,7 +143,7 @@ def test__timestamp_as_iso_8601_local_true(self):
def test__render_default(self):
mom = _moment_mock()
refresh = False
rts = mom._render(format="format") # rts: rendered time stamp
rts = mom._render(format="format") # rts: rendered time stamp

assert isinstance(rts, Markup)
assert rts.find("thisisnotinthemarkup") < 0
Expand All @@ -163,7 +168,7 @@ def test_format_default(self):

def test_fromNow_default(self):
mom = _moment_mock()
no_suffix = False
no_suffix = False
rts = mom.fromNow()

assert rts.find("fromNow(%s)" % int(no_suffix)) > 0
Expand All @@ -181,19 +186,21 @@ def test_fromTime_default(self):
no_suffix = False
rts = mom.fromTime(timestamp=ts)

assert rts.find("from(moment('%s'),%s)" \
% (mom._timestamp_as_iso_8601(ts), int(no_suffix))) > 0
assert rts.find("%s" % mom._timestamp_as_iso_8601(timestamp=mom.timestamp)) > 0
assert rts.find("from(moment('%s'),%s)"
% (mom._timestamp_as_iso_8601(ts), int(no_suffix))) > 0
assert rts.find("%s" % mom._timestamp_as_iso_8601(
timestamp=mom.timestamp)) > 0

def test_fromTime_no_suffix(self):
mom = _moment_mock()
ts = datetime(2017, 1, 15, 22, 47, 6, 479898)
no_suffix = True
rts = mom.fromTime(timestamp=ts, no_suffix=no_suffix)

assert rts.find("from(moment('%s'),%s)" \
% (mom._timestamp_as_iso_8601(ts), int(no_suffix))) > 0
assert rts.find("%s" % mom._timestamp_as_iso_8601(timestamp=mom.timestamp)) > 0
assert rts.find("from(moment('%s'),%s)"
% (mom._timestamp_as_iso_8601(ts), int(no_suffix))) > 0
assert rts.find("%s" % mom._timestamp_as_iso_8601(
timestamp=mom.timestamp)) > 0

def test_calendar_default(self):
mom = _moment_mock()
Expand All @@ -220,12 +227,12 @@ def test_create_default_no_timestamp(self, app):
moment = _Moment()
moment.init_app(app)

assert moment.create().timestamp == _datetime_mock.utc_now()
assert moment.create().timestamp == _datetime_mock.utcnow()

def test_create_default_no_timestamp(self, app):
def test_create_default_with_timestamp(self, app):
moment = _Moment()
moment.init_app(app)

ts = datetime(2017, 1, 15, 22, 47, 6, 479898)

assert moment.create(timestamp=ts).timestamp == ts
17 changes: 17 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[tox]
envlist=flake8,py27,py34,py35,py36,pypy
skip_missing_interpreters=true

[testenv]
commands=py.test --cov-report term-missing --cov=flask_moment tests
deps=
pytest
pytest-cov
mock

[testenv:flake8]
basepython=python3.6
commands=
flake8 flask_moment.py tests example
deps=
flake8

0 comments on commit 71f46e5

Please sign in to comment.