@@ -599,6 +599,88 @@ def test_filter_by_wealth_characteristic(self):
599599 self .assertEqual (response .status_code , 200 )
600600 self .assertEqual (len (response .json ()), 1 )
601601
602+ def test_as_of_date_filter_returns_valid_baselines (self ):
603+ """
604+ Test that the as_of_date filter returns only baselines valid as of the specified date.
605+ """
606+ today = datetime .date .today ()
607+
608+ # Baseline that expired in the past
609+ expired_baseline = LivelihoodZoneBaselineFactory (
610+ valid_from_date = today - datetime .timedelta (days = 365 ),
611+ valid_to_date = today - datetime .timedelta (days = 30 ),
612+ )
613+
614+ # Baseline currently valid
615+ current_baseline = LivelihoodZoneBaselineFactory (
616+ valid_from_date = today - datetime .timedelta (days = 30 ),
617+ valid_to_date = today + datetime .timedelta (days = 365 ),
618+ )
619+
620+ # Baseline that starts in the future (not yet valid)
621+ future_baseline = LivelihoodZoneBaselineFactory (
622+ valid_from_date = today + datetime .timedelta (days = 30 ),
623+ valid_to_date = today + datetime .timedelta (days = 365 ),
624+ )
625+
626+ # Test default behavior (no as_of_date param) - should return all baselines
627+ response = self .client .get (self .url )
628+ self .assertEqual (response .status_code , 200 )
629+ baseline_ids = [b ["id" ] for b in response .json ()]
630+ self .assertIn (current_baseline .id , baseline_ids )
631+ self .assertIn (expired_baseline .id , baseline_ids )
632+ self .assertIn (future_baseline .id , baseline_ids )
633+
634+ # Test with explicit as_of_date=today - should filter to current baselines
635+ response = self .client .get (self .url , {"as_of_date" : today .isoformat ()})
636+ self .assertEqual (response .status_code , 200 )
637+ baseline_ids = [b ["id" ] for b in response .json ()]
638+ self .assertIn (current_baseline .id , baseline_ids )
639+ self .assertNotIn (expired_baseline .id , baseline_ids )
640+ self .assertNotIn (future_baseline .id , baseline_ids )
641+
642+ # Test with a past date when expired_baseline was still valid
643+ # current_baseline hadn't started yet, so it should be excluded
644+ past_date = today - datetime .timedelta (days = 180 )
645+ response = self .client .get (self .url , {"as_of_date" : past_date .isoformat ()})
646+ self .assertEqual (response .status_code , 200 )
647+ baseline_ids = [b ["id" ] for b in response .json ()]
648+ self .assertIn (expired_baseline .id , baseline_ids )
649+ self .assertNotIn (current_baseline .id , baseline_ids ) # hadn't started yet
650+ self .assertNotIn (future_baseline .id , baseline_ids )
651+
652+ # Test with a future date when future_baseline will be valid
653+ future_date = today + datetime .timedelta (days = 60 )
654+ response = self .client .get (self .url , {"as_of_date" : future_date .isoformat ()})
655+ self .assertEqual (response .status_code , 200 )
656+ baseline_ids = [b ["id" ] for b in response .json ()]
657+ self .assertNotIn (expired_baseline .id , baseline_ids )
658+ self .assertIn (current_baseline .id , baseline_ids )
659+ self .assertIn (future_baseline .id , baseline_ids )
660+
661+ # Baseline with null valid_from_date (valid from the beginning of time)
662+ baseline_no_from = LivelihoodZoneBaselineFactory (
663+ valid_from_date = None ,
664+ valid_to_date = today + datetime .timedelta (days = 365 ),
665+ )
666+ # Baseline with null valid_to_date (valid indefinitely)
667+ baseline_no_to = LivelihoodZoneBaselineFactory (
668+ valid_from_date = today - datetime .timedelta (days = 365 ),
669+ valid_to_date = None ,
670+ )
671+ # Baseline with both dates null (always valid)
672+ baseline_no_dates = LivelihoodZoneBaselineFactory (
673+ valid_from_date = None ,
674+ valid_to_date = None ,
675+ )
676+ # Test default behavior - all three should be returned
677+ response = self .client .get (self .url )
678+ self .assertEqual (response .status_code , 200 )
679+ baseline_ids = [b ["id" ] for b in response .json ()]
680+ self .assertIn (baseline_no_from .id , baseline_ids )
681+ self .assertIn (baseline_no_to .id , baseline_ids )
682+ self .assertIn (baseline_no_dates .id , baseline_ids )
683+
602684 def test_conditional_request_headers (self ):
603685 cache .clear () # Clear cache to ensure clean state
604686
@@ -682,7 +764,8 @@ def test_search_with_product(self):
682764 # Apply the filters to the baseline
683765 baseline_url = reverse ("livelihoodzonebaseline-list" )
684766 response = self .client .get (
685- baseline_url , {search_data ["products" ][0 ]["filter" ]: search_data ["products" ][0 ]["value" ]}
767+ baseline_url ,
768+ {search_data ["products" ][0 ]["filter" ]: search_data ["products" ][0 ]["value" ]},
686769 )
687770 self .assertEqual (response .status_code , 200 )
688771 self .assertEqual (len (json .loads (response .content )), 2 )
@@ -691,7 +774,10 @@ def test_search_with_product(self):
691774 self .assertTrue (any (d ["name" ] == self .baseline3 .name for d in data ))
692775 self .assertFalse (any (d ["name" ] == self .baseline2 .name for d in data ))
693776
694- response = self .client .get (baseline_url , {search_data ["items" ][0 ]["filter" ]: search_data ["items" ][0 ]["value" ]})
777+ response = self .client .get (
778+ baseline_url ,
779+ {search_data ["items" ][0 ]["filter" ]: search_data ["items" ][0 ]["value" ]},
780+ )
695781 self .assertEqual (response .status_code , 200 )
696782 self .assertEqual (len (json .loads (response .content )), 1 )
697783 data = json .loads (response .content )
0 commit comments