def setUp(self): self.response = self.get_response() self.request = self.get_request() self.request.check_data() # should be ok self.request.set_logger(logging.getLogger("test_base_request")) self.response.set_request(self.request) # catch the all logging is self.handler = TestHandler(Matcher()) self.logger = logging.getLogger("test_base_response") self.logger.addHandler(self.handler) self.response.set_logger(self.logger)
class MessageStandAloneSend(MessageSend): def setUp(self): self.handler = TestHandler(Matcher()) self.logger = logging.getLogger("test_base_response") self.logger.addHandler(self.handler) tasks.send_message.get_logger = make_fake_get_logger(self.logger) super(MessageStandAloneSend, self).setUp() def tearDown(self): # print self.handler.buffer self.logger.removeHandler(self.handler) self.handler.close() super(MessageStandAloneSend, self).tearDown() def get_message(self): collapse_key = "col_key" registration_id = "reg_id" data = {"key1": "data1", "key2": "data2"} delay_while_idle = True message_request = msg_standalone.MessageRequest(collapse_key, registration_id, data, delay_while_idle) message_response = msg_standalone.MessageResponse(collapse_key, registration_id, data, delay_while_idle) return message_request, message_response
def setUp(self): self.handler = TestHandler(Matcher()) self.logger = logging.getLogger("test_base_response") self.logger.addHandler(self.handler) tasks.send_message.get_logger = make_fake_get_logger(self.logger) super(MessageDbSend, self).setUp()
class MessageDbSend(MessageSend): fixtures = ["test_messages.json"] def setUp(self): self.handler = TestHandler(Matcher()) self.logger = logging.getLogger("test_base_response") self.logger.addHandler(self.handler) tasks.send_message.get_logger = make_fake_get_logger(self.logger) super(MessageDbSend, self).setUp() def tearDown(self): # print self.handler.buffer self.logger.removeHandler(self.handler) self.handler.close() super(MessageDbSend, self).tearDown() def get_message(self): self.collapse_key = "col_key" self.registration_id = "reg_id" self.data_in = {"key1": "data1", "key2": "data2"} self.delay_while_idle = False mc = MessageChannels(name=self.collapse_key) mc.id = 100 mc.save() for key, data in self.data_in.items(): md = MessageData(data=data) if type(key) is int: md.id = key else: md.name = key md.save() mc.message.add(md) mc.save() mg = MessageGroups(name="test_all_db") mg.save() ad = AndroidDevice() ad.id = 1000 ad.device_id = "test_send_message_db_01" ad.registration_id = self.registration_id ad.save() dci = DeviceChannelInfo() dci.device = ad dci.channel = mc dci.save() self.dci_id = dci.id message_request = msg_db.MessageRequest(dci, self.delay_while_idle) message_response = msg_db.MessageResponse(dci) return message_request, message_response def test_no_record_in_db_device_channel_info(self): message_request, message_response = self.get_message() dci = DeviceChannelInfo.objects.get(pk=message_request._id) dci.delete() with self.assertRaises(ObjectDoesNotExist): tasks.send_message(message_request, message_response) def test_failed_push_in_db(self): message_request, message_response = self.get_message() dci = DeviceChannelInfo.objects.get(pk=message_request._id) dci.device.failed_push = True dci.device.save() with self.assertRaises(FailedPushException): tasks.send_message(message_request, message_response) def test_http_resonse_200_fail_InvalidRegistration(self): super(MessageDbSend, self).test_http_resonse_200_fail_InvalidRegistration() dci = DeviceChannelInfo.objects.get(pk=self.dci_id) self.assertTrue(dci.device.failed_push) def test_http_resonse_200_fail_NotRegistered(self): super(MessageDbSend, self).test_http_resonse_200_fail_NotRegistered() dci = DeviceChannelInfo.objects.get(pk=self.dci_id) self.assertTrue(dci.device.failed_push) def test_http_resonse_200_success(self): pre_last_message = timezone.now() super(MessageDbSend, self).test_http_resonse_200_success() ## 200 ID check last change dci = DeviceChannelInfo.objects.get(pk=self.dci_id) self.assertTrue(dci.last_message > pre_last_message)
class MessageResponseTest(MessageTestCase): def setUp(self): self.response = self.get_response() self.request = self.get_request() self.request.check_data() # should be ok self.request.set_logger(logging.getLogger("test_base_request")) self.response.set_request(self.request) # catch the all logging is self.handler = TestHandler(Matcher()) self.logger = logging.getLogger("test_base_response") self.logger.addHandler(self.handler) self.response.set_logger(self.logger) def tearDown(self): # print self.handler.buffer self.logger.removeHandler(self.handler) self.handler.close() def get_request(self): raise NotImplementedError("get_request") def get_response(self): raise NotImplementedError("get_response") def test_error_invalid_registration(self): self.assertRaises(InvalidRegistrationException, self.response.set_error, "invalid_registration") def test_error_not_registered(self): self.assertRaises(NotRegisteredException, self.response.set_error, "not_registered", "dtd") def test_error_quota_exceeded(self): self.assertRaises(QuotaExceededException, self.response.set_error, "quota_exceeded") def test_error_device_quota_exceeded(self): self.assertRaises(DeviceQuotaExceededException, self.response.set_error, "device_quota_exceeded") def test_error_message_too_big(self): self.assertRaises(MessageTooBigException, self.response.set_error, "message_too_big") def test_error_missing_collapse_key(self): self.assertRaises(MissingCollapseKeyException, self.response.set_error, "missing_collapse_key", "dtd") def test_error_unknown_error_code(self): self.assertRaises(UnknownErrorCodeException, self.response.set_error, "unknown_error_code") def test_error_url_error(self): self.assertRaises(UrlErrorException, self.response.set_error, "url_error", "dtd") def test_error_invalid_auth_token(self): self.assertRaises(ErrorInvalidAuthTokenException, self.response.set_error, "invalid_auth_token", "dtd") def test_error_invalid_response(self): self.assertRaises(InvalidResponseException, self.response.set_error, "invalid_response", "dtd") def test_error_not_implemented(self): self.assertRaises(NotImplementedError, self.response.set_error, "qwerty", "dtd") def test_get_result(self): self.response.set_result(111) self.assertEqual(self.response.get_result(), 111)