Very rudimentary tests

This commit is contained in:
Jack Jackson 2024-01-23 22:41:24 -08:00
parent 5af581923d
commit 51d6a85955
6 changed files with 59 additions and 4 deletions

View File

@ -4,7 +4,7 @@
- [X] Basic Player Definition - [X] Basic Player Definition
- [ ] Basic Deck Definition - [ ] Basic Deck Definition
- [X] Figure out how to return JSON or html (`render_template`) - [X] Figure out how to return JSON or html (`render_template`)
- [ ] Basic testing - [X] Basic testing
- [ ] Swagger API - [ ] Swagger API
- [ ] Local development tool to clear/seed database - [ ] Local development tool to clear/seed database
... ...

View File

@ -16,7 +16,7 @@ def create_app():
app.config['SECRET_KEY'] = secret_key app.config['SECRET_KEY'] = secret_key
# TODO - support other database types 🙃 # 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) db.init_app(app)

View File

@ -20,7 +20,12 @@ def create_player():
@main.route("/player/<player_id>") @main.route("/player/<player_id>")
def get_player(player_id: str): 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') content_type = request.headers.get('Content-Type')
if content_type == 'application/json': if content_type == 'application/json':
return player_data return player_data
@ -34,4 +39,4 @@ def get_player(player_id: str):
# extend? # extend?
# (Probably not, as we'd still need to explicitly call it - it wouldn't be implicitly called _by_ Flask) # (Probably not, as we'd still need to explicitly call it - it wouldn't be implicitly called _by_ Flask)
def _jsonify(o): 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
View File

40
tests/conftest.py Normal file
View 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()

View 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'