def test_user_name(self): mock_data_source3 = [ {"created_at": "2012-09-27T16:10:34Z", "followers": 3, "id": 2, "message": "A coke message!", "sentiment": 0.9, "updated_at": "2012-09-27T16:10:34Z", "user_handle": "@coke_lvr"} ] Message.adaptive_api = MagicMock(return_value=mock_data_source3) Message.populate_from_api() message = Message.objects.get(message=mock_data_source3[0]['message']) self.assertTrue(message.user_name == 'coke_lvr')
def coke_list(request): Message.populate_from_api() allmessages = Message.objects.all().order_by('-sentiment') return render_to_response( 'message_api/coke_list.html', RequestContext(request, { 'allmessages':allmessages } ))
def test_duplicate_api_data(self): mock_data_source2 = [ {"created_at": "2012-09-27T16:10:34Z", "followers": 3, "id": 2, "message": "Duplicate coke message2!", "sentiment": 0.9, "updated_at": "2012-09-27T16:10:34Z", "user_handle": "@coke_lvr"}, {"created_at": "2012-09-27T16:10:34Z", "followers": 3, "id": 2, "message": "Duplicate coke message2!", "sentiment": 0.9, "updated_at": "2012-09-27T16:10:34Z", "user_handle": "@coke_lvr"} ] Message.adaptive_api = MagicMock(return_value=mock_data_source2) Message.populate_from_api() m = Message.objects.get(message=mock_data_source2[0]['message']) self.assertEqual(m.times_seen,2)
def test_mocked_api(self): mock_data_source = [ {"created_at": "2012-09-27T16:10:34Z", "followers": 3, "id": 2, "message": "Diet coke is just the best!", "sentiment": 0.9, "updated_at": "2012-09-27T16:10:34Z", "user_handle": "@coke_lvr"}, {"created_at": "2012-09-27T16:11:44Z", "followers": 345, "id": 4, "message": "Who likes coca-cola?", "sentiment": 0.0, "updated_at": "2012-09-27T16:11:44Z", "user_handle": "@questionnr"}] Message.adaptive_api = MagicMock(return_value=mock_data_source) Message.populate_from_api() m = Message.objects.get(pk=1) print m.message self.assertEqual(m.message,mock_data_source[0]['message'])
def test_is_target_word(self): mock_data_source3 = [ {"created_at": "2012-09-27T16:10:34Z", "followers": 3, "id": 2, "message": "A coke message!", "sentiment": 0.9, "updated_at": "2012-09-27T16:10:34Z", "user_handle": "@coke_lvr"}, {"created_at": "2012-09-27T16:10:34Z", "followers": 3, "id": 2, "message": "not a target message2!", "sentiment": 0.9, "updated_at": "2012-09-27T16:10:34Z", "user_handle": "@coke_lvr"} ] Message.adaptive_api = MagicMock(return_value=mock_data_source3) Message.populate_from_api() is_coke_message = Message.objects.get(message=mock_data_source3[0]['message']) self.assertTrue(is_coke_message.is_target_message) is_not_coke_message = Message.objects.get(message=mock_data_source3[1]['message']) self.assertFalse(is_not_coke_message.is_target_message)
def sendMessage(request): # Extracting json from request data = json.loads(request.body) # Defining response response = "message sent successfully" # Trying to save the message into the DB try: Message( sender = data["sender"], receiver = data["receiver"], subject = data["subject"], message = data["message"] ).save() # Changing response incase of failure except: response = "Something went wrong while sending the message" # Returning response return HttpResponse(response)