|
5 | 5 | import dns.version |
6 | 6 | import pymongo |
7 | 7 | import pytest |
| 8 | +from bson import BSON |
| 9 | +from dateutil import tz as dateutil_tz |
8 | 10 | from kombu.exceptions import EncodeError |
9 | 11 |
|
10 | 12 | try: |
@@ -712,6 +714,171 @@ def now_func(): |
712 | 714 | self.backend.cleanup() |
713 | 715 | mock_collection.delete_many.assert_not_called() |
714 | 716 |
|
| 717 | + @pytest.mark.parametrize('tz_factory', [ |
| 718 | + pytest.param(ZoneInfo, id='zoneinfo'), |
| 719 | + pytest.param(dateutil_tz.gettz, id='dateutil'), |
| 720 | + ]) |
| 721 | + @pytest.mark.parametrize('zone,now,expires,expected_cutoff', [ |
| 722 | + pytest.param( |
| 723 | + 'America/New_York', datetime.datetime(2026, 11, 1, 1, 30, fold=0), 30, |
| 724 | + datetime.datetime(2026, 11, 1, 5, 29, 30), |
| 725 | + id='fall-back-first-hour', |
| 726 | + ), |
| 727 | + pytest.param( |
| 728 | + 'America/New_York', datetime.datetime(2026, 11, 1, 1, 30, fold=1), 30, |
| 729 | + datetime.datetime(2026, 11, 1, 6, 29, 30), |
| 730 | + id='fall-back-repeated-hour', |
| 731 | + ), |
| 732 | + pytest.param( |
| 733 | + 'America/New_York', datetime.datetime(2026, 11, 1, 1, 30, microsecond=125000, fold=1), 30.125, |
| 734 | + datetime.datetime(2026, 11, 1, 6, 29, 30), |
| 735 | + id='fractional-expiration-during-fall-back', |
| 736 | + ), |
| 737 | + pytest.param( |
| 738 | + 'America/New_York', datetime.datetime(2026, 11, 1, 1, 30, microsecond=125000, fold=1), |
| 739 | + datetime.timedelta(seconds=30.125), |
| 740 | + datetime.datetime(2026, 11, 1, 6, 29, 30), |
| 741 | + id='timedelta-expiration-during-fall-back', |
| 742 | + ), |
| 743 | + pytest.param( |
| 744 | + 'America/New_York', datetime.datetime(2026, 11, 1, 2, 30), 2 * 60 * 60, |
| 745 | + datetime.datetime(2026, 11, 1, 5, 30), |
| 746 | + id='across-fall-back', |
| 747 | + ), |
| 748 | + pytest.param( |
| 749 | + 'America/New_York', datetime.datetime(2026, 3, 8, 3, 30), 2 * 60 * 60, |
| 750 | + datetime.datetime(2026, 3, 8, 5, 30), |
| 751 | + id='across-spring-forward', |
| 752 | + ), |
| 753 | + pytest.param( |
| 754 | + 'America/New_York', datetime.datetime(2026, 3, 9, 0, 30), 24 * 60 * 60, |
| 755 | + datetime.datetime(2026, 3, 8, 4, 30), |
| 756 | + id='elapsed-day-spring-forward', |
| 757 | + ), |
| 758 | + pytest.param( |
| 759 | + 'America/New_York', datetime.datetime(2026, 11, 2, 0, 30), 24 * 60 * 60, |
| 760 | + datetime.datetime(2026, 11, 1, 5, 30), |
| 761 | + id='elapsed-day-fall-back', |
| 762 | + ), |
| 763 | + pytest.param( |
| 764 | + 'Australia/Lord_Howe', datetime.datetime(2026, 4, 5, 1, 45, fold=1), 30, |
| 765 | + datetime.datetime(2026, 4, 4, 15, 14, 30), |
| 766 | + id='half-hour-fall-back', |
| 767 | + ), |
| 768 | + pytest.param( |
| 769 | + 'Australia/Lord_Howe', datetime.datetime(2026, 10, 4, 2, 45), 60 * 60, |
| 770 | + datetime.datetime(2026, 10, 3, 14, 45), |
| 771 | + id='half-hour-spring-forward', |
| 772 | + ), |
| 773 | + pytest.param( |
| 774 | + 'America/Havana', datetime.datetime(2026, 3, 8, 1, 30), 60 * 60, |
| 775 | + datetime.datetime(2026, 3, 8, 4, 30), |
| 776 | + id='midnight-spring-forward', |
| 777 | + ), |
| 778 | + pytest.param( |
| 779 | + 'America/Havana', datetime.datetime(2026, 11, 1, 0, 30, fold=1), 30, |
| 780 | + datetime.datetime(2026, 11, 1, 5, 29, 30), |
| 781 | + id='repeated-midnight', |
| 782 | + ), |
| 783 | + pytest.param( |
| 784 | + 'Asia/Kathmandu', datetime.datetime(1986, 1, 1, 0, 30), 60 * 60, |
| 785 | + datetime.datetime(1985, 12, 31, 17, 45), |
| 786 | + id='quarter-hour-midnight-gap', |
| 787 | + ), |
| 788 | + pytest.param( |
| 789 | + 'Pacific/Apia', datetime.datetime(2011, 12, 31, 0, 30), 24 * 60 * 60, |
| 790 | + datetime.datetime(2011, 12, 29, 10, 30), |
| 791 | + id='elapsed-day-skipped-date', |
| 792 | + ), |
| 793 | + pytest.param( |
| 794 | + 'America/New_York', datetime.datetime(2026, 6, 15, 12), 30, |
| 795 | + datetime.datetime(2026, 6, 15, 15, 59, 30), |
| 796 | + id='away-from-transitions', |
| 797 | + ), |
| 798 | + ]) |
| 799 | + def test_cleanup_expiration_cutoff_across_timezones(self, tz_factory, zone, now, expires, expected_cutoff): |
| 800 | + self.app.conf.timezone = tz_factory(zone) |
| 801 | + self.app.conf.result_expires = expires |
| 802 | + self.backend = MongoBackend(app=self.app, url=self.default_url) |
| 803 | + self.backend.app.now = Mock(return_value=now.replace(tzinfo=self.app.timezone)) |
| 804 | + self.backend.collection = Mock() |
| 805 | + self.backend.group_collection = Mock() |
| 806 | + |
| 807 | + self.backend.cleanup() |
| 808 | + |
| 809 | + # BSON converts aware datetimes to UTC, matching MongoDB comparisons. |
| 810 | + for collection in (self.backend.collection, self.backend.group_collection): |
| 811 | + collection.delete_many.assert_called_once() |
| 812 | + query = collection.delete_many.call_args[0][0] |
| 813 | + assert BSON.encode(query).decode() == {'date_done': {'$lt': expected_cutoff}} |
| 814 | + |
| 815 | + @pytest.mark.parametrize('now,expires,expected_cutoff', [ |
| 816 | + pytest.param( |
| 817 | + datetime.datetime(2026, 11, 1, 6, 30, tzinfo=datetime.timezone.utc), |
| 818 | + 30, |
| 819 | + datetime.datetime(2026, 11, 1, 6, 29, 30), |
| 820 | + id='utc', |
| 821 | + ), |
| 822 | + pytest.param( |
| 823 | + datetime.datetime(2026, 11, 1, 6, 30), |
| 824 | + 30, |
| 825 | + datetime.datetime(2026, 11, 1, 6, 29, 30), |
| 826 | + id='naive-utc', |
| 827 | + ), |
| 828 | + pytest.param( |
| 829 | + datetime.datetime(2026, 11, 1, 12, tzinfo=datetime.timezone(datetime.timedelta(hours=5, minutes=30))), |
| 830 | + 30, |
| 831 | + datetime.datetime(2026, 11, 1, 6, 29, 30), |
| 832 | + id='fixed-offset', |
| 833 | + ), |
| 834 | + pytest.param( |
| 835 | + datetime.datetime(2026, 11, 1, 6, 30, microsecond=125000, tzinfo=datetime.timezone.utc), |
| 836 | + 0.125, |
| 837 | + datetime.datetime(2026, 11, 1, 6, 30), |
| 838 | + id='fractional-seconds', |
| 839 | + ), |
| 840 | + pytest.param( |
| 841 | + datetime.datetime(2026, 11, 1, 6, 30, microsecond=1001, tzinfo=datetime.timezone.utc), |
| 842 | + 0.0005, |
| 843 | + datetime.datetime(2026, 11, 1, 6, 30), |
| 844 | + id='bson-millisecond-precision', |
| 845 | + ), |
| 846 | + pytest.param( |
| 847 | + datetime.datetime(2026, 11, 1, 6, 30, tzinfo=datetime.timezone.utc), |
| 848 | + -30, |
| 849 | + datetime.datetime(2026, 11, 1, 6, 30, 30), |
| 850 | + id='negative-expiration', |
| 851 | + ), |
| 852 | + ]) |
| 853 | + def test_cleanup_expiration_cutoff_without_dst(self, now, expires, expected_cutoff): |
| 854 | + self.app.conf.result_expires = expires |
| 855 | + self.backend = MongoBackend(app=self.app, url=self.default_url) |
| 856 | + self.backend.app.now = Mock(return_value=now) |
| 857 | + self.backend.collection = Mock() |
| 858 | + self.backend.group_collection = Mock() |
| 859 | + |
| 860 | + self.backend.cleanup() |
| 861 | + |
| 862 | + # BSON converts aware datetimes to UTC, matching MongoDB comparisons. |
| 863 | + for collection in (self.backend.collection, self.backend.group_collection): |
| 864 | + collection.delete_many.assert_called_once() |
| 865 | + query = collection.delete_many.call_args[0][0] |
| 866 | + assert BSON.encode(query).decode() == {'date_done': {'$lt': expected_cutoff}} |
| 867 | + |
| 868 | + @pytest.mark.parametrize('expires', [None, 0]) |
| 869 | + def test_cleanup_without_expiration(self, expires): |
| 870 | + self.app.conf.result_expires = expires |
| 871 | + self.backend = MongoBackend(app=self.app, url=self.default_url) |
| 872 | + self.backend.app.now = Mock() |
| 873 | + self.backend.collection = Mock() |
| 874 | + self.backend.group_collection = Mock() |
| 875 | + |
| 876 | + self.backend.cleanup() |
| 877 | + |
| 878 | + self.backend.app.now.assert_not_called() |
| 879 | + self.backend.collection.delete_many.assert_not_called() |
| 880 | + self.backend.group_collection.delete_many.assert_not_called() |
| 881 | + |
715 | 882 | def test_prepare_client_options(self): |
716 | 883 | with patch('pymongo.version_tuple', new=(3, 0, 3)): |
717 | 884 | options = self.backend._prepare_client_options() |
|
0 commit comments