def test_calculate_values():
    mock_game = get_mock_game

    service = GameService(mock_game)
    service._generate_board(5, 5, 4)

    mock_board_mines = [[-1, 0, 0, 0, 0], [-1, 0, -1, 0, 0], [-1, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

    mock_board = []

    for row in mock_board_mines:
        mock_row = []
        for column in row:
            mock_row.append({"value": column, "status": "C"})
        mock_board.append(mock_row)

    mock_game.board = mock_board

    service._calculate_values(5, 5)

    values = []
    for row in mock_board:
        row_values = []
        for column in row:
            row_values.append(column["value"])
        values.append(row_values)

    expected_values = [[-1, 3, 1, 1, 0], [-1, 4, -1, 1, 0], [-1, 3, 1, 1, 0],
                       [1, 1, 0, 0, 0], [0, 0, 0, 0, 0]]

    assert values == expected_values
def test_encode_game_info():
    service = GameService(get_mock_game())
    service._generate_board(10, 10, 20)
    encoded = service.encode_game_info()

    assert isinstance(encoded["board"], list)
    assert encoded["status"] == "started"
    assert encoded["mines_left"] == 20
    assert encoded["id"] == 1337
def test_place_mines():
    mock_game = get_mock_game()

    service = GameService(mock_game)
    service._generate_board(10, 10, 4)

    mine_count = 0
    for row in mock_game.board:
        for column in row:
            if column["value"] == -1:
                mine_count += 1
    assert mine_count == 4
def test_generate_board():
    mock_game = get_mock_game()

    service = GameService(mock_game)
    service._generate_board(20, 20, 10)

    assert len(mock_game.board) == 20
    assert len(mock_game.board[0]) == 20

    covered_count = 0
    for row in mock_game.board:
        for column in row:
            if column["status"] == "C":
                covered_count += 1

    assert covered_count == 20 * 20
def test_generate_board_should_fail_if_any_input_smaller_than_1():
    with pytest.raises(InvalidGameSettingsException) as e:
        mock_game = get_mock_game()
        service = GameService(mock_game)
        service._generate_board(5, 5, -26)
        assert e.message == "All values must be greater than zero"
def test_generate_board_should_fail_if_mine_count_greater_than_board_size():
    with pytest.raises(InvalidGameSettingsException) as e:
        mock_game = get_mock_game()
        service = GameService(mock_game)
        service._generate_board(5, 5, 26)
        assert e.message == "Number of mines must be less than the total board size"