def test_known_exception(self): # check that exceptions due to unknown configs are handled correctly AsyncIndicator.objects.filter(doc_id__in=self.doc_ids).update( indicator_config_ids=["unknown"]) build_async_indicators(self.doc_ids) # since the only config associated with indicators is # unknown, all the indicators should be deleted self.assertEqual(AsyncIndicator.objects.count(), 0) self.datadog_patch.assert_has_calls([ mock.call('commcare.async_indicator.processed_success', 10), mock.call('commcare.async_indicator.processed_fail', 0) ])
def test_known_exception(self): # check that exceptions due to unknown configs are handled correctly AsyncIndicator.objects.filter( doc_id__in=self.doc_ids ).update(indicator_config_ids=["unknown"]) build_async_indicators(self.doc_ids) # since the only config associated with indicators is # unknown, all the indicators should be deleted self.assertEqual(AsyncIndicator.objects.count(), 0) self.datadog_patch.assert_has_calls([ mock.call('commcare.async_indicator.processed_success', 10), mock.call('commcare.async_indicator.processed_fail', 0) ])
def test_unknown_exception(self): # check that an unknown exception in bulk_save gets handled correctly AsyncIndicator.objects.filter(doc_id__in=self.doc_ids).update( indicator_config_ids=[self.config1._id, "unknown_id"]) with mock.patch("corehq.apps.userreports.tasks.get_indicator_adapter" ) as adapter_mock: adapter_mock.side_effect = Exception("Some random exception") build_async_indicators(self.doc_ids) # no indicator should be deleted since there was no success self.assertEqual(AsyncIndicator.objects.count(), 10) # the non-existent 'unknown_id' should be removed from indicator_config_ids # but self.config1._id should still be present self.assertEqual( AsyncIndicator.objects.filter( indicator_config_ids=[self.config1._id]).count(), 10) self.datadog_patch.assert_has_calls([ mock.call('commcare.async_indicator.processed_success', 0), mock.call('commcare.async_indicator.processed_fail', 10) ])
def test_unknown_exception(self): # check that an unknown exception in bulk_save gets handled correctly AsyncIndicator.objects.filter( doc_id__in=self.doc_ids ).update(indicator_config_ids=[self.config1._id, "unknown_id"]) with mock.patch("corehq.apps.userreports.tasks.get_indicator_adapter") as adapter_mock: adapter_mock.side_effect = Exception("Some random exception") build_async_indicators(self.doc_ids) # no indicator should be deleted since there was no success self.assertEqual(AsyncIndicator.objects.count(), 10) # the non-existent 'unknown_id' should be removed from indicator_config_ids # but self.config1._id should still be present self.assertEqual( AsyncIndicator.objects.filter(indicator_config_ids=[self.config1._id]).count(), 10 ) self.datadog_patch.assert_has_calls([ mock.call('commcare.async_indicator.processed_success', 0), mock.call('commcare.async_indicator.processed_fail', 10) ])
def test_basic_run(self): # map some indicators to first config, other to the second config # make sure that the tables get correctly built AsyncIndicator.objects.filter(doc_id__in=self.doc_ids[0:5]).update( indicator_config_ids=[self.config1._id]) AsyncIndicator.objects.filter(doc_id__in=self.doc_ids[5:]).update( indicator_config_ids=[self.config2._id]) build_async_indicators(self.doc_ids) self._assert_rows_in_ucr_table(self.config1, [{ 'doc_id': d["_id"], 'name': d["name"] } for d in self.docs[0:5]]) self._assert_rows_in_ucr_table(self.config2, [{ 'doc_id': d["_id"], 'color': d["color"] } for d in self.docs[5:]]) self.datadog_patch.assert_has_calls([ mock.call('commcare.async_indicator.processed_success', 10), mock.call('commcare.async_indicator.processed_fail', 0) ])
def test_basic_run(self): # map some indicators to first config, other to the second config # make sure that the tables get correctly built AsyncIndicator.objects.filter( doc_id__in=self.doc_ids[0:5] ).update(indicator_config_ids=[self.config1._id]) AsyncIndicator.objects.filter( doc_id__in=self.doc_ids[5:] ).update(indicator_config_ids=[self.config2._id]) build_async_indicators(self.doc_ids) self._assert_rows_in_ucr_table(self.config1, [ {'doc_id': d["_id"], 'name': d["name"]} for d in self.docs[0:5] ]) self._assert_rows_in_ucr_table(self.config2, [ {'doc_id': d["_id"], 'color': d["color"]} for d in self.docs[5:] ]) self.datadog_patch.assert_has_calls([ mock.call('commcare.async_indicator.processed_success', 10), mock.call('commcare.async_indicator.processed_fail', 0) ])