def test(self): document = { '_id': 'test-id', 'doc_type': 'CommCareCase', 'type': 'mother', 'domain': 'kafka-test-domain', } change = Change(id='test-id', sequence_id='3', document=document) populate_change_metadata(change, SOURCE_COUCH, 'test_commcarehq') with patch('pillow_retry.api.get_pillow_by_name', return_value=self.pillow): # first change creates error message = 'test retry 1' self.pillow.process_change = MagicMock(side_effect=TestException(message)) self.pillow.process_with_error_handling(change, PillowRuntimeContext(changes_seen=0)) errors = self._check_errors(1, message) # second attempt updates error with process_pillow_changes(self.pillow): process_pillow_retry(errors[0]) errors = self._check_errors(2) # third attempt successful self.pillow.process_change = self.original_process_change with process_pillow_changes(self.pillow): process_pillow_retry(errors[0]) errors = list(PillowError.objects.filter(pillow=self.pillow.pillow_id).all()) self.assertEqual(0, len(errors)) self.assertEqual(1, self.processor.count)
def process_change(self, change): populate_change_metadata(change, self._data_source_type, self._data_source_name) if change.metadata: change_meta = change.metadata topic = get_topic_for_doc_type(change_meta.document_type, self._data_source_type) # change.deleted is used for hard deletions whereas change_meta.is_deletion is for soft deletions. # from the consumer's perspective both should be counted as deletions so just "or" them # note: it is strange and hard to reproduce that the couch changes feed is providing a "doc" # along with a hard deletion, but it is doing that in the wild so we might as well support it. change_meta.is_deletion = change_meta.is_deletion or change.deleted self._producer.send_change(topic, change_meta)
def test(self): document = { 'doc_type': 'CommCareCase', 'type': 'mother', 'domain': 'kafka-test-domain', } change = Change(id='test-id', sequence_id='3', document=document) populate_change_metadata(change, SOURCE_COUCH, self._fake_couch.dbname) with patch('pillow_retry.api.get_pillow_by_name', return_value=self.pillow): # first change creates error message = 'test retry 1' self.pillow.process_change = MagicMock( side_effect=TestException(message)) self.pillow.process_with_error_handling( change, PillowRuntimeContext(changes_seen=0)) errors = self._check_errors(1, message) # second attempt updates error process_pillow_retry(errors[0]) errors = self._check_errors(2) # third attempt successful self.pillow.process_change = self.original_process_change process_pillow_retry(errors[0]) errors = list( PillowError.objects.filter(pillow=self.pillow.pillow_id).all()) self.assertEqual(0, len(errors)) message = next(self.consumer) change_meta = change_meta_from_kafka_message(message.value) self.assertEqual(SOURCE_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)