test_spoolman_inventory_helpers.py 23 KB

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