"""Conftest.""" import json import pytest import responses from kafka_utils.consumer.deserializer.avro import AvroDeserializer from kafka_utils.consumer.deserializer.json_schema import JSONDeserializer def load_file_json(filename): """Load json from file.""" with open(f'tests/unit/mock_event_data/{filename}') as event_file: read_data = event_file.read() return json.loads(read_data) @pytest.fixture(scope='session') def mock_event(): """Mock empty event.""" return load_file_json('debezium-art-relations.json') @pytest.fixture(scope='session') def mock_empty_event(): """Mock empty event.""" return load_file_json('empty.json') @pytest.fixture(scope='session') def mock_multi_topic_msk_message(): """Mock MSK message with multiple topics and events.""" return load_file_json('multiple_topic_msk_message.json') @pytest.fixture(scope='session') def mock_event_producer_event(): """Mock MSK event from EventProducer class.""" return load_file_json('product_review.json') @pytest.fixture(scope='session') def mock_avro_event(): """Mock avro serialized event.""" return load_file_json('avro.json') @pytest.fixture(scope='session') def mock_json_schema_event(): """Mock JSON schema serialized event.""" return load_file_json('json_schema.json') def mock_review_schema_string(): """Mock review schema string.""" schema_json = { 'type': 'record', 'name': 'review', 'namespace': 'owsContentReview', 'fields': [ { 'name': 'operation', 'type': { 'type': 'record', 'name': 'operationInformation', 'fields': [ { 'name': 'type', 'type': { 'type': 'enum', 'name': 'operationOptions', 'symbols': ['create', 'update', 'delete'] }, 'doc': 'Operation type.' }, { 'name': 'context', 'type': [ 'null', { 'type': 'enum', 'name': 'operationContextOptions', 'doc': 'Additional information about event.', 'symbols': ['new', 'approve', 'reject', 'escalate', 'close'] } ], 'default': None }, { 'name': 'timestamp', 'type': { 'type': 'long', 'logicalType': 'timestamp-millis' }, 'doc': 'timestamp of the operation.' } ] }, 'doc': 'Operation information' }, { 'name': 'payload', 'type': { 'type': 'record', 'name': 'payloadInformation', 'fields': [ { 'name': 'product_id', 'type': 'int', 'doc': 'Product unique identifier generated by art_relations database.' }, { 'name': 'review_queue_id', 'type': 'int', 'doc': 'Review unique identifier generated by content_review database.' }, { 'name': 'review_note', 'type': ['null', 'string'], 'doc': 'A note from the reviewer regarding the approval / rejection.' }, { 'name': 'user_id', 'type': ['null', 'string'], 'doc': 'foo' } ] }, 'doc': 'Payload information.' } ] } return json.dumps(schema_json).replace('"', '\"') @pytest.fixture def mock_avro_review_serializer(): """Mock avro serializer.""" with responses.RequestsMock() as rsps: rsps.add( responses.GET, 'http://example.com/schemas/ids/78', json={'schema': mock_review_schema_string()}, status=200, ) avro_serializer = AvroDeserializer('http://example.com') yield avro_serializer @pytest.fixture def mock_schema_string(): """Mock schema string.""" schema_json = { 'type': 'record', 'name': 'test', 'namespace': 'com.orchard.kafka.data.highway', 'fields': [ { 'name': 'foo', 'type': 'int', 'doc': 'foo' }, { 'name': 'bar', 'type': ['null', 'string', 'int'], 'doc': 'bar' }, { 'name': 'baz', 'type': ['null', 'int', 'string'], 'doc': 'baz' } ] } return json.dumps(schema_json).replace('"', '\"') @pytest.fixture def mock_subject_response(): """Mock subjects/{name}/{version} response.""" return { 'subject': 'event.owsContentReview.review-value', 'version': 3, 'id': 78, 'schema': ( '{\"type\":\"record\",\"name\":\"review\",\"namespace\":\"owsContentReview\",' '\"fields\":[{\"name\":\"operation\",\"type\":{\"type\":\"record\",' '\"name\":\"operationInformation\",\"fields\":[{\"name\":\"type\",' '\"type\":{\"type\":\"enum\",\"name\":\"operationOptions\",' '\"symbols\":[\"create\",\"update\",\"delete\"]},\"doc\":\"Operation type.\"},' '{\"name\":\"context\",\"type\":[\"null\",{\"type\":\"enum\",' '\"name\":\"operationContextOptions\",\"doc\":\"foo.\",\"symbols\":[\"new\",' '\"approve\",\"reject\",\"escalate\",\"close\"]}],\"default\":null},' '{\"name\":\"timestamp\",\"type\":{\"type\":\"long\",' '\"logicalType\":\"timestamp-millis\"},\"doc\":\"foo.\"}]},' '\"doc\":\"Operation information\"},{\"name\":\"payload\",' '\"type\":{\"type\":\"record\",\"name\":\"payloadInformation\",' '\"fields\":[{\"name\":\"product_id\",\"type\":\"int\",\"doc\":\"foo.\"},' '{\"name\":\"review_queue_id\",\"type\":\"int\",\"doc\":\"foo.\"},' '{\"name\":\"review_note\",\"type\":[\"null\",\"string\"],\"doc\":\"foo.\"},' '{\"name\":\"user_id\",\"type\":[\"null\",\"string\"],\"doc\":\"foo.\"}]},' '\"doc\":\"Payload information.\"}]}' ) } @pytest.fixture def mock_review_schema_response(): """Mock review schema response.""" return ( '{\"type\":\"record\",\"name\":\"review\",\"namespace\":\"owsContentReview\",' '\"fields\":[{\"name\":\"operation\",\"type\":{\"type\":\"record\",\"name\":' '\"operationInformation\",\"fields\":[{\"name\":\"type\",\"type\":{\"type\":' '\"enum\",\"name\":\"operationOptions\",\"symbols\":[\"create\",\"update\",' '\"patch\",\"delete\"]},\"doc\":\"Operation type.\"},{\"name\":\"context\",' '\"type\":[\"null\",{\"type\":\"enum\",\"name\":\"operationContextOptions\",' '\"doc\":\"Additional information about event.\",\"symbols\":[\"new\",\"approve\"' ',\"reject\",\"escalate\",\"close\",\"lock\",\"unlock\"]}],\"default\":null},{' '\"name\":\"timestamp\",\"type\":{\"type\":\"long\",\"logicalType\":' '\"timestamp-millis\"},\"doc\":\"timestamp of the operation.\"}]},\"doc\":' '\"Operation information\"},{\"name\":\"payload\",\"type\":{\"type\":\"record\",' '\"name\":\"payloadInformation\",\"fields\":[{\"name\":\"product_id\",\"type\":' '\"int\",\"doc\":\"Product unique identifier generated by art_relations database.' '\"},{\"name\":\"review_queue_id\",\"type\":\"int\",\"doc\":\"Review unique ' 'identifier generated by content_review database.\"},{\"name\":\"review_note\",' '\"type\":[\"null\",\"string\"],\"doc\":\"A note from the reviewer regarding the ' 'approval / rejection.\"},{\"name\":\"user_id\",\"type\":[\"null\",\"string\"],' '\"doc\":\"The user_id of the reviewer who approved / rejected the product.\"},{' '\"name\":\"locked_by_user_id\",\"type\":[\"null\",{\"type\":\"string\",' '\"logicalType\":\"uuid\"}],\"doc\":\"Orchard Identity Id of user with exclusive' ' access to review this review queue item.\"},{\"name\":\"locked_until_datetime\",' '\"type\":[\"null\",{\"type\":\"long\",\"logicalType\":\"timestamp-micros\"}],' '\"doc\":\"UTC time when locked_by_user_id will lose exclusive access to review ' 'this review queue item.\"},{\"name\":\"submission_type\",\"type\":[\"null\",' '\"string\"],\"doc\":\"The submission type of the review.\"}]},\"doc\":\"Payload ' 'information.\"}]}' ) @pytest.fixture def mock_avro_serializer(mock_schema_string): """Mock avro serializer.""" with responses.RequestsMock() as rsps: rsps.add( responses.GET, 'http://example.com/schemas/ids/3', json={'schema': mock_schema_string}, status=200, ) avro_serializer = AvroDeserializer('http://example.com') yield avro_serializer @pytest.fixture def mock_json_schema_string(): """Mock JSON schema string.""" schema_json = { 'title': 'test-value', 'type': 'object', 'name': 'review', 'properties': { 'operation': { 'type': 'object', 'properties': { 'type': { 'enum': ['create', 'update', 'delete'] }, 'context': { 'type': ['string', 'null'] }, 'timestamp': { 'type': 'number' } } }, 'payload': { 'type': 'object', 'properties': { 'product_id': { 'type': 'integer' }, 'review_queue_id': { 'type': 'integer' }, 'review_note': { 'type': ['string', 'null'] }, 'user_id': { 'type': ['string', 'null'] } } } } } return json.dumps(schema_json).replace('"', '\"') @pytest.fixture def mock_json_schema_serializer(mock_json_schema_string): """Mock JSON schema serializer.""" with responses.RequestsMock() as rsps: rsps.add( responses.GET, 'http://example.com/schemas/ids/81', json={'schema': mock_json_schema_string}, status=200, ) serializer = JSONDeserializer('http://example.com') yield serializer @pytest.fixture def mock_avro_serializer_id_79(): """Mock avro serializer.""" with responses.RequestsMock() as rsps: rsps.add( responses.GET, 'http://example.com/schemas/ids/79', json={'schema': mock_review_schema_string()}, status=200, ) avro_serializer = AvroDeserializer('http://example.com') yield avro_serializer @pytest.fixture def mock_avro_serializer_id_103(mock_review_schema_response): """Mock avro serializer.""" with responses.RequestsMock() as rsps: rsps.add( responses.GET, 'http://example.com/schemas/ids/103', json={'schema': mock_review_schema_response}, status=200, ) avro_serializer = AvroDeserializer('http://example.com') yield avro_serializer