Skip to content

Commit

Permalink
Moved callscreener unit tests to test_callsreener.py
Browse files Browse the repository at this point in the history
- Re: #67
  • Loading branch information
emxsys committed Aug 19, 2020
1 parent 8892dde commit 5a53c4a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 92 deletions.
92 changes: 0 additions & 92 deletions src/screening/callscreener.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,95 +95,3 @@ def __init__(self, db, config):

if self.config["DEBUG"]:
print("CallScreener initialized")


def test(db, config):
""" Unit Tests """

print("*** Running CallScreener Unit Tests ***")

# Create the screener to be tested
screener = CallScreener(db, config)

# Add a record to the blacklist
caller1 = {"NAME": "caller1", "NMBR": "1234567890", "DATE": "1012", "TIME": "0600"}
screener._blacklist.add_caller(caller1)
# Add a record to the whitelist
caller2 = {"NAME": "caller2", "NMBR": "1111111111", "DATE": "1012", "TIME": "0600",}
screener._whitelist.add_caller(caller2)
# Create a V123456789012345 Telemarketer caller
caller3 = {"NAME": "V123456789012345", "NMBR": "80512345678", "DATE": "1012", "TIME": "0600"}
# Create a robocaller
caller4 = {"NAME": "caller4", "NMBR": "3105241189", "DATE": "1012", "TIME": "0600"}
# Create a Private Number
caller5 = {"NAME": "caller5", "NMBR": "P", "DATE": "1012", "TIME": "0600"}

# Perform tests
try:
print("Assert is blacklisted: " + caller1['NMBR'])
is_blacklisted, reason = screener.is_blacklisted(caller1)
assert is_blacklisted, "caller1 should be blocked"

print("Assert not is whitelisted: " + caller1['NMBR'])
is_whitelisted, reason = screener.is_whitelisted(caller1)
assert not is_whitelisted, "caller1 should not be permitted"

print("Assert not is blacklisted: " + caller2['NMBR'])
is_blacklisted, reason = screener.is_blacklisted(caller2)
assert not is_blacklisted, "caller2 should not be blocked"

print("Assert is whitelisted: " + caller2['NMBR'])
is_whitelisted, reason = screener.is_whitelisted(caller2)
assert is_whitelisted, "caller2 should be permitted"

print("Assert a blocked name pattern: " + caller3['NAME'])
is_blacklisted, reason = screener.is_blacklisted(caller3)
assert is_blacklisted, "caller3 should be blocked by name pattern"

print("Assert is blacklisted by nomorobo: " + caller4['NMBR'])
is_blacklisted, reason = screener.is_blacklisted(caller4)
assert is_blacklisted, "caller4 should be blocked by nomorobo"

print("Assert a blocked number pattern: " + caller5['NMBR'])
is_blacklisted, reason = screener.is_blacklisted(caller5)
assert is_blacklisted, "caller1 should be blocked by number pattern"


except AssertionError as e:

print("*** Unit test FAILED ***")
pprint(e)
return 1

print("*** Unit tests PASSED ***")
return 0


if __name__ == '__main__':
""" Run Unit Tests """

# 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)

# Load and tweak the default config
from callattendant import make_config, print_config
config = make_config()
config['DEBUG'] = True
config['BLOCK_NAME_PATTERNS'] = {
"V[0-9]{15}": "Telemarketer Caller ID",
}
config['BLOCK_NUMBER_PATTERNS'] = {
"P": "Private number",
}
print_config(config)

# Create the test db in RAM
import sqlite3
db = sqlite3.connect(":memory:")

sys.exit(test(db, config))
print("Done")
101 changes: 101 additions & 0 deletions tests/test_callscreener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# test_callscreener.py
#
# Copyright 2020 Bruce Schubert <[email protected]>
#
# 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.callscreener import CallScreener


# Create a blocked caller
caller1 = {"NAME": "caller1", "NMBR": "1234567890", "DATE": "1012", "TIME": "0600"}
# Create a permitted caller
caller2 = {"NAME": "caller2", "NMBR": "1111111111", "DATE": "1012", "TIME": "0600",}
# Create a V123456789012345 Telemarketer caller
caller3 = {"NAME": "V123456789012345", "NMBR": "80512345678", "DATE": "1012", "TIME": "0600"}
# Create a robocaller
caller4 = {"NAME": "caller4", "NMBR": "3105241189", "DATE": "1012", "TIME": "0600"}
# Create a Private Number
caller5 = {"NAME": "caller5", "NMBR": "P", "DATE": "1012", "TIME": "0600"}


@pytest.fixture(scope='module')
def screener():

# Create the test db in RAM
db = sqlite3.connect(":memory:")

# Load and tweak the default config
config = make_config()
config['DEBUG'] = True
config['BLOCK_NAME_PATTERNS'] = {
"V[0-9]{15}": "Telemarketer Caller ID",
}
config['BLOCK_NUMBER_PATTERNS'] = {
"P": "Private number",
}

# Create the blacklist to be tested
screener = CallScreener(db, config)
# Add a record to the blacklist
screener._blacklist.add_caller(caller1)
# Add a record to the whitelist
screener._whitelist.add_caller(caller2)

return screener


def test_is_blacklisted(screener):
is_blacklisted, reason = screener.is_blacklisted(caller1)
assert is_blacklisted

def test_not_is_whitelisted(screener):
is_whitelisted, reason = screener.is_whitelisted(caller1)
assert not is_whitelisted, "caller1 should not be permitted"

def test_not_is_blacklisted(screener):
is_blacklisted, reason = screener.is_blacklisted(caller2)
assert not is_blacklisted, "caller2 should not be blocked"

def test_is_whitelisted(screener):
is_whitelisted, reason = screener.is_whitelisted(caller2)
assert is_whitelisted, "caller2 should be permitted"

def test_blocked_name_pattern(screener):
is_blacklisted, reason = screener.is_blacklisted(caller3)
assert is_blacklisted, "caller3 should be blocked by name pattern"

def test_is_blacklisted_by_nomorobo(screener):
is_blacklisted, reason = screener.is_blacklisted(caller4)
assert is_blacklisted, "caller4 should be blocked by nomorobo"

def test_blocked_number_pattern(screener):
is_blacklisted, reason = screener.is_blacklisted(caller5)
assert is_blacklisted, "caller1 should be blocked by number pattern"

0 comments on commit 5a53c4a

Please sign in to comment.