]> www.average.org Git - loctrkd.git/blob - test/test_storage.py
test: include lookaside and termconfig in the loop
[loctrkd.git] / test / test_storage.py
1 """ Send junk to the collector """
2
3 from random import Random
4 from socket import getaddrinfo, socket, AF_INET, SOCK_STREAM
5 from sqlite3 import connect
6 from time import sleep
7 from typing import Any
8 import unittest
9 from .common import send_and_drain, TestWithServers
10 from gps303.gps303proto import *
11 from gps303.ocid_dload import SCHEMA
12
13
14 class Storage(TestWithServers):
15     def setUp(self, *args: str, **kwargs: Any) -> None:
16         super().setUp("collector", "storage", "lookaside", "termconfig")
17         with connect(self.conf.get("opencellid", "dbfn")) as ldb:
18             ldb.execute(SCHEMA)
19             ldb.executemany(
20                 """insert into cells
21                     (radio, mcc, net, area, cell, unit, lon, lat, range,
22                      samples, changeable, created, updated, averageSignal)
23                     values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
24                 (
25                     (
26                         "GSM",
27                         262,
28                         3,
29                         24420,
30                         16594,
31                         -1,
32                         12.681939,
33                         53.52603,
34                         22733,
35                         1999,
36                         1,
37                         1556575612,
38                         1653387028,
39                         0,
40                     ),
41                     (
42                         "GSM",
43                         262,
44                         3,
45                         24420,
46                         36243,
47                         -1,
48                         12.66442,
49                         53.527534,
50                         21679,
51                         1980,
52                         1,
53                         1540870608,
54                         1653387028,
55                         0,
56                     ),
57                     (
58                         "GSM",
59                         262,
60                         3,
61                         24420,
62                         17012,
63                         -1,
64                         12.741093,
65                         53.529854,
66                         23463,
67                         874,
68                         1,
69                         1563404603,
70                         1653268184,
71                         0,
72                     ),
73                     (
74                         "GSM",
75                         262,
76                         3,
77                         24420,
78                         26741,
79                         -1,
80                         12.658822,
81                         53.530832,
82                         18809,
83                         1687,
84                         1,
85                         1539939964,
86                         1653265176,
87                         0,
88                     ),
89                     (
90                         "GSM",
91                         262,
92                         2,
93                         24420,
94                         36243,
95                         -1,
96                         12.61111,
97                         53.536626,
98                         1000,
99                         4,
100                         1,
101                         1623218739,
102                         1652696033,
103                         0,
104                     ),
105                     (
106                         "GSM",
107                         262,
108                         1,
109                         24420,
110                         36243,
111                         -1,
112                         12.611135,
113                         53.536636,
114                         1000,
115                         3,
116                         1,
117                         1568587946,
118                         1628827437,
119                         0,
120                     ),
121                     (
122                         "GSM",
123                         262,
124                         2,
125                         24420,
126                         17012,
127                         -1,
128                         12.829655,
129                         53.536654,
130                         1000,
131                         2,
132                         1,
133                         1609913384,
134                         1612934718,
135                         0,
136                     ),
137                     (
138                         "GSM",
139                         262,
140                         3,
141                         24000,
142                         35471,
143                         -1,
144                         11.505135,
145                         53.554216,
146                         11174,
147                         829,
148                         1,
149                         1544494558,
150                         1651063300,
151                         0,
152                     ),
153                     (
154                         "GSM",
155                         262,
156                         3,
157                         24420,
158                         37156,
159                         -1,
160                         11.918188,
161                         53.870522,
162                         1000,
163                         1,
164                         1,
165                         1550199983,
166                         1550199983,
167                         0,
168                     ),
169                 ),
170             )
171             ldb.commit()
172         for fam, typ, pro, cnm, skadr in getaddrinfo(
173             "127.0.0.1",
174             self.conf.getint("collector", "port"),
175             family=AF_INET,
176             type=SOCK_STREAM,
177         ):
178             break  # Just take the first element
179         self.sock = socket(AF_INET, SOCK_STREAM)
180         self.sock.connect(skadr)
181
182     def tearDown(self) -> None:
183         sleep(1)  # give collector some time
184         super().tearDown()
185
186     def test_storage(self) -> None:
187         for buf in (
188             LOGIN.In(imei="9999123456780000", ver=9).packed,
189             WIFI_POSITIONING.In(
190                 mnc=3,
191                 mcc=262,
192                 wifi_aps=[
193                     ("02:03:04:05:06:07", -89),
194                     ("92:93:94:95:96:97", -70),
195                 ],
196                 gsm_cells=[
197                     (24420, 27178, -90),
198                     (24420, 36243, -78),
199                     (24420, 17012, -44),
200                 ],
201             ).packed,
202             SETUP.In().packed,
203             STATUS.In(signal=87).packed,
204             HIBERNATION.In().packed,
205         ):
206             send_and_drain(self.sock, b"xx" + buf + b"\r\n")
207         self.sock.close()
208         sleep(1)
209         got = set()
210         with connect(self.conf.get("storage", "dbfn")) as db:
211             for is_incoming, packet in db.execute(
212                 "select is_incoming, packet from events"
213             ):
214                 msg = parse_message(packet, is_incoming=is_incoming)
215                 print(msg)
216                 got.add(type(msg))
217         self.assertEqual(
218             got,
219             {
220                 LOGIN.Out,
221                 HIBERNATION.In,
222                 LOGIN.In,
223                 SETUP.In,
224                 SETUP.Out,
225                 STATUS.Out,
226                 STATUS.In,
227                 WIFI_POSITIONING.In,
228                 WIFI_POSITIONING.Out,
229             },
230         )
231
232
233 if __name__ == "__main__":
234     unittest.main()