class TestCmgD(unittest.TestCase): def setUp(self): self.port = MockPort(self) self.gsm = GSM(self.port) self.sms = GSM0705(self.gsm) def test_delete(self): self.port.mock_put_read_multi(CMG_DELETE_1) self.assertTrue(self.sms.delete(1)) self.assertEqual('AT+CMGD=1\r\n', self.port.mock_get_write())
class TestCmgS(unittest.TestCase): def setUp(self): self.port = MockPort(self) self.gsm = GSM(self.port) self.sms = GSM0705(self.gsm) def test_send(self): self.port.mock_put_read_multi(CMG_CFG_0) self.port.mock_put_read_multi(CMG_SEND) self.assertTrue(self.sms.send(u'+8613814120678', u'hello')) self.assertEqual('AT+CMGF=0\r\nAT+CMGS=25\r\n0011000D91683118140276F80008A70A00680065006C006C006F\x1a', self.port.mock_get_write())
class TestCmgDAll(unittest.TestCase): def setUp(self): self.port = MockPort(self) self.gsm = GSM(self.port) self.sms = GSM0705(self.gsm) def test_delete(self): self.port.mock_put_read('AT+CMGD=1,4\r\n') self.port.mock_put_read('OK\r\n') self.assertTrue(self.sms.delete_all()) self.assertEqual('AT+CMGD=1,4\r\n', self.port.mock_get_write()) def test_delete_fail(self): self.port.mock_put_read('AT+CMGD=1,4\r\n') self.port.mock_put_read('ERROR\r\n') self.assertFalse(self.sms.delete_all()) self.assertEqual('AT+CMGD=1,4\r\n', self.port.mock_get_write())
class TestCmti(unittest.TestCase): def setUp(self): self.record_sms = [] self.port = MockPort(self) self.gsm = GSM(self.port) self.sms = GSM0705(self.gsm) def record(self, where, who, when, what): self.record_sms.append((where, who, when, what)) def test_cmti_ok(self): self.port.mock_put_read_multi(CMG_CFG_0) self.port.mock_put_read_multi(CMG_READ_1) handle = self.sms.GSM0705_CMTI_HANDLE(self.record) cmds = handle('+CMTI: "SM",1,"MMS PUSH"') self.assertEqual(1, len(cmds)) for cmd in cmds: cmd() self.assertEqual(1, len(self.record_sms)) where, who, when, what = self.record_sms[0] self.assertEqual(1, where) self.assertEqual('106581541003', who)
class TestEngine(unittest.TestCase): def setUp(self): self.record_event = [] self.port = MockPort(self) self.gsm = GSM(self.port) self.daemon = DAEMON(self.gsm, [self.record,]) def record(self, line): def execute(): self.record_event.append(line) return [execute,] def test_event(self): self.port.mock_put_read('+CMTI: "SM",1,"MMS PUSH"\r\n') self.port.mock_put_read('') self.port.mock_put_read('') self.daemon.run(1) output = self.record_event self.assertEqual(['+CMTI: "SM",1,"MMS PUSH"\r\n',], self.record_event) def test_events(self): events = [ '+CMTI: "SM",1,"MMS PUSH"\r\n', '+CMTI: "SM",2,"MMS PUSH"\r\n', '+CMTI: "SM",3,"MMS PUSH"\r\n'] for event in events: self.port.mock_put_read(event) self.port.mock_put_read('') self.port.mock_put_read('') self.daemon.run(10) self.assertEqual(events, self.record_event) def test_exception_log(self): memory_handler = logging.handlers.MemoryHandler(100) log = logging.getLogger('gsm.daemon') log.addHandler(memory_handler) def div_0(): 1 / 0 self.daemon.add_command(div_0, 0) self.daemon.run(0) log.removeHandler(memory_handler) log_string = '\n'.join(map(lambda log: '\n'.join(log.getMessage().split('\n')[:2]), memory_handler.buffer)) self.assertEqual(log_string, 'Execute div_0 error.\nTraceback (most recent call last):')
class TestAtOk(unittest.TestCase): def setUp(self): self.port = MockPort(self) self.gsm = GSM(self.port) def test_ok(self): self.port.mock_put_read('AT\r\n\r\nOK\r\n') self.port.mock_put_read('+CMTI: "SM",1,"MMS PUSH"\r\n') self.assertTrue(self.gsm.test()) self.assertEqual('AT\r\n', self.port.mock_get_write()) def test_no_echo(self): self.assertFalse(self.gsm.test()) def test_error(self): self.port.mock_put_read('AT\r\n\r\nERROR\r\n') self.assertFalse(self.gsm.test())
class TestSendAndCheckEcho(unittest.TestCase): def setUp(self): self.port = MockPort(self) self.gsm = GSM(self.port) def test_match(self): self.port.mock_put_read('AT') self.assertTrue(self.gsm.send_and_check_echo('AT')) self.assertEqual(self.port.mock_get_write(), 'AT') def test_read_more(self): self.port.mock_put_read('AT\r\n\r\nOK\r\n') self.assertTrue(self.gsm.send_and_check_echo('AT\r\n')) def test_unmatch(self): self.port.mock_put_read('AT\r') self.assertFalse(self.gsm.send_and_check_echo('AT\r\n'))
def setUp(self): self.record_sms = [] self.port = MockPort(self) self.gsm = GSM(self.port) self.sms = GSM0705(self.gsm)
class TestCmgR(unittest.TestCase): def setUp(self): self.port = MockPort(self) self.gsm = GSM(self.port) self.sms = GSM0705(self.gsm) def test_read(self): self.port.mock_put_read_multi(CMG_CFG_0) self.port.mock_put_read_multi(CMG_READ_1) sms = self.sms.read(1) who, when, what = sms self.assertEqual(who, '106581541003') self.assertEqual(str(when), '2014-10-22 12:02:48+08:00') self.assertEqual(what, u'\x06\x05\x04\x0b\x84#\xf0@\x06$application/vnd.wap.mms-message\x00\xb4\x87\xaf\x84\x8c\x82\x98VOxaBQFKO4FC\x00\x8d\x90\x83http://221.131.128.129/VOxaBQFKO4FC\x00\x88\x05\x81\x03\x02\xca\r\x89\x07\x8012371\x00\x8a\x80\x8e\x02\xcd~') self.assertEqual('AT+CMGF=0\r\nAT+CMGR=1\r\n', self.port.mock_get_write()) def test_read_simple(self): read = ( 'AT+CMGR=4\r\n', '\r\n', '+CMGR: 4,"",28\r\n', '0891683108200505F0040D91683118140276F800004111502164332309F0BA7B8C66C55832\r\n', '\r\n', 'OK\r\n') self.port.mock_put_read_multi(CMG_CFG_0) self.port.mock_put_read_multi(read) sms = self.sms.read(4) who, when, what = sms self.assertEqual(who, '+8613814120678') self.assertEqual(str(when), '2014-11-05 12:46:33+08:00') self.assertEqual(what, u'punch,1,2') self.assertEqual('AT+CMGF=0\r\nAT+CMGR=4\r\n', self.port.mock_get_write()) def test_noexist(self): self.port.mock_put_read_multi(CMG_CFG_0) self.port.mock_put_read('AT+CMGR=1\r\n') self.port.mock_put_read('\r\n') self.port.mock_put_read('OK\r\n') self.assertEqual(None, self.sms.read(1)) self.assertEqual('AT+CMGF=0\r\nAT+CMGR=1\r\n', self.port.mock_get_write())
def setUp(self): self.record_event = [] self.port = MockPort(self) self.gsm = GSM(self.port) self.daemon = DAEMON(self.gsm, [self.record,])
def setUp(self): self.port = MockPort(self) self.gsm = GSM(self.port)
class TestSendCmd(unittest.TestCase): def setUp(self): self.port = MockPort(self) self.gsm = GSM(self.port) def test_match(self): self.port.mock_put_read('AT+CMGS=15\r\n> ') self.port.mock_put_read('') self.assertEqual('> ', self.gsm.send_cmd('AT+CMGS=15\r\n')) self.assertEqual('AT+CMGS=15\r\n', self.port.mock_get_write()) def test_multiline(self): self.port.mock_put_read('AT\r\n\r\nOK\r\n') self.port.mock_put_read('+CMTI: "SM",1,"MMS PUSH"\r\n') self.port.mock_put_read('') self.assertEqual('\r\nOK\r\n+CMTI: "SM",1,"MMS PUSH"\r\n', self.gsm.send_cmd('AT\r\n')) def test_with_end(self): self.port.mock_put_read('AT\r\n\r\nOK\r\n') self.port.mock_put_read('+CMTI: "SM",1,"MMS PUSH"\r\n') self.port.mock_put_read('') self.assertEqual('\r\nOK\r\n', self.gsm.send_cmd('AT\r\n', ['OK', 'ERROR']))