From 0ca4a6bfb8c34fd165f1aa5f7e05894ca4cb9205 Mon Sep 17 00:00:00 2001 From: Bruce Schubert Date: Wed, 19 Aug 2020 08:05:32 -0700 Subject: [PATCH] Added pytest based unit test stub for webapp - Use `python -m pytest -v ../tests/` command from `src/` folder - Re: #67 --- requirements.txt | 2 +- tests/test_webapp.py | 72 ++++++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/requirements.txt b/requirements.txt index 234520c..ca9833d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,9 +14,9 @@ lxml==4.5.2 MarkupSafe==1.1.1 pigpio==1.46 pyserial==3.4 +pytest=6.0.1 PyYAML==5.3.1 RPi.GPIO==0.7.0 RPIO==0.10.0 soupsieve==1.9.6 Werkzeug==1.0.1 - diff --git a/tests/test_webapp.py b/tests/test_webapp.py index a5b302b..5b4e58a 100644 --- a/tests/test_webapp.py +++ b/tests/test_webapp.py @@ -23,45 +23,59 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. - - import os import sys -import unittest +import tempfile + +import pytest + +# ~ from hardware.indicators import MessageIndicator +from src.userinterface.webapp import app, get_random_string, get_db + + +# Read in SQL for populating test data +with open(os.path.join(os.path.dirname(__file__), "callattendant.db.sql"), "rb") as f: + _data_sql = f.read().decode("utf8") -currentdir = os.path.dirname(os.path.realpath(__file__)) -sys.path.append(os.path.join(currentdir, "../src")) -sys.path.append(os.path.join(currentdir, "../src/hardware")) -sys.path.append(os.path.join(currentdir, "../src/messaging")) -sys.path.append(os.path.join(currentdir, "../src/screening")) -from userinterface.webapp import app -from hardware.indicators import MessageIndicator +@pytest.fixture +def myapp(): + """Create and configure a new app instance for each test.""" -class TestFlask(unittest.TestCase): + # create a temporary file to isolate the database for each test + db_fd, db_path = tempfile.mkstemp() + + app.secret_key = get_random_string() + with app.app_context(): - def setUp(self): - app.testing = True app.config["TESTING"] = True app.config["DEBUG"] = True - # Add addtional settings from callattendant config - app.config["ROOT_PATH"] = os.path.dirname(os.path.realpath(__file__)) - app.config["DATABASE"] = "../data/callattendant.db" - app.config["VOICE_MAIL_MESSAGE_FOLDER"] = "../data/messages" - #app.config["MESSAGE_INDICATOR_LED"] = MessageIndicator(10) - app.config["DB_PATH"] = os.path.join(app.config["ROOT_PATH"], app.config["DATABASE"]) + app.config['DATABASE'] = db_path + app.config["DB_PATH"] = db_path + # ~ app.config["VOICE_MAIL_MESSAGE_FOLDER"] = "../data/messages" + # ~ app.config["MESSAGE_INDICATOR_LED"] = MessageIndicator(10) + + get_db().executescript(_data_sql) + + yield app + + # close and remove the temporary database + os.close(db_fd) + os.unlink(db_path) + - self.app = app.test_client() +@pytest.fixture +def client(myapp): + """A test client for the app.""" + return app.test_client() - self.assertEqual(app.debug, True) - def test_dashboard(self): - response = self.app.get('/', follow_redirects = True) - self.assertEqual(response.status_code, 200) +def test_dashboard(client): + response = client.get('/', follow_redirects = True) + assert response.status_code == 200 + assert b"Dashboard" in response.data + assert b"Statistics" in response.data + assert b"Recent Calls" in response.data + assert b"Calls per Day" in response.data - def test_calls(self): - response = self.app.get('/calls', follow_redirects = True) - self.assertEqual(response.status_code, 200) -if __name__ == '__main__': - unittest.main()