fix karung view data and sync only sync with the current date
This commit is contained in:
1 parent
b3556c6ef4
commit
5ae477a99d
8 files changed
+396
-32
No files matched your search
@@ -6,13 +6,18 @@ from django.test import TestCase, override_settings
|
||||
|
||||
from apps.accounts.models import User
|
||||
from apps.farms.models import Cycle, Kandang, Site
|
||||
from apps.operations.models import FeedSacks
|
||||
from apps.operations.services.karung_web import (
|
||||
KarungWebError,
|
||||
backfill_karung_from_combined,
|
||||
daily_totals_from_combined_history,
|
||||
fetch_iot_masuk_for_date,
|
||||
initial_balance_accuracy,
|
||||
masuk_source_for_kandang,
|
||||
recompute_feed_sack_totals,
|
||||
request_karung,
|
||||
source_for_kandang,
|
||||
sync_karung_for_cycle,
|
||||
)
|
||||
|
||||
ACTIVE_SECTIONS_SAMPLE = {
|
||||
@@ -50,6 +55,29 @@ COMBINED_SAMPLE = {
|
||||
}
|
||||
}
|
||||
|
||||
COMBINED_HISTORY_FULL = {
|
||||
"masuk": {
|
||||
"history": [
|
||||
{"counter_value": 192, "date": "2026-09-02", "source": "K1 - In"},
|
||||
{"counter_value": 1, "date": "2026-09-02", "source": "K1 - Out"},
|
||||
{"counter_value": 50, "date": "2026-09-03", "source": "K1 - In"},
|
||||
{"counter_value": 0, "date": "2026-09-03", "source": "K1 - Out"},
|
||||
{"counter_value": 120, "date": "2026-09-02", "source": "K2 - In"},
|
||||
{"counter_value": 2, "date": "2026-09-02", "source": "K2 - Out"},
|
||||
{"counter_value": 112, "date": "2026-08-27", "source": "K1 - In"},
|
||||
],
|
||||
"today": [],
|
||||
},
|
||||
"tuang": {
|
||||
"history": [
|
||||
{"counter_value": 7, "date": "2026-09-02", "source": "K1 - Use"},
|
||||
{"counter_value": 4, "date": "2026-09-03", "source": "K1 - Use"},
|
||||
{"counter_value": 9, "date": "2026-09-02", "source": "K2 - Use"},
|
||||
],
|
||||
"today": [],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class MasukSourceForKandangTests(TestCase):
|
||||
def test_maps_kandang_number_to_iot_source(self):
|
||||
@@ -166,3 +194,120 @@ class RequestKarungTests(TestCase):
|
||||
self.assertEqual(feed_sack2.out_today, 5)
|
||||
self.assertEqual(summary1["kandang_totals"]["in_today"], 192)
|
||||
self.assertEqual(summary2["kandang_totals"]["in_today"], 120)
|
||||
|
||||
def test_daily_totals_from_combined_history_groups_by_date_for_kandang(self):
|
||||
by_date = daily_totals_from_combined_history(
|
||||
COMBINED_HISTORY_FULL, kandang_name="Kandang 1"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
by_date[date(2026, 9, 2)],
|
||||
{"in_today": 192, "out_today": 1, "feed_use_today": 7},
|
||||
)
|
||||
self.assertEqual(
|
||||
by_date[date(2026, 9, 3)],
|
||||
{"in_today": 50, "out_today": 0, "feed_use_today": 4},
|
||||
)
|
||||
self.assertEqual(
|
||||
by_date[date(2026, 8, 27)],
|
||||
{"in_today": 112, "out_today": 0, "feed_use_today": 0},
|
||||
)
|
||||
self.assertNotIn(date(2026, 9, 1), by_date)
|
||||
|
||||
@patch("apps.operations.services.karung_web.timezone")
|
||||
def test_backfill_upserts_in_cycle_range_and_skips_outside(self, mock_tz):
|
||||
mock_tz.localdate.return_value = date(2026, 9, 3)
|
||||
|
||||
dates = backfill_karung_from_combined(self.cycle1, COMBINED_HISTORY_FULL)
|
||||
|
||||
self.assertEqual(dates, [date(2026, 9, 2), date(2026, 9, 3)])
|
||||
row_sep2 = FeedSacks.objects.get(cycle=self.cycle1, date=date(2026, 9, 2))
|
||||
self.assertEqual(row_sep2.in_today, 192)
|
||||
self.assertEqual(row_sep2.feed_use_today, 7)
|
||||
self.assertEqual(row_sep2.out_today, 1)
|
||||
self.assertFalse(
|
||||
FeedSacks.objects.filter(cycle=self.cycle1, date=date(2026, 8, 27)).exists()
|
||||
)
|
||||
|
||||
@patch("apps.operations.services.karung_web.timezone")
|
||||
def test_backfill_refreshes_existing_row_from_upstream(self, mock_tz):
|
||||
mock_tz.localdate.return_value = date(2026, 9, 3)
|
||||
FeedSacks.objects.create(
|
||||
cycle=self.cycle1,
|
||||
date=date(2026, 9, 2),
|
||||
in_today=1,
|
||||
out_today=0,
|
||||
feed_use_today=0,
|
||||
in_total=1,
|
||||
out_total=0,
|
||||
feed_use_total=0,
|
||||
)
|
||||
|
||||
backfill_karung_from_combined(self.cycle1, COMBINED_HISTORY_FULL)
|
||||
|
||||
row = FeedSacks.objects.get(cycle=self.cycle1, date=date(2026, 9, 2))
|
||||
self.assertEqual(row.in_today, 192)
|
||||
self.assertEqual(row.feed_use_today, 7)
|
||||
self.assertEqual(row.out_today, 1)
|
||||
|
||||
def test_recompute_feed_sack_totals_accumulates_in_date_order(self):
|
||||
FeedSacks.objects.create(
|
||||
cycle=self.cycle1,
|
||||
date=date(2026, 9, 2),
|
||||
in_today=192,
|
||||
out_today=1,
|
||||
feed_use_today=7,
|
||||
in_total=0,
|
||||
out_total=0,
|
||||
feed_use_total=0,
|
||||
)
|
||||
FeedSacks.objects.create(
|
||||
cycle=self.cycle1,
|
||||
date=date(2026, 9, 3),
|
||||
in_today=50,
|
||||
out_today=0,
|
||||
feed_use_today=4,
|
||||
in_total=0,
|
||||
out_total=0,
|
||||
feed_use_total=0,
|
||||
)
|
||||
|
||||
recompute_feed_sack_totals(self.cycle1)
|
||||
|
||||
sep2 = FeedSacks.objects.get(cycle=self.cycle1, date=date(2026, 9, 2))
|
||||
sep3 = FeedSacks.objects.get(cycle=self.cycle1, date=date(2026, 9, 3))
|
||||
self.assertEqual((sep2.in_total, sep2.feed_use_total, sep2.out_total), (192, 7, 1))
|
||||
self.assertEqual((sep3.in_total, sep3.feed_use_total, sep3.out_total), (242, 11, 1))
|
||||
|
||||
@patch("apps.operations.services.karung_web.timezone")
|
||||
@patch("apps.operations.services.karung_web.KarungWebClient.fetch_combined")
|
||||
@patch("apps.operations.services.karung_web.KarungWebClient.fetch_active_sections")
|
||||
def test_sync_karung_for_cycle_runs_today_then_history_backfill(
|
||||
self, mock_sections, mock_combined, mock_tz
|
||||
):
|
||||
mock_tz.localdate.return_value = date(2026, 9, 3)
|
||||
mock_sections.return_value = {
|
||||
"sections": {
|
||||
"today": {
|
||||
"date": "2026-09-03",
|
||||
"masuk": [
|
||||
{"karung": 50, "source": "K1 - In"},
|
||||
{"karung": 0, "source": "K1 - Out"},
|
||||
],
|
||||
"tuang": [{"karung": 4, "source": "K1 - Use"}],
|
||||
"totals": {"masuk": 50, "tuang": 4},
|
||||
}
|
||||
},
|
||||
"cycle": {"saldo_awal": 0},
|
||||
}
|
||||
mock_combined.return_value = COMBINED_HISTORY_FULL
|
||||
|
||||
feed_sack, summary = sync_karung_for_cycle(self.cycle1, section="today")
|
||||
|
||||
self.assertEqual(feed_sack.date, date(2026, 9, 3))
|
||||
self.assertEqual(feed_sack.in_today, 50)
|
||||
self.assertEqual(feed_sack.in_total, 242)
|
||||
sep2 = FeedSacks.objects.get(cycle=self.cycle1, date=date(2026, 9, 2))
|
||||
self.assertEqual(sep2.in_today, 192)
|
||||
self.assertEqual(sep2.in_total, 192)
|
||||
self.assertEqual(summary["history_dates"], ["2026-09-02", "2026-09-03"])
|
||||
Reference in new issue
Block a user