Ejemplo n.º 1
0
def test_gamelist_new(client):
    game_ids = [o[0] for o in Game.select(Game.id).tuples()]
    db_wrapper.database.close()

    resp = client.post('/games')
    assert resp.status_code == 201
    data = resp.json
    assert data['id'] not in game_ids
Ejemplo n.º 2
0
def test_gameslist_get(client):
    resp = client.get('/games')
    assert resp.status_code == 200
    data = resp.json
    games = data['games']
    assert len(games) == 1
    game = games[0]
    game_id = Game.get(Game.id == game['id']).id
    assert game['id'] == game_id
Ejemplo n.º 3
0
def test_roundslist_post(client):
    expected_game = Game.select()[:1][0]
    db_wrapper.database.close()

    req = {'hand': ['GREEN', 'BLUE', 'YELLOW', 'MAGENTA']}
    resp = client.post(
        f"/games/{expected_game.id}/rounds",
        data=json.dumps(req),
        content_type='application/json',
    )
    assert resp.status_code == 201
    data = resp.json
    assert data == {'answer': ['BLACK', 'BLACK', 'WHITE']}
Ejemplo n.º 4
0
def test_roundslist_post_win_edge(client):
    expected_game = Game.select()[:1][0]
    # we have 2 rounds from conftest, make it so that the game ends on next round
    expected_game.num_rounds = 3
    expected_game.save()
    db_wrapper.database.close()

    # should be case insensitive
    # win combination
    req = {'hand': ['GREEN', 'BLUE', 'cyan', 'yellow']}
    resp = client.post(
        f"/games/{expected_game.id}/rounds",
        data=json.dumps(req),
        content_type='application/json',
    )
    assert resp.status_code == 201
    data = resp.json
    # win condition
    assert data == {'answer': ['BLACK', 'BLACK', 'BLACK', 'BLACK']}

    game = Game.get(Game.id == expected_game.id)
    assert game.over
Ejemplo n.º 5
0
def test_roundslist_post_wrong_type(client):
    expected_game = Game.get(Game.play_colors == "1234")
    db_wrapper.database.close()

    req = {'hand': 'RED'}
    resp = client.post(
        f"/games/{expected_game.id}/rounds",
        data=json.dumps(req),
        content_type='application/json',
    )
    assert resp.status_code == 400
    data = resp.json
    assert data == {'errors': {'hand': ["<class 'str'> is not list"]}}
Ejemplo n.º 6
0
def test_roundslist_get(client):
    expected_game = Game.get(Game.play_colors == "1234")
    expected_rounds = expected_game.rounds_chrono
    expected_rounds_ids = [o.id for o in expected_rounds]
    db_wrapper.database.close()

    resp = client.get(f"/games/{expected_game.id}/rounds")
    assert resp.status_code == 200
    data = resp.json
    rounds = data['rounds']
    assert len(rounds) == 2
    rounds_ids = [o['id'] for o in rounds]
    assert rounds_ids == expected_rounds_ids
Ejemplo n.º 7
0
def test_gamesdetail_get(client):
    game_id = Game.select(Game.id).tuples()[0][0]
    db_wrapper.database.close()

    resp = client.get(f'/games/{game_id}')
    assert resp.status_code == 200
    data = resp.json
    game = data['game']
    assert game['id'] == game_id
    rounds = game['rounds_chrono']
    assert len(rounds) == 2
    # TODO add date modified to serializer and do a more robust check
    # slopy check for desc order
    assert rounds[0]['id'] > rounds[1]['id']
Ejemplo n.º 8
0
def test_roundslist_post_win(client):
    expected_game = Game.select()[:1][0]
    db_wrapper.database.close()

    # should be case insensitive
    req = {'hand': ['GREEN', 'BLUE', 'cyan', 'yellow']}
    resp = client.post(
        f"/games/{expected_game.id}/rounds",
        data=json.dumps(req),
        content_type='application/json',
    )
    assert resp.status_code == 201
    data = resp.json
    # win condition
    assert data == {'answer': ['BLACK', 'BLACK', 'BLACK', 'BLACK']}
Ejemplo n.º 9
0
def test_game_basic(db_games):
    app, games = db_games
    g1 = games[0]
    assert not g1.over

    g1.over = True
    last_modified = g1.modified
    g1.save()

    with app.app_context():
        from mastermind.db import Game
        g1_again = Game.get(Game.id == g1.id)
        assert g1.modified == g1_again.modified
        assert last_modified < g1_again.modified
        assert g1_again.over
Ejemplo n.º 10
0
def test_roundsdetail_get(client):
    expected_game = Game.get(Game.play_colors == "1234")
    expected_round1 = expected_game.rounds.where(Round.hand == "0011")[0]
    # expected_round1 = expected_rounds[0]
    # expected_round2 = expected_rounds[1]
    db_wrapper.database.close()

    resp = client.get(f"/games/{expected_game.id}/rounds/{expected_round1.id}")
    assert resp.status_code == 200
    data = resp.json
    round = data['round']
    assert round['id'] == expected_round1.id
    colors = round['hand']
    assert colors == ["RED", "RED", "GREEN", "GREEN"]
    pegs = round['answer']
    assert pegs == ['BLACK', 'BLACK', 'WHITE']
Ejemplo n.º 11
0
def test_roundslist_post_wrong_game(client):
    # fetch a finished game
    expected_game = Game.select().where(Game.over == True)[:1][0]
    db_wrapper.database.close()

    req = {'hand': None}
    resp = client.post(
        f"/games/{expected_game.id}/rounds",
        data=json.dumps(req),
        content_type='application/json',
    )
    assert resp.status_code == 400
    data = resp.json
    assert data == {
        'errors': {
            'game': [f'No such open game: {expected_game.id}']
        }
    }
Ejemplo n.º 12
0
def test_roundsdetail_get_nonexistent(client):
    expected_game = Game.get(Game.play_colors == "1234")
    db_wrapper.database.close()

    resp = client.get(f"/games/{expected_game.id}/rounds/1111111")
    assert resp.status_code == 404