Skip to content

Commit 29de62b

Browse files
authored
Address pytest warnings (#101)
* Fix old style assertEquals * Remove unused import * Add session tags for filtering * Add tests * Fix pypy warnings * Fail on warnings from within the project * Add pointer to noxfile help * Fix formatting
1 parent 7c9ab9c commit 29de62b

6 files changed

Lines changed: 51 additions & 13 deletions

File tree

docs/CONTRIBUTING.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ To run the tests and linters, you can use the following command:
3838
3939
$ uv run noxfile.py
4040
41+
For futher details, refer to the ``noxfile.py`` script.
4142

4243
.. |IRC Freenode| image:: https://img.shields.io/badge/irc-freenode-blue.svg
4344
:target: https://webchat.freenode.net/?channels=sacrud

noxfile.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
$ uv run noxfile.py
2626
Run all tests with coverage and linting:
2727
$ uv run noxfile.py -- --coverage
28+
Run tests for a specific SQLAlchemy version:
29+
$ uv run noxfile.py -t sqla12
30+
Run tests for a specific Python version:
31+
$ uv run noxfile.py -s test -p 3.X
32+
$ uv run noxfile.py -s test -p pypy-3.X # For PyPy
2833
2934
Set up a development environment with the default Python version (3.8):
3035
$ uv run noxfile.py -s dev
@@ -56,7 +61,7 @@ def lint(session):
5661
"flake8", ".", "--count", "--select=E9,F63,F7,F82", "--show-source", "--statistics", "--extend-exclude", ".venv")
5762
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
5863
session.run(
59-
"flake8", ".", "--count", "--exit-zero", "--max-complexity=10",
64+
"flake8", ".", "--count", "--exit-zero", "--max-complexity=10", "--extend-ignore=E711",
6065
"--max-line-length=127", "--statistics", "--extend-exclude", ".venv")
6166

6267

@@ -81,7 +86,10 @@ def parametrize_test_versions():
8186
]
8287

8388
return [
84-
(f"{interpreter}3.{python_minor}", str(sqlalchemy_version))
89+
nox.param(
90+
f"{interpreter}3.{python_minor}", str(sqlalchemy_version),
91+
tags=[f"sqla{sqlalchemy_version.major}{sqlalchemy_version.minor}"]
92+
)
8593
for interpreter in ("", "pypy-")
8694
for python_minor in range(PYTHON_MINOR_VERSION_MIN, PYTHON_MINOR_VERSION_MAX + 1)
8795
for sqlalchemy_version in filtered_sqlalchemy_versions
@@ -95,15 +103,27 @@ def test(session, sqlalchemy):
95103
"""Run tests with pytest.
96104
97105
Use the --coverage option to run tests with coverage.
106+
107+
For running tests for a specific SQLAlchemy version, use the tags option:
108+
109+
$ uv run noxfile.py -s test -t sqla12
110+
98111
For fine-grained control over running the tests, refer the nox documentation: https://nox.thea.codes/en/stable/usage.html
99112
"""
100113
session.install("-r", "requirements-test.txt")
101114
session.install(f"sqlalchemy~={sqlalchemy}.0")
102-
if "--coverage" in session.posargs:
103-
session.run("coverage", "run", "--source=sqlalchemy_mptt", "-m", "pytest", "sqlalchemy_mptt/")
104-
session.run("coverage", "xml")
115+
session.install("-e", ".")
116+
try:
117+
session.posargs.remove("--coverage")
118+
except ValueError:
119+
with_coverage = False
105120
else:
106-
session.run("pytest", "sqlalchemy_mptt/")
121+
with_coverage = True
122+
pytest_cmd = [
123+
"pytest", "--pyargs", "sqlalchemy_mptt",
124+
"--cov", "sqlalchemy_mptt", "--cov-report", "term-missing:skip-covered"
125+
] + ["--cov-report", "xml"] if with_coverage else [] + session.posargs
126+
session.run(*pytest_cmd)
107127

108128

109129
@nox.session(default=False)

sqlalchemy_mptt/tests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def __init__(self, session):
5656

5757
def add(self, model, fixtures):
5858
here = os.path.dirname(os.path.realpath(__file__))
59-
file = open(os.path.join(here, fixtures))
60-
fixtures = json.loads(file.read())
59+
with open(os.path.join(here, fixtures)) as file:
60+
fixtures = json.loads(file.read())
6161
for fixture in fixtures:
6262
if hasattr(model, "sqlalchemy_mptt_pk_name"):
6363
fixture[model.sqlalchemy_mptt_pk_name] = fixture.pop("id")

sqlalchemy_mptt/tests/cases/get_node.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# Copyright © 2015 uralbash <root@uralbash.ru>
66
#
77
# Distributed under terms of the MIT license.
8-
import os
98

109

1110
class GetNodes(object):

sqlalchemy_mptt/tests/cases/get_tree.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,24 @@ def go(id):
267267
]
268268
self.assertEqual(tree, reference_tree)
269269

270+
def test_drilldown_tree_without_session(self):
271+
def go(id):
272+
return get_obj(self.session, self.model, id)
273+
node = go(7)
274+
tree = node.drilldown_tree()
275+
reference_tree = [
276+
{'node': go(7),
277+
'children': [
278+
{'node': go(8),
279+
'children': [
280+
{'node': go(9)}]},
281+
{'node': go(10),
282+
'children': [
283+
{'node': go(11)}]}]
284+
}
285+
]
286+
self.assertEqual(tree, reference_tree)
287+
270288
def test_path_to_root(self):
271289
r"""Generate path from a leaf or intermediate node to the root.
272290

sqlalchemy_mptt/tests/test_inheritance.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ def test_create_delete(self):
9090
self.session.delete(child1)
9191
self.session.commit()
9292

93-
self.assertEquals(None, self.session.query(SpecializedTree).get(2))
93+
self.assertEqual(None, self.session.query(SpecializedTree).get(2))
9494

9595
self.session.delete(child2)
9696
self.session.commit()
9797

98-
self.assertEquals(None, self.session.query(SpecializedTree).get(3))
99-
self.assertEquals(None, self.session.query(SpecializedTree).get(4))
100-
self.assertEquals(None, self.session.query(SpecializedTree).get(5))
98+
self.assertEqual(None, self.session.query(SpecializedTree).get(3))
99+
self.assertEqual(None, self.session.query(SpecializedTree).get(4))
100+
self.assertEqual(None, self.session.query(SpecializedTree).get(5))
101101

102102

103103
class TestGenericTree(TreeTestingMixin, unittest.TestCase):

0 commit comments

Comments
 (0)