Skip to content

Commit

Permalink
Merge pull request #111 from level12/110-pytest
Browse files Browse the repository at this point in the history
Replace nose with pytest
  • Loading branch information
guruofgentoo committed Jun 26, 2020
2 parents 2dfbba5 + 1fe563c commit dfdb3b0
Show file tree
Hide file tree
Showing 13 changed files with 550 additions and 581 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -12,7 +12,7 @@ jobs:
command: ls -al

- run:
name: install tox from wheelhouse
name: install tox
command: >
pip install --upgrade --force-reinstall tox
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
@@ -0,0 +1,2 @@
[pytest]
addopts = --blazeweb_package webgrid_blazeweb_ta --blazeweb_profile Test
9 changes: 5 additions & 4 deletions setup.py
Expand Up @@ -16,7 +16,9 @@
'SQLAlchemy>=0.9.0'
'SQLAlchemyBWC',
'mock',
'nose',
'nose', # required to import some blazeweb helpers
'pytest',
'pytest-cov',
'Flask',
'Flask-Bootstrap',
'Flask-Script',
Expand Down Expand Up @@ -56,6 +58,7 @@
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
],
license='BSD-3-Clause',
packages=['webgrid'],
Expand All @@ -76,12 +79,10 @@
'SQLAlchemy',
'jinja2',
'python-dateutil',
'Werkzeug',
'Werkzeug<1.0.0',
],
entry_points="""
[console_scripts]
webgrid_ta = webgrid_ta.manage:script_entry
[nose.plugins]
webgridta_initapp = webgrid.webgrid_nose:WebGridNosePlugin
""",
)
30 changes: 15 additions & 15 deletions tox.ini
Expand Up @@ -12,20 +12,20 @@ deps =
py37: formencode>=2.0.0a1

commands =
base: pip install -e .[develop]
i18n: pip install -e .[develop,i18n]
nosetests \
--nologcapture \
--with-coverage \
--with-xunit \
--xunit-file={toxinidir}/.ci/test-reports/{envname}.nose.xml \
--cover-tests \
--cover-branches \
--cover-package webgrid \
--cover-package webgrid_ta \
--cover-package webgrid_blazeweb_ta \
--cover-xml \
{posargs}
base: pip install --progress-bar off -e .[develop]
i18n: pip install --progress-bar off -e .[develop,i18n]
pip freeze
py.test \
-ra \
--tb native \
--strict \
--cov webgrid \
--cov webgrid_ta \
--cov webgrid_blazeweb_ta \
--cov-config .coveragerc \
--no-cov-on-fail \
--junit-xml={toxinidir}/.ci/test-reports/{envname}.pytests.xml \
webgrid

[testenv:i18n]
skip_install = true
Expand All @@ -42,4 +42,4 @@ commands = flake8 webgrid webgrid_ta webgrid_blazeweb_ta
[flake8]
max-line-length = 100
max-complexity = 10
ignore = E402,W503
ignore = W503
8 changes: 8 additions & 0 deletions webgrid/tests/conftest.py
@@ -0,0 +1,8 @@

def pytest_configure(config):
from webgrid_ta.app import create_app
app = create_app(config='Test')
app.test_request_context().push()

from webgrid_ta.model import load_db
load_db()
176 changes: 87 additions & 89 deletions webgrid/tests/test_blazeweb.py
@@ -1,91 +1,89 @@
from __future__ import absolute_import

from os import path

import six

# !!!: Should blazeweb be tested for Python 3?
if six.PY2: # noqa
from blazeutils import tolist
from blazeweb.events import signal
from blazeweb.globals import ag, settings, user
from blazeweb.hierarchy import findobj
from blazeweb.scripting import load_current_app
from blazeweb.testing import inrequest, TestApp
from nose.tools import eq_
from sqlalchemy.orm.session import Session as SASession
import mock

from webgrid.renderers import HTML

class TestBlazeWeb(object):
@classmethod
def setup_class(cls):
# these lines necessary because we are sharing test space with a Flask
# app built with FlaskSQLAlchemy. That lib places listeners on the
# session class but expects its own code will run to set up a session
SASession._model_changes = mock.Mock()
SASession.app = mock.Mock()

_, _, _, wsgiapp = load_current_app('webgrid_blazeweb_ta', 'Test')

# make the app available to the tests
ag.wsgi_test_app = wsgiapp

