Exemple #1
0
 def setUp(self):
     self.schema = BatchPayloadSchema()
Exemple #2
0
class BatchSchemaTest(unittest.TestCase):
    def setUp(self):
        self.schema = BatchPayloadSchema()

    def assertInvalid(self, payload):
        self.assertRaises(colander.Invalid, self.schema.deserialize, payload)

    def test_requests_is_mandatory(self):
        self.assertInvalid({})

    def test_raise_invalid_on_unknown_attributes(self):
        self.assertInvalid({"requests": [], "unknown": 42})

    def test_list_of_requests_can_be_empty(self):
        self.schema.deserialize({"requests": []})

    def test_list_of_requests_must_be_a_list(self):
        self.assertInvalid({"requests": {}})

    def test_list_of_requests_must_be_dicts(self):
        request = 42
        self.assertInvalid({"defaults": {"path": "/"}, "requests": [request]})

    def test_request_path_must_start_with_slash(self):
        request = {"path": "http://localhost"}
        self.assertInvalid({"requests": [request]})

    def test_request_path_is_mandatory(self):
        request = {"method": "HEAD"}
        self.assertInvalid({"requests": [request]})

    def test_request_method_must_be_known_uppercase_word(self):
        request = {"path": "/", "method": "get"}
        self.assertInvalid({"requests": [request]})

    def test_raise_invalid_on_request_unknown_attributes(self):
        request = {"path": "/", "method": "GET", "foo": 42}
        self.assertInvalid({"requests": [request]})

    #
    # headers
    #

    def test_request_headers_should_be_strings(self):
        headers = {"Accept": 3.14}
        request = {"path": "/", "headers": headers}
        self.assertInvalid({"requests": [request]})

    def test_request_headers_cannot_be_recursive(self):
        headers = {"Accept": {"sub": "dict"}}
        request = {"path": "/", "headers": headers}
        self.assertInvalid({"requests": [request]})

    def test_request_headers_are_preserved(self):
        headers = {"Accept": "audio/*"}
        request = {"path": "/", "headers": headers}
        deserialized = self.schema.deserialize({"requests": [request]})
        self.assertEqual(deserialized["requests"][0]["headers"]["Accept"],
                         "audio/*")

    #
    # body
    #

    def test_body_is_an_arbitrary_mapping(self):
        payload = {"json": "payload"}
        request = {"path": "/", "body": payload}
        deserialized = self.schema.deserialize({"requests": [request]})
        self.assertEqual(deserialized["requests"][0]["body"], payload)

    #
    # defaults
    #

    def test_defaults_must_be_a_mapping_if_specified(self):
        request = {"path": "/"}
        batch_payload = {"requests": [request], "defaults": 42}
        self.assertInvalid(batch_payload)

    def test_defaults_must_be_a_request_schema_if_specified(self):
        request = {"path": "/"}
        defaults = {"body": 3}
        batch_payload = {"requests": [request], "defaults": defaults}
        self.assertInvalid(batch_payload)

    def test_raise_invalid_on_default_unknown_attributes(self):
        request = {"path": "/"}
        defaults = {"foo": "bar"}
        self.assertInvalid({"requests": [request], "defaults": defaults})

    def test_defaults_can_be_specified_empty(self):
        request = {"path": "/"}
        defaults = {}
        batch_payload = {"requests": [request], "defaults": defaults}
        self.schema.deserialize(batch_payload)

    def test_defaults_path_is_applied_to_requests(self):
        request = {"method": "GET"}
        defaults = {"path": "/"}
        batch_payload = {"requests": [request], "defaults": defaults}
        result = self.schema.deserialize(batch_payload)
        self.assertEqual(result["requests"][0]["path"], "/")

    def test_defaults_body_is_applied_to_requests(self):
        request = {"path": "/"}
        defaults = {"body": {"json": "payload"}}
        batch_payload = {"requests": [request], "defaults": defaults}
        result = self.schema.deserialize(batch_payload)
        self.assertEqual(result["requests"][0]["body"], {"json": "payload"})

    def test_defaults_headers_are_applied_to_requests(self):
        request = {"path": "/"}
        defaults = {"headers": {"Content-Type": "text/html"}}
        batch_payload = {"requests": [request], "defaults": defaults}
        result = self.schema.deserialize(batch_payload)
        self.assertEqual(result["requests"][0]["headers"]["Content-Type"],
                         "text/html")

    def test_defaults_values_do_not_overwrite_requests_values(self):
        request = {"path": "/", "headers": {"Authorization": "me"}}
        defaults = {"headers": {"Authorization": "you", "Accept": "*/*"}}
        batch_payload = {"requests": [request], "defaults": defaults}
        result = self.schema.deserialize(batch_payload)
        self.assertEqual(result["requests"][0]["headers"], {
            "Authorization": "me",
            "Accept": "*/*"
        })

    def test_defaults_values_for_path_must_start_with_slash(self):
        request = {}
        defaults = {"path": "http://localhost"}
        batch_payload = {"requests": [request], "defaults": defaults}
        self.assertInvalid(batch_payload)
 def setUp(self):
     self.schema = BatchPayloadSchema()
