<commit_before>from unittest import TestCase
from hamcrest import assert_that, is_, instance_of
from backdrop.read.api import validate_request_args


class TestRequestValidation(TestCase):
    def test_reject_invalid_start_at(self):
        assert_that(
            validate_request_args({'start_at': 'i am not a time'}).is_valid,
            is_(False)
        )

    def test_accepts_valid_start_at(self):
        validation_result = validate_request_args({
            'start_at': '2000-02-02T00:02:02+00:00'
        })
        assert_that(validation_result.is_valid, is_(True))

    def test_reject_invalid_end_at(self):
        assert_that(
            validate_request_args({'end_at': 'foo'}).is_valid,
            is_(False)
        )

    def test_accepts_valid_end_at(self):
        validation_result = validate_request_args({
            'end_at': '2000-02-02T00:02:02+00:00'
        })
        assert_that(validation_result.is_valid, is_(True))

    def test_reject_filter_with_no_colon(self):
        assert_that(
            validate_request_args({'filter_by': 'bar'}).is_valid,
            is_(False)
        )

    def test_accepts_valid_filter(self):
        validation_result = validate_request_args({
            'filter_by': 'foo:bar'
        })
        assert_that(validation_result.is_valid, is_(True))

    def test_accepts_period_with_start_at_and_end_at_present(self):
        validation_result = validate_request_args({
            'period': 'week',
            'start_at': '2010-01-01T00:10:10+00:00',
            'end_at': '2010-01-07T00:10:10+00:00',
        })
        assert_that( validation_result.is_valid, is_(True) )

    def test_rejects_group_by_on_internal_field(self):
        validation_result = validate_request_args({
            "group_by": "_internal"
        })
        assert_that( validation_result.is_valid, is_(False))

    def test_accepts_ascending_sort_order(self):
        validation_result = validate_request_args({
            'sort_by': 'foo:ascending',
        })
        assert_that( validation_result.is_valid, is_(True) )

    def test_accepts_descending_sort_order(self):
        validation_result = validate_request_args({
            'sort_by': 'foo:descending',
        })
        assert_that( validation_result.is_valid, is_(True) )

    def test_rejects_unknown_sort_order(self):
        validation_result = validate_request_args({
            'sort_by': 'foo:random',
        })
        assert_that( validation_result.is_valid, is_(False) )

    def test_accepts_valid_limit(self):
        validation_result = validate_request_args({
            'limit': '3'
        })
        assert_that( validation_result.is_valid, is_(True) )

    def test_rejects_non_integer_limit(self):
        validation_result = validate_request_args({
            'limit': 'not_a_number'
        })
        assert_that( validation_result.is_valid, is_(False) )

    def test_rejects_negative_limit(self):
        validation_result = validate_request_args({
            'limit': '-3'
        })
        assert_that( validation_result.is_valid, is_(False) )

    def test_rejects_sort_being_provided_with_period_query(self):
        validation_result = validate_request_args({
            "sort_by": "foo:ascending",
            "period": "week"
        })
        assert_that( validation_result.is_valid, is_(False) )

    def test_accepts_sort_with_grouped_period_query(self):
        validation_result = validate_request_args({
            "sort_by": "foo:ascending",
            "period": "week",
            "group_by": "foo"
        })
        assert_that( validation_result.is_valid, is_(True) )

    def test_unrecognised_parameters_are_not_allowed(self):
        validation_result = validate_request_args({
            "unrecognised_parameter": "value"
        })
        assert_that( validation_result.is_valid, is_(False) )

    def test_accepts_collect_with_a_grouping(self):
        validation_result_with_group_by = validate_request_args({
            "collect": 'foo',
            "group_by": 'bar'
        })
        assert_that(validation_result_with_group_by.is_valid, is_(True))

    def test_rejects_collect_when_there_is_not_grouping(self):
        validation_result_without_group_by = validate_request_args({
            "collect": 'foo'
        })
        assert_that(validation_result_without_group_by.is_valid, is_(False))

    def test_accepts_collect_when_is_valid(self):
        validation_result_without_group_by = validate_request_args({
            "group_by": 'bar',
            "collect": '_-aAbBzZ-_'
        })
        assert_that(validation_result_without_group_by.is_valid, is_(True))

    def test_rejects_collect_when_is_not_valid(self):
        validation_result_without_group_by = validate_request_args({
            "group_by": 'bar',
            "collect": '$foo'
        })
        assert_that(validation_result_without_group_by.is_valid, is_(False))

    def test_rejects_collect_when_any_is_not_valid(self):
        validation_result_without_group_by = validate_request_args({
            "group_by": 'bar',
            "collect": 'foo',
            "collect": '$foo'
        })
        assert_that(validation_result_without_group_by.is_valid, is_(False))<commit_msg>MultiDict is required instead of dict<commit_after>from unittest import TestCase
from hamcrest import assert_that, is_, instance_of
from backdrop.read.api import validate_request_args
from werkzeug.datastructures import MultiDict


