Skip to content

Commit f1c772f

Browse files
committed
Fixed majority of Unit (non CI) tests to run green based on current functionality
1 parent 2af3940 commit f1c772f

File tree

7 files changed

+167
-170
lines changed

7 files changed

+167
-170
lines changed

tests/test_app.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ def app(mocker):
5858
config["SCREENED_ACTIONS"] = ("answer", "greeting", "record_message")
5959
config["SCREENED_RINGS_BEFORE_ANSWER"] = 0
6060
config["PERMITTED_ACTIONS"] = ("ignore",)
61-
config["PERMITTED_RINGS_BEFORE_ANSWER"] = 4
61+
config["PERMITTED_RINGS_BEFORE_ANSWER"] = 0
62+
config["SREENING_MODE"] = ("whitelist", "blacklist")
6263

6364
# Mock the hardware interfaces
6465
mocker.patch("hardware.modem.Modem._open_serial_port", return_value=True)
6566
mocker.patch("hardware.modem.Modem.start", return_value=True)
67+
mocker.patch("hardware.modem.Modem.stop", return_value=True)
6668
mocker.patch("hardware.modem.Modem.pick_up", return_value=True)
6769
mocker.patch("hardware.modem.Modem.hang_up", return_value=True)
6870
mocker.patch("hardware.modem.Modem.play_audio", return_value=True)
@@ -85,6 +87,9 @@ def app(mocker):
8587
mocker.patch("hardware.indicators.RingIndicator.blink")
8688
mocker.patch("hardware.indicators.RingIndicator.close")
8789

90+
# Mock signal.signal to avoid errors in test threads
91+
mocker.patch("signal.signal", return_value=None)
92+
8893
def mock_is_whitelisted(caller):
8994
if caller["NAME"] in ["CALLER1", "CALLER3"]:
9095
return (True, "whitelisted")
@@ -175,12 +180,12 @@ def test_ignore_permitted(app):
175180
ignore_call_called = False
176181

177182
app.handle_caller(caller1) # Queue a permitted caller with 4 rings
178-
time.sleep(15)
183+
time.sleep(2)
179184

180185
assert ignore_call_called
181186

182187
# Stop the run thread
183-
app._stop_event.set()
188+
app.set_stop_flag()
184189

185190

186191
def test_answer_blocked(app):
@@ -199,7 +204,7 @@ def test_answer_blocked(app):
199204
assert answer_call_called
200205

201206
# Stop the run thread
202-
app._stop_event.set()
207+
app.set_stop_flag()
203208

204209

205210
def test_answer_screened(app):
@@ -218,7 +223,7 @@ def test_answer_screened(app):
218223
assert answer_call_called
219224

220225
# Stop the run thread
221-
app._stop_event.set()
226+
app.set_stop_flag()
222227

223228

224229
def test_queued_call(app):
@@ -245,7 +250,7 @@ def test_queued_call(app):
245250
assert answer_call_called
246251

247252
# Stop the run thread
248-
app._stop_event.set()
253+
app.set_stop_flag()
249254

250255

251256
def test_answer_call_no_actions(app, mocker):

tests/test_blacklist.py

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@
2424
# SOFTWARE.
2525

2626
import sqlite3
27-
from pprint import pprint
2827

2928
import pytest
3029

3130
from callattendant.screening.blacklist import Blacklist
3231

32+
BRUCE = {"NAME": "Bruce", "NMBR": "1234567890", "DATE": "1012", "TIME": "0600"}
3333

34-
@pytest.fixture(scope='module')
35-
def blacklist():
3634

35+
@pytest.fixture(scope='function')
36+
def blacklist():
3737
# Create the test db in RAM
3838
db = sqlite3.connect(":memory:")
3939

4040
# Mock the application config, which is a dict-based object
4141
config = {}
42-
config['DEBUG'] = True
42+
config['DEBUG'] = False
4343
config['TESTING'] = True
4444

4545
# Create the blacklist to be tested
@@ -48,68 +48,54 @@ def blacklist():
4848
return blacklist
4949

5050

51-
def test_add_caller(blacklist):
52-
# Add a record
53-
callerid = {"NAME": "Bruce", "NMBR": "1234567890", "DATE": "1012", "TIME": "0600", }
54-
assert blacklist.add_caller(callerid, "Test")
51+
def test_can_add_caller_to_blacklist(blacklist):
52+
assert blacklist.add_caller(BRUCE, "Test")
5553

5654

57-
def test_check_number(blacklist):
58-
number = "1234567890"
55+
def test_adding_duplicate_fails(blacklist):
56+
assert blacklist.add_caller(BRUCE, "Test Add")
57+
assert blacklist.add_caller(BRUCE, "Test Update") is False
5958

60-
is_blacklisted, reason = blacklist.check_number(number)
6159

