-
Notifications
You must be signed in to change notification settings - Fork 45
/
tests.py
138 lines (113 loc) · 4.17 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import config
import sys
import mongoengine
import unittest
from models import Message
from models import GPSDevice
from models import User
tk102_heartbeat_string = u'865328021048409;'
tk102_location_low_string = 'imei:865328021048409,tracker,141210172556,0411959136,L,,,0BD4,,7A78,,,,,0,0,0.0%,,;'
tk102_location_full_string = 'imei:865328021048409,tracker,141210110820,,F,030823.000,A,3745.9502,S,14458.2049,E,1.83,119.35,,0,0,0.0%,,;'
tk102_init_string = '##,imei:865328021048409,A;'
tk102_datastrings = [
tk102_heartbeat_string,
tk102_location_low_string,
tk102_location_full_string,
tk102_init_string,
]
class TestModels(unittest.TestCase):
def setUp(self):
User.objects.delete({'email':'[email protected]'})
self.user = User(email='[email protected]', password='test')
self.user.save()
GPSDevice.objects.delete({'imei':'test'})
self.device = GPSDevice(imei='test')
self.device.save()
self.user.devices.append(self.device)
self.user.save()
def tearDown(self):
self.user.delete()
self.device.delete()
# User
def test_user(self):
assert(self.user.check_password('test'))
# GPSDevice
def test_device_user(self):
assert(self.user==self.device.user)
def test_unique_imei(self):
try:
d = GPSDevice(imei='test')
d.save()
except mongoengine.NotUniqueError, e:
pass
assert(mongoengine.NotUniqueError == type(e))
def test_device_get_by_data_bad(self):
e = None
try:
device = GPSDevice.get_by_data('x')
except Exception, e:
pass
assert(Exception==type(e))
def test_device_get_by_data_good(self):
for s in tk102_datastrings:
device = GPSDevice.get_by_data(s)
assert(GPSDevice==type(device))
assert(unicode==type(device.imei))
# Messages
def test_location_req(self):
m = Message()
m.imei = '865328021048409'
m.message_type = config.MESSAGE_TYPE_REQ_LOCATION
m.save()
def test_message_fifo(self):
imei = 'test1'
# Maybe we need to clean up some from previous run
m = Message.dequeue_response(imei=imei)
m = Message.dequeue_response(imei=imei)
m = Message.dequeue_response(imei=imei)
Message(imei=imei, message_datastring='111').save()
Message(imei=imei, message_datastring='222').save()
Message(imei=imei, message_datastring='333').save()
m = Message.dequeue_response(imei=imei)
assert('111' == m.message_datastring)
m = Message.dequeue_response(imei=imei)
assert('222' == m.message_datastring)
m = Message.dequeue_response(imei=imei)
assert('333' == m.message_datastring)
m = Message.dequeue_response(imei=imei)
assert(None == m)
m = Message.dequeue_response(imei=imei)
assert(None == m)
m = Message.dequeue_response(imei=imei)
assert(None == m)
def test_gpsdevice_sent(self):
d = GPSDevice.get_by_data(tk102_init_string)
d.sent(tk102_init_string)
d.sent(tk102_heartbeat_string)
d.sent(tk102_location_full_string)
assert(d.imei)
assert(d.latitude)
assert(d.longitude)
def test_gpsdevice_tk102_response_init(self):
self.device.sent(tk102_init_string)
assert(self.device.imei)
r = self.device.pop_response()
assert('LOAD'==r)
def test_gpsdevice_tk102_response_multi(self):
self.device.sent(tk102_init_string + tk102_heartbeat_string)
assert(self.device.imei)
r = self.device.pop_response()
assert('LOAD'==r)
r = self.device.pop_response()
assert('ON'==r)
def test_gpsdevice_tk102_response_hb(self):
self.device.sent(tk102_heartbeat_string)
assert(self.device.imei)
r = self.device.pop_response()
assert('ON'==r)
def test_gpsdevice_tk102_response_location(self):
d = GPSDevice.get_by_data(tk102_location_full_string)
d.sent(tk102_location_full_string)
def test_device_online(self):
d = GPSDevice()
assert(False==d.is_online)