| 900 | class DatePartTests(TestCase): |
| 901 | |
| 902 | def setUp(self): |
| 903 | self.same_month_day = Experiment.objects.create( |
| 904 | name='same_month_day_experiment', |
| 905 | assigned=datetime.date(2014, 10, 15), |
| 906 | completed=datetime.date(2015, 10, 15), |
| 907 | estimated_time=datetime.timedelta(days=1), |
| 908 | start=datetime.datetime.now(), |
| 909 | end=datetime.datetime.now(), |
| 910 | ) |
| 911 | |
| 912 | self.same_month = Experiment.objects.create( |
| 913 | name='same_month_experiment', |
| 914 | assigned=datetime.date(2014, 10, 1), |
| 915 | completed=datetime.date(2015, 10, 12), |
| 916 | estimated_time=datetime.timedelta(days=1), |
| 917 | start=datetime.datetime.now(), |
| 918 | end=datetime.datetime.now(), |
| 919 | ) |
| 920 | |
| 921 | self.same_day = Experiment.objects.create( |
| 922 | name='same_day_experiment', |
| 923 | assigned=datetime.date(2013, 7, 15), |
| 924 | completed=datetime.date(2015, 10, 15), |
| 925 | estimated_time=datetime.timedelta(days=1), |
| 926 | start=datetime.datetime.now(), |
| 927 | end=datetime.datetime.now(), |
| 928 | ) |
| 929 | |
| 930 | def test_datepart(self): |
| 931 | same_day_experiments = Experiment.objects.filter(assigned__day=DatePart('completed', 'day')) |
| 932 | |
| 933 | self.assertEqual(same_day_experiments.count(), 2) |
| 934 | self.assertIn(self.same_day, same_day_experiments) |
| 935 | self.assertIn(self.same_month_day, same_day_experiments) |
| 936 | |
| 937 | same_month_experiments = Experiment.objects.filter(assigned__month=DatePart('completed', 'month')) |
| 938 | |
| 939 | self.assertEqual(same_month_experiments.count(), 2) |
| 940 | self.assertIn(self.same_month, same_month_experiments) |
| 941 | self.assertIn(self.same_month_day, same_month_experiments) |
| 942 | |
| 943 | same_month_day_experiment = Experiment.objects.get( |
| 944 | assigned__day=DatePart('completed', 'day'), |
| 945 | assigned__month=DatePart('completed', 'month'), |
| 946 | ) |
| 947 | |
| 948 | self.assertEqual(self.same_month_day, same_month_day_experiment) |
| 949 | |
| 950 | |
| 951 | class DateTimePartTests(TestCase): |
| 952 | |
| 953 | def setUp(self): |
| 954 | self.same_month_day = Experiment.objects.create( |
| 955 | name='same_month_day_experiment', |
| 956 | assigned=datetime.date.today(), |
| 957 | completed=datetime.date.today(), |
| 958 | estimated_time=datetime.timedelta(days=1), |
| 959 | start=datetime.datetime(2014, 7, 4, 12, 33, 15), |
| 960 | end=datetime.datetime(2015, 7, 4, 10, 13, 12), |
| 961 | ) |
| 962 | |
| 963 | self.same_month = Experiment.objects.create( |
| 964 | name='same_month_experiment', |
| 965 | assigned=datetime.date.today(), |
| 966 | completed=datetime.date.today(), |
| 967 | estimated_time=datetime.timedelta(days=1), |
| 968 | start=datetime.datetime(2014, 6, 12, 7, 33, 15), |
| 969 | end=datetime.datetime(2015, 6, 5, 11, 13, 12), |
| 970 | ) |
| 971 | |
| 972 | self.same_day = Experiment.objects.create( |
| 973 | name='same_day_experiment', |
| 974 | assigned=datetime.date.today(), |
| 975 | completed=datetime.date.today(), |
| 976 | estimated_time=datetime.timedelta(days=1), |
| 977 | start=datetime.datetime(2014, 10, 6, 4, 33, 15), |
| 978 | end=datetime.datetime(2015, 3, 6, 1, 13, 12), |
| 979 | ) |
| 980 | |
| 981 | def test_datetimepart(self): |
| 982 | same_day_experiments = Experiment.objects.filter(start__day=DateTimePart('end', 'day')) |
| 983 | |
| 984 | self.assertEqual(same_day_experiments.count(), 2) |
| 985 | self.assertIn(self.same_day, same_day_experiments) |
| 986 | self.assertIn(self.same_month_day, same_day_experiments) |
| 987 | |
| 988 | same_month_experiments = Experiment.objects.filter(start__month=DateTimePart('end', 'month')) |
| 989 | |
| 990 | self.assertEqual(same_month_experiments.count(), 2) |
| 991 | self.assertIn(self.same_month, same_month_experiments) |
| 992 | self.assertIn(self.same_month_day, same_month_experiments) |
| 993 | |
| 994 | same_month_day_experiment = Experiment.objects.get( |
| 995 | start__day=DatePart('end', 'day'), |
| 996 | start__month=DatePart('end', 'month'), |
| 997 | ) |
| 998 | |
| 999 | self.assertEqual(self.same_month_day, same_month_day_experiment) |
| 1000 | |
| 1001 | |