# an application can define functions to be called after the app
# is initialized but before any test inspection is done or tests
# are ran. We call those functions here:
for callstring in tolist(settings.testing.init_callables):
tocall = findobj(callstring)
tocall()

# we also support events for pre-test setup
signal('blazeweb.pre_test_init').send()

cls.ta = TestApp(ag.wsgi_test_app)

def test_xls_as_response(self):
r = self.ta.get('/people/manage?export_to=xls')
eq_(r.headers['Content-Type'], 'application/vnd.ms-excel')

def test_xlsx_as_response(self):
r = self.ta.get('/people/manage?export_to=xlsx')
eq_(r.headers['Content-Type'],
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

def test_csv_as_response(self):
r = self.ta.get('/people/manage?export_to=csv')
eq_(r.headers['Content-Type'], 'text/csv')

@inrequest('/')
def test_nonstandard_templating(self):
from webgrid_blazeweb_ta.views import PeopleGrid as PGBase

class PeopleGridHTML(HTML):
def header(self):
return self.load_content(
'nonstandard_header.html',
session_key=self.grid.session_key,
renderer=self,
)

class PeopleGrid(PGBase):
def set_renderers(self):
self.html = PeopleGridHTML(self)

g = PeopleGrid()
g.apply_qs_args()
assert '#something' in g.html().data['text/css'][0]

@inrequest('/path?session_key=123456')
def test_session(self):
from webgrid_blazeweb_ta.views import PeopleGrid
g = PeopleGrid()
g.apply_qs_args()
assert '123456' in user.dgsessions

@inrequest('/path?session_key=123456')
def test_static_path(self):
from webgrid_blazeweb_ta.views import PeopleGrid
g = PeopleGrid()
assert g.manager.static_path().endswith('webgrid{}static'.format(path.sep))
from blazeutils import tolist
from blazeweb.events import signal
from blazeweb.globals import ag, settings, user
from blazeweb.hierarchy import findobj
from blazeweb.scripting import load_current_app
from blazeweb.testing import inrequest
from sqlalchemy.orm.session import Session as SASession
import mock

from webgrid.renderers import HTML


class TestBlazeWeb(object):
@classmethod
def setup_class(cls):
from blazeweb.testing import TestApp

# these lines necessary because we are sharing test space with a Flask
# app built with FlaskSQLAlchemy. That lib places listeners on the
# session class but expects its own code will run to set up a session
SASession._model_changes = mock.Mock()
SASession.app = mock.Mock()

_, _, _, wsgiapp = load_current_app('webgrid_blazeweb_ta', 'Test')

# make the app available to the tests
ag.wsgi_test_app = wsgiapp

# an application can define functions to be called after the app
# is initialized but before any test inspection is done or tests
# are ran. We call those functions here:
for callstring in tolist(settings.testing.init_callables):
tocall = findobj(callstring)
tocall()

# we also support events for pre-test setup
signal('blazeweb.pre_test_init').send()

cls.ta = TestApp(ag.wsgi_test_app)

def test_xls_as_response(self):
r = self.ta.get('/people/manage?export_to=xls')
assert r.headers['Content-Type'] == 'application/vnd.ms-excel'

def test_xlsx_as_response(self):
r = self.ta.get('/people/manage?export_to=xlsx')
assert (
r.headers['Content-Type']
== 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)

def test_csv_as_response(self):
r = self.ta.get('/people/manage?export_to=csv')
assert r.headers['Content-Type'] == 'text/csv'

@inrequest('/')
def test_nonstandard_templating(self):
from webgrid_blazeweb_ta.views import PeopleGrid as PGBase

class PeopleGridHTML(HTML):
def header(self):
return self.load_content(
'nonstandard_header.html',
session_key=self.grid.session_key,
renderer=self,
)

class PeopleGrid(PGBase):
def set_renderers(self):
self.html = PeopleGridHTML(self)

g = PeopleGrid()
g.apply_qs_args()
assert '#something' in g.html().data['text/css'][0]

@inrequest('/path?session_key=123456')
def test_session(self):
from webgrid_blazeweb_ta.views import PeopleGrid
g = PeopleGrid()
g.apply_qs_args()
assert '123456' in user.dgsessions

@inrequest('/path?session_key=123456')
def test_static_path(self):
from webgrid_blazeweb_ta.views import PeopleGrid
g = PeopleGrid()
assert g.manager.static_path().endswith('webgrid{}static'.format(path.sep))

0 comments on commit dfdb3b0

Please sign in to comment.