Пример #1
0
    async def test_games_fully_seated(self, guild: Guild, channel: Channel):
        started_game = GameFactory.create(guild=guild, channel=channel)
        pending_game = GameFactory.create(guild=guild, channel=channel)
        for _ in range(started_game.seats):
            UserFactory.create(game=started_game)
        UserFactory.create(game=pending_game)

        games = GamesService()
        await games.select(started_game.id)
        assert await games.fully_seated()
        await games.select(pending_game.id)
        assert not await games.fully_seated()
Пример #2
0
    async def test_games_select_by_message_xid(self, guild: Guild,
                                               channel: Channel):
        game = GameFactory.create(guild=guild, channel=channel)

        games = GamesService()
        assert await games.select_by_message_xid(game.message_xid)
        assert not await games.select_by_message_xid(404)
Пример #3
0
    async def test_lfg_with_friend_when_game_wrong_format(
            self, guild: Guild, channel: Channel):
        user1 = UserFactory.create(xid=101)
        user2 = UserFactory.create(xid=102)
        user3 = UserFactory.create(xid=103)
        bad_game = GameFactory.create(
            seats=4,
            channel=channel,
            guild=guild,
            format=GameFormat.TWO_HEADED_GIANT.value,
        )

        games = GamesService()
        new = await games.upsert(
            guild_xid=guild.xid,
            channel_xid=channel.xid,
            author_xid=user1.xid,
            friends=[user2.xid, user3.xid],
            seats=4,
            format=GameFormat.COMMANDER.value,
        )
        assert new

        DatabaseSession.expire_all()
        game = DatabaseSession.query(Game).filter(Game.id != bad_game.id).one()
        assert game.guild_xid == guild.xid
        assert game.channel_xid == channel.xid
        rows = DatabaseSession.query(
            User.xid).filter(User.game_id == game.id).all()
        assert set(row[0] for row in rows) == {101, 102, 103}
 def it_draws_from_the_expected_range(self):
     matrix = Factory.create_observation_matrix(sample_count=5)
     game = matrix.toGame()
     indexes = np.random.random_integers(0, game.max_samples - 1, size=1000)
     assert min(indexes) == 0
     assert max(indexes) == 4
     assert len(indexes) == 1000
 def it_stores_profile_info(self):
     noise_model = MultimodalNormalNoise(1)
     game = Factory.create_symmetric_game()
     profile = game.knownProfiles()[0]
     noise_model.generate_samples(game, profile, 1)
     profile_info = noise_model.profile_info.get(profile, None)
     assert profile_info != None
     profile_info = profile_info.values()[0].values()[0]
     assert profile_info.get("offset", None) != None
     assert profile_info.get("stdev", None) != None
 def it_does_not_overwrite_profile_info(self):
     noise_model = MultimodalNormalNoise(1)
     game = Factory.create_symmetric_game()
     profile = game.knownProfiles()[0]
     noise_model.generate_samples(game, profile, 1)
     profile_info = noise_model.profile_info.get(profile, None)
     noise_model.generate_samples(game, profile, 1)
     profile_info2 = noise_model.profile_info.get(profile, None)
     r = profile.keys()[0]
     s = profile[r].keys()[0]
     assert profile_info[r][s]["offset"] == profile_info2[r][s]["offset"]
     assert profile_info[r][s]["stdev"] == profile_info2[r][s]["stdev"]
 def test_prediction_serializers(self):
     user = UserFactory()
     game = GameFactory()
     prediction = PredictionFactory(user=user, game=game)
     expected = {
         "game": game.id,
         "id": prediction.id,
         "scores": list(prediction.scores.all()),
         "user": user.id,
     }
     serializer = PredictionSerializer(prediction)
     assert serializer.data == expected
 def it_returns_the_correct_number_of_samples(self):
     noise_model = MultimodalNormalNoise(1)
     game = Factory.create_symmetric_game()
     profile = game.knownProfiles()[0]
     samples = noise_model.generate_samples(game, profile, 5)
     for role, strategy_hash in profile.items():
         counters = {s: 0 for s in strategy_hash.keys()}
         for payoff_data in samples[role]:
             counters[payoff_data.strategy] += 1
             assert payoff_data.count == strategy_hash[payoff_data.strategy]
             assert len(payoff_data.value) == 5
         for s in strategy_hash.keys():
             assert counters[s] == 1