class BatchSchemaTest(unittest.TestCase):
    def setUp(self):
        self.schema = BatchPayloadSchema()

    def assertInvalid(self, payload):
        self.assertRaises(colander.Invalid, self.schema.deserialize, payload)

    def test_requests_is_mandatory(self):
        self.assertInvalid({})

    def test_unknown_attributes_are_dropped(self):
        deserialized = self.schema.deserialize({"requests": [], "unknown": 42})
        self.assertNotIn("unknown", deserialized)

    def test_list_of_requests_can_be_empty(self):
        self.schema.deserialize({"requests": []})

    def test_list_of_requests_must_be_a_list(self):
        self.assertInvalid({"requests": {}})

    def test_list_of_requests_must_be_dicts(self):
        request = 42
        self.assertInvalid({"requests": [request]})

    def test_request_path_must_start_with_slash(self):
        request = {"path": "http://localhost"}
        self.assertInvalid({"requests": [request]})

    def test_request_path_is_mandatory(self):
        request = {"method": "HEAD"}
        self.assertInvalid({"requests": [request]})

    def test_request_method_must_be_known_uppercase_word(self):
        request = {"path": "/", "method": "get"}
        self.assertInvalid({"requests": [request]})

    #
    # headers
    #

    def test_request_headers_should_be_strings(self):
        headers = {"Accept": 3.14}
        request = {"path": "/", "headers": headers}
        self.assertInvalid({"requests": [request]})

    def test_request_headers_cannot_be_recursive(self):
        headers = {"Accept": {"sub": "dict"}}
        request = {"path": "/", "headers": headers}
        self.assertInvalid({"requests": [request]})

    def test_request_headers_are_preserved(self):
        headers = {"Accept": "audio/*"}
        request = {"path": "/", "headers": headers}
        deserialized = self.schema.deserialize({"requests": [request]})
        self.assertEqual(deserialized["requests"][0]["headers"]["Accept"], "audio/*")

    #
    # body
    #

    def test_body_is_an_arbitrary_mapping(self):
        payload = {"json": "payload"}
        request = {"path": "/", "body": payload}
        deserialized = self.schema.deserialize({"requests": [request]})
        self.assertEqual(deserialized["requests"][0]["body"], payload)

    #
    # defaults
    #

    def test_defaults_must_be_a_mapping_if_specified(self):
        request = {"path": "/"}
        batch_payload = {"requests": [request], "defaults": 42}
        self.assertInvalid(batch_payload)

    def test_defaults_must_be_a_request_schema_if_specified(self):
        request = {"path": "/"}
        defaults = {"body": 3}
        batch_payload = {"requests": [request], "defaults": defaults}
        self.assertInvalid(batch_payload)

    def test_unknown_defaults_are_ignored_silently(self):
        request = {"path": "/"}
        defaults = {"foo": "bar"}
        batch_payload = {"requests": [request], "defaults": defaults}
        result = self.schema.deserialize(self.schema.unflatten(batch_payload))
        self.assertNotIn("foo", result["requests"][0])

    def test_defaults_can_be_specified_empty(self):
        request = {"path": "/"}
        defaults = {}
        batch_payload = {"requests": [request], "defaults": defaults}
        self.schema.deserialize(self.schema.unflatten(batch_payload))

    def test_defaults_path_is_applied_to_requests(self):
        request = {"method": "GET"}
        defaults = {"path": "/"}
        batch_payload = {"requests": [request], "defaults": defaults}
        result = self.schema.deserialize(self.schema.unflatten(batch_payload))
        self.assertEqual(result["requests"][0]["path"], "/")

    def test_defaults_body_is_applied_to_requests(self):
        request = {"path": "/"}
        defaults = {"body": {"json": "payload"}}
        batch_payload = {"requests": [request], "defaults": defaults}
        result = self.schema.deserialize(self.schema.unflatten(batch_payload))
        self.assertEqual(result["requests"][0]["body"], {"json": "payload"})

    def test_defaults_headers_are_applied_to_requests(self):
        request = {"path": "/"}
        defaults = {"headers": {"Content-Type": "text/html"}}
        batch_payload = {"requests": [request], "defaults": defaults}
        result = self.schema.deserialize(self.schema.unflatten(batch_payload))
        self.assertEqual(result["requests"][0]["headers"]["Content-Type"], "text/html")

    def test_defaults_values_do_not_overwrite_requests_values(self):
        request = {"path": "/", "headers": {"Authorization": "me"}}
        defaults = {"headers": {"Authorization": "you", "Accept": "*/*"}}
        batch_payload = {"requests": [request], "defaults": defaults}
        result = self.schema.deserialize(self.schema.unflatten(batch_payload))
        self.assertEqual(result["requests"][0]["headers"], {"Authorization": "me", "Accept": "*/*"})

    def test_defaults_values_for_path_must_start_with_slash(self):
        request = {}
        defaults = {"path": "http://localhost"}
        batch_payload = {"requests": [request], "defaults": defaults}
        self.assertInvalid(batch_payload)
