File: test_avro_producer.py

package info (click to toggle)
python-confluent-kafka 2.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,660 kB
  • sloc: python: 30,428; ansic: 9,487; sh: 1,477; makefile: 192
file content (126 lines) | stat: -rw-r--r-- 6,266 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
#
# Copyright 2016 Confluent Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


#
# derived from https://github.com/verisign/python-confluent-schemaregistry.git
#
import os

from confluent_kafka import avro

from requests.exceptions import ConnectionError

import unittest
from confluent_kafka.avro import AvroProducer
from confluent_kafka.avro.serializer import (KeySerializerError,
                                             ValueSerializerError)

from tests.avro.mock_schema_registry_client import MockSchemaRegistryClient


avsc_dir = os.path.dirname(os.path.realpath(__file__))


class TestAvroProducer(unittest.TestCase):

    def test_instantiation(self):
        obj = AvroProducer({'schema.registry.url': 'http://127.0.0.1:0'})
        self.assertTrue(isinstance(obj, AvroProducer))
        self.assertNotEqual(obj, None)

    def test_produce_no_key(self):
        value_schema = avro.load(os.path.join(avsc_dir, "basic_schema.avsc"))
        producer = AvroProducer({'schema.registry.url': 'http://127.0.0.1:9001'}, default_value_schema=value_schema)
        with self.assertRaises(ConnectionError):  # Unexistent schema-registry
            producer.produce(topic='test', value={"name": 'abc"'})

    def test_produce_no_value(self):
        key_schema = avro.load(os.path.join(avsc_dir, "basic_schema.avsc"))
        producer = AvroProducer({'schema.registry.url': 'http://127.0.0.1:9001'}, default_key_schema=key_schema)
        with self.assertRaises(ConnectionError):  # Unexistent schema-registry
            producer.produce(topic='test', key={"name": 'abc"'})

    def test_produce_no_value_schema(self):
        producer = AvroProducer({'schema.registry.url': 'http://127.0.0.1:9001'})
        with self.assertRaises(ValueSerializerError):
            # Producer should not accept a value with no schema
            producer.produce(topic='test', value={"name": 'abc"'})

    def test_produce_no_key_schema(self):
        producer = AvroProducer({'schema.registry.url': 'http://127.0.0.1:9001'})
        with self.assertRaises(KeySerializerError):
            # If the key is provided as a dict an avro schema must also be provided
            producer.produce(topic='test', key={"name": 'abc"'})

    def test_produce_value_and_key_schemas(self):
        value_schema = avro.load(os.path.join(avsc_dir, "basic_schema.avsc"))
        producer = AvroProducer({'schema.registry.url': 'http://127.0.0.1:9001'}, default_value_schema=value_schema,
                                default_key_schema=value_schema)
        with self.assertRaises(ConnectionError):  # Unexistent schema-registry
            producer.produce(topic='test', value={"name": 'abc"'}, key={"name": 'abc"'})

    def test_produce_primitive_string_key(self):
        value_schema = avro.load(os.path.join(avsc_dir, "basic_schema.avsc"))
        key_schema = avro.load(os.path.join(avsc_dir, "primitive_string.avsc"))
        producer = AvroProducer({'schema.registry.url': 'http://127.0.0.1:9001'})
        with self.assertRaises(ConnectionError):  # Unexistent schema-registry
            producer.produce(topic='test', value={"name": 'abc"'}, value_schema=value_schema, key='mykey',
                             key_schema=key_schema)

    def test_produce_primitive_key_and_value(self):
        value_schema = avro.load(os.path.join(avsc_dir, "primitive_float.avsc"))
        key_schema = avro.load(os.path.join(avsc_dir, "primitive_string.avsc"))
        producer = AvroProducer({'schema.registry.url': 'http://127.0.0.1:9001'})
        with self.assertRaises(ConnectionError):  # Unexistent schema-registry
            producer.produce(topic='test', value=32., value_schema=value_schema, key='mykey', key_schema=key_schema)

    def test_produce_with_custom_registry(self):
        schema_registry = MockSchemaRegistryClient()
        value_schema = avro.load(os.path.join(avsc_dir, "basic_schema.avsc"))
        key_schema = avro.load(os.path.join(avsc_dir, "primitive_string.avsc"))
        producer = AvroProducer({}, schema_registry=schema_registry)
        producer.produce(topic='test', value={"name": 'abc"'}, value_schema=value_schema, key='mykey',
                         key_schema=key_schema)

    def test_produce_with_custom_registry_and_registry_url(self):
        schema_registry = MockSchemaRegistryClient()
        with self.assertRaises(ValueError):
            AvroProducer({'schema.registry.url': 'http://127.0.0.1:9001'}, schema_registry=schema_registry)

    def test_produce_with_empty_value_no_schema(self):
        schema_registry = MockSchemaRegistryClient()
        producer = AvroProducer({}, schema_registry=schema_registry)
        with self.assertRaises(ValueSerializerError):
            producer.produce(topic='test', value='', key='not empty')

    def test_produce_with_empty_key_no_schema(self):
        value_schema = avro.load(os.path.join(avsc_dir, "primitive_float.avsc"))
        schema_registry = MockSchemaRegistryClient()
        producer = AvroProducer({}, schema_registry=schema_registry,
                                default_value_schema=value_schema)
        with self.assertRaises(KeySerializerError):
            producer.produce(topic='test', value=0.0, key='')

    def test_produce_with_empty_key_value_with_schema(self):
        key_schema = avro.load(os.path.join(avsc_dir, "primitive_string.avsc"))
        value_schema = avro.load(os.path.join(avsc_dir, "primitive_float.avsc"))
        schema_registry = MockSchemaRegistryClient()
        producer = AvroProducer({}, schema_registry=schema_registry,
                                default_key_schema=key_schema,
                                default_value_schema=value_schema)
        producer.produce(topic='test', value=0.0, key='')