Пример #9
0
 def test_Game_game_serializers(self):
     game = GameFactory()
     expected = {
         "date": game.date.isoformat() + "Z",
         "matches": list(game.matches.all()),
         "number_of_matches": game.number_of_matches,
         "players": list(game.players.all()),
         "score": "0:0",
         "slug": game.slug,
         "tournament": TournamentSerializerSmall(game.tournament).data,
         "winner": game.winner,
     }
     serializer = GameSerializer(game)
     assert serializer.data == expected
Пример #10
0
    def test_delete_game(self, client, db, admin_headers):
        # test 404
        game_url = url_for('api.game_by_id', game_id=1000000)
        rep = client.delete(game_url, headers=admin_headers)
        assert rep.status_code == HTTPStatus.NOT_FOUND

        game = GameFactory.build()
        db.session.add(game)
        db.session.commit()

        # test get game after deletion
        game_url = url_for('api.game_by_id', game_id=game.id)
        rep = client.delete(game_url, headers=admin_headers)
        assert rep.status_code == HTTPStatus.NO_CONTENT
        assert db.session.query(Game).filter_by(id=game.id).first() is None
Пример #11
0
def db(app):
    _db.app = app
    _db.create_all()

    LanguageFactory.create_batch(4)
    _db.session.commit()

    WordFactory.create_batch(60)
    _db.session.commit()

    GameFactory.create_batch(5)
    _db.session.commit()

    PlayerFactory.create_batch(2)
    _db.session.commit()

    PlayFactory.create_batch(60)
    _db.session.commit()

    yield _db

    _db.session.rollback()
    _db.session.close()
    _db.drop_all()
 def it_gives_the_requested_observations_with_duplicates(self):
     matrix = Factory.create_observation_matrix(sample_count=5)
     game = matrix.toGame()
     sbg = SimulationBasedGame(game)
     target_profile = game.knownProfiles()[0]
     observations = sbg.get_observations(target_profile, [0, 2, 2])
     assert observations == {
         r: [
             PayoffData(
                 s,
                 count,
                 [
                     game.getPayoffData(target_profile, r, s)[0],
                     game.getPayoffData(target_profile, r, s)[2],
                     game.getPayoffData(target_profile, r, s)[2],
                 ],
             )
             for s, count in s_hash.items()
         ]
         for r, s_hash in target_profile.items()
     }
Пример #13
0
    def test_put_game(self, client, db, admin_headers):
        # test 404
        game_url = url_for('api.game_by_id', game_id=1000000)
        rep = client.put(game_url, headers=admin_headers)
        assert rep.status_code == HTTPStatus.NOT_FOUND

        game = GameFactory.build()
        db.session.add(game)
        db.session.commit()

        new_data = {"date": str(datetime(2020, 12, 31))}

        # test updated game
        game_url = url_for('api.game_by_id', game_id=game.id)
        rep = client.put(game_url, json=new_data, headers=admin_headers)
        assert rep.status_code == HTTPStatus.OK

        data = rep.get_json()["game"]
        assert data["date"] == '2020-12-31T00:00:00'

        db.session.refresh(game)
Пример #14
0
    async def test_games_record_plays(self, guild: Guild, channel: Channel):
        game = GameFactory.create(
            guild=guild,
            channel=channel,
            seats=2,
            status=GameStatus.STARTED.value,
        )
        user1 = UserFactory.create(xid=101, game=game)
        user2 = UserFactory.create(xid=102, game=game)

        games = GamesService()
        await games.select(game.id)
        await games.record_plays()

        DatabaseSession.expire_all()
        found = DatabaseSession.query(Play).filter(
            Play.user_xid == user1.xid).one()
        assert found.user_xid == user1.xid and found.game_id == game.id
        found = DatabaseSession.query(Play).filter(
            Play.user_xid == user2.xid).one()
        assert found.user_xid == user2.xid and found.game_id == game.id
        found = DatabaseSession.query(Play).filter(
            Play.user_xid == 103).one_or_none()
        assert not found
 def it_provides_a_two_sided_confidence_interval(self):
     bootstrap_method = lambda *args: [i+1 for i in range(100)]
     ci = BootstrapConfidenceInterval(bootstrap_method)
     print ci.two_sided_interval(Factory.create_observation_matrix(), "fake profile", 0.90)
     assert ci.two_sided_interval(Factory.create_observation_matrix(), "fake profile", 0.95) == [3, 97]