Very rudimentary tests
This commit is contained in:
parent
5af581923d
commit
51d6a85955
2
NOTES.md
2
NOTES.md
@ -4,7 +4,7 @@
|
||||
- [X] Basic Player Definition
|
||||
- [ ] Basic Deck Definition
|
||||
- [X] Figure out how to return JSON or html (`render_template`)
|
||||
- [ ] Basic testing
|
||||
- [X] Basic testing
|
||||
- [ ] Swagger API
|
||||
- [ ] Local development tool to clear/seed database
|
||||
...
|
||||
|
@ -16,7 +16,7 @@ def create_app():
|
||||
app.config['SECRET_KEY'] = secret_key
|
||||
|
||||
# TODO - support other database types 🙃
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URI', 'sqlite:///db.sqlite')
|
||||
|
||||
db.init_app(app)
|
||||
|
||||
|
@ -20,7 +20,12 @@ def create_player():
|
||||
|
||||
@main.route("/player/<player_id>")
|
||||
def get_player(player_id: str):
|
||||
player_data = _jsonify(db.session.get(Player, int(player_id)))
|
||||
player_from_db = db.session.get(Player, int(player_id))
|
||||
if not player_from_db:
|
||||
return 'Not Found', 404
|
||||
|
||||
player_data = _jsonify(player_from_db)
|
||||
|
||||
content_type = request.headers.get('Content-Type')
|
||||
if content_type == 'application/json':
|
||||
return player_data
|
||||
@ -34,4 +39,4 @@ def get_player(player_id: str):
|
||||
# extend?
|
||||
# (Probably not, as we'd still need to explicitly call it - it wouldn't be implicitly called _by_ Flask)
|
||||
def _jsonify(o):
|
||||
return {k: v for (k, v) in o.__dict__.items() if k is not '_sa_instance_state'}
|
||||
return {k: v for (k, v) in o.__dict__.items() if k != '_sa_instance_state'}
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
40
tests/conftest.py
Normal file
40
tests/conftest.py
Normal file
@ -0,0 +1,40 @@
|
||||
import os
|
||||
import pathlib
|
||||
import pytest
|
||||
|
||||
# TODO - this seems to be a "magic path" which makes fixtures available. Learn more about it by reading
|
||||
# https://stackoverflow.com/questions/34466027/what-is-conftest-py-for-in-pytest
|
||||
|
||||
from app import create_app
|
||||
|
||||
# https://flask.palletsprojects.com/en/2.3.x/testing/
|
||||
|
||||
@pytest.fixture()
|
||||
def app_fixture():
|
||||
# Start afresh!
|
||||
test_database_name = 'testing-db.sqlite'
|
||||
database_location = pathlib.Path('instance').joinpath(test_database_name)
|
||||
if database_location.exists():
|
||||
database_location.unlink()
|
||||
|
||||
os.environ['DATABASE_URI'] = f'sqlite:///{test_database_name}'
|
||||
os.environ['SECRET_KEY'] = 'testing-secret-key'
|
||||
|
||||
app = create_app()
|
||||
# app.config.update({
|
||||
# 'TESTING': True
|
||||
# })
|
||||
|
||||
# other setup can go here
|
||||
|
||||
yield app
|
||||
|
||||
# clean up / reset resources here
|
||||
|
||||
@pytest.fixture()
|
||||
def client(app_fixture):
|
||||
return app_fixture.test_client()
|
||||
|
||||
@pytest.fixture()
|
||||
def runner(app_fixture):
|
||||
return app_fixture.test_cli_runner()
|
10
tests/test_fresh_db_tests.py
Normal file
10
tests/test_fresh_db_tests.py
Normal file
@ -0,0 +1,10 @@
|
||||
# These tests expect that the database starts empty.
|
||||
# TODO: create tests with initialized states
|
||||
|
||||
def test_add_and_retrieve(client):
|
||||
response = client.get('/player/1', headers={'Content-Type': 'application/json'})
|
||||
assert response.status_code == 404
|
||||
|
||||
client.post('/player', headers={'Content-Type': 'application/json'}, json={'name': 'jason'})
|
||||
response_1 = client.get('/player/1', headers={'Content-Type': 'application/json'})
|
||||
assert response_1.json['name'] == 'jason'
|
Loading…
x
Reference in New Issue
Block a user