Skip to content

Commit

Permalink
Added pytest based unit test stub for webapp
Browse files Browse the repository at this point in the history
- Use `python -m pytest -v ../tests/` command from `src/` folder
- Re: #67
  • Loading branch information
emxsys committed Aug 19, 2020
1 parent 1c1c8d7 commit 0ca4a6b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

72 changes: 43 additions & 29 deletions tests/test_webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 0ca4a6b

Please sign in to comment.