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_apply(self): ExperimentFactory(declarations={'rate': 1, 'loss': 'foo'}) ExperimentFactory(declarations={'rate': -1, 'loss': 'bar'}) ExperimentFactory(declarations={'rate': 11.1, 'loss': 'moo'}) eq_cond = ValueCondition(op='eq') neq_cond = ValueCondition(op='eq', negation=True) in_cond = ValueCondition(op='in') nin_cond = ValueCondition(op='in', negation=True) # eq queryset = eq_cond.apply(queryset=Experiment.objects, name='declarations__loss', params='foo') assert queryset.count() == 1 queryset = eq_cond.apply(queryset=Experiment.objects, name='declarations__rate', params=0.2) assert queryset.count() == 0 queryset = eq_cond.apply(queryset=Experiment.objects, name='declarations__rate', params=11.1) assert queryset.count() == 1 # neq queryset = neq_cond.apply(queryset=Experiment.objects, name='declarations__loss', params='foo') assert queryset.count() == 2 queryset = neq_cond.apply(queryset=Experiment.objects, name='declarations__rate', params=0.2) assert queryset.count() == 3 queryset = neq_cond.apply(queryset=Experiment.objects, name='declarations__rate', params=11.1) assert queryset.count() == 2 # in queryset = in_cond.apply(queryset=Experiment.objects, name='declarations__loss', params=['foo', 'bar']) assert queryset.count() == 2 queryset = in_cond.apply(queryset=Experiment.objects, name='declarations__rate', params=[0.2, 11.1]) assert queryset.count() == 1 queryset = in_cond.apply(queryset=Experiment.objects, name='declarations__loss', params=['lll', 'ppp']) assert queryset.count() == 0 queryset = in_cond.apply(queryset=Experiment.objects, name='declarations__loss', params=['lll', 'ppp', 'foo', 'bar', 'moo']) assert queryset.count() == 3 # in queryset = nin_cond.apply(queryset=Experiment.objects, name='declarations__loss', params=['foo', 'bar']) assert queryset.count() == 1 queryset = nin_cond.apply(queryset=Experiment.objects, name='declarations__rate', params=[0.2, 11.1]) assert queryset.count() == 2 queryset = nin_cond.apply(queryset=Experiment.objects, name='declarations__loss', params=['lll', 'ppp']) assert queryset.count() == 3 queryset = nin_cond.apply(queryset=Experiment.objects, name='declarations__loss', params=['lll', 'ppp', 'foo', 'bar', 'moo']) assert queryset.count() == 0
def test_value_operators(self): op = ValueCondition._in_operator('field', ['v1', 'v2']) assert op == Q(field__in=['v1', 'v2']) op = ValueCondition._nin_operator('field', ['v1', 'v2']) assert op == ~Q(field__in=['v1', 'v2'])