Exemple #5
0
class BatchSchemaTest(unittest.TestCase):
    def setUp(self):
        self.schema = BatchPayloadSchema()

    def assertInvalid(self, payload):
        self.assertRaises(colander.Invalid, self.schema.deserialize, payload)

    def test_requests_is_mandatory(self):
        self.assertInvalid({})

    def test_raise_invalid_on_unknown_attributes(self):
        self.assertInvalid({'requests': [], 'unknown': 42})

    def test_list_of_requests_can_be_empty(self):
        self.schema.deserialize({'requests': []})

    def test_list_of_requests_must_be_a_list(self):
        self.assertInvalid({'requests': {}})

    def test_list_of_requests_must_be_dicts(self):
        request = 42
        self.assertInvalid({'defaults': {'path': '/'}, 'requests': [request]})

    def test_request_path_must_start_with_slash(self):
        request = {'path': 'http://localhost'}
        self.assertInvalid({'requests': [request]})

    def test_request_path_is_mandatory(self):
        request = {'method': 'HEAD'}
        self.assertInvalid({'requests': [request]})

    def test_request_method_must_be_known_uppercase_word(self):
        request = {'path': '/', 'method': 'get'}
        self.assertInvalid({'requests': [request]})

    def test_raise_invalid_on_request_unknown_attributes(self):
        request = {'path': '/', 'method': 'GET', 'foo': 42}
        self.assertInvalid({'requests': [request]})

    #
    # headers
    #

    def test_request_headers_should_be_strings(self):
        headers = {'Accept': 3.14}
        request = {'path': '/', 'headers': headers}
        self.assertInvalid({'requests': [request]})

    def test_request_headers_cannot_be_recursive(self):
        headers = {'Accept': {'sub': 'dict'}}
        request = {'path': '/', 'headers': headers}
        self.assertInvalid({'requests': [request]})

    def test_request_headers_are_preserved(self):
        headers = {'Accept': 'audio/*'}
        request = {'path': '/', 'headers': headers}
        deserialized = self.schema.deserialize({'requests': [request]})
        self.assertEqual(deserialized['requests'][0]['headers']['Accept'],
                         'audio/*')

    #
    # body
    #

    def test_body_is_an_arbitrary_mapping(self):
        payload = {"json": "payload"}
        request = {'path': '/', 'body': payload}
        deserialized = self.schema.deserialize({'requests': [request]})
        self.assertEqual(deserialized['requests'][0]['body'], payload)

    #
    # defaults
    #

    def test_defaults_must_be_a_mapping_if_specified(self):
        request = {'path': '/'}
        batch_payload = {'requests': [request], 'defaults': 42}
        self.assertInvalid(batch_payload)

    def test_defaults_must_be_a_request_schema_if_specified(self):
        request = {'path': '/'}
        defaults = {'body': 3}
        batch_payload = {'requests': [request], 'defaults': defaults}
        self.assertInvalid(batch_payload)

    def test_raise_invalid_on_default_unknown_attributes(self):
        request = {'path': '/'}
        defaults = {'foo': 'bar'}
        self.assertInvalid({'requests': [request], 'defaults': defaults})

    def test_defaults_can_be_specified_empty(self):
        request = {'path': '/'}
        defaults = {}
        batch_payload = {'requests': [request], 'defaults': defaults}
        self.schema.deserialize(batch_payload)

    def test_defaults_path_is_applied_to_requests(self):
        request = {'method': 'GET'}
        defaults = {'path': '/'}
        batch_payload = {'requests': [request], 'defaults': defaults}
        result = self.schema.deserialize(batch_payload)
        self.assertEqual(result['requests'][0]['path'], '/')

    def test_defaults_body_is_applied_to_requests(self):
        request = {'path': '/'}
        defaults = {'body': {'json': 'payload'}}
        batch_payload = {'requests': [request], 'defaults': defaults}
        result = self.schema.deserialize(batch_payload)
        self.assertEqual(result['requests'][0]['body'], {'json': 'payload'})

    def test_defaults_headers_are_applied_to_requests(self):
        request = {'path': '/'}
        defaults = {'headers': {'Content-Type': 'text/html'}}
        batch_payload = {'requests': [request], 'defaults': defaults}
        result = self.schema.deserialize(batch_payload)
        self.assertEqual(result['requests'][0]['headers']['Content-Type'],
                         'text/html')

    def test_defaults_values_do_not_overwrite_requests_values(self):
        request = {'path': '/', 'headers': {'Authorization': 'me'}}
        defaults = {'headers': {'Authorization': 'you', 'Accept': '*/*'}}
        batch_payload = {'requests': [request], 'defaults': defaults}
        result = self.schema.deserialize(batch_payload)
        self.assertEqual(result['requests'][0]['headers'], {
            'Authorization': 'me',
            'Accept': '*/*'
        })

    def test_defaults_values_for_path_must_start_with_slash(self):
        request = {}
        defaults = {'path': 'http://localhost'}
        batch_payload = {'requests': [request], 'defaults': defaults}
        self.assertInvalid(batch_payload)
