def test_process_change(self): consumer = KafkaConsumer( topics.CASE, group_id='test-consumer', bootstrap_servers=[settings.KAFKA_URL], consumer_timeout_ms=100, ) pillow = ChangeFeedPillow(self._fake_couch, kafka=get_kafka_client(), checkpoint=None) document = { 'doc_type': 'CommCareCase', 'type': 'mother', 'domain': 'kafka-test-domain', } pillow.process_change(Change(id='test-id', sequence_id='3', document=document)) message = consumer.next() change_meta = change_meta_from_kafka_message(message.value) self.assertEqual(COUCH, change_meta.data_source_type) self.assertEqual(self._fake_couch.dbname, change_meta.data_source_name) self.assertEqual('test-id', change_meta.document_id) self.assertEqual(document['doc_type'], change_meta.document_type) self.assertEqual(document['type'], change_meta.document_subtype) self.assertEqual(document['domain'], change_meta.domain) self.assertEqual(False, change_meta.is_deletion) with self.assertRaises(ConsumerTimeout): consumer.next()
class ChangeFeedPillowTest(SimpleTestCase): # note: these tests require a valid kafka setup running def setUp(self): self._fake_couch = FakeCouchDb() self._fake_couch.dbname = 'test-couchdb' with trap_extra_setup(KafkaUnavailableError): self.consumer = KafkaConsumer( topics.CASE, group_id='test-consumer', bootstrap_servers=[settings.KAFKA_URL], consumer_timeout_ms=100, ) self.pillow = ChangeFeedPillow( 'fake-changefeed-pillow-id', self._fake_couch, kafka=get_kafka_client(), checkpoint=None ) def test_process_change(self): document = { 'doc_type': 'CommCareCase', 'type': 'mother', 'domain': 'kafka-test-domain', } self.pillow.process_change(Change(id='test-id', sequence_id='3', document=document)) message = self.consumer.next() change_meta = change_meta_from_kafka_message(message.value) self.assertEqual(COUCH, change_meta.data_source_type) self.assertEqual(self._fake_couch.dbname, change_meta.data_source_name) self.assertEqual('test-id', change_meta.document_id) self.assertEqual(document['doc_type'], change_meta.document_type) self.assertEqual(document['type'], change_meta.document_subtype) self.assertEqual(document['domain'], change_meta.domain) self.assertEqual(False, change_meta.is_deletion) with self.assertRaises(ConsumerTimeout): self.consumer.next() def test_process_change_with_unicode_domain(self): document = { 'doc_type': 'CommCareCase', 'type': 'mother', 'domain': u'हिंदी', } self.pillow.process_change(Change(id='test-id', sequence_id='3', document=document)) message = self.consumer.next() change_meta = change_meta_from_kafka_message(message.value) self.assertEqual(document['domain'], change_meta.domain) def test_no_domain(self): document = { 'doc_type': 'CommCareCase', 'type': 'mother', 'domain': None, } self.pillow.process_change(Change(id='test-id', sequence_id='3', document=document)) message = self.consumer.next() change_meta = change_meta_from_kafka_message(message.value) self.assertEqual(document['domain'], change_meta.domain)