def test_datetime_as_str_value(datetime_format: str):
    now = datetime.now()
    value = now.strftime(datetime_format)
    result = UrlParamsSchema().load({'field__date': value})

    date_string = now.strftime(datetime_format)
    compare_to = datetime.strptime(date_string, datetime_format)
    assert result['filters'][0]['value'] == compare_to
def test_not_allowed_field(map_allowed_sort_field: Callable):
    fields = ['field0', '-field1', 'field2', '-field3']
    test_data = {'sort': ','.join(fields)}

    sort_fields = map_allowed_sort_field(fields)[len(fields) - 1:]

    with pytest.raises(ValidationError):
        UrlParamsSchema(allowed_sort_fields=sort_fields) \
            .load(test_data.copy())
Пример #3
0
def test_default_limit():
    test_data = {'offset': 10}

    result = UrlParamsSchema().load(test_data.copy())

    assert 'page' in result

    assert 'limit' in result['page']
    assert result['page']['limit'] == UrlParamsSchema.MAX_LIMIT

    assert 'offset' in result['page']
    assert result['page']['offset'] == test_data['offset']
Пример #4
0
def test_default_offset():
    test_data = {'limit': 10}

    result = UrlParamsSchema().load(test_data.copy())

    assert 'page' in result

    assert 'limit' in result['page']
    assert result['page']['limit'] == test_data['limit']

    assert 'offset' in result['page']
    assert result['page']['offset'] == UrlParamsSchema.START_OFFSET
def test_wrong_field(map_allowed_sort_field: Callable):
    test_data = {'sort': 'field%$^&-'}

    sort_fields = map_allowed_sort_field(test_data['sort'])
    result = UrlParamsSchema(allowed_sort_fields=sort_fields) \
        .load(test_data.copy())

    assert 'sort' in result
    assert len(result['sort']) == 1

    assert 'field' in result['sort'][0]
    assert result['sort'][0]['field'] == test_data['sort']
def test_success_ask(map_allowed_sort_field: Callable):
    test_data = {'sort': 'field'}

    sort_fields = map_allowed_sort_field(test_data['sort'])
    result = UrlParamsSchema(allowed_sort_fields=sort_fields) \
        .load(test_data.copy())

    assert 'sort' in result
    assert len(result['sort']) == 1

    assert 'field' in result['sort'][0]
    assert result['sort'][0]['field'] == 'field'

    assert 'direction' in result['sort'][0]
    assert result['sort'][0]['direction'] == SortSchema.ASC
Пример #7
0
def test_success():
    test_data = {
        'limit': 10,
        'offset': 50,
    }

    result = UrlParamsSchema().load(test_data.copy())

    assert 'page' in result

    assert 'limit' in result['page']
    assert result['page']['limit'] == test_data['limit']

    assert 'offset' in result['page']
    assert result['page']['offset'] == test_data['offset']
Пример #8
0
def test_over_max_limit():
    test_data = {
        'limit': UrlParamsSchema.MAX_LIMIT + 100,
        'offset': 50,
    }

    result = UrlParamsSchema().load(test_data.copy())

    assert 'page' in result

    assert 'limit' in result['page']
    assert result['page']['limit'] == UrlParamsSchema.MAX_LIMIT

    assert 'offset' in result['page']
    assert result['page']['offset'] == test_data['offset']
def test_one_field():
    filter_name = 'filter_name'
    field_name = 'field_name'
    field = f'{field_name}__{filter_name}'
    value = 'value'

    result = UrlParamsSchema().load({field: value})

    assert 'filters' in result
    assert len(result['filters']) == 1

    assert 'field' in result['filters'][0]
    assert result['filters'][0]['field'] == field_name

    assert 'value' in result['filters'][0]
    assert result['filters'][0]['value'] == value

    assert 'value' in result['filters'][0]
    assert result['filters'][0]['filter'] == filter_name
def test_success_many(map_allowed_sort_field: Callable):
    fields = ['field0', '-field1', 'field2', '-field3']
    test_data = {'sort': ','.join(fields)}

    sort_fields = map_allowed_sort_field(fields)
    result = UrlParamsSchema(allowed_sort_fields=sort_fields) \
        .load(test_data.copy())

    assert 'sort' in result
    count = len(result['sort'])
    assert len(result['sort']) == count

    for i in range(count):
        assert 'field' in result['sort'][i]
        assert result['sort'][i]['field'] == f'field{i}'

        assert 'direction' in result['sort'][0]
        msg = f'Direction of field{i} is wrong'
        direction = SortSchema.ASC if i + 1 % 2 else SortSchema.DESC
        assert result['sort'][0]['direction'] == direction, msg
def test_not_allowed_fields():
    fields = ['field0', '-field1', 'field2', '-field3']
    test_data = {'sort': ','.join(fields)}

    with pytest.raises(ValidationError):
        UrlParamsSchema().load(test_data.copy())
def test_range_filter_from_float_in_list():
    value = [-1, 2.2]
    result = UrlParamsSchema().load({'field__range': value})

    assert result['filters'][0]['value'] == value
Пример #13
0
def test_bad_offset_data():
    with pytest.raises(ValidationError):
        UrlParamsSchema().load({'offset': 'bad offset'})
Пример #14
0
def test_bad_limit_data():
    with pytest.raises(ValidationError):
        UrlParamsSchema().load({'limit': 'bad limit'})
def test_none_filter():
    result = UrlParamsSchema().load({'field': None})

    assert result['filters'][0]['filter'] is None
def test_str_value():
    value = 'value'
    result = UrlParamsSchema().load({'field': value})

    assert result['filters'][0]['value'] == value
def test_range_filter_from_sting_in_list():
    with pytest.raises(ValidationError):
        UrlParamsSchema().load({'field__range': ','.join(['-1,', '2,2'])})
def test_dict_value():
    value = {'k': 'v'}
    result = UrlParamsSchema().load({'field': value})

    assert result['filters'][0]['value'] == value
def test_bool_value(value):
    result = UrlParamsSchema().load({'field__isnull': value})

    assert result['filters'][0]['value'] == bool(value)
def test_numeric_value(value):
    result = UrlParamsSchema().load({'field': value})

    assert result['filters'][0]['value'] == value
def test_time_value():
    value = datetime.now().time()
    result = UrlParamsSchema().load({'field': value})

    assert result['filters'][0]['value'] == value
def test_bad_datetime_format():
    value = datetime.now().strftime('wrong--%Y__%m__%d')
    with pytest.raises(ValidationError):
        UrlParamsSchema().load({'field__date': value})
def test_range_filter_from_sting():
    value = ['-1', '2.2']
    result = UrlParamsSchema().load({'field__range': ','.join(value)})

    assert result['filters'][0]['value'] == [-1., 2.2]