Exemple #6
0
class BatchSchemaTest(unittest.TestCase):
    def setUp(self):
        self.schema = BatchPayloadSchema()

    def assertInvalid(self, payload):
        self.assertRaises(colander.Invalid, self.schema.deserialize, payload)

    def test_requests_is_mandatory(self):
        self.assertInvalid({})

    def test_unknown_attributes_are_dropped(self):
        deserialized = self.schema.deserialize({'requests': [], 'unknown': 42})
        self.assertNotIn('unknown', deserialized)

    def test_list_of_requests_can_be_empty(self):
        self.schema.deserialize({'requests': []})

    def test_list_of_requests_must_be_a_list(self):
        self.assertInvalid({'requests': {}})

    def test_list_of_requests_must_be_dicts(self):
        request = 42
        self.assertInvalid({'requests': [request]})

    def test_request_path_must_start_with_slash(self):
        request = {'path': 'http://localhost'}
        self.assertInvalid({'requests': [request]})

    def test_request_path_is_mandatory(self):
        request = {'method': 'HEAD'}
        self.assertInvalid({'requests': [request]})

    def test_request_method_must_be_known_uppercase_word(self):
        request = {'path': '/', 'method': 'get'}
        self.assertInvalid({'requests': [request]})

    #
    # headers
    #

    def test_request_headers_should_be_strings(self):
        headers = {'Accept': 3.14}
        request = {'path': '/', 'headers': headers}
        self.assertInvalid({'requests': [request]})

    def test_request_headers_cannot_be_recursive(self):
        headers = {'Accept': {'sub': 'dict'}}
        request = {'path': '/', 'headers': headers}
        self.assertInvalid({'requests': [request]})

    def test_request_headers_are_preserved(self):
        headers = {'Accept': 'audio/*'}
        request = {'path': '/', 'headers': headers}
        deserialized = self.schema.deserialize({'requests': [request]})
        self.assertEqual(deserialized['requests'][0]['headers']['Accept'],
                         'audio/*')

    #
    # body
    #

    def test_body_is_an_arbitrary_mapping(self):
        payload = {"json": "payload"}
        request = {'path': '/', 'body': payload}
        deserialized = self.schema.deserialize({'requests': [request]})
        self.assertEqual(deserialized['requests'][0]['body'], payload)

    #
    # defaults
    #

    def test_defaults_must_be_a_mapping_if_specified(self):
        request = {'path': '/'}
        batch_payload = {'requests': [request], 'defaults': 42}
        self.assertInvalid(batch_payload)

    def test_defaults_must_be_a_request_schema_if_specified(self):
        request = {'path': '/'}
        defaults = {'body': 3}
        batch_payload = {'requests': [request], 'defaults': defaults}
        self.assertInvalid(batch_payload)

    def test_unknown_defaults_are_ignored_silently(self):
        request = {'path': '/'}
        defaults = {'foo': 'bar'}
        batch_payload = {'requests': [request], 'defaults': defaults}
        result = self.schema.deserialize(self.schema.unflatten(batch_payload))
        self.assertNotIn('foo', result['requests'][0])

    def test_defaults_can_be_specified_empty(self):
        request = {'path': '/'}
        defaults = {}
        batch_payload = {'requests': [request], 'defaults': defaults}
        self.schema.deserialize(self.schema.unflatten(batch_payload))

    def test_defaults_path_is_applied_to_requests(self):
        request = {'method': 'GET'}
        defaults = {'path': '/'}
        batch_payload = {'requests': [request], 'defaults': defaults}
        result = self.schema.deserialize(self.schema.unflatten(batch_payload))
        self.assertEqual(result['requests'][0]['path'], '/')

    def test_defaults_body_is_applied_to_requests(self):
        request = {'path': '/'}
        defaults = {'body': {'json': 'payload'}}
        batch_payload = {'requests': [request], 'defaults': defaults}
        result = self.schema.deserialize(self.schema.unflatten(batch_payload))
        self.assertEqual(result['requests'][0]['body'], {'json': 'payload'})

    def test_defaults_headers_are_applied_to_requests(self):
        request = {'path': '/'}
        defaults = {'headers': {'Content-Type': 'text/html'}}
        batch_payload = {'requests': [request], 'defaults': defaults}
        result = self.schema.deserialize(self.schema.unflatten(batch_payload))
        self.assertEqual(result['requests'][0]['headers']['Content-Type'],
                         'text/html')

    def test_defaults_values_do_not_overwrite_requests_values(self):
        request = {'path': '/', 'headers': {'Authorization': 'me'}}
        defaults = {'headers': {'Authorization': 'you', 'Accept': '*/*'}}
        batch_payload = {'requests': [request], 'defaults': defaults}
        result = self.schema.deserialize(self.schema.unflatten(batch_payload))
        self.assertEqual(result['requests'][0]['headers'],
                         {'Authorization': 'me', 'Accept': '*/*'})

    def test_defaults_values_for_path_must_start_with_slash(self):
        request = {}
        defaults = {'path': 'http://localhost'}
        batch_payload = {'requests': [request], 'defaults': defaults}
        self.assertInvalid(batch_payload)