71 lines
3.2 KiB
Python
71 lines
3.2 KiB
Python
"""Monthly dashboard summary boundaries and manual planning classification."""
|
|
import unittest
|
|
from datetime import date
|
|
from unittest.mock import patch
|
|
|
|
from routes import spot
|
|
|
|
|
|
class SpotSummaryMonthTest(unittest.TestCase):
|
|
def summary(self, month):
|
|
with patch.object(spot, 'query', side_effect=[
|
|
[{'cnt': 8}], [{'sampled_count': 3, 'unauthorized_count': 2}], [{'cnt': 5}],
|
|
]) as query:
|
|
result, selected = spot._spot_dashboard_summary(month)
|
|
return result, selected, query
|
|
|
|
def test_month_filters_all_four_cards_and_uses_planning_new_old(self):
|
|
result, selected, query = self.summary('2026-09')
|
|
self.assertEqual(selected, '2026-09')
|
|
self.assertEqual(result, {'total_forms':8, 'total_sample_quantity':3,
|
|
'unauthorized_count':2, 'listed_count':5})
|
|
start, end = date(2026, 9, 1), date(2026, 10, 1)
|
|
calls = query.call_args_list
|
|
self.assertEqual(calls[0].args[1], (start,end))
|
|
self.assertEqual(calls[1].args[1], (start,end,start,end))
|
|
self.assertEqual(calls[2].args[1], (start,end))
|
|
sql = calls[2].args[0]
|
|
self.assertIn('COUNT(DISTINCT', sql)
|
|
self.assertIn("p.product_type='现货'", sql)
|
|
self.assertIn('p.deleted_at IS NULL', sql)
|
|
self.assertIn("p.planning_new_old,'')) IN ('新品','是')", sql)
|
|
self.assertIn('p.created_at >= %s AND p.created_at < %s', sql)
|
|
self.assertNotIn('final_product_name', sql)
|
|
|
|
def test_year_end_and_leap_february_use_exclusive_next_month(self):
|
|
for month,start,end in [('2026-12',date(2026,12,1),date(2027,1,1)),
|
|
('2024-02',date(2024,2,1),date(2024,3,1))]:
|
|
_,_,query=self.summary(month)
|
|
self.assertEqual(query.call_args.args[1],(start,end))
|
|
|
|
def test_missing_or_invalid_month_defaults_to_current_month(self):
|
|
for value in (None, 'invalid', '2026-13'):
|
|
_, selected, _ = self.summary(value)
|
|
self.assertEqual(selected, date.today().strftime('%Y-%m'))
|
|
|
|
def test_empty_month_returns_zero_counts(self):
|
|
with patch.object(spot, 'query', side_effect=[
|
|
[{'cnt':0}],[{'sampled_count':None,'unauthorized_count':None}],[{'cnt':0}],
|
|
]):
|
|
result,_=spot._spot_dashboard_summary('2020-01')
|
|
self.assertTrue(all(value==0 for value in result.values()))
|
|
|
|
def test_all_months_removes_date_limits_and_keeps_product_rules(self):
|
|
result, selected, query = self.summary('all')
|
|
self.assertEqual(selected, 'all')
|
|
self.assertEqual(result, {'total_forms':8, 'total_sample_quantity':3,
|
|
'unauthorized_count':2, 'listed_count':5})
|
|
for call in query.call_args_list:
|
|
self.assertEqual(len(call.args), 1)
|
|
self.assertNotIn('>= %s', call.args[0])
|
|
self.assertNotIn('< %s', call.args[0])
|
|
sql = query.call_args.args[0]
|
|
self.assertIn('COUNT(DISTINCT', sql)
|
|
self.assertIn("p.product_type='现货'", sql)
|
|
self.assertIn('p.deleted_at IS NULL', sql)
|
|
self.assertIn("('新品','是')", sql)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|