diff --git a/src/screening/calllogger.py b/src/screening/calllogger.py index ef9e566..e33f18f 100644 --- a/src/screening/calllogger.py +++ b/src/screening/calllogger.py @@ -132,71 +132,3 @@ def __init__(self, db, config): if self.config["DEBUG"]: print("CallLogger initialized") - - -def test(db, config): - ''' Unit Tests ''' - print("*** Running CallLogger Unit Tests ***") - - from screening.query_db import query_db - # Create the logger to be tested - logger = CallLogger(db, config) - - # Caller to be added - callerid = { - "NAME": "Bruce", - "NMBR": "1234567890", - "DATE": "1012", - "TIME": "0600", - } - - print("Adding caller:") - pprint(callerid) - - try: - print("Assert log_caller returns #1") - assert logger.log_caller(callerid, "Permitted", "Test1") == 1, "call # should be 1" - - print("Assert log_caller returns #2") - assert logger.log_caller(callerid) == 2, "call # should be 2" - - # List the records - query = "SELECT * FROM CallLog" - results = query_db(db, query) - print(query + " results:") - pprint(results) - - except AssertionError as e: - print("*** Unit Test FAILED ***") - pprint(e) - return 1 - - print("*** Unit Tests PASSED ***") - return 0 - - -if __name__ == '__main__': - ''' - Run the unit tests - ''' - # Create the test db in RAM - import sqlite3 - db = sqlite3.connect(":memory:") - - # Add the parent directory to the path so callattendant can be found - import os - import sys - currentdir = os.path.dirname(os.path.realpath(__file__)) - parentdir = os.path.dirname(currentdir) - sys.path.append(parentdir) - - # Create and tweak a default config suitable for unit testing - from callattendant import make_config, print_config - config = make_config() - config['DEBUG'] = True - print_config(config) - - # Run the tests - sys.exit(test(db, config)) - - print("Tests complete") diff --git a/tests/test_calllogger.py b/tests/test_calllogger.py new file mode 100644 index 0000000..ef6f5a0 --- /dev/null +++ b/tests/test_calllogger.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# test_calllogger.py +# +# Copyright 2020 Bruce Schubert +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import os +import sys +import sqlite3 +from pprint import pprint + +import pytest + +from src.callattendant import make_config +from src.screening.calllogger import CallLogger + +@pytest.fixture(scope='module') +def calllogger(): + + # Create the test db in RAM + db = sqlite3.connect(":memory:") + + # Load and tweak the default config + config = make_config() + config['DEBUG'] = True + config['TESTING'] = True + + # Create the CallLogger to be tested + calllogger = CallLogger(db, config) + + return calllogger + +def test_add_caller(calllogger): + + # Caller to be added + callerid = { + "NAME": "Bruce", + "NMBR": "1234567890", + "DATE": "1012", + "TIME": "0600", + } + + assert calllogger.log_caller(callerid, "Permitted", "Test1") == 1 + + assert calllogger.log_caller(callerid) == 2