diff --git a/NOTES.md b/NOTES.md index 0298bce..bbbb26c 100644 --- a/NOTES.md +++ b/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 ... diff --git a/app/__init__.py b/app/__init__.py index 48b4f98..a111afe 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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) diff --git a/app/main.py b/app/main.py index fb51730..3536e6d 100644 --- a/app/main.py +++ b/app/main.py @@ -20,7 +20,12 @@ def create_player(): @main.route("/player/") 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'} \ No newline at end of file + return {k: v for (k, v) in o.__dict__.items() if k != '_sa_instance_state'} \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..816cbdd --- /dev/null +++ b/tests/conftest.py @@ -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() diff --git a/tests/test_fresh_db_tests.py b/tests/test_fresh_db_tests.py new file mode 100644 index 0000000..c5f0822 --- /dev/null +++ b/tests/test_fresh_db_tests.py @@ -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'