@@ -596,50 +596,75 @@ def test_filter_by_wealth_characteristic(self):
596596 self .assertEqual (len (response .json ()), 1 )
597597
598598 def test_as_of_date_filter_returns_valid_baselines (self ):
599- # Test that the as_of_date filter returns valid baselines as of the specified date.
599+ """
600+ Test that the as_of_date filter returns only baselines valid as of the specified date.
601+ """
600602 today = datetime .date .today ()
603+
604+ # Baseline that expired in the past
601605 expired_baseline = LivelihoodZoneBaselineFactory (
602606 valid_from_date = today - datetime .timedelta (days = 365 ),
603607 valid_to_date = today - datetime .timedelta (days = 30 ),
604608 )
609+
610+ # Baseline currently valid
605611 current_baseline = LivelihoodZoneBaselineFactory (
606612 valid_from_date = today - datetime .timedelta (days = 30 ),
607613 valid_to_date = today + datetime .timedelta (days = 365 ),
608614 )
609- # Test default behavior (as of today) - should exclude expired_baseline
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
610623 response = self .client .get (self .url )
611624 self .assertEqual (response .status_code , 200 )
612625 baseline_ids = [b ["id" ] for b in response .json ()]
613626 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 )
614635 self .assertNotIn (expired_baseline .id , baseline_ids )
636+ self .assertNotIn (future_baseline .id , baseline_ids )
615637
616- # Test with a past date before the expired_baseline's valid_to_date
638+ # Test with a past date when expired_baseline was still valid
639+ # current_baseline hadn't started yet, so it should be excluded
617640 past_date = today - datetime .timedelta (days = 180 )
618641 response = self .client .get (self .url , {"as_of_date" : past_date .isoformat ()})
619642 self .assertEqual (response .status_code , 200 )
620643 baseline_ids = [b ["id" ] for b in response .json ()]
621644 self .assertIn (expired_baseline .id , baseline_ids )
622- self .assertIn (current_baseline .id , baseline_ids )
645+ self .assertNotIn (current_baseline .id , baseline_ids ) # hadn't started yet
646+ self .assertNotIn (future_baseline .id , baseline_ids )
623647
624- # Test with a future date - expired_baseline should still be excluded
648+ # Test with a future date when future_baseline will be valid
625649 future_date = today + datetime .timedelta (days = 60 )
626650 response = self .client .get (self .url , {"as_of_date" : future_date .isoformat ()})
627651 self .assertEqual (response .status_code , 200 )
628652 baseline_ids = [b ["id" ] for b in response .json ()]
629653 self .assertNotIn (expired_baseline .id , baseline_ids )
630654 self .assertIn (current_baseline .id , baseline_ids )
655+ self .assertIn (future_baseline .id , baseline_ids )
631656
632- # test as of date filter handles null dates
657+ # Baseline with null valid_from_date (valid from the beginning of time)
633658 baseline_no_from = LivelihoodZoneBaselineFactory (
634659 valid_from_date = None ,
635660 valid_to_date = today + datetime .timedelta (days = 365 ),
636661 )
637- # Create a baseline with null valid_to_date (valid indefinitely)
662+ # Baseline with null valid_to_date (valid indefinitely)
638663 baseline_no_to = LivelihoodZoneBaselineFactory (
639664 valid_from_date = today - datetime .timedelta (days = 365 ),
640665 valid_to_date = None ,
641666 )
642- # Create a baseline with both dates null (always valid)
667+ # Baseline with both dates null (always valid)
643668 baseline_no_dates = LivelihoodZoneBaselineFactory (
644669 valid_from_date = None ,
645670 valid_to_date = None ,
0 commit comments