Esempio n. 1
0
    def _range_operator(name: str, params: Any, query_backend: Any,
                        timezone: str) -> Any:
        assert len(params) == 2
        try:
            start_date = DateTimeFormatter.extract(params[0], timezone)
            end_date = DateTimeFormatter.extract(params[1], timezone)
        except PolyaxonDateTimeFormatterException as e:
            raise PQLException(e)

        name = "{}__range".format(name)
        return query_backend(**{name: (start_date, end_date)})
Esempio n. 2
0
    def test_range_operators(self):
        with self.assertRaises(AssertionError):
            DateTimeCondition._range_operator(
                "field", "value", query_backend=Q, timezone=settings.TIME_ZONE
            )

        with self.assertRaises(AssertionError):
            DateTimeCondition._nrange_operator(
                "field", "value", query_backend=Q, timezone=settings.TIME_ZONE
            )

        with self.assertRaises(PQLException):
            DateTimeCondition._range_operator(
                "field", ("v1", "v2"), query_backend=Q, timezone=settings.TIME_ZONE
            )

        with self.assertRaises(PQLException):
            DateTimeCondition._nrange_operator(
                "field",
                ("v1", "2010-01-01"),
                query_backend=Q,
                timezone=settings.TIME_ZONE,
            )

        with self.assertRaises(PQLException):
            DateTimeCondition._nrange_operator(
                "field",
                ("2010-01-01", "v2"),
                query_backend=Q,
                timezone=settings.TIME_ZONE,
            )

        assert DateTimeCondition._range_operator(
            "field",
            ("2010-01-01", "2010-01-01"),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        ) == Q(
            field__range=(
                DateTimeFormatter.extract("2010-01-01", settings.TIME_ZONE),
                DateTimeFormatter.extract("2010-01-01", settings.TIME_ZONE),
            )
        )
        assert DateTimeCondition._nrange_operator(
            "field",
            ("2010-01-01 10:10", "2010-01-01"),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        ) == ~Q(
            field__range=(
                DateTimeFormatter.extract("2010-01-01 10:10", settings.TIME_ZONE),
                DateTimeFormatter.extract("2010-01-01", settings.TIME_ZONE),
            )
        )
Esempio n. 3
0
 def test_extract_works_as_expected_for_given_datetime_string(self):
     result = DateTimeFormatter.extract("2012-12-24 16:17:18",
                                        timezone="Europe/Berlin")
     self.assertEqual(result.year, 2012)
     self.assertEqual(result.month, 12)
     self.assertEqual(result.day, 24)
     self.assertEqual(result.hour, 16)
     self.assertEqual(result.minute, 17)
     self.assertEqual(result.second, 18)
Esempio n. 4
0
 def test_extract_rejects_invalid_datetime(self):
     with self.assertRaises(PolyaxonDateTimeFormatterException):
         DateTimeFormatter.extract("foo", timezone="Europe/Berlin")
Esempio n. 5
0
 def test_extract_accepts_raises_for_none(self):
     with self.assertRaises(PolyaxonDateTimeFormatterException):
         DateTimeFormatter.extract(None, timezone="Europe/Berlin")
Esempio n. 6
0
 def test_extract_works_as_expected_for_given_date_string(self):
     result = DateTimeFormatter.extract("2012-12-24",
                                        timezone="Europe/Berlin")
     self.assertEqual(result.year, 2012)
     self.assertEqual(result.month, 12)
     self.assertEqual(result.day, 24)
Esempio n. 7
0
    def test_range_apply(self):
        # Delete current run
        self.run.delete()

        run = RunFactory(
            project=self.project,
            is_managed=False,
            outputs={
                "accuracy": 0.9,
                "precision": 0.9
            },
        )
        run.created_at = datetime.datetime(2018, 1, 1)
        run.save()
        run = RunFactory(
            project=self.project,
            is_managed=False,
            outputs={
                "accuracy": 0.9,
                "precision": 0.9
            },
        )
        run.created_at = datetime.datetime(2010, 1, 1)
        run.save()

        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=Run.objects,
            name="created_at",
            params="2018-01-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 1

        queryset = eq_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2018-02-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 0

        # lt
        queryset = lt_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2018-02-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 2

        queryset = lt_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2018-01-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 1

        queryset = lt_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2008-01-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 0

        # lte
        queryset = lte_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2018-02-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 2

        queryset = lte_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2018-01-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 2

        queryset = lte_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2008-01-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 0

        # gt
        queryset = gt_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2018-02-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 0

        queryset = gt_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2018-01-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 0

        queryset = gt_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2008-01-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 2

        # lte
        queryset = gte_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2018-02-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 0

        queryset = gte_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2018-01-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 1

        queryset = gte_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params="2008-01-01",
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 2

        # range
        queryset = range_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params=(
                DateTimeFormatter.extract("2018-02-01 00:00",
                                          settings.TIME_ZONE),
                DateTimeFormatter.extract("2018-03-01 00:00",
                                          settings.TIME_ZONE),
            ),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 0

        queryset = range_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params=(
                DateTimeFormatter.extract("2018-02-01", settings.TIME_ZONE),
                DateTimeFormatter.extract("2008-03-01", settings.TIME_ZONE),
            ),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 0

        queryset = range_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params=(
                DateTimeFormatter.extract("2017-02-01", settings.TIME_ZONE),
                DateTimeFormatter.extract("2018-03-01", settings.TIME_ZONE),
            ),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 1

        queryset = range_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params=(
                DateTimeFormatter.extract("2008-02-01 00:00:12",
                                          settings.TIME_ZONE),
                DateTimeFormatter.extract("2018-03-01", settings.TIME_ZONE),
            ),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 2

        queryset = range_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params=(
                DateTimeFormatter.extract("2010-01-01", settings.TIME_ZONE),
                DateTimeFormatter.extract("2010-01-01", settings.TIME_ZONE),
            ),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 0

        # nrange
        queryset = nrange_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params=(
                DateTimeFormatter.extract("2018-02-01 00:00",
                                          settings.TIME_ZONE),
                DateTimeFormatter.extract("2018-03-01 00:00",
                                          settings.TIME_ZONE),
            ),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 2

        queryset = nrange_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params=(
                DateTimeFormatter.extract("2018-02-01", settings.TIME_ZONE),
                DateTimeFormatter.extract("2008-03-01", settings.TIME_ZONE),
            ),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 2

        queryset = nrange_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params=(
                DateTimeFormatter.extract("2017-02-01", settings.TIME_ZONE),
                DateTimeFormatter.extract("2018-03-01", settings.TIME_ZONE),
            ),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 1

        queryset = nrange_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params=(
                DateTimeFormatter.extract("2008-02-01 00:00:12",
                                          settings.TIME_ZONE),
                DateTimeFormatter.extract("2018-03-01", settings.TIME_ZONE),
            ),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 0

        queryset = nrange_cond.apply(
            queryset=Run.objects,
            name="created_at",
            params=(
                DateTimeFormatter.extract("2010-01-01", settings.TIME_ZONE),
                DateTimeFormatter.extract("2010-01-01", settings.TIME_ZONE),
            ),
            query_backend=Q,
            timezone=settings.TIME_ZONE,
        )
        assert queryset.count() == 2