def test_namespace(): # pylint: disable=line-too-long table = ("P1:0", "P2:2", "P3:3", "P4", "P5") game = Game.from_table(table, date=dateutil.parser.parse("2018-03-20"), namespace=parse_namespace("something")) assert list(game.namespace) == ["something"] assert str( game ) == "2018-03-20: P1 \u25b6 P2 2VP \u25b6 P3 3VP GW \u25b6 P4 \u25b6 P5 | something" assert game.in_namespace(('something', )) assert not game.in_namespace(('something', 'else')) assert not game.in_namespace(('some', )) game = Game.from_table(table, date=dateutil.parser.parse("2018-03-20"), namespace=parse_namespace("something/else")) assert list(game.namespace) == ["something", "else"] assert str( game ) == "2018-03-20: P1 \u25b6 P2 2VP \u25b6 P3 3VP GW \u25b6 P4 \u25b6 P5 | something/else" assert game.in_namespace(('something', )) assert game.in_namespace(('something', 'else')) assert not game.in_namespace(('something', 'else', 'matters')) game = Game.from_table(table, date=dateutil.parser.parse("2018-03-20")) assert not game.in_namespace(('something', )) assert not game.in_namespace(('some', 'thing'))
def test_store_deck_rankings(): store = GameStore() store.add( Game.from_table(("1(A):3", "2(B):1", "3(C):1", "4(D)", "5"), namespace="na/me/space")) store.add( Game.from_table(("A(A):4", "2(B):1", "C(C)", "D", "E"), namespace="na/me/spade")) rankings = store.decks(player=None) assert len(rankings) == 6 assert rankings[0] == DeckRanking("A", "A", 1, 1, 4, 5) assert rankings[1] == DeckRanking("A", "1", 1, 1, 3, 5) assert rankings[2] == DeckRanking("B", "2", 0, 2, 2, 10) assert rankings[3] == DeckRanking("C", "3", 0, 1, 1, 5) player_rankings = store.decks(player="2") assert len(player_rankings) == 1 assert player_rankings[0] == DeckRanking("B", "2", 0, 2, 2, 10) store.add( Game.from_table(("A(AA):3", "2(BB):1", "C(CC):1", "D", "E"), namespace="diff/er/ent")) deck_rankings = store.decks(namespace="na/me/space") assert len(deck_rankings) == 4 assert deck_rankings[0] == DeckRanking("A", "1", 1, 1, 3, 5) deck_rankings = store.decks(namespace="na") assert len(deck_rankings) == 6 assert deck_rankings[0] == DeckRanking("A", "A", 1, 1, 4, 5) deck_rankings = store.decks(player="2", namespace="na/me/spade") assert len(deck_rankings) == 1 assert deck_rankings[0] == DeckRanking("B", "2", 0, 1, 1, 5)
def test_fix(): store = GameStore() store.add(Game.from_table(("1", "2", "3", "4", "5"))) store.fix(0, Game.from_table(("A", "B", "C", "D", "E"))) assert len(store) == 1 games = list(store) assert str(games[0]) == "A \u25b6 B \u25b6 C \u25b6 D \u25b6 E"
def test_store(): store = GameStore() assert len(store) == 0 # pylint: disable=len-as-condition store.add(Game.from_table(("1", "2", "3", "4", "5"))) assert len(store) == 1 store.add(Game.from_table(("A", "B", "C", "D", "E"))) assert len(store) == 2 games = list(store) assert str(games[0]) == "1 \u25b6 2 \u25b6 3 \u25b6 4 \u25b6 5" assert str(games[1]) == "A \u25b6 B \u25b6 C \u25b6 D \u25b6 E"
def test_store_rankings(): store = GameStore() store.add(Game.from_table(("1:3", "2:1", "3:1", "4", "5"))) store.add(Game.from_table(("A:4", "2:1", "C", "D", "E"))) rankings = store.rankings() assert len(rankings) == 9 assert rankings[0] == Ranking("A", 1, 4, 1) assert rankings[1] == Ranking("1", 1, 3, 1) assert rankings[2] == Ranking("2", 0, 2, 2) assert rankings[3] == Ranking("3", 0, 1, 1) for loser in ("4", "5", "C", "D", "E"): assert Ranking(loser, 0, 0, 1) in rankings
def test_picklestore_save_load(fs): # pylint: disable=unused-argument, invalid-name store = PickleStore("file") store.add(Game.from_table(("1", "2", "3", "4", "5"))) store.add(Game.from_table(("A", "B", "C", "D", "E"))) store.save() new_store = PickleStore("file") new_store.open() assert len(new_store) == 2 games = list(new_store) assert str(games[0]) == "1 \u25b6 2 \u25b6 3 \u25b6 4 \u25b6 5" assert str(games[1]) == "A \u25b6 B \u25b6 C \u25b6 D \u25b6 E"
def as_game(self) -> Game: """Returns a 'Game' object from a database object""" players: Sequence[Player] = [ player.as_player() for player in self.players ] return Game(players, self.winner, self.winning_points, self.date, self.namespaces)
def test_date(): table = ("P1:0", "P2:2", "P3:3", "P4", "P5") game = Game.from_table(table, date=dateutil.parser.parse("2018-03-20")) assert game.date == dateutil.parser.parse("2018-03-20") assert str( game ) == "2018-03-20: P1 \u25b6 P2 2VP \u25b6 P3 3VP GW \u25b6 P4 \u25b6 P5"
def test_colorize(): table = ("P1:0", "P2:2", "P3:3", "P4", "P5") game = Game.from_table(table) set_colorize(True) assert str(game) == "P1 \u25b6 P2 2VP \u25b6 P3 3VP GW \u25b6 P4 \u25b6 P5" set_colorize(False) assert str(game) == "P1 \u25b6 P2 2VP \u25b6 P3 3VP GW \u25b6 P4 \u25b6 P5"
def test_game_with_vps(): table = ("P1:0", "P2:2", "P3:3", "P4", "P5") game = Game.from_table(table) assert game.players == ["P1", "P2", "P3", "P4", "P5"] assert str(game) == "P1 \u25b6 P2 2VP \u25b6 P3 3VP GW \u25b6 P4 \u25b6 P5" assert game.winner == "P3" assert game.winning_points == 3
def test_game_with_draw(): table = ("P1:0", "P2:2", "P3:2", "P4:1", "P5") game = Game.from_table(table) assert game.players == ["P1", "P2", "P3", "P4", "P5"] assert str( game) == "P1 \u25b6 P2 2VP \u25b6 P3 2VP \u25b6 P4 1VP \u25b6 P5" assert game.winner is None assert game.winning_points is None
def add_command(players: Sequence[str], journal: StorageBackedStore, date: datetime.datetime, namespace: Optional[GameNamespace]) -> None: """Create a new Game and add it to the store""" try: journal.open() except FileNotFoundError: # No problem, we will create the file when we save pass game = Game.from_table(players, date=date, namespace=namespace) journal.add(game) journal.save()
def gamefix_command(game_index: int, journal: StorageBackedStore, players: Sequence[str], date: datetime.datetime, namespace: Optional[GameNamespace]) -> None: """Change the properties of an existing game""" journal.open() old = list(journal)[game_index] game = Game.from_table(players) if players else old game.date = date or old.date game.namespace = namespace or old.namespace journal.fix(game_index, game) journal.save()
def store_with_two_games(fs): # pylint: disable=invalid-name, unused-argument store = PickleStore("file") store.add(Game.from_table(("1:3", "2:2", "3", "4", "5"))) store.add(Game.from_table(("A:2", "B:1", "C:1", "D:1", "E"))) return store
def test_game(): table = ("P1", "P2", "P3", "P4", "P5") game = Game.from_table(table) assert str(game) == "P1 \u25b6 P2 \u25b6 P3 \u25b6 P4 \u25b6 P5"