62-
assert is_blacklisted
63-
assert reason == "Test"
60+
def test_check_number_returns_correct_reason(blacklist):
61+
test_reason = "SpecificReason"
62+
blacklist.add_caller(BRUCE, test_reason)
63+
is_blacklisted, (reason, name) = blacklist.check_number(BRUCE.get("NMBR"))
6464

65-
number = "1111111111"
65+
assert is_blacklisted is True
66+
assert reason == test_reason
6667

67-
is_blacklisted, reason = blacklist.check_number(number)
6868

69+
def test_check_number_returns_none_if_not_present(blacklist):
70+
blacklist.add_caller(BRUCE, "Test Reason")
71+
test_number = "1111111111"
72+
is_blacklisted, reason = blacklist.check_number(test_number)
6973
assert not is_blacklisted
7074

7175

72-
def test_get_number(blacklist):
73-
number = "1234567890"
74-
75-
caller = blacklist.get_number(number)
76-
pprint(caller)
77-
78-
assert caller[0][0] == number
79-
80-
81-
def test_multiple(blacklist):
82-
new_caller = {"NAME": "New Caller", "NMBR": "12312351234", "DATE": "1012", "TIME": "0600"}
83-
number = new_caller["NMBR"]
84-
name = new_caller["NAME"]
85-
reason = "Test"
86-
pprint(new_caller)
87-
88-
assert blacklist.add_caller(new_caller, reason)
89-
90-
caller = blacklist.get_number(number)
91-
pprint(caller)
92-
93-
assert caller[0][0] == number
94-
assert caller[0][1] == name
95-
assert caller[0][2] == reason
96-
97-
name = "Joe"
98-
reason = "Confirm"
99-
100-
assert blacklist.update_number(number, name, reason)
101-
102-
caller = blacklist.get_number(number)
103-
pprint(caller)
104-
105-
assert caller[0][0] == number
106-
assert caller[0][1] == name
107-
assert caller[0][2] == reason
108-
109-
assert blacklist.remove_number(number)
110-
111-
is_blacklisted, reason = blacklist.check_number(number)
112-
assert not is_blacklisted
113-
114-
caller = blacklist.get_number(number)
115-
pprint(caller)
76+
def test_get_number_returns_entry(blacklist):
77+
test_reason = "ABC12345"
78+
blacklist.add_caller(BRUCE, test_reason)
79+
caller = blacklist.get_number(BRUCE.get("NMBR"))
80+
assert caller[0][0] == BRUCE.get("NMBR")
81+
assert caller[0][1] == BRUCE.get("NAME")
82+
assert caller[0][2] == test_reason
83+
84+
85+
def test_update_number(blacklist):
86+
test_reason = "ABC12345"
87+
new_reason = "XYZ98765"
88+
new_name = "Joe"
89+
blacklist.add_caller(BRUCE, test_reason)
90+
blacklist.update_number(BRUCE.get("NMBR"), new_name, new_reason)
91+
caller = blacklist.get_number(BRUCE.get("NMBR"))
92+
assert caller[0][0] == BRUCE.get("NMBR")
93+
assert caller[0][1] == new_name
94+
assert caller[0][2] == new_reason
95+
96+
97+
def test_removing_number(blacklist):
98+
blacklist.add_caller(BRUCE, "Test Reason")
99+
assert len(blacklist.get_number(BRUCE.get("NMBR"))) == 1
100+
blacklist.remove_number(BRUCE.get("NMBR"))
101+
assert len(blacklist.get_number(BRUCE.get("NMBR"))) == 0

tests/test_calllogger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from callattendant.screening.calllogger import CallLogger
3030

3131

32-
@pytest.fixture(scope='module')
32+
@pytest.fixture(scope='function')
3333
def calllogger():
3434

3535
# Create the test db in RAM
@@ -52,7 +52,7 @@ def test_add_caller(calllogger):
5252
callerid = {
5353
"NAME": "Bruce",
5454
"NMBR": "1234567890",
55-
"DATE": "1012",
55+
"DATE": "10122023",
5656
"TIME": "0600",
5757
}
5858

tests/test_callscreener.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from callattendant.config import Config
3030
from callattendant.screening.callscreener import CallScreener
3131

32-
3332
# Create a blocked caller
3433
caller1 = {"NAME": "CALLER1", "NMBR": "1234567890", "DATE": "1012", "TIME": "0600"}
3534
# Create a permitted caller
@@ -44,36 +43,35 @@
4443
caller6 = {"NAME": "JOHN DOE", "NMBR": "0987654321", "DATE": "1012", "TIME": "0600"}
4544
# Create a unique number (permitted number pattern match)
4645
caller7 = {"NAME": "CALLER7", "NMBR": "09876543210", "DATE": "1012", "TIME": "0600"}
46+
# ShouldIAnswer Spam
47+
caller8 = {"NAME": "CALLER8", "NMBR": "8554188397", "DATE": "1012", "TIME": "0600"}
4748

4849

49-
@pytest.fixture(scope='module')
50+
@pytest.fixture(scope='function')
5051
def screener():
51-
5252
# Create the test db in RAM
5353
db = sqlite3.connect(":memory:")
5454

