@@ -595,6 +595,88 @@ def test_filter_by_wealth_characteristic(self):
595595 self .assertEqual (response .status_code , 200 )
596596 self .assertEqual (len (response .json ()), 1 )
597597
598+ def test_as_of_date_filter_returns_valid_baselines (self ):
599+ """
600+ Test that the as_of_date filter returns only baselines valid as of the specified date.
601+ """
602+ today = datetime .date .today ()
603+
604+ # Baseline that expired in the past
605+ expired_baseline = LivelihoodZoneBaselineFactory (
606+ valid_from_date = today - datetime .timedelta (days = 365 ),
607+ valid_to_date = today - datetime .timedelta (days = 30 ),
608+ )
609+
610+ # Baseline currently valid
611+ current_baseline = LivelihoodZoneBaselineFactory (
612+ valid_from_date = today - datetime .timedelta (days = 30 ),
613+ valid_to_date = today + datetime .timedelta (days = 365 ),
614+ )
615+
616+ # Baseline that starts in the future (not yet valid)
617+ future_baseline = LivelihoodZoneBaselineFactory (
618+ valid_from_date = today + datetime .timedelta (days = 30 ),
619+ valid_to_date = today + datetime .timedelta (days = 365 ),
620+ )
621+
622+ # Test default behavior (no as_of_date param) - should return all baselines
623+ response = self .client .get (self .url )
624+ self .assertEqual (response .status_code , 200 )
625+ baseline_ids = [b ["id" ] for b in response .json ()]
626+ self .assertIn (current_baseline .id , baseline_ids )
627+ self .assertIn (expired_baseline .id , baseline_ids )
628+ self .assertIn (future_baseline .id , baseline_ids )
629+
630+ # Test with explicit as_of_date=today - should filter to current baselines
631+ response = self .client .get (self .url , {"as_of_date" : today .isoformat ()})
632+ self .assertEqual (response .status_code , 200 )
633+ baseline_ids = [b ["id" ] for b in response .json ()]
634+ self .assertIn (current_baseline .id , baseline_ids )
635+ self .assertNotIn (expired_baseline .id , baseline_ids )
636+ self .assertNotIn (future_baseline .id , baseline_ids )
637+
638+ # Test with a past date when expired_baseline was still valid
639+ # current_baseline hadn't started yet, so it should be excluded
640+ past_date = today - datetime .timedelta (days = 180 )
641+ response = self .client .get (self .url , {"as_of_date" : past_date .isoformat ()})
642+ self .assertEqual (response .status_code , 200 )
643+ baseline_ids = [b ["id" ] for b in response .json ()]
644+ self .assertIn (expired_baseline .id , baseline_ids )
645+ self .assertNotIn (current_baseline .id , baseline_ids ) # hadn't started yet
646+ self .assertNotIn (future_baseline .id , baseline_ids )
647+
648+ # Test with a future date when future_baseline will be valid
649+ future_date = today + datetime .timedelta (days = 60 )
650+ response = self .client .get (self .url , {"as_of_date" : future_date .isoformat ()})
651+ self .assertEqual (response .status_code , 200 )
652+ baseline_ids = [b ["id" ] for b in response .json ()]
653+ self .assertNotIn (expired_baseline .id , baseline_ids )
654+ self .assertIn (current_baseline .id , baseline_ids )
655+ self .assertIn (future_baseline .id , baseline_ids )
656+
657+ # Baseline with null valid_from_date (valid from the beginning of time)
658+ baseline_no_from = LivelihoodZoneBaselineFactory (
659+ valid_from_date = None ,
660+ valid_to_date = today + datetime .timedelta (days = 365 ),
661+ )
662+ # Baseline with null valid_to_date (valid indefinitely)
663+ baseline_no_to = LivelihoodZoneBaselineFactory (
664+ valid_from_date = today - datetime .timedelta (days = 365 ),
665+ valid_to_date = None ,
666+ )
667+ # Baseline with both dates null (always valid)
668+ baseline_no_dates = LivelihoodZoneBaselineFactory (
669+ valid_from_date = None ,
670+ valid_to_date = None ,
671+ )
672+ # Test default behavior - all three should be returned
673+ response = self .client .get (self .url )
674+ self .assertEqual (response .status_code , 200 )
675+ baseline_ids = [b ["id" ] for b in response .json ()]
676+ self .assertIn (baseline_no_from .id , baseline_ids )
677+ self .assertIn (baseline_no_to .id , baseline_ids )
678+ self .assertIn (baseline_no_dates .id , baseline_ids )
679+
598680
599681class LivelihoodZoneBaselineFacetedSearchViewTestCase (APITestCase ):
600682 def setUp (self ):
@@ -641,7 +723,8 @@ def test_search_with_product(self):
641723 # Apply the filters to the baseline
642724 baseline_url = reverse ("livelihoodzonebaseline-list" )
643725 response = self .client .get (
644- baseline_url , {search_data ["products" ][0 ]["filter" ]: search_data ["products" ][0 ]["value" ]}
726+ baseline_url ,
727+ {search_data ["products" ][0 ]["filter" ]: search_data ["products" ][0 ]["value" ]},
645728 )
646729 self .assertEqual (response .status_code , 200 )
647730 self .assertEqual (len (json .loads (response .content )), 2 )
@@ -650,7 +733,10 @@ def test_search_with_product(self):
650733 self .assertTrue (any (d ["name" ] == self .baseline3 .name for d in data ))
651734 self .assertFalse (any (d ["name" ] == self .baseline2 .name for d in data ))
652735
653- response = self .client .get (baseline_url , {search_data ["items" ][0 ]["filter" ]: search_data ["items" ][0 ]["value" ]})
736+ response = self .client .get (
737+ baseline_url ,
738+ {search_data ["items" ][0 ]["filter" ]: search_data ["items" ][0 ]["value" ]},
739+ )
654740 self .assertEqual (response .status_code , 200 )
655741 self .assertEqual (len (json .loads (response .content )), 1 )
656742 data = json .loads (response .content )
0 commit comments