def test_comparison_operators(self): op = ComparisonCondition._lt_operator('field', 'value') assert op == Q(field__lt='value') op = ComparisonCondition._gt_operator('field', 'value') assert op == Q(field__gt='value') op = ComparisonCondition._lte_operator('field', 'value') assert op == Q(field__lte='value') op = ComparisonCondition._gte_operator('field', 'value') assert op == Q(field__gte='value')
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_comparison_condition_init_with_correct_operator(self): lt_cond = ComparisonCondition(op='lt') assert lt_cond.operator == ComparisonCondition._lt_operator nlt_cond = ComparisonCondition(op='lt', negation=True) assert nlt_cond.operator == ComparisonCondition._gte_operator gt_cond = ComparisonCondition(op='gt') assert gt_cond.operator == ComparisonCondition._gt_operator ngt_cond = ComparisonCondition(op='gt', negation=True) assert ngt_cond.operator == ComparisonCondition._lte_operator lte_cond = ComparisonCondition(op='lte') assert lte_cond.operator == ComparisonCondition._lte_operator nlte_cond = ComparisonCondition(op='lte', negation=True) assert nlte_cond.operator == ComparisonCondition._gt_operator gte_cond = ComparisonCondition(op='gte') assert gte_cond.operator == ComparisonCondition._gte_operator ngte_cond = ComparisonCondition(op='gte', negation=True) assert ngte_cond.operator == ComparisonCondition._lt_operator
def test_comparison_apply(self): ExperimentMetricFactory(values={'loss': 0.1, 'step': 1}) ExperimentMetricFactory(values={'loss': 0.3, 'step': 10}) ExperimentMetricFactory(values={'loss': 0.9, 'step': 100}) ExperimentFactory(declarations={'rate': 1, 'loss': 'foo'}) ExperimentFactory(declarations={'rate': -1, 'loss': 'bar'}) eq_cond = ComparisonCondition(op='eq') neq_cond = ComparisonCondition(op='eq', negation=True) lt_cond = ComparisonCondition(op='lt') lte_cond = ComparisonCondition(op='lte') gt_cond = ComparisonCondition(op='gt') gte_cond = ComparisonCondition(op='gte') # eq queryset = eq_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.1) assert queryset.count() == 1 queryset = eq_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.2) assert queryset.count() == 0 queryset = eq_cond.apply(queryset=Experiment.objects, name='last_metric__step', params=10) assert queryset.count() == 1 # neq must use the table directly queryset = neq_cond.apply(queryset=Experiment.objects, name='declarations__rate', params=1) assert queryset.count() == 4 queryset = neq_cond.apply(queryset=Experiment.objects, name='declarations__rate', params=-1) assert queryset.count() == 4 queryset = neq_cond.apply(queryset=Experiment.objects, name='declarations__rate', params=-12) assert queryset.count() == 5 queryset = neq_cond.apply(queryset=Experiment.objects, name='declarations__loss', params='foo') assert queryset.count() == 4 queryset = neq_cond.apply(queryset=Experiment.objects, name='declarations__loss', params='moo') assert queryset.count() == 5 # lt queryset = lt_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.1) assert queryset.count() == 0 queryset = lt_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.2) assert queryset.count() == 1 queryset = lt_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.9) assert queryset.count() == 2 # lte queryset = lte_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.1) assert queryset.count() == 1 queryset = lte_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.2) assert queryset.count() == 1 queryset = lte_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.9) assert queryset.count() == 3 # gt queryset = gt_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.1) assert queryset.count() == 2 queryset = gt_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.2) assert queryset.count() == 2 queryset = gt_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.9) assert queryset.count() == 0 # gte queryset = gte_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.1) assert queryset.count() == 3 queryset = gte_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.2) assert queryset.count() == 2 queryset = gte_cond.apply(queryset=Experiment.objects, name='last_metric__loss', params=0.9) assert queryset.count() == 1