def _range_operator(name, params): assert len(params) == 2 try: start_date = DateTimeFormatter.extract(params[0]) end_date = DateTimeFormatter.extract(params[1]) except DateTimeFormatterException as e: raise QueryConditionException(e) name = '{}__range'.format(name) return Q(**{name: (start_date, end_date)})
def test_range_operators(self): with self.assertRaises(AssertionError): DateTimeCondition._range_operator('field', 'value') with self.assertRaises(AssertionError): DateTimeCondition._nrange_operator('field', 'value') with self.assertRaises(QueryConditionException): DateTimeCondition._range_operator('field', ('v1', 'v2')) with self.assertRaises(QueryConditionException): DateTimeCondition._nrange_operator('field', ('v1', '2010-01-01')) with self.assertRaises(QueryConditionException): DateTimeCondition._nrange_operator('field', ('2010-01-01', 'v2')) assert DateTimeCondition._range_operator( 'field', ('2010-01-01', '2010-01-01')) == Q( field__range=(DateTimeFormatter.extract('2010-01-01'), DateTimeFormatter.extract('2010-01-01'))) assert DateTimeCondition._nrange_operator( 'field', ('2010-01-01 10:10', '2010-01-01')) == ~Q( field__range=(DateTimeFormatter.extract('2010-01-01 10:10'), DateTimeFormatter.extract('2010-01-01')))
def test_range_apply(self): ExperimentMetricFactory(created_at=datetime.datetime(2018, 1, 1)) ExperimentMetricFactory(created_at=datetime.datetime(2010, 1, 1)) eq_cond = DateTimeCondition(op='eq') lt_cond = DateTimeCondition(op='lt') lte_cond = DateTimeCondition(op='lte') gt_cond = DateTimeCondition(op='gt') gte_cond = DateTimeCondition(op='gte') range_cond = DateTimeCondition(op='range') nrange_cond = DateTimeCondition(op='range', negation=True) # eq queryset = eq_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2018-01-01') assert queryset.count() == 1 queryset = eq_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2018-02-01') assert queryset.count() == 0 # lt queryset = lt_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2018-02-01') assert queryset.count() == 2 queryset = lt_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2018-01-01') assert queryset.count() == 1 queryset = lt_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2008-01-01') assert queryset.count() == 0 # lte queryset = lte_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2018-02-01') assert queryset.count() == 2 queryset = lte_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2018-01-01') assert queryset.count() == 2 queryset = lte_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2008-01-01') assert queryset.count() == 0 # gt queryset = gt_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2018-02-01') assert queryset.count() == 0 queryset = gt_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2018-01-01') assert queryset.count() == 0 queryset = gt_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2008-01-01') assert queryset.count() == 2 # lte queryset = gte_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2018-02-01') assert queryset.count() == 0 queryset = gte_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2018-01-01') assert queryset.count() == 1 queryset = gte_cond.apply(queryset=ExperimentMetric.objects, name='created_at', params='2008-01-01') assert queryset.count() == 2 # range queryset = range_cond.apply( queryset=ExperimentMetric.objects, name='created_at', params=(DateTimeFormatter.extract('2018-02-01 00:00'), DateTimeFormatter.extract('2018-03-01 00:00'))) assert queryset.count() == 0 queryset = range_cond.apply( queryset=ExperimentMetric.objects, name='created_at', params=(DateTimeFormatter.extract('2018-02-01'), DateTimeFormatter.extract('2008-03-01'))) assert queryset.count() == 0 queryset = range_cond.apply( queryset=ExperimentMetric.objects, name='created_at', params=(DateTimeFormatter.extract('2017-02-01'), DateTimeFormatter.extract('2018-03-01'))) assert queryset.count() == 1 queryset = range_cond.apply( queryset=ExperimentMetric.objects, name='created_at', params=(DateTimeFormatter.extract('2008-02-01 00:00:12'), DateTimeFormatter.extract('2018-03-01'))) assert queryset.count() == 2 queryset = range_cond.apply( queryset=ExperimentMetric.objects, name='created_at', params=(DateTimeFormatter.extract('2010-01-01'), DateTimeFormatter.extract('2010-01-01'))) assert queryset.count() == 0 # nrange queryset = nrange_cond.apply( queryset=ExperimentMetric.objects, name='created_at', params=(DateTimeFormatter.extract('2018-02-01 00:00'), DateTimeFormatter.extract('2018-03-01 00:00'))) assert queryset.count() == 2 queryset = nrange_cond.apply( queryset=ExperimentMetric.objects, name='created_at', params=(DateTimeFormatter.extract('2018-02-01'), DateTimeFormatter.extract('2008-03-01'))) assert queryset.count() == 2 queryset = nrange_cond.apply( queryset=ExperimentMetric.objects, name='created_at', params=(DateTimeFormatter.extract('2017-02-01'), DateTimeFormatter.extract('2018-03-01'))) assert queryset.count() == 1 queryset = nrange_cond.apply( queryset=ExperimentMetric.objects, name='created_at', params=(DateTimeFormatter.extract('2008-02-01 00:00:12'), DateTimeFormatter.extract('2018-03-01'))) assert queryset.count() == 0 queryset = nrange_cond.apply( queryset=ExperimentMetric.objects, name='created_at', params=(DateTimeFormatter.extract('2010-01-01'), DateTimeFormatter.extract('2010-01-01'))) assert queryset.count() == 2