5555
# Create a config object with default settings
5656
config = Config()
5757
config['DEBUG'] = True
5858
config['TESTING'] = True
59-
config['BLOCK_NAME_PATTERNS'] = {
60-
"V[0-9]{15}": "Telemarketer Caller ID",
61-
}
62-
config['BLOCK_NUMBER_PATTERNS'] = {
63-
"P": "Private number",
64-
}
65-
config['PERMIT_NAME_PATTERNS'] = {
66-
".*DOE": "Anyone",
67-
}
68-
config['PERMIT_NUMBER_PATTERNS'] = {
69-
"987654": "Anyone",
70-
}
59+
60+
config['BLOCK_SERVICE_THRESHOLD'] = 1
61+
config['BLOCK_SERVICE'] = "NOMOROBO"
7162
# Create the blacklist to be tested
7263
screener = CallScreener(db, config)
64+
65+
screener.config['CALLERID_PATTERNS'] = {
66+
'blocknames': {'V[0-9]{15}': "Telemarketer Caller ID"},
67+
'blocknumbers': {'P': "Private Number"},
68+
'permitnames': {'.*DOE': "Anyone"},
69+
'permitnumbers': {'987654': "Anyone"}
70+
}
7371
# Add a record to the blacklist
74-
screener._blacklist.add_caller(caller1)
72+
screener.blacklist_caller(caller1, "Test Blacklist")
7573
# Add a record to the whitelist
76-
screener._whitelist.add_caller(caller2)
74+
screener.whitelist_caller(caller2, "Test Whitelist")
7775

7876
return screener
7977

@@ -104,10 +102,17 @@ def test_blocked_name_pattern(screener):
104102

105103

106104
def test_is_blacklisted_by_nomorobo(screener):
105+
screener.config['BLOCK_SERVICE'] = "NOMOROBO"
107106
is_blacklisted, reason = screener.is_blacklisted(caller4)
108107
assert is_blacklisted, "caller4 should be blocked by nomorobo"
109108

110109

110+
def test_is_blacklisted_by_shouldianswer(screener):
111+
screener.config['BLOCK_SERVICE'] = "SHOULDIANSWER"
112+
is_blacklisted, reason = screener.is_blacklisted(caller8)
113+
assert is_blacklisted, "caller8 should be blocked by nomorobo"
114+
115+
111116
def test_blocked_number_pattern(screener):
112117
is_blacklisted, reason = screener.is_blacklisted(caller5)
113118
assert is_blacklisted, "caller1 should be blocked by number pattern"

tests/test_nomorobo.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,54 @@
2525

2626
from pprint import pprint
2727

28+
import pytest
29+
2830
from callattendant.screening.nomorobo import NomoroboService
2931

3032

31-
def test_5622862616_should_score_1():
33+
@pytest.fixture(scope='function')
34+
def nomorobo():
3235
nomorobo = NomoroboService()
36+
return nomorobo
37+
38+
39+
def test_5622862616_should_score_1(nomorobo):
3340
result = nomorobo.lookup_number("5622862616")
3441
pprint(result)
3542
assert result["score"] == 1
3643

37-
def test_8886727156_should_be_spam():
38-
nomorobo = NomoroboService()
44+
45+
def test_8886727156_should_be_spam(nomorobo):
3946
result = nomorobo.lookup_number("8886727156")
4047
pprint(result)
4148
assert result["spam"] is True
4249

43-
def test_8886727156_should_score_2():
44-
nomorobo = NomoroboService()
50+
51+
def test_8886727156_should_score_2(nomorobo):
4552
result = nomorobo.lookup_number("8886727156")
4653
pprint(result)
4754
assert result["score"] == 2
4855

49-
def test_404_not_marked_as_spam():
50-
nomorobo = NomoroboService()
56+
57+
def test_404_not_marked_as_spam(nomorobo):
5158
result = nomorobo.lookup_number("1234567890")
5259
pprint(result)
5360
assert result["spam"] is False
5461

55-
def test_9725551356_is_unknown():
56-
nomorobo = NomoroboService()
62+
63+
def test_9725551356_is_unknown(nomorobo):
5764
result = nomorobo.lookup_number("9725551356")
5865
pprint(result)
5966
assert result["score"] == 0
6067

61-
def test_9725551356_is_not_spam():
62-
nomorobo = NomoroboService()
68+
69+
def test_9725551356_is_not_spam(nomorobo):
6370
result = nomorobo.lookup_number("9725551356")
6471
pprint(result)
6572
assert result["spam"] is False
6673

67-
def test_8554188397_should_score_2():
68-
nomorobo = NomoroboService()
74+
75+
def test_8554188397_should_score_2(nomorobo):
6976
result = nomorobo.lookup_number("8554188397")
7077
pprint(result)
71-
assert result["score"] == 2
78+
assert result["score"] == 2

0 commit comments

Comments
 (0)