-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_build_database.py
More file actions
276 lines (230 loc) * 9.22 KB
/
test_build_database.py
File metadata and controls
276 lines (230 loc) * 9.22 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import hashlib
import importlib.util
import json
import sqlite3
from pathlib import Path
import pytest
MODULE_PATH = Path(__file__).resolve().parents[1] / "scripts" / "build_database.py"
spec = importlib.util.spec_from_file_location("build_database", MODULE_PATH)
if spec is None or spec.loader is None:
raise RuntimeError("Unable to load build_database module")
build_db = importlib.util.module_from_spec(spec)
spec.loader.exec_module(build_db)
def setup_sample_data(tmp_path):
data_dir = tmp_path / "data"
manual_dir = data_dir / "manual"
manual_dir.mkdir(parents=True)
rasp_content = (
"; First line\n"
"; Second line\n"
"F32 29 124 0 0.05 0.07 Test Motors\n"
"0 5\n"
"0.5 0\n"
)
(manual_dir / "test.eng").write_text(rasp_content)
rse_content = (
""
""
"\" G64W\" mfg=\"RSE Co\" dia=\"29\" len=\"150\" "
"propWt=\"30\" initWt=\"60\" delays=\"0\">"
""
"\" 0.0\" f=\"0.0\" />"
"\" 0.2\" f=\"20.0\" />"
"\" 0.4\" f=\"0.0\" />"
""
""
""
""
)
(manual_dir / "test.rse").write_text(rse_content)
tc_dir = data_dir / "thrustcurve.org"
tc_dir.mkdir(parents=True)
(tc_dir / "motors_metadata.json").write_text('{"motors": {}}')
(tc_dir / "simfile_to_motor.json").write_text("{}")
(tc_dir / "manufacturers.json").write_text(
json.dumps(
{
"manufacturers": [
{"name": "Test Motors", "abbrev": "TM"},
{"name": "RSE Co", "abbrev": "RSE"},
]
}
)
)
return data_dir, tc_dir
def test_parse_rasp_parses_header_and_data(tmp_path):
content = (
"; comment\n"
"F32 29 124 5-10-15 0.053 0.068 Test Motors\n"
"0.0 5\n"
"0.5 0\n"
)
path = tmp_path / "motor.eng"
path.write_text(content)
meta, points = build_db.parse_rasp(str(path))
assert meta["common_name"] == "F32"
assert meta["diameter"] == 29.0
assert meta["delays"] == "5-10-15"
assert meta["prop_weight"] == pytest.approx(53.0)
assert meta["total_weight"] == pytest.approx(68.0)
assert meta["manufacturer"] == "Test Motors"
assert points == [(0.0, 5.0), (0.5, 0.0)]
def test_parse_rasp_all_handles_multiple_motors_and_comments(tmp_path):
content = (
"; First line\n"
"; Second line\n"
"F32 29 124 0 0.05 0.07 Test Motors\n"
"0 5\n"
"0.5 0\n"
"; Next motor\n"
"G64 29 150 P 0.08 0.1 Test Motors\n"
"0 10\n"
"0.6 0\n"
)
path = tmp_path / "multi.eng"
path.write_text(content)
motors = build_db.parse_rasp_all(str(path))
assert len(motors) == 2
meta0 = motors[0][0]
meta1 = motors[1][0]
assert meta0["description"] == "First line Second line"
assert meta0["delays"] is None
assert meta1["description"] == "Next motor"
assert meta1["delays"] is None
def test_parse_rse_parses_single_engine(tmp_path):
content = (
""
""
"\" H128W\" mfg=\"RSE Co\" dia=\"38\" len=\"200\" "
"propWt=\"50\" initWt=\"90\" delays=\"6,10\">"
""
"\" 0.0\" f=\"0.0\" />"
"\" 0.3\" f=\"15.0\" />"
"\" 0.6\" f=\"0.0\" />"
""
""
""
""
)
path = tmp_path / "motor.rse"
path.write_text(content)
meta, points = build_db.parse_rse(str(path))
assert meta["designation"] == "H128W"
assert meta["common_name"] == "H128"
assert meta["manufacturer"] == "RSE Co"
assert meta["diameter"] == 38.0
assert meta["length"] == 200.0
assert meta["prop_weight"] == 50.0
assert meta["total_weight"] == 90.0
assert meta["delays"] == "6,10"
assert len(points) == 3
def test_parse_rse_all_parses_multiple_engines(tmp_path):
content = (
""
""
"\" G64W\" mfg=\"RSE Co\" dia=\"29\" len=\"150\" "
"propWt=\"30\" initWt=\"60\" delays=\"0\">"
""
"\" 0.0\" f=\"0.0\" />"
"\" 0.2\" f=\"20.0\" />"
"\" 0.4\" f=\"0.0\" />"
""
""
"\" H128W\" mfg=\"RSE Co\" dia=\"38\" len=\"200\" "
"propWt=\"50\" initWt=\"90\" delays=\"6,10\">"
""
"\" 0.0\" f=\"0.0\" />"
"\" 0.3\" f=\"15.0\" />"
"\" 0.6\" f=\"0.0\" />"
""
""
""
""
)
path = tmp_path / "multi.rse"
path.write_text(content)
motors = build_db.parse_rse_all(str(path))
assert len(motors) == 2
assert motors[0][0]["designation"] == "G64W"
assert motors[1][0]["designation"] == "H128W"
def test_calculate_thrust_stats():
points = [(0.0, 0.0), (1.0, 10.0), (2.0, 0.0)]
impulse, avg_thrust, max_thrust, burn_time = build_db.calculate_thrust_stats(points)
assert impulse == pytest.approx(10.0)
assert avg_thrust == pytest.approx(5.0)
assert max_thrust == pytest.approx(10.0)
assert burn_time == pytest.approx(2.0)
def test_extract_simfile_info_from_filename():
mapping = {"abcdef123456abcdef123456": "motor-1"}
sim_id, info = build_db.extract_simfile_info_from_filename(
"foo_abcdef123456abcdef123456.eng", mapping
)
assert sim_id == "abcdef123456abcdef123456"
assert info == {"motorId": "motor-1"}
def test_build_creates_database_and_metadata(tmp_path, monkeypatch):
data_dir, tc_dir = setup_sample_data(tmp_path)
db_path = tmp_path / "motors.db"
gz_path = tmp_path / "motors.db.gz"
meta_path = tmp_path / "metadata.json"
build_state_path = tmp_path / "state" / "last_build.json"
schema_path = Path(__file__).resolve().parents[1] / "schema" / "V1__initial_schema.sql"
monkeypatch.setattr(build_db, "DATA_DIR", str(data_dir))
monkeypatch.setattr(build_db, "DB_NAME", str(db_path))
monkeypatch.setattr(build_db, "GZ_NAME", str(gz_path))
monkeypatch.setattr(build_db, "METADATA_FILE", str(meta_path))
monkeypatch.setattr(build_db, "BUILD_STATE_FILE", str(build_state_path))
monkeypatch.setattr(build_db, "SCHEMA_FILE", str(schema_path))
monkeypatch.setattr(build_db, "MOTORS_METADATA_FILE", str(tc_dir / "motors_metadata.json"))
monkeypatch.setattr(build_db, "MANUFACTURERS_FILE", str(tc_dir / "manufacturers.json"))
monkeypatch.setattr(build_db, "SIMFILE_MAPPING_FILE", str(tc_dir / "simfile_to_motor.json"))
build_db.build()
assert db_path.exists()
assert gz_path.exists()
assert meta_path.exists()
metadata = json.loads(meta_path.read_text())
assert metadata["motor_count"] == 2
assert metadata["curve_count"] == 2
assert "last_checked" in metadata
sha = hashlib.sha256(gz_path.read_bytes()).hexdigest()
assert metadata["sha256"] == sha
with sqlite3.connect(db_path) as conn:
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM motors")
assert cursor.fetchone()[0] == 2
cursor.execute("SELECT COUNT(*) FROM thrust_curves")
assert cursor.fetchone()[0] == 2
cursor.execute("SELECT description, source FROM motors WHERE designation = 'F32'")
description, source = cursor.fetchone()
assert description == "First line Second line"
assert source == "manual"
def test_build_reuses_version_when_state_matches(tmp_path, monkeypatch):
data_dir, tc_dir = setup_sample_data(tmp_path)
db_path = tmp_path / "motors.db"
gz_path = tmp_path / "motors.db.gz"
meta_path = tmp_path / "metadata.json"
build_state_path = tmp_path / "state" / "last_build.json"
schema_path = Path(__file__).resolve().parents[1] / "schema" / "V1__initial_schema.sql"
monkeypatch.setattr(build_db, "DATA_DIR", str(data_dir))
monkeypatch.setattr(build_db, "DB_NAME", str(db_path))
monkeypatch.setattr(build_db, "GZ_NAME", str(gz_path))
monkeypatch.setattr(build_db, "METADATA_FILE", str(meta_path))
monkeypatch.setattr(build_db, "BUILD_STATE_FILE", str(build_state_path))
monkeypatch.setattr(build_db, "SCHEMA_FILE", str(schema_path))
monkeypatch.setattr(build_db, "MOTORS_METADATA_FILE", str(tc_dir / "motors_metadata.json"))
monkeypatch.setattr(build_db, "MANUFACTURERS_FILE", str(tc_dir / "manufacturers.json"))
monkeypatch.setattr(build_db, "SIMFILE_MAPPING_FILE", str(tc_dir / "simfile_to_motor.json"))
source_hash = build_db.compute_source_hash()
build_state_path.parent.mkdir(parents=True, exist_ok=True)
build_state = {
"source_hash": source_hash,
"database_version": 20240102030405,
"generated_at": "2024-01-02T03:04:05",
"motor_count": 2,
"curve_count": 2,
"sha256": "deadbeef",
}
build_state_path.write_text(json.dumps(build_state))
build_db.build()
metadata = json.loads(meta_path.read_text())
assert metadata["database_version"] == build_state["database_version"]
assert metadata["generated_at"] == build_state["generated_at"]