Skip to content

Commit

Permalink
Setup unit tests and attempt database mock
Browse files Browse the repository at this point in the history
  • Loading branch information
rmarow committed Aug 29, 2023
1 parent 6aa6c1e commit 7aa0c1c
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 22 deletions.
90 changes: 68 additions & 22 deletions conda-lock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
version: 1
metadata:
content_hash:
linux-64: 9ca80891f27e24e051d196d3207ca70b13a78ad0e6a235fac7dd3981069b6b5b
osx-64: b1a86cfc4d33775b9f37a8bb04c9164feae7fb434c18b6dd8d8042f088cba2ba
linux-64: d8b69bc636a23839893edf789d637d665cafe2d0cbb2b26c9bea77e579ad120d
osx-64: de64b803a77cc09f032fd1ade06f72e28161d09174ea21ded1c8823d6e6bdd31
channels:
- url: conda-forge
used_env_vars: []
Expand Down Expand Up @@ -478,15 +478,15 @@ package:
category: main
optional: false
- name: filelock
version: 3.12.2
version: 3.12.3
manager: conda
platform: linux-64
dependencies:
python: '>=3.7'
url: https://conda.anaconda.org/conda-forge/noarch/filelock-3.12.2-pyhd8ed1ab_0.conda
url: https://conda.anaconda.org/conda-forge/noarch/filelock-3.12.3-pyhd8ed1ab_0.conda
hash:
md5: 53522ec72e6adae42bd373ef58357230
sha256: 1cbae9f05860f2e566e2977f14dfcd5494beb22c028b0a853ade4ec381d9de71
md5: 3104cf0ab9fb9de393051bf92b10dbe9
sha256: 47635be45aa6cbfd6af65c13b5f649aef84c484e1897aef625e4ad717663eebc
category: main
optional: false
- name: greenlet
Expand Down Expand Up @@ -1187,16 +1187,16 @@ package:
sha256: 2577c501a2fb8d05a304c09d090d6e47c306fef15809d102b327cf8364bddab5
category: main
optional: false
- name: wtforms-sqlalchemy
version: '0.3'
- name: sqlalchemy
version: 2.0.20
manager: pip
platform: linux-64
dependencies:
wtforms: '>=1.0.5'
sqlalchemy: '>=0.7.10'
url: https://files.pythonhosted.org/packages/62/71/a5624ff63f4024d27489263b56e3ab6a5122087eb432d14617cfed90ec3d/WTForms_SQLAlchemy-0.3-py3-none-any.whl
typing-extensions: '>=4.2.0'
greenlet: '!=0.4.17'
url: https://files.pythonhosted.org/packages/9f/94/8a20e0b867857cacf7e1269efc02aa81866071df844a2598fa34245a50df/SQLAlchemy-2.0.20-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
hash:
sha256: 90195d7592bf256d82498c42c79d416832e4a4e6fbca4f1e745a018f66d26c47
sha256: a3f0dd6d15b6dc8b28a838a5c48ced7455c3e1fb47b89da9c79cc2090b072a50
category: main
optional: false
- name: flask-dance
Expand All @@ -1215,6 +1215,29 @@ package:
sha256: 935f5806d587eed94b53cf356ee921466bb32ba83405ed5a4e8ac8ea859620dc
category: main
optional: false
- name: mock-alchemy
version: 0.2.6
manager: pip
platform: linux-64
dependencies:
sqlalchemy: '>=2.0.6,<3.0.0'
url: https://files.pythonhosted.org/packages/aa/93/8d1f7ee9fa858d1c13511de6e0bedf7f57d15e9c7aab0e9fdf4d66074417/mock_alchemy-0.2.6-py3-none-any.whl
hash:
sha256: d5e17f2c92d0299d70cd9a0d3c8f96f759a81c285c76b03f4192451583bf865f
category: main
optional: false
- name: wtforms-sqlalchemy
version: '0.3'
manager: pip
platform: linux-64
dependencies:
wtforms: '>=1.0.5'
sqlalchemy: '>=0.7.10'
url: https://files.pythonhosted.org/packages/62/71/a5624ff63f4024d27489263b56e3ab6a5122087eb432d14617cfed90ec3d/WTForms_SQLAlchemy-0.3-py3-none-any.whl
hash:
sha256: 90195d7592bf256d82498c42c79d416832e4a4e6fbca4f1e745a018f66d26c47
category: main
optional: false
- name: bzip2
version: 1.0.8
manager: conda
Expand Down Expand Up @@ -1568,15 +1591,15 @@ package:
category: main
optional: false
- name: filelock
version: 3.12.2
version: 3.12.3
manager: conda
platform: osx-64
dependencies:
python: '>=3.7'
url: https://conda.anaconda.org/conda-forge/noarch/filelock-3.12.2-pyhd8ed1ab_0.conda
url: https://conda.anaconda.org/conda-forge/noarch/filelock-3.12.3-pyhd8ed1ab_0.conda
hash:
md5: 53522ec72e6adae42bd373ef58357230
sha256: 1cbae9f05860f2e566e2977f14dfcd5494beb22c028b0a853ade4ec381d9de71
md5: 3104cf0ab9fb9de393051bf92b10dbe9
sha256: 47635be45aa6cbfd6af65c13b5f649aef84c484e1897aef625e4ad717663eebc
category: main
optional: false
- name: greenlet
Expand Down Expand Up @@ -2267,16 +2290,16 @@ package:
sha256: 2577c501a2fb8d05a304c09d090d6e47c306fef15809d102b327cf8364bddab5
category: main
optional: false
- name: wtforms-sqlalchemy
version: '0.3'
- name: sqlalchemy
version: 2.0.20
manager: pip
platform: osx-64
dependencies:
wtforms: '>=1.0.5'
sqlalchemy: '>=0.7.10'
url: https://files.pythonhosted.org/packages/62/71/a5624ff63f4024d27489263b56e3ab6a5122087eb432d14617cfed90ec3d/WTForms_SQLAlchemy-0.3-py3-none-any.whl
typing-extensions: '>=4.2.0'
greenlet: '!=0.4.17'
url: https://files.pythonhosted.org/packages/fe/1c/d3ca3095528c262125251ee42782086d1d2bad4e59adf0dbdd9540147c23/SQLAlchemy-2.0.20-cp310-cp310-macosx_10_9_x86_64.whl
hash:
sha256: 90195d7592bf256d82498c42c79d416832e4a4e6fbca4f1e745a018f66d26c47
sha256: 759b51346aa388c2e606ee206c0bc6f15a5299f6174d1e10cadbe4530d3c7a98
category: main
optional: false
- name: flask-dance
Expand All @@ -2295,3 +2318,26 @@ package:
sha256: 935f5806d587eed94b53cf356ee921466bb32ba83405ed5a4e8ac8ea859620dc
category: main
optional: false
- name: mock-alchemy
version: 0.2.6
manager: pip
platform: osx-64
dependencies:
sqlalchemy: '>=2.0.6,<3.0.0'
url: https://files.pythonhosted.org/packages/aa/93/8d1f7ee9fa858d1c13511de6e0bedf7f57d15e9c7aab0e9fdf4d66074417/mock_alchemy-0.2.6-py3-none-any.whl
hash:
sha256: d5e17f2c92d0299d70cd9a0d3c8f96f759a81c285c76b03f4192451583bf865f
category: main
optional: false
- name: wtforms-sqlalchemy
version: '0.3'
manager: pip
platform: osx-64
dependencies:
wtforms: '>=1.0.5'
sqlalchemy: '>=0.7.10'
url: https://files.pythonhosted.org/packages/62/71/a5624ff63f4024d27489263b56e3ab6a5122087eb432d14617cfed90ec3d/WTForms_SQLAlchemy-0.3-py3-none-any.whl
hash:
sha256: 90195d7592bf256d82498c42c79d416832e4a4e6fbca4f1e745a018f66d26c47
category: main
optional: false
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ dependencies:
- wtforms-sqlalchemy
- flask-dance
- bootstrap-flask ~=2.3.0
- mock-alchemy~=0.2.6
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[tool.pytest.ini_options]
testpaths = ['tests']
pythonpath = ['.']

