Skip to content

Commit d7a3dc0

Browse files
committed
Basic geocom tests finished
1 parent b5b18c1 commit d7a3dc0

File tree

3 files changed

+72
-14
lines changed

3 files changed

+72
-14
lines changed

src/geocompy/tps1200p/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,13 @@ def __init__(
166166
"""Theodolite measurement and calculation subsystem."""
167167

168168
for i in range(retry):
169-
self._conn.send("\n")
170-
response = self.com.nullproc()
171-
if response.comcode and response.rpccode:
172-
break
169+
try:
170+
self._conn.send("\n")
171+
response = self.com.nullproc()
172+
if response.comcode and response.rpccode:
173+
break
174+
except Exception:
175+
pass
173176

174177
sleep(1)
175178
else:

tests/helpers_geocom.py

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Callable, Any
12
import re
23

34
from geocompy import GeoComProtocol
@@ -6,6 +7,8 @@
67
Byte
78
)
89

10+
from helpers import faulty_parser
11+
912

1013
class DummyGeoComConnection(Connection):
1114
_RESP = re.compile(
@@ -19,31 +22,63 @@ class DummyGeoComConnection(Connection):
1922
_CMD = re.compile(
2023
r"^%R1Q,"
2124
r"(?P<rpc>\d+):"
22-
r"(?:,(?P<params>.*))?$"
25+
r"(?:(?P<params>.*))?$"
2326
)
2427

2528
def send(self, message: str):
2629
return
2730

2831
def exchange1(self, cmd: str) -> str:
29-
print(cmd)
30-
3132
if not self._CMD.match(cmd):
32-
return "%R1P,0,2:"
33+
return "%R1P,0,0:2"
3334

34-
if cmd == "%R1Q,0:":
35-
return "%R1P,0,0:0"
35+
if cmd == "%R1Q,5008:":
36+
return "%R1P,0,0:0,1996,'07','19','10','13','2f'"
3637

37-
return ""
38+
return "%R1P,0,0:0"
3839

3940

4041
class GeoComTester:
4142
@staticmethod
4243
def test_parse_response(instrument: GeoComProtocol):
44+
cmd = "%R1Q,5008:"
45+
answer = "%R1P,0,0:0,1996,'07','19','10','13','2f'"
46+
parsers: dict[str, Callable[[str], Any]] = {
47+
"year": int,
48+
"month": Byte.parse,
49+
"day": Byte.parse,
50+
"hour": Byte.parse,
51+
"minute": Byte.parse,
52+
"second": Byte.parse
53+
}
54+
response = instrument.parse_response(
55+
cmd,
56+
answer,
57+
parsers
58+
)
59+
assert response.params["year"] == 1996
60+
4361
response = instrument.parse_response(
44-
"%R1Q,5008:",
45-
"%R1P,0,0:0,1996,'07','19','10','13','2f'",
46-
{
62+
cmd,
63+
"%R1P,1,0:",
64+
parsers
65+
)
66+
assert len(response.params) == 0
67+
68+
parsers_faulty = parsers.copy()
69+
parsers_faulty["year"] = faulty_parser
70+
response = instrument.parse_response(
71+
cmd,
72+
answer,
73+
parsers_faulty
74+
)
75+
assert len(response.params) == 0
76+
77+
@staticmethod
78+
def test_request(instrument: GeoComProtocol):
79+
response = instrument.request(
80+
5008,
81+
parsers={
4782
"year": int,
4883
"month": Byte.parse,
4984
"day": Byte.parse,
@@ -53,3 +88,10 @@ def test_parse_response(instrument: GeoComProtocol):
5388
}
5489
)
5590
assert response.params["year"] == 1996
91+
92+
response = instrument.request(
93+
1,
94+
(1, 2.0)
95+
)
96+
assert response.cmd == "%R1Q,1:1,2.0"
97+
assert response.response == "%R1P,0,0:0"

tests/test_tps1200p.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
from geocompy.communication import Connection
34
from geocompy.tps1200p import TPS1200P
45

56
from helpers_geocom import (
@@ -14,5 +15,17 @@ def tps() -> TPS1200P:
1415

1516

1617
class TestTPS1200P:
18+
def test_init(self):
19+
conn_bad = Connection()
20+
with pytest.raises(ConnectionError):
21+
TPS1200P(conn_bad, retry=1)
22+
23+
conn_good = DummyGeoComConnection()
24+
instrument = TPS1200P(conn_good)
25+
assert instrument._precision == 15
26+
1727
def test_parse_response(self, tps: TPS1200P):
1828
GeoComTester.test_parse_response(tps)
29+
30+
def test_request(self, tps: TPS1200P):
31+
GeoComTester.test_request(tps)

0 commit comments

Comments
 (0)