Skip to content

Commit f4839be

Browse files
GloosyNeang-Rothmnykylyheang
authored
fix: Test code (#3)
* first commit * add model code * add git ignore * set up frontend+backend * set up frontend+backend * remove test * test code * fix test code --------- Co-authored-by: Neang-Rothmny <neangrothmony@gmail.com> Co-authored-by: lyheang <lyheang.ky@khmerenterprise.info>
1 parent 9218c54 commit f4839be

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

backend/tests/conftest.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,43 @@ def _install_fake_detector_module():
2020
"""
2121
fake_detector = types.ModuleType("detector")
2222

23+
class _TensorLike:
24+
"""Mimics a tensor with tolist() method"""
25+
def __init__(self, data):
26+
self._data = data
27+
28+
def tolist(self):
29+
return self._data if isinstance(self._data, list) else [self._data]
30+
31+
def __float__(self):
32+
return float(self._data)
33+
34+
def __int__(self):
35+
return int(self._data)
36+
2337
class _SeqWithToList:
2438
def __init__(self, data):
2539
self._data = data
2640

2741
def tolist(self):
2842
return list(self._data)
43+
44+
def __iter__(self):
45+
# Wrap each item in _TensorLike so it has .tolist()
46+
for item in self._data:
47+
yield _TensorLike(item)
2948

3049
class _FakeBoxes:
3150
def __init__(self):
32-
# minimal shape similar to ultralytics result
33-
self.xywh = _SeqWithToList([[10, 20, 30, 40]])
34-
self.cls = _SeqWithToList([0])
51+
# Match the new API: xyxy format (x1, y1, x2, y2), conf, cls
52+
self.xyxy = _SeqWithToList([[10, 20, 40, 60]]) # x1, y1, x2, y2
53+
self.conf = _SeqWithToList([0.95]) # confidence score
54+
self.cls = _SeqWithToList([0]) # class id
3555

3656
class _FakeResult:
3757
def __init__(self):
3858
self.boxes = _FakeBoxes()
59+
self.names = {0: "ក"} # Fake class names mapping
3960

4061
class Detector: # noqa: N801 - match name imported in app.py
4162
instance = None

backend/tests/test_detect.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,18 @@ def test_detect_endpoint_returns_boxes_and_classes(client):
2525
assert resp.status_code == 200
2626
body = resp.json()
2727

28-
# From our fake detector we expect one box and one class id
29-
assert "boxes" in body and isinstance(body["boxes"], list)
30-
assert "classes" in body and isinstance(body["classes"], list)
31-
assert body["boxes"] == [[10, 20, 30, 40]]
32-
assert body["classes"] == [0]
28+
# Check response structure matches new API
29+
assert "width" in body and body["width"] == 50
30+
assert "height" in body and body["height"] == 50
31+
assert "detections" in body and isinstance(body["detections"], list)
32+
assert "inference_ms" in body and isinstance(body["inference_ms"], (int, float))
33+
34+
# From our fake detector we expect one detection
35+
assert len(body["detections"]) == 1
36+
detection = body["detections"][0]
37+
38+
assert "box" in detection
39+
assert detection["box"] == {"x1": 10, "y1": 20, "x2": 40, "y2": 60}
40+
assert detection["confidence"] == 0.95
41+
assert detection["class_id"] == 0
42+
assert detection["class_name"] == "ក"

0 commit comments

Comments
 (0)