Beispiel #1
0
    def post(self, game_id):
        json_data = request.get_json() or {}
        try:
            g_data = self._game_repository.first_or_raise(
                **{'id': str(game_id)})

            guess = Guess(json_data.get('guess', []))

            board = Board(
                game=Game.from_data(g_data),
                turns=[Guess(g.value.split(';')) for g in g_data.guesses])
            result = board.new_turn(guess=guess)

            self._guess_repository.create(guess, g_data)

            return make_response(json.dumps({'result': result}))

        except DataNotFoundException as _:
            return make_response(
                'Game with id \'{0}\' not found'.format(game_id), 404)
        except InvalidGuessLength as e:
            return make_response(e.args[0], 400)
        except FinishedGameException as e:
            return make_response(e.args[0], 400)
        except KeyError as e:
            return make_response('{0} is not a valid color'.format(e.args[0]),
                                 400)
        except Exception as e:
            return make_response(e.args[0], 500)
 def test_new_turn_raise_exception(self, input_secret, input_old_guesses, input_new_guess, expected_msg):
     with pytest.raises(FinishedGameException) as e:
         Board(
             game=Game(secret=Guess(input_secret)),
             turns=[Guess(g) for g in input_old_guesses]
         ).new_turn(guess=Guess(input_new_guess))
     assert e.value.args[0] == expected_msg
Beispiel #3
0
    def post(self):
        json_data = request.get_json() or {}
        try:
            secret = Guess(json_data.get('secret', []))
            game = self._game_repo.create(Game(secret=secret))

            response = make_response(json.dumps(self._game_repo.dump(game)),
                                     201)
            response.headers['Content-Type'] = 'application/json'
            return response
        except InvalidGuessLength as e:
            return make_response(e.args[0], 400)
        except KeyError as e:
            return make_response('{0} is not a valid color'.format(e.args[0]),
                                 400)
        except Exception as e:
            return make_response(e.args[0], 500)
Beispiel #4
0
 def from_data(cls, data: GameData):
     return cls(secret=Guess(values=data.secret.split(';')))
Beispiel #5
0
 def eval_guess(self, guess: Guess) -> Result:
     return guess.eval(self._secret)
 def test_eval(self, input_guess, secret):
     res = Guess(input_guess).eval(Guess(secret))
     assert isinstance(res, Result)
 def test_values(self, input_guess, expected):
     guess = Guess(input_guess)
     assert guess.values == expected
 def test_guess_creation(self, input_guess, expected):
     guess = Guess(input_guess)
     expected_guess = Guess(expected)
     assert guess == expected_guess
 def test_raise_when_invalid_length(self, input_guess):
     with pytest.raises(InvalidGuessLength) as _:
         Guess(input_guess)
Beispiel #10
0
 def test_guessed(self, input_secret, input_guess, expected):
     secret = Guess(input_secret)
     guess = Guess(input_guess)
     assert Result(secret=secret, current=guess).guessed() == expected
 def test_secret_raw_values(self, input_secret, expected):
     secret = Guess(input_secret)
     result = self._service(secret).secret_raw_values
     assert result == expected
 def test_eval_guess(self, input_secret, input_guess, expected):
     secret = Guess(input_secret)
     guess = Guess(input_guess)
     result = self._service(secret).eval_guess(guess)
     assert result.values == expected