def test_should_fail_when_the_guesses_limit_is_surpassed(self): expected_guess = random_guess() game = random_game(game_id=expected_guess.game_id) self.context.game_repository.insert(game) for _ in range(10): self.context.guess_repository.insert( random_guess(game_id=expected_guess.game_id) ) request_data = { "guess_id": expected_guess.guess_id.guess_id, "first_code_peg": expected_guess.first_peg.peg_type, "second_code_peg": expected_guess.second_peg.peg_type, "third_code_peg": expected_guess.third_peg.peg_type, "fourth_code_peg": expected_guess.fourth_peg.peg_type } endpoint = self.__guess_endpoint_for_game_id(expected_guess.game_id) response = self.test_client.post(endpoint, data=request_data) self.assertEqual(response.status_code, 409) self.assertNotEquals(json.loads(response.data), {}) self.assertEqual( self.context.guess_repository.search(expected_guess.guess_id), None )
def test_should_fail_when_guess_does_already_exist(self): expected_guess = random_guess() game = random_game(game_id=expected_guess.game_id) existing_guess = random_guess(guess_id=expected_guess.guess_id) self.context.game_repository.insert(game) self.context.guess_repository.insert(existing_guess) request_data = { "guess_id": expected_guess.guess_id.guess_id, "first_code_peg": expected_guess.first_peg.peg_type, "second_code_peg": expected_guess.second_peg.peg_type, "third_code_peg": expected_guess.third_peg.peg_type, "fourth_code_peg": expected_guess.fourth_peg.peg_type } endpoint = self.__guess_endpoint_for_game_id(expected_guess.game_id) response = self.test_client.post(endpoint, data=request_data) self.assertEqual(response.status_code, 409) self.assertNotEquals(json.loads(response.data), {}) self.assertEqual( self.context.guess_repository.search(expected_guess.guess_id), existing_guess )
def test_should_search_guesses_by_game_id(self): game_id = random_game_id() guesses_of_game = [random_guess(game_id=game_id) for _ in range(5)] guesses_not_of_game = [random_guess() for _ in range(5)] for guess in chain(guesses_of_game, guesses_not_of_game): self.guess_repository.insert(guess) found = self.guess_repository.search_by_game_id(game_id) self.assertEqual(len(found), len(guesses_of_game)) for guess in guesses_of_game: self.assertIn(guess, found) for guess in guesses_not_of_game: self.assertNotIn(guess, found)
def test_should_search_guess(self): guess = random_guess() self.guess_repository.insert(guess) self.assertEqual( guess, self.guess_repository.search(guess.guess_id) )
def test_invalid_fourth_code_peg(self): guess = random_guess() request_data = { "guess_id": guess.guess_id.guess_id, "first_code_peg": guess.first_peg.peg_type, "second_code_peg": guess.second_peg.peg_type, "third_code_peg": guess.third_peg.peg_type, "fourth_code_peg": invalid_code_peg() } endpoint = self.__guess_endpoint_for_game_id(guess.game_id) response = self.test_client.post(endpoint, data=request_data) self.assertEqual(response.status_code, 422)
def test_should_fail_if_guess_does_already_exist(self): command = random_create_guess_command() game_id = GameId(command.game_id) guess_id = GuessId(command.guess_id) existing_guess = random_guess(guess_id=guess_id) search_game_response = SearchGameResponse( random_game_response(game_id=game_id.game_id)) self.should_ask_query(SearchGameQuery(command.game_id), search_game_response) self.should_find_guess(guess_id, existing_guess) with self.assertRaises(GuessAlreadyExists) as context: self.handler.handle(command) self.assertEqual(context.exception, GuessAlreadyExists(guess_id))
def test_should_fail_if_ten_guesses_already_exist_for_game(self): command = random_create_guess_command() game_id = GameId(command.game_id) guess_id = GuessId(command.guess_id) search_game_response = SearchGameResponse( random_game_response(game_id=game_id.game_id)) already_existing_guesses = [random_guess() for _ in range(10)] self.should_ask_query(SearchGameQuery(command.game_id), search_game_response) self.should_not_find_guess(guess_id) self.should_find_guesses_by_game_id(game_id, already_existing_guesses) with self.assertRaises(GuessesLimitExceeded) as context: self.handler.handle(command) self.assertEqual(context.exception, GuessesLimitExceeded(GameId(command.game_id), 10))
def test_should_insert_guess(self): guess = random_guess() self.guess_repository.insert(guess)