Ejemplo n.º 1
0
    def test_datetime_two_formats_supported(self):
        start_date = self.date.add(days=-1).to_date_string()
        start_datetime = format_datetime(self.date.add(days=-1))
        end_date = self.date.add(days=3).to_date_string()
        end_datetime = format_datetime(self.date.add(days=3))

        #: URL with datetime filters
        url_date_time = ('/api/v1.1/date-utc/?datetime__range='
                         f'{start_datetime},{end_datetime}')

        resp = self.client.get(url_date_time,
                               HTTP_AUTHORIZATION="Token {}".format(
                                   self.token))
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(resp.data['objects_count'], 4)

        #: URL with exclude datetime filters
        url_date_time = ('/api/v1.1/date-utc/?datetime__range!='
                         f'{start_datetime},{end_datetime}')

        resp = self.client.get(url_date_time,
                               HTTP_AUTHORIZATION="Token {}".format(
                                   self.token))
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(resp.data['objects_count'], 5)

        #: URL with date filters
        url_date = (
            f'/api/v1.1/date-utc/?datetime__range={start_date},{end_date}')

        resp = self.client.get(url_date,
                               HTTP_AUTHORIZATION="Token {}".format(
                                   self.token))
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
 def test_filter_range_date_and_datetime_fields(self):
     start_date = self.date.add(days=-1).to_date_string()
     start_datetime = format_datetime(self.date.add(days=-1))
     end_date = self.date.add(days=3).to_date_string()
     end_datetime = format_datetime(self.date.add(days=3))
     url_date = '/api/v1/date-utc/?date__range={},{}&datetime__range={},{}'.format(
         start_date, end_date, start_datetime, end_datetime)
     resp = self.client.get(url_date,
                            HTTP_AUTHORIZATION="Token {}".format(
                                self.token))
     self.assertEqual(resp.status_code, status.HTTP_200_OK)
def convert_type(string, field_type, close_period=True):
    if string == '':
        raise ValidationError({
            "message":
            "Attempting to convert an empty string to a date format"
        })
    if field_type in ('DateTimeField', 'DateField'):
        if field_type == 'DateTimeField':
            # Expected format YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]
            dt = ensure_pendulum(string)
            if close_period:
                dt.end_of('day')
            else:
                dt.start_of('day')
            return format_datetime(dt)
        if field_type == 'DateField':
            # Expected YYYY-MM-DD
            dt = ensure_pendulum(string)
            # Deactivated by lco, a date is a date, no time in it
            # if close_period:
            #     dt.end_of('day')
            return dt.to_date_string()
    elif field_type in ('DecimalField', 'FloatField'):
        return float(string)
    else:
        return int(string)
Ejemplo n.º 4
0
    def test_bad_formatted_range_values(self):
        start_date = self.date.add(days=-1).to_date_string()
        start_datetime = format_datetime(self.date.add(days=-1))
        end_date = self.date.add(days=3).to_date_string()

        #: URL with only one value
        url_date_time = f'/api/v1.1/date-utc/?datetime__range={start_datetime}'

        resp = self.client.get(url_date_time,
                               HTTP_AUTHORIZATION="Token {}".format(
                                   self.token))
        self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertDictEqual(
            resp.json(),
            {
                "message":
                "A comma is expected in the value of the filter. Expected values are '<date1>,<date2>', '<date1>,' or ',<date2>'"
            },
        )

        #: URL with 3 values (2 commas)
        url_date = (
            f'/api/v1.1/date-utc/?datetime__range={start_date},{end_date},')

        resp = self.client.get(url_date,
                               HTTP_AUTHORIZATION="Token {}".format(
                                   self.token))
        self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertDictEqual(
            resp.json(),
            {
                "message":
                f'Only two comma-separated values are expected, got 3: [\'{start_date}\', \'{end_date}\', \'\']'
            },
        )
Ejemplo n.º 5
0
 def test_parse_string(self):
     now = timezone.now()
     d1 = ensure_pendulum(str(now))
     d2 = ensure_pendulum(now)
     self.assertEqual(format_datetime(d1), format_datetime(d2))