class TestRequestValidation(TestCase):
    def test_reject_invalid_start_at(self):
        assert_that(
            validate_request_args({'start_at': 'i am not a time'}).is_valid,
            is_(False)
        )

    def test_accepts_valid_start_at(self):
        validation_result = validate_request_args({
            'start_at': '2000-02-02T00:02:02+00:00'
        })
        assert_that(validation_result.is_valid, is_(True))

    def test_reject_invalid_end_at(self):
        assert_that(
            validate_request_args({'end_at': 'foo'}).is_valid,
            is_(False)
        )

    def test_accepts_valid_end_at(self):
        validation_result = validate_request_args({
            'end_at': '2000-02-02T00:02:02+00:00'
        })
        assert_that(validation_result.is_valid, is_(True))

    def test_reject_filter_with_no_colon(self):
        assert_that(
            validate_request_args({'filter_by': 'bar'}).is_valid,
            is_(False)
        )

    def test_accepts_valid_filter(self):
        validation_result = validate_request_args({
            'filter_by': 'foo:bar'
        })
        assert_that(validation_result.is_valid, is_(True))

    def test_accepts_period_with_start_at_and_end_at_present(self):
        validation_result = validate_request_args({
            'period': 'week',
            'start_at': '2010-01-01T00:10:10+00:00',
            'end_at': '2010-01-07T00:10:10+00:00',
        })
        assert_that( validation_result.is_valid, is_(True) )

    def test_rejects_group_by_on_internal_field(self):
        validation_result = validate_request_args({
            "group_by": "_internal"
        })
        assert_that( validation_result.is_valid, is_(False))

    def test_accepts_ascending_sort_order(self):
        validation_result = validate_request_args({
            'sort_by': 'foo:ascending',
        })
        assert_that( validation_result.is_valid, is_(True) )

    def test_accepts_descending_sort_order(self):
        validation_result = validate_request_args({
            'sort_by': 'foo:descending',
        })
        assert_that( validation_result.is_valid, is_(True) )

    def test_rejects_unknown_sort_order(self):
        validation_result = validate_request_args({
            'sort_by': 'foo:random',
        })
        assert_that( validation_result.is_valid, is_(False) )

    def test_accepts_valid_limit(self):
        validation_result = validate_request_args({
            'limit': '3'
        })
        assert_that( validation_result.is_valid, is_(True) )

    def test_rejects_non_integer_limit(self):
        validation_result = validate_request_args({
            'limit': 'not_a_number'
        })
        assert_that( validation_result.is_valid, is_(False) )

    def test_rejects_negative_limit(self):
        validation_result = validate_request_args({
            'limit': '-3'
        })
        assert_that( validation_result.is_valid, is_(False) )

    def test_rejects_sort_being_provided_with_period_query(self):
        validation_result = validate_request_args({
            "sort_by": "foo:ascending",
            "period": "week"
        })
        assert_that( validation_result.is_valid, is_(False) )

    def test_accepts_sort_with_grouped_period_query(self):
        validation_result = validate_request_args({
            "sort_by": "foo:ascending",
            "period": "week",
            "group_by": "foo"
        })
        assert_that( validation_result.is_valid, is_(True) )

    def test_unrecognised_parameters_are_not_allowed(self):
        validation_result = validate_request_args({
            "unrecognised_parameter": "value"
        })
        assert_that( validation_result.is_valid, is_(False) )

    def test_accepts_collect_with_a_grouping(self):
        validation_result_with_group_by = validate_request_args({
            "collect": 'foo',
            "group_by": 'bar'
        })
        assert_that(validation_result_with_group_by.is_valid, is_(True))

    def test_rejects_collect_when_there_is_not_grouping(self):
        validation_result_without_group_by = validate_request_args({
            "collect": 'foo'
        })
        assert_that(validation_result_without_group_by.is_valid, is_(False))

    def test_accepts_collect_when_is_valid(self):
        validation_result_without_group_by = validate_request_args({
            "group_by": 'bar',
            "collect": '_-aAbBzZ-_'
        })
        assert_that(validation_result_without_group_by.is_valid, is_(True))

    def test_rejects_collect_when_is_not_valid(self):
        validation_result_without_group_by = validate_request_args({
            "group_by": 'bar',
            "collect": '$foo'
        })
        assert_that(validation_result_without_group_by.is_valid, is_(False))

    def test_rejects_collect_when_any_is_not_valid(self):
        validation_result_without_group_by = validate_request_args(MultiDict([
            ("group_by", 'bar'),
            ("collect", '$foo'),
            ("collect", 'foo')
        ]))
        assert_that(validation_result_without_group_by.is_valid, is_(False))
{'fraction_non_alphanumeric': 0.09974521090874776, 'fraction_numerical': 0.013966216853826555, 'mean_word_length': 2.9353880430746377, 'pattern_counts': {'":': 31, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3841698', 'n_tokens_mistral': 3368, 'n_tokens_neox': 3242, 'n_words': 450}