def test_build(self): tokenized_query = ExperimentQueryManager.tokenize(self.query1) parsed_query = ExperimentQueryManager.parse(tokenized_query) built_query = ExperimentQueryManager.build(parsed_query) assert built_query == { 'updated_at': [ QueryCondSpec(DateTimeCondition(op='<=', negation=False), params='2020-10-10') ], 'started_at': [ QueryCondSpec(DateTimeCondition(op='>', negation=False), params='2010-10-10'), QueryCondSpec(DateTimeCondition(op='=', negation=True), params='2016-10-01') ], } tokenized_query = ExperimentQueryManager.tokenize(self.query2) parsed_query = ExperimentQueryManager.parse(tokenized_query) built_query = ExperimentQueryManager.build(parsed_query) assert built_query == { 'metric__loss': [ QueryCondSpec(ComparisonCondition(op='<=', negation=False), params=0.8) ], 'status': [ QueryCondSpec(ValueCondition(op='|', negation=False), params=['starting', 'running']) ], } tokenized_query = ExperimentQueryManager.tokenize(self.query3) parsed_query = ExperimentQueryManager.parse(tokenized_query) built_query = ExperimentQueryManager.build(parsed_query) assert built_query == { 'finished_at': [ QueryCondSpec(DateTimeCondition(op='..', negation=False), params=['2012-12-12', '2042-12-12']) ], } tokenized_query = ExperimentQueryManager.tokenize(self.query4) parsed_query = ExperimentQueryManager.parse(tokenized_query) built_query = ExperimentQueryManager.build(parsed_query) assert built_query == { 'tags': [ QueryCondSpec(ArrayCondition(op='|', negation=True), params=['tag1', 'tag2']), QueryCondSpec(ArrayCondition(op='=', negation=False), params='tag3') ], }
def test_range_condition_init_with_correct_operator(self): range_cond = DateTimeCondition(op='range') assert range_cond.operator == DateTimeCondition._range_operator nrange_cond = DateTimeCondition(op='range', negation=True) assert nrange_cond.operator == DateTimeCondition._nrange_operator
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
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')))