diff options
| author | Mitsuo Tokumori <[email protected]> | 2023-10-29 06:59:37 -0500 |
|---|---|---|
| committer | Mitsuo Tokumori <[email protected]> | 2023-10-29 06:59:37 -0500 |
| commit | 9baa55436bc5a98d118a17656bbf25e563522964 (patch) | |
| tree | daa01118bd11f5cf13d386d2c727a384563df1da /tests/test_db.py | |
| parent | 71e7d6516608486f67afad5aad1f7b7f9a45886f (diff) | |
| download | ustayml-9baa55436bc5a98d118a17656bbf25e563522964.tar.gz ustayml-9baa55436bc5a98d118a17656bbf25e563522964.tar.bz2 ustayml-9baa55436bc5a98d118a17656bbf25e563522964.zip | |
Add unit tests
Diffstat (limited to 'tests/test_db.py')
| -rw-r--r-- | tests/test_db.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/test_db.py b/tests/test_db.py new file mode 100644 index 0000000..6ac4635 --- /dev/null +++ b/tests/test_db.py @@ -0,0 +1,33 @@ +import sqlite3 + +import pytest +from flaskr.db import get_db + + +def test_get_close_db(app): + with app.app_context(): + db = get_db() + assert db is get_db() + + with pytest.raises(sqlite3.ProgrammingError) as e: + db.execute('SELECT 1') + + assert 'closed' in str(e.value) + + +def test_init_db_command(runner, monkeypatch): + """ + This test uses Pytest’s monkeypatch fixture to replace the init_db + function with one that records that it’s been called. The runner fixture you + wrote above is used to call the init-db command by name. + """ + class Recorder(object): + called = False + + def fake_init_db(): + Recorder.called = True + + monkeypatch.setattr('flaskr.db.init_db', fake_init_db) + result = runner.invoke(args=['init-db']) + assert 'Initialized' in result.output + assert Recorder.called
\ No newline at end of file |
