def test_get_information_ioerror(): app = TindevQA() filename = "configuration.json" when(builtins.open(filename)).read().thenRaise(IOError) actual_result = app.get_information_from_file(filename) assert actual_result == "ERROR"
def test_get_information_valueerror(): app = TindevQA() filename = "configuration.json" when(json).loads(...).thenRaise(ValueError) actual_result = app.get_information_from_file(filename) assert actual_result == "ERROR"
def test_send_sms_get_timeout(): app = TindevQA() user = '******' text = 'Quieres enredarte entre mis pelos?' when(requests).post(...).thenRaise(Timeout) response = app.send_sms(user, text) assert response == "ERROR"
def test_person2_dislikes_person1_parameters(mock_let_down): app = TindevQA() person1 = 'Luke' person2 = {'name': 'Leia', 'likes': ['Han Solo'], 'dislikes': ['Luke']} app.evaluate(person1, person2) mock_let_down.assert_called_once_with(person1)
def test_person2_dislikes_person1(mock_let_down): app = TindevQA() person1 = 'Luke' person2 = {'name': 'Leia', 'likes': ['Han Solo'], 'dislikes': ['Luke']} app.evaluate(person1, person2) assert mock_let_down.call_count == 1
def test_get_information_ioerror(mock_open): app = TindevQA() filename = "configuration.json" mock_open.side_effect = IOError actual_result = app.get_information_from_file(filename) assert actual_result == "ERROR"
def test_send_sms_with_mockito(): app = TindevQA() user = '******' text = 'Quieres enredarte entre mis pelos?' response_mock = mock({'status_code': 200, 'text': 'Ok'}) when(requests).post(...).thenReturn(response_mock) response = app.send_sms(user, text) assert response.status_code == 200
def test_send_sms_get_500_error(): app = TindevQA() user = '******' text = 'Quieres enredarte entre mis pelos?' response_mock = mock({'status_code': 501}) when(requests).post(...).thenReturn(response_mock) response = app.send_sms(user, text) assert response.status_code == 501
def test_person2_dislikes_person1(): app = TindevQA() person1 = 'Luke' person2 = {'name': 'Leia', 'likes': ['Han Solo'], 'dislikes': ['Luke']} when(app).let_down_gently(...) app.evaluate(person1, person2) verify(app, times=1).let_down_gently(...)
def test_get_valid_json(): app = TindevQA() filename = "configuration.json" expected_result = {'name': 'Leia', 'likes': ['Han Solo'], 'dislikes': ['Luke']} mock_file = mock() when(mock_file).read().thenReturn('{"name": "Leia", "likes": ["Han Solo"], "dislikes": ["Luke"]}') when(builtins).open(filename).thenReturn(mock_file) actual_result = app.get_information_from_file(filename) assert expected_result == actual_result
def test_new_person(): # Arrange app = TindevQA() user = {'people_seen': []} expected_person = 'Alexandra' # Action actual_person = app.get_next_person(user) # Assert assert actual_person == expected_person
def test_send_sms_with_error(): app = TindevQA() user = '******' text = 'Quieres enredarte entre mis pelos?' data = {'status_code': 500, 'message': 'Servidor Caido'} r = requests.post('http://localhost:8080/set_response', json=data) response = app.send_sms(user, text) assert response.status_code == 500 assert response.text == 'Servidor Caido'
def test_person2_dislikes_person1_multiple_mocks(mock_let_down, mock_send_mail, mock_give_it_time): app = TindevQA() person1 = 'Luke' person2 = {'name': 'Leia', 'likes': ['Han Solo'], 'dislikes': ['Luke']} app.evaluate(person1, person2) mock_let_down.assert_called_once_with(person1) assert mock_send_mail.call_count == 0 assert mock_give_it_time.call_count == 0
def test_new_person_several_times(mock_get_random_user): app = TindevQA() user = {'people_seen': ['Sara', 'Alexandra']} expected_person = 'Leia' mock_get_random_user.side_effect = ['Alexandra', 'Sara', 'Leia'] # Action actual_person = app.get_next_person(user) # Assert assert actual_person == expected_person
def test_new_person_decorator(mock_get_random_user): # Arrange app = TindevQA() user = {'people_seen': []} expected_person = 'Alexandra' mock_get_random_user.return_value = 'Alexandra' # Action actual_person = app.get_next_person(user) # Assert assert actual_person == expected_person
def test_new_person_decorator(): # Arrange app = TindevQA() user = {'people_seen': []} expected_person = 'Alexandra' when(app).get_random_user().thenReturn('Alexandra') # Action actual_person = app.get_next_person(user) # Assert assert actual_person == expected_person
def test_person2_likes_person1(mock_send_mail): app = TindevQA() person1 = 'Han Solo' person2 = {'name': 'Leia', 'likes': ['Han Solo'], 'dislikes': ['Luke']} app.evaluate(person1, person2) person1_mail_received = mock_send_mail.call_args_list[0] person2_mail_received = mock_send_mail.call_args_list[1] assert person1_mail_received == call(person1) assert person2_mail_received == call(person2)
def test_get_information_valueerror(mock_open, mock_loads): app = TindevQA() filename = "configuration.json" mock_file = Mock() mock_file.read.return_value = '{"name": "Leia", "likes": ["Han Solo"], "dislikes": ["Luke"]}' mock_open.return_value = mock_file mock_loads.side_effect = ValueError actual_result = app.get_information_from_file(filename) assert actual_result == "ERROR"
def test_new_person_several_times(): app = TindevQA() user = {'people_seen': ['Sara', 'Alexandra']} expected_person = 'Leia' when(app).get_random_user().thenReturn('Alexandra', 'Sara', 'Leia') # Action actual_person = app.get_next_person(user) # Assert assert actual_person == expected_person
def test_new_person_context_manager(): with patch.object(TindevQA, "get_random_user") as mock_get_random_user: # Arrange app = TindevQA() user = {'people_seen': []} expected_person = 'Alexandra' mock_get_random_user.return_value = 'Alexandra' # Action actual_person = app.get_next_person(user) # Assert assert actual_person == expected_person
def test_person2_likes_person1(): app = TindevQA() person1 = 'Han Solo' person2 = {'name': 'Leia', 'likes': ['Han Solo'], 'dislikes': ['Luke']} when(app).send_email(...) app.evaluate(person1, person2) verify(app, times=2).send_email(...) verify(app, times=1).send_email(person1) verify(app, times=1).send_email(person2)
def test_send_sms(): app = TindevQA() user = '******' text = 'Quieres enredarte entre mis pelos?' response = app.send_sms(user, text) r = requests.get('http://localhost:8080/stats') body = r.json() assert body['requests'][0]['from'] == 'Chewaka' assert body['requests'][0]['to'] == '34677722314' assert body['requests'][0]['text'] == 'Quieres enredarte entre mis pelos?'
def test_person2_dislikes_person1_multiple_mocks(): app = TindevQA() person1 = 'Luke' person2 = {'name': 'Leia', 'likes': ['Han Solo'], 'dislikes': ['Luke']} when(app).let_down_gently(person1) when(app).send_email(...) when(app).give_it_time(...) app.evaluate(person1, person2) verify(app, times=1).let_down_gently(person1) verify(app, times=0).send_email(...) verify(app, times=0).give_it_time(...)
def test_get_valid_json(mock_open): app = TindevQA() filename = "configuration.json" expected_result = { 'name': 'Leia', 'likes': ['Han Solo'], 'dislikes': ['Luke'] } mock_file = Mock() mock_file.read.return_value = '{"name": "Leia", "likes": ["Han Solo"], "dislikes": ["Luke"]}' mock_open.return_value = mock_file actual_result = app.get_information_from_file(filename) assert actual_result == expected_result
def test_send_sms(): app = TindevQA() user = '******' text = 'Quieres enredarte entre mis pelos?' response = app.send_sms(user, text) assert response.ok == True