diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..250dbae --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/example/app.py b/example/app.py index ff534a1..73b1978 100644 --- a/example/app.py +++ b/example/app.py @@ -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) diff --git a/flask_moment.py b/flask_moment.py index 64bc96e..2fe7d1e 100644 --- a/flask_moment.py +++ b/flask_moment.py @@ -33,7 +33,7 @@ def include_moment(version='2.10.3', local_js=None): $(document).ready(function() { flask_moment_render_all(); }); -''' % js) +''' % js) # noqa: E501 @staticmethod def include_jquery(version='2.1.0', local_js=None): diff --git a/tests/conftest.py b/tests/conftest.py index ef3ba28..b712e65 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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) \ No newline at end of file + return datetime.datetime(2017, 3, 10, 1, 49, 59, 38650) diff --git a/tests/test_flask_moment.py b/tests/test_flask_moment.py index b44ef52..3b697ed 100644 --- a/tests/test_flask_moment.py +++ b/tests/test_flask_moment.py @@ -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 @@ -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): @@ -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): @@ -37,6 +37,7 @@ def init_app(self, app): app.extensions['moment'] = _moment_mock app.context_processor(self.context_processor) + _Moment = NewPublicMoment @@ -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): @@ -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 "" in str(include_moment) + assert "" in str( + include_moment) def test_include_moment_renders_properly(self, app, moment): ts = str(render_template_string("{{ moment.include_moment() }}")) @@ -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 ['']]) + assert all([each in str(include_jquery) for each in [ + '']]) class TestPrivateMomentClass(object): @@ -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() @@ -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() @@ -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 @@ -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 @@ -181,9 +186,10 @@ 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() @@ -191,9 +197,10 @@ def test_fromTime_no_suffix(self): 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() @@ -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 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..1aef3bf --- /dev/null +++ b/tox.ini @@ -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