Skip to content

Commit 5061c76

Browse files
authored
Merge pull request #4 from MrClock8163/geocom-tests
GeoCom tests
2 parents 6914f97 + 8feab35 commit 5061c76

File tree

8 files changed

+177
-11
lines changed

8 files changed

+177
-11
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Test with pytest
2929
run: |
3030
python -m pip install -r requirements.txt
31-
python -m pytest -v --color=yes
31+
python -m pytest
3232
3333
linting:
3434
name: Linting

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ source = "vcs"
3939
version-file = "src/geocompy/_version.py"
4040

4141
[tool.pytest.ini_options]
42-
addopts = ["-ra"]
43-
testpaths = ["tests", "src", "examples"]
42+
addopts = ["-ra", "-v", "--color=yes"]
43+
testpaths = ["tests"]
4444
required_plugins = "pytest-mock"

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:

src/geocompy/vivatps/__init__.py

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

182182
for i in range(retry):
183-
self._conn.send("\n")
184-
response = self.com.nullproc()
185-
if response.comcode and response.rpccode:
186-
break
183+
try:
184+
self._conn.send("\n")
185+
response = self.com.nullproc()
186+
if response.comcode and response.rpccode:
187+
break
188+
except Exception:
189+
pass
187190

188191
sleep(1)
189192
else:

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
pytest.register_assert_rewrite(
44
"helpers",
5+
"helpers_geocom",
56
"helpers_gsionline"
67
)

tests/helpers_geocom.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from typing import Callable, Any
2+
import re
3+
4+
from geocompy import GeoComProtocol
5+
from geocompy.communication import Connection
6+
from geocompy.data import (
7+
Byte
8+
)
9+
10+
from helpers import faulty_parser
11+
12+
13+
class DummyGeoComConnection(Connection):
14+
_RESP = re.compile(
15+
r"^%R1P,"
16+
r"(?P<comrc>\d+),"
17+
r"(?P<tr>\d+):"
18+
r"(?P<rc>\d+)"
19+
r"(?:,(?P<params>.*))?$"
20+
)
21+
22+
_CMD = re.compile(
23+
r"^%R1Q,"
24+
r"(?P<rpc>\d+):"
25+
r"(?:(?P<params>.*))?$"
26+
)
27+
28+
def send(self, message: str):
29+
return
30+
31+
def exchange1(self, cmd: str) -> str:
32+
if not self._CMD.match(cmd):
33+
return "%R1P,0,0:2"
34+
35+
if cmd == "%R1Q,5008:":
36+
return "%R1P,0,0:0,1996,'07','19','10','13','2f'"
37+
38+
return "%R1P,0,0:0"
39+
40+
41+
class GeoComTester:
42+
@staticmethod
43+
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+
61+
response = instrument.parse_response(
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={
82+
"year": int,
83+
"month": Byte.parse,
84+
"day": Byte.parse,
85+
"hour": Byte.parse,
86+
"minute": Byte.parse,
87+
"second": Byte.parse
88+
}
89+
)
90+
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
3+
from geocompy.communication import Connection
4+
from geocompy.tps1200p import TPS1200P
5+
6+
from helpers_geocom import (
7+
DummyGeoComConnection,
8+
GeoComTester
9+
)
10+
11+
12+
@pytest.fixture
13+
def tps() -> TPS1200P:
14+
return TPS1200P(DummyGeoComConnection())
15+
16+
17+
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+
27+
def test_parse_response(self, tps: TPS1200P):
28+
GeoComTester.test_parse_response(tps)
29+
30+
def test_request(self, tps: TPS1200P):
31+
GeoComTester.test_request(tps)

tests/test_vivatps.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
3+
from geocompy.communication import Connection
4+
from geocompy.vivatps import VivaTPS
5+
6+
from helpers_geocom import (
7+
DummyGeoComConnection,
8+
GeoComTester
9+
)
10+
11+
12+
@pytest.fixture
13+
def tps() -> VivaTPS:
14+
return VivaTPS(DummyGeoComConnection())
15+
16+
17+
class TestVivaTPS:
18+
def test_init(self):
19+
conn_bad = Connection()
20+
with pytest.raises(ConnectionError):
21+
VivaTPS(conn_bad, retry=1)
22+
23+
conn_good = DummyGeoComConnection()
24+
instrument = VivaTPS(conn_good)
25+
assert instrument._precision == 15
26+
27+
def test_parse_response(self, tps: VivaTPS):
28+
GeoComTester.test_parse_response(tps)
29+
30+
def test_request(self, tps: VivaTPS):
31+
GeoComTester.test_request(tps)

0 commit comments

Comments
 (0)