| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import unittest
- import httpx
- import json
- class TestMealMath(unittest.TestCase):
- BASE_URL = "http://192.168.130.171:8000/api"
- TOKEN = None
- FOOD_ID_1 = None # Will be populated
- FOOD_1_DATA = None
- @classmethod
- def setUpClass(cls):
- with httpx.Client(timeout=30.0) as client:
- # 1. Login to get token
- login_res = client.post(f"{cls.BASE_URL}/login", json={
- "username": "ferro988",
- "password": "password"
- })
- if login_res.status_code == 200:
- cls.TOKEN = login_res.json()["token"]
-
- # 2. Search for a baseline food (e.g., Chicken)
- search_res = client.get(f"{cls.BASE_URL}/food/search?q=Chicken", headers={
- "Authorization": f"Bearer {cls.TOKEN}"
- })
- if search_res.status_code == 200:
- foods = search_res.json()
- if foods:
- cls.FOOD_ID_1 = foods[0]["id"]
- cls.FOOD_1_DATA = foods[0]
- def test_baseline_100g(self):
- """Verify that 100g returns the exact database values."""
- payload = {"items": [{"food_id": self.FOOD_ID_1, "amount_g": 100}]}
- with httpx.Client(timeout=30.0) as client:
- res = client.post(f"{self.BASE_URL}/meal/calculate", json=payload, headers={
- "Authorization": f"Bearer {self.TOKEN}"
- })
- self.assertEqual(res.status_code, 200)
- data = res.json()
-
- # Check macros
- self.assertEqual(data["macros"]["calories"], self.FOOD_1_DATA["calories"])
- self.assertEqual(data["macros"]["protein_g"], self.FOOD_1_DATA["protein_g"])
- def test_half_portion_50g(self):
- """Verify that 50g returns exactly 50% of the database values."""
- payload = {"items": [{"food_id": self.FOOD_ID_1, "amount_g": 50}]}
- with httpx.Client(timeout=30.0) as client:
- res = client.post(f"{self.BASE_URL}/meal/calculate", json=payload, headers={
- "Authorization": f"Bearer {self.TOKEN}"
- })
- self.assertEqual(res.status_code, 200)
- data = res.json()
-
- expected_cal = round(self.FOOD_1_DATA["calories"] * 0.5, 2)
- expected_pro = round(self.FOOD_1_DATA["protein_g"] * 0.5, 2)
-
- self.assertEqual(data["macros"]["calories"], expected_cal)
- self.assertEqual(data["macros"]["protein_g"], expected_pro)
- def test_irregular_portion_237g(self):
- """Verify that 237g correctly applies the ratio (2.37x)."""
- payload = {"items": [{"food_id": self.FOOD_ID_1, "amount_g": 237}]}
- with httpx.Client(timeout=30.0) as client:
- res = client.post(f"{self.BASE_URL}/meal/calculate", json=payload, headers={
- "Authorization": f"Bearer {self.TOKEN}"
- })
- self.assertEqual(res.status_code, 200)
- data = res.json()
-
- ratio = 2.37
- expected_cal = round(self.FOOD_1_DATA["calories"] * ratio, 2)
-
- self.assertEqual(data["macros"]["calories"], expected_cal)
- def test_multi_item_aggregation(self):
- """Verify that two items sum up correctly."""
- with httpx.Client(timeout=30.0) as client:
- # Get a second food
- search_res = client.get(f"{self.BASE_URL}/food/search?q=Rice", headers={
- "Authorization": f"Bearer {self.TOKEN}"
- })
- food2 = search_res.json()[0]
-
- payload = {
- "items": [
- {"food_id": self.FOOD_ID_1, "amount_g": 100},
- {"food_id": food2["id"], "amount_g": 200}
- ]
- }
- res = client.post(f"{self.BASE_URL}/meal/calculate", json=payload, headers={
- "Authorization": f"Bearer {self.TOKEN}"
- })
- self.assertEqual(res.status_code, 200)
- data = res.json()
-
- expected_cal = round(self.FOOD_1_DATA["calories"] + (food2["calories"] * 2.0), 2)
- expected_weight = 100 + 200
-
- self.assertEqual(data["macros"]["calories"], expected_cal)
- self.assertEqual(data["total_weight_g"], expected_weight)
- if __name__ == "__main__":
- unittest.main()
|