update add feed initial balance on cycle and fix its feature

This commit is contained in:
Alberto-Audrix committed 2026-09-01 14:02:41 +07:00
1 parent 91444faa31
commit 7a9d600aa6
16 files changed
+439 -88

No files matched your search

@@ -0,0 +1,99 @@
from datetime import date
from unittest.mock import patch
from django.core.exceptions import ValidationError
from django.test import TestCase, override_settings
from apps.operations.services.karung_web import (
KarungWebError,
fetch_iot_masuk_for_date,
initial_balance_accuracy,
masuk_source_for_kandang,
)
COMBINED_SAMPLE = {
"masuk": {
"history": [
{"counter_value": 160, "date": "2026-09-02", "id": 17, "source": "K1 - In"},
{"counter_value": 80, "date": "2026-09-02", "id": 19, "source": "K2 - In"},
{"counter_value": 112, "date": "2026-08-27", "id": 9, "source": "K1 - In"},
{"counter_value": 32, "date": "2026-08-27", "id": 11, "source": "K2 - In"},
],
"today": [
{"karung": 160, "source": "K1 - In"},
{"karung": 80, "source": "K2 - In"},
],
}
}
class MasukSourceForKandangTests(TestCase):
def test_maps_kandang_number_to_iot_source(self):
self.assertEqual(masuk_source_for_kandang("Kandang 1"), "K1 - In")
self.assertEqual(masuk_source_for_kandang("Kandang 2"), "K2 - In")
def test_rejects_kandang_name_without_number(self):
with self.assertRaises(ValidationError):
masuk_source_for_kandang("Utara")
class InitialBalanceAccuracyTests(TestCase):
def test_returns_ratio_when_both_values_positive(self):
self.assertEqual(initial_balance_accuracy(120, 115), 95.8)
def test_returns_100_when_both_zero(self):
self.assertEqual(initial_balance_accuracy(0, 0), 100.0)
def test_returns_none_when_iot_missing(self):
self.assertIsNone(initial_balance_accuracy(120, None))
@override_settings(KARUNG_WEB_ADMIN_BASE_URL="http://karung.internal")
class KarungWebCombinedTests(TestCase):
@patch("apps.operations.services.karung_web.KarungWebClient.fetch_combined")
def test_fetch_iot_masuk_for_date_uses_kandang_specific_history_row(self, mock_fetch):
mock_fetch.return_value = COMBINED_SAMPLE
result = fetch_iot_masuk_for_date(date(2026, 8, 27), kandang_name="Kandang 2")
self.assertEqual(result, 32)
@patch("apps.operations.services.karung_web.KarungWebClient.fetch_combined")
def test_fetch_iot_masuk_for_date_matches_source_case_insensitively(self, mock_fetch):
mock_fetch.return_value = {
"masuk": {
"history": [
{"counter_value": 99, "date": "2026-08-27", "source": "K1 - in"},
]
}
}
result = fetch_iot_masuk_for_date(date(2026, 8, 27), kandang_name="Kandang 1")
self.assertEqual(result, 99)
@patch("apps.operations.services.karung_web.date")
@patch("apps.operations.services.karung_web.KarungWebClient.fetch_combined")
def test_fetch_iot_masuk_for_date_falls_back_to_today_live_rows(self, mock_fetch, mock_date):
mock_date.today.return_value = date(2026, 9, 2)
mock_fetch.return_value = {"masuk": {"history": [], "today": COMBINED_SAMPLE["masuk"]["today"]}}
result = fetch_iot_masuk_for_date(date(2026, 9, 2), kandang_name="Kandang 1")
self.assertEqual(result, 160)
@patch("apps.operations.services.karung_web.KarungWebClient.fetch_combined")
def test_fetch_iot_masuk_for_date_returns_none_when_day_missing(self, mock_fetch):
mock_fetch.return_value = COMBINED_SAMPLE
result = fetch_iot_masuk_for_date(date(2026, 8, 26), kandang_name="Kandang 1")
self.assertIsNone(result)
@patch(
"apps.operations.services.karung_web.KarungWebClient.fetch_combined",
side_effect=KarungWebError("upstream down"),
)
def test_fetch_iot_masuk_for_date_propagates_upstream_errors(self, _mock_fetch):
with self.assertRaises(KarungWebError):
fetch_iot_masuk_for_date(date(2026, 8, 27), kandang_name="Kandang 1")