Skip to content

Commit 32c46bd

Browse files
committed
modified: lib/pygsm/gsmmodem.py
make sure max_message argument is sane modified: run_tests.py added #! line so can be run directly modified: test/gsmmodem_test.py added tests for max_message argument
1 parent 981dfa9 commit 32c46bd

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

lib/pygsm/gsmmodem.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,29 @@ def send_sms(self, recipient, text, max_messages = 255):
441441
multiple SMSs up to max_messages.
442442
443443
To enforce only a single SMS, set max_messages=1
444+
445+
If max_messages > 255 it is forced to 255
446+
If max_messages < 1 it is forced to 1
444447
445448
Raises 'ValueError' if text will not fit in max_messages
446449
447450
NOTE: Only PDU mode respects max_messages! It has no effect in TEXT mode
448451
449452
"""
453+
mm = 255
454+
try:
455+
mm = int(max_messages)
456+
except:
457+
# dunno what type mm was, so just leave at deafult 255
458+
pass
459+
460+
if mm > 255:
461+
mm = 255
462+
elif mm < 1:
463+
mm = 1
464+
450465
with self.modem_lock:
451-
self.smshandler.send_sms(recipient, text, max_messages)
466+
self.smshandler.send_sms(recipient, text, mm)
452467

453468
def break_out_of_prompt(self):
454469
self._write(chr(27))

run_tests.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env python
2+
# vim: ai ts=4 sts=4 et sw=4
3+
14
import os
25
import sys
36
import re
@@ -30,4 +33,4 @@ def callback( arg, dirname, fnames ):
3033
print f
3134
suite.addTests(loader.loadTestsFromName(f))
3235
unittest.TextTestRunner(verbosity=2).run(suite)
33-
36+

test/gsmmodem_test.py

+45
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,51 @@
99

1010

1111
class TestModem(unittest.TestCase):
12+
13+
def testMaxMessageArgs(self):
14+
# this device is much more complicated than
15+
# most, so is tucked away in mock.device
16+
device = MockSenderDevice()
17+
gsm = pygsm.GsmModem(device=device, mode="PDU")
18+
19+
# test with no max_message arg
20+
gsm.send_sms("1234", "Test Message")
21+
self.assertEqual(device.sent_messages[0]["recipient"], "21")
22+
self.assertEqual(device.sent_messages[0]["text"], "00110004A821430000AA0CD4F29C0E6A96E7F3F0B90C")
23+
24+
# test with reasonable max_message arg, should have no impact
25+
gsm.send_sms("1234", "Test Message", max_messages = 20)
26+
self.assertEqual(device.sent_messages[0]["recipient"], "21")
27+
self.assertEqual(device.sent_messages[0]["text"], "00110004A821430000AA0CD4F29C0E6A96E7F3F0B90C")
28+
29+
# test with max_message = 0, should internally set to 1 with no problems
30+
gsm.send_sms("1234", "Test Message", -1)
31+
self.assertEqual(device.sent_messages[0]["recipient"], "21")
32+
self.assertEqual(device.sent_messages[0]["text"], "00110004A821430000AA0CD4F29C0E6A96E7F3F0B90C")
33+
34+
# test with max_message > 255, should internally force to 255
35+
gsm.send_sms("1234", "Test Message", 1024)
36+
self.assertEqual(device.sent_messages[0]["recipient"], "21")
37+
self.assertEqual(device.sent_messages[0]["text"], "00110004A821430000AA0CD4F29C0E6A96E7F3F0B90C")
38+
39+
# test with max_message = 1 and message too long to fit
40+
# should throw a value exception
41+
msg="""
42+
0123456789012345678901234567890123456789012345678901234567890123456789
43+
0123456789012345678901234567890123456789012345678901234567890123456789
44+
0123456789012345678901234567890123456789012345678901234567890123456789
45+
0123456789012345678901234567890123456789012345678901234567890123456789
46+
0123456789012345678901234567890123456789012345678901234567890123456789
47+
0123456789012345678901234567890123456789012345678901234567890123456789
48+
0123456789012345678901234567890123456789012345678901234567890123456789
49+
"""
50+
try:
51+
gsm.send_sms("1234", msg, max_messages=1)
52+
except ValueError:
53+
print "ValueError caught"
54+
else:
55+
# Should have thrown an error!
56+
self.assertTrue(False)
1257

1358
def testSendSmsPDUMode(self):
1459
"""Checks that the GsmModem in PDU mode accepts outgoing SMS,

0 commit comments

Comments
 (0)