diff --git a/src/screening/whitelist.py b/src/screening/whitelist.py index d2e10e0..35b7ad3 100644 --- a/src/screening/whitelist.py +++ b/src/screening/whitelist.py @@ -108,10 +108,17 @@ def remove_number(self, phone_no): arguments = {'phone_no': phone_no} self.db.execute(query, arguments) self.db.commit() - + try: + self.db.execute(query, arguments) + self.db.commit() + except Exception as e: + print("** Failed to delete caller from whitelist:") + pprint(e) + return False if self.config["DEBUG"]: - print("** whitelist entry removed") + print("Whitelist entry removed") pprint(arguments) + return True def update_number(self, phone_no, name, reason): """ @@ -129,12 +136,17 @@ def update_number(self, phone_no, name, reason): "reason": reason, "time": (datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:19]) } - self.db.execute(sql, arguments) - self.db.commit() - + try: + self.db.execute(sql, arguments) + self.db.commit() + except Exception as e: + print("** Failed to update caller in whitelist:") + pprint(e) + return False if self.config["DEBUG"]: - print("whitelist entry updated") + print("Whitelist entry updated") pprint(arguments) + return True def check_number(self, number): query = "SELECT Reason FROM Whitelist WHERE PhoneNo=:number" @@ -150,91 +162,3 @@ def get_number(self, number): args = (number,) results = query_db(self.db, query, args, False) return results - - -def test(db, config): - """ Unit Tests """ - - print("*** Running Whitelist Unit Tests ***") - - # Create the whitelist to be tested - whitelist = Whitelist(db, config) - - # Add a record - call_record = {"NAME": "Bruce", "NMBR": "1234567890", "DATE": "1012", "TIME": "0600"} - whitelist.add_caller(call_record, "Test") - - # List the records - query = 'SELECT * from Whitelist' - results = query_db(db, query) - print(query + " results:") - pprint(results) - - try: - number = "1234567890" - print("Assert is whitelisted: " + number) - is_whitelisted, reason = whitelist.check_number(number) - assert is_whitelisted, number + " should be whitelisted" - print(reason) - - number = "1111111111" - print("Assert not whitelisted: " + number) - is_whitelisted, reason = whitelist.check_number(number) - assert not is_whitelisted, number + " should not be whitelisted" - - new_caller = {"NAME": "New Caller", "NMBR": "12312351234", "DATE": "1012", "TIME": "0600"} - number = new_caller["NMBR"] - name = new_caller["NAME"] - reason = "Test" - print("Assert add caller:") - pprint(new_caller) - whitelist.add_caller(new_caller, reason) - caller = whitelist.get_number(number) - pprint(caller) - assert caller[0][0] == number, number + " != "+ caller[0][0] - assert caller[0][1] == name, name + " != "+ caller[0][1] - assert caller[0][2] == reason, reason + " != "+ caller[0][2] - - name = "Joe" - reason = "Confirm" - print("Assert update number: " + number) - whitelist.update_number(number, name, reason) - caller = whitelist.get_number(number) - pprint(caller) - assert caller[0][0] == number, number + " != "+ caller[0][0] - assert caller[0][1] == name, name + " != "+ caller[0][1] - assert caller[0][2] == reason, reason + " != "+ caller[0][2] - - 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_whitelist.py b/tests/test_whitelist.py new file mode 100644 index 0000000..8d499c1 --- /dev/null +++ b/tests/test_whitelist.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# test_whitelist.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.whitelist import Whitelist +from src.screening.query_db import query_db +from src.screening.query_db import query_db + +@pytest.fixture(scope='module') +def whitelist(): + + # 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 whitelist to be tested + whitelist = Whitelist(db, config) + + return whitelist + +def test_add_caller(whitelist): + # Add a record + callerid = {"NAME": "Bruce", "NMBR": "1234567890", "DATE": "1012", "TIME": "0600",} + assert whitelist.add_caller(callerid, "Test") + + +def test_check_number(whitelist): + number = "1234567890" + + is_whitelisted, reason = whitelist.check_number(number) + + assert is_whitelisted + assert reason == "Test" + + number = "1111111111" + + is_whitelisted, reason = whitelist.check_number(number) + + assert not is_whitelisted + +def test_get_number(whitelist): + number = "1234567890" + + caller = whitelist.get_number(number) + pprint(caller) + + assert caller[0][0] == number + +def test_multiple(whitelist): + new_caller = {"NAME": "New Caller", "NMBR": "12312351234", "DATE": "1012", "TIME": "0600"} + number = new_caller["NMBR"] + name = new_caller["NAME"] + reason = "Test" + pprint(new_caller) + + assert whitelist.add_caller(new_caller, reason) + + caller = whitelist.get_number(number) + pprint(caller) + + assert caller[0][0] == number + assert caller[0][1] == name + assert caller[0][2] == reason + + name = "Joe" + reason = "Confirm" + + assert whitelist.update_number(number, name, reason) + + caller = whitelist.get_number(number) + pprint(caller) + + assert caller[0][0] == number + assert caller[0][1] == name + assert caller[0][2] == reason + + assert whitelist.remove_number(number) + + is_whitelisted, reason = whitelist.check_number(number) + assert not is_whitelisted + + caller = whitelist.get_number(number) + pprint(caller)