[tool.black]
target-version = ['py310']
skip-string-normalization = true
Expand Down
7 changes: 7 additions & 0 deletions tasks/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def typecheck(ctx):
print('🎉🦆 Type checking passed.')


@task()
def unit(ctx):
"""Run unit tests."""
print_and_run(f'cd {PROJECT_DIR} && pytest')
print('🎉🦆 Unit checking passed.')


@task(
pre=[typecheck],
default=True,
Expand Down
38 changes: 38 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from unittest import mock

import pytest
from flask_login import FlaskLoginClient
from mock_alchemy.mocking import UnifiedAlchemyMagicMock

from usaon_vta_survey import app as my_app
from usaon_vta_survey.models.tables import User


@pytest.fixture()
def app():
yield my_app


@pytest.fixture()
def client(app):
return app.test_client()


@pytest.fixture()
def session_with_user():
session = UnifiedAlchemyMagicMock(
data=[
(
[mock.call.query(User), mock.call.get(1)],
[User(id=1, email='[email protected]', name='foo bar', role_id='admin')],
)
]
)
return session


@pytest.fixture()
def client_logged_in(app, session_with_user):
app.test_client_class = FlaskLoginClient
user = session_with_user.query(User).get(1)
return app.test_client(user=user)
2 changes: 2 additions & 0 deletions tests/test_foo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_foo():
assert True
5 changes: 5 additions & 0 deletions tests/test_logged_in.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test_logged_in(client_logged_in):
actual = client_logged_in.get('/')
# breakpoint()
expected = ...
assert actual == expected
4 changes: 4 additions & 0 deletions tests/test_logged_out.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_logged_out(client):
actual = client.get('/').location
expected = '/login?next=%2F'
assert actual == expected
7 changes: 7 additions & 0 deletions tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from usaon_vta_survey.constants.version import VERSION


def test_version_matches(client):
actual = client.get('/login', follow_redirects=True).text
expected = VERSION
assert expected in actual

0 comments on commit 7aa0c1c

Please sign in to comment.