test_spoolman_inventory_helpers.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. """Unit tests for _safe_int, _safe_float, and _map_spoolman_spool helpers."""
  2. import math
  3. import pytest
  4. from backend.app.api.routes._spoolman_helpers import (
  5. _map_spoolman_spool,
  6. _safe_float,
  7. _safe_int,
  8. )
  9. # ---------------------------------------------------------------------------
  10. # _safe_int
  11. # ---------------------------------------------------------------------------
  12. class TestSafeInt:
  13. def test_normal_int(self):
  14. assert _safe_int(1000, 0) == 1000
  15. def test_float_rounds_down(self):
  16. assert _safe_int(750.9, 0) == 750
  17. def test_none_returns_fallback(self):
  18. assert _safe_int(None, 999) == 999
  19. def test_nan_returns_fallback(self):
  20. assert _safe_int(math.nan, 999) == 999
  21. def test_inf_returns_fallback(self):
  22. assert _safe_int(math.inf, 999) == 999
  23. def test_neg_inf_returns_fallback(self):
  24. assert _safe_int(-math.inf, 999) == 999
  25. def test_string_numeric(self):
  26. assert _safe_int("500", 0) == 500
  27. def test_string_non_numeric_returns_fallback(self):
  28. assert _safe_int("abc", 42) == 42
  29. def test_zero(self):
  30. assert _safe_int(0, 999) == 0
  31. # ---------------------------------------------------------------------------
  32. # _safe_float
  33. # ---------------------------------------------------------------------------
  34. class TestSafeFloat:
  35. def test_normal_float(self):
  36. assert _safe_float(123.45, 0.0) == pytest.approx(123.45)
  37. def test_none_returns_fallback(self):
  38. assert _safe_float(None, -1.0) == -1.0
  39. def test_nan_returns_fallback(self):
  40. assert _safe_float(math.nan, -1.0) == -1.0
  41. def test_inf_returns_fallback(self):
  42. assert _safe_float(math.inf, -1.0) == -1.0
  43. def test_neg_inf_returns_fallback(self):
  44. assert _safe_float(-math.inf, -1.0) == -1.0
  45. def test_string_numeric(self):
  46. assert _safe_float("3.14", 0.0) == pytest.approx(3.14)
  47. def test_string_non_numeric_returns_fallback(self):
  48. assert _safe_float("bad", 0.0) == 0.0
  49. def test_zero(self):
  50. assert _safe_float(0.0, 99.0) == 0.0
  51. # ---------------------------------------------------------------------------
  52. # _map_spoolman_spool
  53. # ---------------------------------------------------------------------------
  54. MINIMAL_SPOOL = {
  55. "id": 1,
  56. "filament": {
  57. "material": "PLA",
  58. "name": "PLA Basic",
  59. "color_hex": "FF0000",
  60. "weight": 1000.0,
  61. "vendor": {"name": "Bambu Lab"},
  62. },
  63. "used_weight": 250.0,
  64. "archived": False,
  65. "registered": "2024-01-01T00:00:00Z",
  66. }
  67. class TestMapSpoolmanSpool:
  68. def test_basic_mapping(self):
  69. result = _map_spoolman_spool(MINIMAL_SPOOL)
  70. assert result["id"] == 1
  71. assert result["material"] == "PLA"
  72. assert result["rgba"] == "FF0000FF"
  73. assert result["label_weight"] == 1000
  74. assert result["weight_used"] == pytest.approx(250.0)
  75. assert result["data_origin"] == "spoolman"
  76. def test_missing_id_raises(self):
  77. spool = {k: v for k, v in MINIMAL_SPOOL.items() if k != "id"}
  78. with pytest.raises(ValueError, match="missing required 'id'"):
  79. _map_spoolman_spool(spool)
  80. def test_none_id_raises(self):
  81. with pytest.raises(ValueError):
  82. _map_spoolman_spool({**MINIMAL_SPOOL, "id": None})
  83. def test_string_id_raises(self):
  84. with pytest.raises(ValueError, match="not a valid integer"):
  85. _map_spoolman_spool({**MINIMAL_SPOOL, "id": "abc"})
  86. def test_zero_id_raises(self):
  87. with pytest.raises(ValueError, match="positive integer"):
  88. _map_spoolman_spool({**MINIMAL_SPOOL, "id": 0})
  89. def test_negative_id_raises(self):
  90. with pytest.raises(ValueError, match="positive integer"):
  91. _map_spoolman_spool({**MINIMAL_SPOOL, "id": -5})
  92. def test_numeric_string_id_accepted(self):
  93. result = _map_spoolman_spool({**MINIMAL_SPOOL, "id": "42"})
  94. assert result["id"] == 42
  95. def test_zero_price_not_converted_to_none(self):
  96. spool = {**MINIMAL_SPOOL, "price": 0.0}
  97. result = _map_spoolman_spool(spool)
  98. assert result["cost_per_kg"] == 0.0
  99. def test_nonzero_price_preserved(self):
  100. spool = {**MINIMAL_SPOOL, "price": 9.99}
  101. result = _map_spoolman_spool(spool)
  102. assert result["cost_per_kg"] == pytest.approx(9.99)
  103. def test_none_price_stays_none(self):
  104. spool = {**MINIMAL_SPOOL, "price": None}
  105. result = _map_spoolman_spool(spool)
  106. assert result["cost_per_kg"] is None
  107. def test_infinity_weight_falls_back(self):
  108. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "weight": math.inf}}
  109. result = _map_spoolman_spool(spool)
  110. assert result["label_weight"] == 1000
  111. def test_nan_used_weight_falls_back(self):
  112. spool = {**MINIMAL_SPOOL, "used_weight": math.nan}
  113. result = _map_spoolman_spool(spool)
  114. assert result["weight_used"] == 0.0
  115. def test_invalid_color_hex_falls_back_to_grey(self):
  116. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "color_hex": "ZZZZZZ"}}
  117. result = _map_spoolman_spool(spool)
  118. assert result["rgba"] == "808080FF"
  119. def test_short_color_hex_falls_back(self):
  120. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "color_hex": "FFF"}}
  121. result = _map_spoolman_spool(spool)
  122. assert result["rgba"] == "808080FF"
  123. def test_eight_char_color_hex_falls_back(self):
  124. # Only 6-char hex is valid from Spoolman; 8-char (RGBA) should fall back
  125. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "color_hex": "FF0000FF"}}
  126. result = _map_spoolman_spool(spool)
  127. assert result["rgba"] == "808080FF"
  128. def test_color_name_uses_explicit_field_when_present(self):
  129. """When Spoolman's filament has color_name set, that wins over the subtype fallback."""
  130. spool = {
  131. **MINIMAL_SPOOL,
  132. "filament": {**MINIMAL_SPOOL["filament"], "color_name": "Sunrise Orange"},
  133. }
  134. result = _map_spoolman_spool(spool)
  135. assert result["color_name"] == "Sunrise Orange"
  136. # Real stored value — not synthesised from subtype.
  137. assert result["color_name_is_synthesized"] is False
  138. def test_color_name_flags_synthesized_when_falling_back_to_subtype(self):
  139. """#1319: when the read falls back to subtype, the response must flag it
  140. so the edit form doesn't round-trip the synth value back to Spoolman."""
  141. spool = {
  142. **MINIMAL_SPOOL,
  143. "filament": {
  144. **MINIMAL_SPOOL["filament"],
  145. "name": "PLA Basic Red",
  146. # No color_name field.
  147. },
  148. }
  149. result = _map_spoolman_spool(spool)
  150. assert result["color_name"] == "Basic Red"
  151. assert result["color_name_is_synthesized"] is True
  152. def test_color_name_falls_back_to_subtype_when_field_missing(self):
  153. """Spoolman doesn't standardise color_name; the LinkSpoolModal would
  154. otherwise show 'Unknown color' for every Spoolman spool. The mapper
  155. falls back to the filament's name minus material prefix (which the
  156. subtype field already carries) so the user can tell spools apart at a
  157. glance even on installs that don't fill color_name.
  158. """
  159. spool = {
  160. **MINIMAL_SPOOL,
  161. "filament": {
  162. **MINIMAL_SPOOL["filament"],
  163. "name": "PLA Basic Red",
  164. # No color_name field — the common case for default Spoolman installs.
  165. },
  166. }
  167. result = _map_spoolman_spool(spool)
  168. # subtype is filament_name minus material prefix → "Basic Red"
  169. assert result["subtype"] == "Basic Red"
  170. # color_name falls back to subtype.
  171. assert result["color_name"] == "Basic Red"
  172. def test_color_name_read_from_spool_extra_first(self):
  173. """#1357: the canonical store for color_name is
  174. spool.extra.bambu_color_name (JSON-encoded). Read priority is
  175. extra > filament.color_name > subtype-synth. The user's
  176. Bambuddy-saved value MUST win even when Spoolman's own
  177. filament.color_name happens to be populated from some other source.
  178. """
  179. spool = {
  180. **MINIMAL_SPOOL,
  181. "extra": {"bambu_color_name": '"Galaxy Black"'},
  182. "filament": {
  183. **MINIMAL_SPOOL["filament"],
  184. "name": "PLA Glow",
  185. "color_name": "Glow", # would be picked up if extra weren't preferred
  186. },
  187. }
  188. result = _map_spoolman_spool(spool)
  189. assert result["color_name"] == "Galaxy Black"
  190. assert result["color_name_is_synthesized"] is False
  191. def test_color_name_empty_extra_falls_through_to_filament(self):
  192. """An explicit empty string in spool.extra.bambu_color_name (the
  193. "user cleared the field" shape) must NOT mask Spoolman's own
  194. filament.color_name if one exists — it falls through to the next
  195. layer instead of suppressing it."""
  196. spool = {
  197. **MINIMAL_SPOOL,
  198. "extra": {"bambu_color_name": '""'},
  199. "filament": {
  200. **MINIMAL_SPOOL["filament"],
  201. "color_name": "Sunset",
  202. },
  203. }
  204. result = _map_spoolman_spool(spool)
  205. assert result["color_name"] == "Sunset"
  206. assert result["color_name_is_synthesized"] is False
  207. def test_color_name_empty_extra_falls_through_to_synth(self):
  208. """When extra is cleared and filament has no color_name either,
  209. fall all the way through to the subtype synth — same UX as a fresh
  210. Spoolman install."""
  211. spool = {
  212. **MINIMAL_SPOOL,
  213. "extra": {"bambu_color_name": '""'},
  214. "filament": {
  215. **MINIMAL_SPOOL["filament"],
  216. "name": "PLA Basic Red",
  217. },
  218. }
  219. result = _map_spoolman_spool(spool)
  220. assert result["color_name"] == "Basic Red"
  221. assert result["color_name_is_synthesized"] is True
  222. def test_color_name_none_when_both_fields_empty(self):
  223. """If neither color_name nor a usable subtype exists, return None — UI
  224. falls back to its own 'Unknown color' string rather than showing a
  225. misleading material-only label.
  226. """
  227. spool = {
  228. **MINIMAL_SPOOL,
  229. "filament": {
  230. **MINIMAL_SPOOL["filament"],
  231. "name": "PLA", # name == material → subtype becomes None
  232. },
  233. }
  234. result = _map_spoolman_spool(spool)
  235. assert result["subtype"] is None
  236. assert result["color_name"] is None
  237. # No synth happened — nothing to fall back to.
  238. assert result["color_name_is_synthesized"] is False
  239. def test_color_hex_with_hash_prefix_stripped(self):
  240. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "color_hex": "#00FF00"}}
  241. result = _map_spoolman_spool(spool)
  242. assert result["rgba"] == "00FF00FF"
  243. def test_color_hex_lowercase_normalised(self):
  244. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "color_hex": "ff0000"}}
  245. result = _map_spoolman_spool(spool)
  246. assert result["rgba"] == "FF0000FF"
  247. def test_none_filament(self):
  248. spool = {**MINIMAL_SPOOL, "filament": None}
  249. result = _map_spoolman_spool(spool)
  250. assert result["material"] == ""
  251. assert result["rgba"] == "808080FF"
  252. assert result["label_weight"] == 1000
  253. def test_archived_spool_has_archived_at(self):
  254. spool = {**MINIMAL_SPOOL, "archived": True}
  255. result = _map_spoolman_spool(spool)
  256. assert result["archived_at"] is not None
  257. def test_subtype_strips_material_prefix(self):
  258. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "material": "PLA", "name": "PLA Basic"}}
  259. result = _map_spoolman_spool(spool)
  260. assert result["subtype"] == "Basic"
  261. def test_brand_from_vendor(self):
  262. result = _map_spoolman_spool(MINIMAL_SPOOL)
  263. assert result["brand"] == "Bambu Lab"
  264. def test_no_vendor_brand_is_none(self):
  265. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "vendor": None}}
  266. result = _map_spoolman_spool(spool)
  267. assert result["brand"] is None
  268. def test_spoolman_location_mapped_to_storage_location(self):
  269. spool = {**MINIMAL_SPOOL, "location": "Shelf A"}
  270. result = _map_spoolman_spool(spool)
  271. assert result["storage_location"] == "Shelf A"
  272. def test_no_location_gives_none_storage_location(self):
  273. result = _map_spoolman_spool(MINIMAL_SPOOL)
  274. assert result["storage_location"] is None
  275. def test_empty_location_gives_none_storage_location(self):
  276. spool = {**MINIMAL_SPOOL, "location": ""}
  277. result = _map_spoolman_spool(spool)
  278. assert result["storage_location"] is None
  279. def test_spoolman_location_key_not_in_result(self):
  280. spool = {**MINIMAL_SPOOL, "location": "Shelf A"}
  281. result = _map_spoolman_spool(spool)
  282. assert "spoolman_location" not in result
  283. def test_core_weight_from_filament_spool_weight(self):
  284. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "spool_weight": 196}}
  285. result = _map_spoolman_spool(spool)
  286. assert result["core_weight"] == 196
  287. def test_core_weight_fallback_when_spool_weight_missing(self):
  288. result = _map_spoolman_spool(MINIMAL_SPOOL)
  289. assert result["core_weight"] == 250
  290. def test_core_weight_fallback_when_spool_weight_none(self):
  291. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "spool_weight": None}}
  292. result = _map_spoolman_spool(spool)
  293. assert result["core_weight"] == 250
  294. def test_core_weight_float_truncated_to_int(self):
  295. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "spool_weight": 180.9}}
  296. result = _map_spoolman_spool(spool)
  297. assert result["core_weight"] == 180
  298. def test_spool_level_spool_weight_takes_priority_over_filament(self):
  299. spool = {**MINIMAL_SPOOL, "spool_weight": 300, "filament": {**MINIMAL_SPOOL["filament"], "spool_weight": 196}}
  300. assert _map_spoolman_spool(spool)["core_weight"] == 300
  301. def test_spool_level_zero_spool_weight_not_treated_as_missing(self):
  302. spool = {**MINIMAL_SPOOL, "spool_weight": 0, "filament": {**MINIMAL_SPOOL["filament"], "spool_weight": 196}}
  303. assert _map_spoolman_spool(spool)["core_weight"] == 0
  304. def test_spool_level_none_falls_back_to_filament(self):
  305. spool = {**MINIMAL_SPOOL, "spool_weight": None, "filament": {**MINIMAL_SPOOL["filament"], "spool_weight": 196}}
  306. assert _map_spoolman_spool(spool)["core_weight"] == 196
  307. def test_spool_level_absent_falls_back_to_filament(self):
  308. spool = {**MINIMAL_SPOOL, "filament": {**MINIMAL_SPOOL["filament"], "spool_weight": 196}}
  309. assert _map_spoolman_spool(spool)["core_weight"] == 196
  310. def test_both_levels_none_uses_fallback(self):
  311. spool = {**MINIMAL_SPOOL, "spool_weight": None, "filament": {**MINIMAL_SPOOL["filament"], "spool_weight": None}}
  312. assert _map_spoolman_spool(spool)["core_weight"] == 250
  313. # ---------------------------------------------------------------------------
  314. # F4: _safe_optional_float unit tests
  315. # ---------------------------------------------------------------------------
  316. class TestSafeOptionalFloat:
  317. """F4: Direct unit tests for _safe_optional_float (NaN/Inf safety)."""
  318. def test_normal_value(self):
  319. import pytest
  320. from backend.app.api.routes._spoolman_helpers import _safe_optional_float
  321. assert _safe_optional_float(9.99) == pytest.approx(9.99)
  322. def test_none_returns_none(self):
  323. from backend.app.api.routes._spoolman_helpers import _safe_optional_float
  324. assert _safe_optional_float(None) is None
  325. def test_nan_returns_none(self):
  326. import math
  327. from backend.app.api.routes._spoolman_helpers import _safe_optional_float
  328. assert _safe_optional_float(math.nan) is None
  329. def test_inf_returns_none(self):
  330. import math
  331. from backend.app.api.routes._spoolman_helpers import _safe_optional_float
  332. assert _safe_optional_float(math.inf) is None
  333. def test_neg_inf_returns_none(self):
  334. import math
  335. from backend.app.api.routes._spoolman_helpers import _safe_optional_float
  336. assert _safe_optional_float(-math.inf) is None
  337. def test_zero_returns_zero(self):
  338. from backend.app.api.routes._spoolman_helpers import _safe_optional_float
  339. assert _safe_optional_float(0.0) == 0.0
  340. def test_string_numeric(self):
  341. import pytest
  342. from backend.app.api.routes._spoolman_helpers import _safe_optional_float
  343. assert _safe_optional_float("3.14") == pytest.approx(3.14)
  344. def test_string_non_numeric_returns_none(self):
  345. from backend.app.api.routes._spoolman_helpers import _safe_optional_float
  346. assert _safe_optional_float("bad") is None
  347. class TestMapSpoolmanSpoolSlicerFilament:
  348. """slicer_filament round-trip via Spoolman extra dict.
  349. Spoolman has no native slicer_filament field, so we persist BambuStudio
  350. presets under bambu_slicer_filament[_name] keys in the spool's extra
  351. dict (JSON-encoded strings, like every Spoolman extra value). The map
  352. function unwraps those values and exposes them as slicer_filament /
  353. slicer_filament_name on the InventorySpool shape. Without this round-trip
  354. the user's selected slicer preset is silently dropped on save (#1114).
  355. """
  356. def test_slicer_filament_unwrapped_from_extra(self):
  357. spool = {
  358. **MINIMAL_SPOOL,
  359. "extra": {
  360. "bambu_slicer_filament": '"PFUSf543b298f8ea66"',
  361. "bambu_slicer_filament_name": '"Devil Design PLA Basic @Bambu Lab H2D 0.4 nozzle (Custom)"',
  362. },
  363. }
  364. result = _map_spoolman_spool(spool)
  365. assert result["slicer_filament"] == "PFUSf543b298f8ea66"
  366. assert result["slicer_filament_name"] == "Devil Design PLA Basic @Bambu Lab H2D 0.4 nozzle (Custom)"
  367. def test_slicer_filament_falls_back_to_filament_name(self):
  368. # Spool has no bambu_slicer_filament_name override → use Spoolman's filament.name
  369. spool = {**MINIMAL_SPOOL, "extra": {}}
  370. result = _map_spoolman_spool(spool)
  371. assert result["slicer_filament"] is None
  372. assert result["slicer_filament_name"] == "PLA Basic" # from filament.name
  373. def test_empty_string_extra_treated_as_unset(self):
  374. # JSON-encoded empty string is how the user clears the field
  375. spool = {
  376. **MINIMAL_SPOOL,
  377. "extra": {
  378. "bambu_slicer_filament": '""',
  379. "bambu_slicer_filament_name": '""',
  380. },
  381. }
  382. result = _map_spoolman_spool(spool)
  383. assert result["slicer_filament"] is None
  384. # Falls back to filament.name when the override is cleared
  385. assert result["slicer_filament_name"] == "PLA Basic"
  386. def test_non_json_extra_value_passed_through(self):
  387. # Tolerate bare-string values written without JSON encoding
  388. # (older data, manual writes via Spoolman UI, etc.)
  389. spool = {
  390. **MINIMAL_SPOOL,
  391. "extra": {"bambu_slicer_filament": "GFL05"},
  392. }
  393. result = _map_spoolman_spool(spool)
  394. assert result["slicer_filament"] == "GFL05"
  395. class TestExtractExtraStr:
  396. """JSON-encoded extra-string unwrapper used by _map_spoolman_spool."""
  397. def test_unwraps_quoted_string(self):
  398. from backend.app.api.routes._spoolman_helpers import _extract_extra_str
  399. assert _extract_extra_str({"k": '"hello"'}, "k") == "hello"
  400. def test_returns_empty_for_missing_key(self):
  401. from backend.app.api.routes._spoolman_helpers import _extract_extra_str
  402. assert _extract_extra_str({}, "k") == ""
  403. def test_returns_empty_for_non_string_value(self):
  404. from backend.app.api.routes._spoolman_helpers import _extract_extra_str
  405. # Spoolman extra values are stringified; numeric values shouldn't sneak in
  406. # but if they do we treat them as unset rather than crashing
  407. assert _extract_extra_str({"k": 42}, "k") == ""
  408. def test_returns_empty_for_json_null(self):
  409. from backend.app.api.routes._spoolman_helpers import _extract_extra_str
  410. # null isn't a string after decode → treat as unset
  411. assert _extract_extra_str({"k": "null"}, "k") == ""
  412. def test_passes_through_bare_string_on_decode_error(self):
  413. from backend.app.api.routes._spoolman_helpers import _extract_extra_str
  414. # Tolerate non-JSON-encoded values
  415. assert _extract_extra_str({"k": "GFL05"}, "k") == "GFL05"
  416. class TestMapSpoolmanSpoolPrice:
  417. """F4: NaN/Inf price in _map_spoolman_spool gives None cost_per_kg."""
  418. def test_nan_price_gives_none_cost_per_kg(self):
  419. import math
  420. from backend.app.api.routes._spoolman_helpers import _map_spoolman_spool
  421. spool = {**MINIMAL_SPOOL, "price": math.nan}
  422. assert _map_spoolman_spool(spool)["cost_per_kg"] is None
  423. def test_inf_price_gives_none_cost_per_kg(self):
  424. import math
  425. from backend.app.api.routes._spoolman_helpers import _map_spoolman_spool
  426. spool = {**MINIMAL_SPOOL, "price": math.inf}
  427. assert _map_spoolman_spool(spool)["cost_per_kg"] is None