def setUp(self): self.game = testhelper.createMock(euchre.Game) self.sequence = testhelper.createMock(euchre.Sequence) self.round = testhelper.createMock(euchre.Round) self.teamRetriever = testhelper.createSingletonMock(retriever.TeamRetriever) self.gameScores = [random.randint(0, 9), random.randint(0, 9)] self.playerIds = ["1234", "4567", "7890", "0123"] self.players = [game.Player.getInstance(playerId) for playerId in self.playerIds] self.teams = [self.playerIds[:2], self.playerIds[2:]] self.roundScores = {} for playerId in self.playerIds: self.roundScores[playerId] = random.randint(0, 5) when(self.game).getSequence().thenReturn(self.sequence) when(self.game).getPlayers().thenReturn(self.players) when(self.game).getTeamScore(0).thenReturn(self.gameScores[0]) when(self.game).getTeamScore(1).thenReturn(self.gameScores[1]) when(self.sequence).getRound().thenReturn(self.round) for playerId in self.playerIds: when(self.round).getScore(playerId).thenReturn(self.roundScores[playerId]) for teamId in range(len(self.teams)): for playerId in self.teams[teamId]: when(self.teamRetriever).retrieveTeamByPlayerId(self.game, playerId).thenReturn(teamId) self._buildTestObj()
def testGetGameByGameIdReturnsCorrectGameModel(self): gameId = "864" gameModel = testhelper.createMock(model.GameModel) key = testhelper.createMock(ndb.Key) when(key).get().thenReturn(gameModel) self.testObj._getKeyByUrl = lambda url: key if gameId == url else None result = self.testObj.getGameByGameId(gameId) self.assertEqual(gameModel, result)
def testGetGamesForPlayerIdReturnsGameModelsFromQuery(self): playerId = "12345" models = [testhelper.createMock(model.GameModel), testhelper.createMock(model.GameModel)] query = testhelper.createMock(ndb.Query) when(query).fetch(any(int)).thenReturn(models).thenReturn([]) self.testObj._getQuery = lambda pid: query if playerId == pid else None result = self.testObj.getGamesForPlayerId(playerId) self.assertEqual(models, result)
def setUp(self): self.game = testhelper.createMock(euchre.Game) self.sequence = testhelper.createMock(euchre.Sequence) self.targetScore = euchre.WINNING_SCORE when(self.game).getSequence().thenReturn(self.sequence) when(self.game).getTeamScore(0).thenReturn(0) when(self.game).getTeamScore(1).thenReturn(0) self.testObj = retriever.GameStatusRetriever.getInstance(self.targetScore)
def setUp(self): self.trumpSelector = testhelper.createMock(euchre.TrumpSelector) self.round = testhelper.createMock(euchre.Round) self.upCard = euchre.Card(euchre.SUIT_DIAMONDS, euchre.VALUE_KING) self.sequence = euchre.Sequence(self.trumpSelector, self.round, self.upCard) self.trumpSelectorSerializer = testhelper.createSingletonMock(serializer.TrumpSelectorSerializer) self.roundSerializer = testhelper.createSingletonMock(serializer.RoundSerializer) self.cardSerializer = testhelper.createSingletonMock(serializer.CardSerializer) self.testObj = serializer.SequenceSerializer.getInstance()
def setUp(self): self.playerSerializer = testhelper.createSingletonMock(serializer.PlayerSerializer) self.scoreTrackerSerializer = testhelper.createSingletonMock(serializer.ScoreTrackerSerializer) self.sequenceSerializer = testhelper.createSingletonMock(serializer.SequenceSerializer) self.players = [game.Player("1"), game.Player("2"), game.Player("3"), game.Player("4")] self.scoreTracker = testhelper.createSingletonMock(euchre.ScoreTracker) self.sequence = testhelper.createMock(euchre.Sequence) self.prevSequence = testhelper.createMock(euchre.Sequence) self.game = euchre.Game.getInstance(self.players, [["1", "2"], ["3", "4"]]) self.testObj = serializer.GameSerializer.getInstance()
def testGetPreviousSequenceReturnsPreviousSequenceIfAvailable(self): sequenceFactory = testhelper.createSingletonMock(euchre.SequenceFactory) firstSequence = testhelper.createMock(euchre.Sequence) secondSequence = testhelper.createMock(euchre.Sequence) when(firstSequence).getState().thenReturn(euchre.Sequence.STATE_COMPLETE) when(sequenceFactory).buildSequence(any(), any(), any()).thenReturn(firstSequence).thenReturn(secondSequence) self._buildTestObj() self.game.startGame() self.game.playCard(self.players[0], euchre.Card(euchre.SUIT_DIAMONDS, euchre.VALUE_JACK)) self.assertEqual(firstSequence, self.game.getPreviousSequence())
def setUp(self): self.game = testhelper.createMock(euchre.Game) self.sequence = testhelper.createMock(euchre.Sequence) self.round = testhelper.createMock(euchre.Round) self.trumpSuit = random.randint(1, 4) when(self.game).getSequence().thenReturn(self.sequence) when(self.sequence).getRound().thenReturn(self.round) when(self.round).getTrump().thenReturn(self.trumpSuit) self._buildTestObj()
def testRetrieveUpCardReturnsCorrectData(self): expectedCard = euchre.Card(euchre.SUIT_SPADES, 10) gameObj = testhelper.createMock(euchre.Game) sequence = testhelper.createMock(euchre.Sequence) when(gameObj).getSequence().thenReturn(sequence) when(sequence).getState().thenReturn(euchre.Sequence.STATE_TRUMP_SELECTION) when(sequence).getUpCard().thenReturn(expectedCard) result = self.testObj.retrieveUpCard(gameObj) self.assertEqual(expectedCard, result)
def testRetrieveUpCardReturnsNoneIfSequenceNotInCorrectState(self): expectedCard = euchre.Card(euchre.SUIT_SPADES, 10) gameObj = testhelper.createMock(euchre.Game) sequence = testhelper.createMock(euchre.Sequence) when(gameObj).getSequence().thenReturn(sequence) when(sequence).getState().thenReturn(euchre.Sequence.STATE_PLAYING_ROUND) when(sequence).getUpCard().thenReturn(expectedCard) result = self.testObj.retrieveUpCard(gameObj) self.assertEqual(None, result)
def testDeleteGameDeletesSpecifiedGameAndReturnsTrue(self): gameId = "8237" gameModel = testhelper.createMock(model.GameModel) key = testhelper.createMock(ndb.Key) gameModel.key = key when(key).get().thenReturn(gameModel) self.testObj._getKeyByUrl = lambda url: key if gameId == url else None result = self.testObj.deleteGame(gameId) verify(key).delete() self.assertTrue(result)
def setUp(self): self.hands = {"1" : [self._randomCard(), self._randomCard()], "2" : [self._randomCard(), self._randomCard()]} self.trickEvaluator = testhelper.createMock(euchre.TrickEvaluator) self.trickEvaluatorSerializer = testhelper.createSingletonMock(serializer.TrickEvaluatorSerializer) self.trump = random.randint(1, 4) when(self.trickEvaluator).getTrump().thenReturn(self.trump) self.turnTracker = testhelper.createMock(game.TurnTracker) self.turnTrackerSerializer = testhelper.createSingletonMock(serializer.TurnTrackerSerializer) self.trickSerializer = testhelper.createSingletonMock(serializer.TrickSerializer) self.cardSerializer = testhelper.createSingletonMock(serializer.CardSerializer) self.round = euchre.Round(self.turnTracker, self.trickEvaluator, self.hands, euchre.CardTranslator.getInstance(euchre.SUIT_NONE)) self.testObj = serializer.RoundSerializer.getInstance()
def testDeserializesRoundCorrectly(self): players = "some players" serializedTurnTracker = "a turn tracker" serializedTrickEvaluator = "a trick evaluator" hands = {"1" : ["card 1", "card 2"], "2" : ["card 3", "card 4"]} curTrick = "trick 1" prevTricks = ["trick 2", "trick 3"] scores = {"1" : 3, "2" : 2} data = {"turnTracker" : serializedTurnTracker, "trickEvaluator" : serializedTrickEvaluator, "hands" : hands, "curTrick" : curTrick, "prevTricks" : prevTricks, "scores" : scores} when(self.turnTrackerSerializer).deserialize(serializedTurnTracker, players).thenReturn(self.turnTracker) when(self.trickEvaluatorSerializer).deserialize(serializedTrickEvaluator).thenReturn(self.trickEvaluator) expectedCards = {} for playerId, hand in hands.iteritems(): for card in hand: deserializedCard = self._randomCard() when(self.cardSerializer).deserialize(card).thenReturn(deserializedCard) expectedCards[card] = deserializedCard expectedTricks = {curTrick : testhelper.createMock(euchre.Trick)} when(self.trickSerializer).deserialize(curTrick, self.trump).thenReturn(expectedTricks[curTrick]) for trick in prevTricks: deserializedTrick = testhelper.createMock(euchre.Trick) expectedTricks[trick] = deserializedTrick when(self.trickSerializer).deserialize(trick, self.trump).thenReturn(deserializedTrick) testhelper.replaceClass(src.euchre, "CardTranslator", testhelper.createSimpleMock()) self.wasCalled = False def assertCorrectTrump(trump): self.assertEqual(self.trump, trump) self.wasCalled = True src.euchre.CardTranslator.getInstance = assertCorrectTrump obj = self.testObj.deserialize(data, players) self.assertTrue(self.wasCalled) self.assertEqual(self.turnTracker, obj._turnTracker) verify(self.turnTrackerSerializer).deserialize(serializedTurnTracker, players) self.assertEqual(self.trickEvaluator, obj._trickEvaluator) for playerId, hand in hands.iteritems(): for i in range(len(hand)): self.assertEqual(expectedCards[hand[i]], obj._hands[playerId][i]) self.assertEqual(expectedTricks[curTrick], obj.getCurrentTrick()) for i in range(len(prevTricks)): self.assertEqual(expectedTricks[prevTricks[i]], obj.getPreviousTricks()[i]) for playerId, score in scores.iteritems(): self.assertEqual(score, obj._scores[playerId])
def testRetrieveBlackListedSuitsReturnsCorrectData(self): expectedSuits = [euchre.SUIT_CLUBS] gameObj = testhelper.createMock(euchre.Game) sequence = testhelper.createMock(euchre.Sequence) trumpSelector = testhelper.createMock(euchre.TrumpSelector) when(gameObj).getSequence().thenReturn(sequence) when(sequence).getState().thenReturn(euchre.Sequence.STATE_TRUMP_SELECTION_2) when(sequence).getTrumpSelector().thenReturn(trumpSelector) when(trumpSelector).getBlackListedSuits().thenReturn(expectedSuits) result = self.testObj.retrieveBlackListedSuits(gameObj) self.assertEqual(expectedSuits, result)
def setUp(self): self.game = testhelper.createMock(euchre.Game) self.sequence = testhelper.createMock(euchre.Sequence) self.round = testhelper.createMock(euchre.Round) self.trick = testhelper.createMock(euchre.Trick) self.ledSuit = random.randint(1, 4) when(self.game).getSequence().thenReturn(self.sequence) when(self.sequence).getRound().thenReturn(self.round) when(self.round).getCurrentTrick().thenReturn(self.trick) when(self.trick).getLedSuit().thenReturn(self.ledSuit) self.testObj = retriever.LedSuitRetriever.getInstance()
def testRetrieveDealerReturnsCorrectResult(self): gameObj = testhelper.createMock(euchre.Game) players = [game.Player("1"), game.Player("2"), game.Player("3"), game.Player("4")] when(gameObj).getPlayers().thenReturn(players) actualResult = self.testObj.retrieveDealer(gameObj) self.assertEqual(players[-1].playerId, actualResult)
def setUp(self): self.game = testhelper.createMock(euchre.Game) self.sequence = testhelper.createMock(euchre.Sequence) self.round = testhelper.createMock(euchre.Round) self.trick = testhelper.createMock(euchre.Trick) self.cards = {"1234" : euchre.Card(euchre.SUIT_HEARTS, 10), "4567" : euchre.Card(euchre.SUIT_HEARTS, 12), "7890" : euchre.Card(euchre.SUIT_HEARTS, 11), "0123" : euchre.Card(euchre.SUIT_HEARTS, 14)} self.expectedResult = {} for playerId, card in self.cards.iteritems(): self.expectedResult[playerId] = {"suit" : card.suit, "value" : card.value} when(self.game).getSequence().thenReturn(self.sequence) when(self.sequence).getRound().thenReturn(self.round) when(self.round).getCurrentTrick().thenReturn(self.trick) when(self.trick).getPlayedCards().thenReturn(self.cards) self.testObj = retriever.CurrentTrickRetriever.getInstance()
def testFilterListOnlyReturnsItemsGameModelsWithoutPlayerId(self): gameModel2 = testhelper.createMock(model.GameModel) gameModel2.readyToRemove = [self.playerId] result = self.testObj.filterList([self.gameModel, gameModel2]) self.assertEqual([self.gameModel], result)
def setUp(self): self.playerId = "asd123" self.gameModel = testhelper.createMock(model.GameModel) self.gameModel.readyToRemove = [] self._buildTestObj()
def testDeleteGameReturnsFalseAndDoesNothingElseIfCannotFindGameModel(self): gameId = "8237" key = testhelper.createMock(ndb.Key) when(key).get().thenReturn(None) self.testObj._getKeyByUrl = lambda url: None result = self.testObj.deleteGame(gameId) self.assertFalse(result)
def setUp(self): self.game = testhelper.createMock(euchre.Game) self.sequence = testhelper.createMock(euchre.Sequence) self.round = testhelper.createMock(euchre.Round) self.trump = random.randint(1, 4) self.trickEvaluator = euchre.TrickEvaluator.getInstance(self.trump) self.trick = euchre.Trick.getInstance(self.trump) self.playerIds = ["12345", "56789", "90123", "34567"] self.cards = {playerId : euchre.Card(suit=random.randint(1, 4), value=random.randint(9, 14)) for playerId in self.playerIds} when(self.game).getSequence().thenReturn(self.sequence) when(self.sequence).getRound().thenReturn(self.round) when(self.round).getCurrentTrick().thenReturn(self.trick) when(self.round).getTrump().thenReturn(self.trump) self._buildTestObj()
def setUp(self): self.ticketModel = testhelper.createMock(model.MatchmakingTicketModel) self.ticketKey = testhelper.createMock(ndb.Key) self.ticketMetaKey = testhelper.createMock(ndb.Key) self.ticketQuery = testhelper.createMock(ndb.Query) self.playerId = "asdlkj432" self.ticketModel.playerId = self.playerId self.ticketModelSearchResult = [testhelper.createMock(model.MatchmakingTicketModel), testhelper.createMock(model.MatchmakingTicketModel), testhelper.createMock(model.MatchmakingTicketModel)] self.ticketModelSearchResultIds = ["23409asflkj", "659wlkj", "1094234lkjad"] for i in range(len(self.ticketModelSearchResult)): self.ticketModelSearchResult[i].playerId = self.ticketModelSearchResultIds[i] self.ticketModelSearchResult[i].lookingForMatch = True self._buildTestObj() self._doTraining()
def setUp(self): self.turnTracker = testhelper.createMock(game.TurnTracker) self.availableTrump = euchre.SUIT_HEARTS self.selectedTrump = euchre.SUIT_DIAMONDS self.blackListedSuits = [euchre.SUIT_SPADES] self.selectingPlayerId = "123456" self.trumpSelector = euchre.TrumpSelector(self.turnTracker, self.availableTrump) self.turnTrackerSerializer = testhelper.createSingletonMock(serializer.TurnTrackerSerializer) self.testObj = serializer.TrumpSelectorSerializer.getInstance()
def setUp(self): self.players = [game.Player("1"), game.Player("2"), game.Player("3"), game.Player("4")] self.teams = [["1", "2"], ["3", "4"]] self.requestingPlayerId = random.choice(self.players).playerId self.sequence = testhelper.createSingletonMock(euchre.Sequence) self.trumpTurnTracker = testhelper.createMock(game.TurnTracker) self.roundTurnTracker = testhelper.createMock(game.TurnTracker) self.round = testhelper.createSingletonMock(euchre.Round) self.trumpSelector = testhelper.createSingletonMock(euchre.TrumpSelector) when(self.sequence).getRound().thenReturn(self.round) when(self.sequence).getTrumpSelector().thenReturn(self.trumpSelector) when(self.round).getTurnTracker().thenReturn(self.roundTurnTracker) when(self.trumpSelector).getTurnTracker().thenReturn(self.trumpTurnTracker) self.game = euchre.Game.getInstance(self.players, self.teams) when(self.game).getPlayers().thenReturn(self.players) self.handRetriever = testhelper.createSingletonMock(retriever.HandRetriever) self.testObj = retriever.TurnRetriever.getInstance()
def testSelectTrumpCreatesANewSequenceShufflesDeckAndAdvancesDealerIfTrumpSelectionFailed(self): initialPlayers = self.players[:] secondSequencePlayers = self.players[1:] + [self.players[0]] actualDeck = euchre.Deck.getInstance() deck = testhelper.createSingletonMock(euchre.Deck) when(deck).deal(5).thenReturn(actualDeck.deal(5)).thenReturn(actualDeck.deal(5)).thenReturn(actualDeck.deal(5)).thenReturn(actualDeck.deal(5)) when(deck).peekTop().thenReturn(actualDeck.peekTop()) sequenceFactory = testhelper.createSingletonMock(euchre.SequenceFactory) firstSequence = testhelper.createMock(euchre.Sequence) secondSequence = testhelper.createMock(euchre.Sequence) when(sequenceFactory).buildSequence(any(), any(), actualDeck.peekTop()).thenReturn(firstSequence).thenReturn(secondSequence) self._buildTestObj() self.game.startGame() sequence = self.game.getSequence() when(sequence).getState().thenReturn(euchre.Sequence.STATE_TRUMP_SELECTION_FAILED) self.game.selectTrump(self.players[0], euchre.SUIT_NONE) self.assertNotEqual(sequence, self.game.getSequence()) verify(deck, times=2).shuffle() verify(sequenceFactory).buildSequence(initialPlayers, any(), any()) verify(sequenceFactory).buildSequence(secondSequencePlayers, any(), any())
def testPlayCardCreatesANewSequenceShufflesDeckAndAdvancesDealerIfCurrentOneIsComplete(self): initialPlayers = self.players[:] secondSequencePlayers = self.players[1:] + [self.players[0]] actualDeck = euchre.Deck.getInstance() deck = testhelper.createSingletonMock(euchre.Deck) when(deck).deal(5).thenReturn(actualDeck.deal(5)).thenReturn(actualDeck.deal(5)).thenReturn(actualDeck.deal(5)).thenReturn(actualDeck.deal(5)) when(deck).peekTop().thenReturn(actualDeck.peekTop()) sequenceFactory = testhelper.createSingletonMock(euchre.SequenceFactory) firstSequence = testhelper.createMock(euchre.Sequence) secondSequence = testhelper.createMock(euchre.Sequence) when(sequenceFactory).buildSequence(any(), any(), actualDeck.peekTop()).thenReturn(firstSequence).thenReturn(secondSequence) self._buildTestObj() self.game.startGame() sequence = self.game.getSequence() when(sequence).getState().thenReturn(euchre.Sequence.STATE_COMPLETE) self.game.playCard(self.players[0], euchre.Card(euchre.SUIT_CLUBS, 9)) self.assertNotEqual(sequence, self.game.getSequence()) verify(deck, times=2).shuffle() verify(sequenceFactory).buildSequence(initialPlayers, any(), any()) verify(sequenceFactory).buildSequence(secondSequencePlayers, any(), any())
def testStartGameDealsCards(self): prevFactory = self.game._sequenceFactory self.game._sequenceFactory = testhelper.createMock(euchre.SequenceFactory) hands = {} def verifyHandSize(players, hands, topCard): for player in self.players: self.assertEqual(euchre.HAND_SIZE, len(hands[player.playerId])) self.assertTrue(topCard.suit > euchre.SUIT_NONE) self.assertTrue(topCard.suit <= euchre.NUM_SUITS) return prevFactory.buildSequence(player, hands, topCard) self.game._sequenceFactory.buildSequence = verifyHandSize self.game.startGame()
def setUp(self): self.requestDataAccessor = testhelper.createMock(util.RequestDataAccessor) self.session = testhelper.createSingletonMock(util.Session) self.graph = testhelper.createMock(facebook.GraphAPI) self.configManager = testhelper.createSingletonMock(util.ConfigManager) self.requestCookies = "pretty sweet cookies" self.accessToken = "23042098dlakj" self.fbCookie = {"access_token" : self.accessToken} self.playerId = "234ojlksdj323" self.expectedName = "Foobar Bingbaz, Jr." self.expectedUser = testhelper.createMock(social.User) self.nullUser = testhelper.createMock(social.User) self.profile = {"name" : self.expectedName, "id" : self.playerId} self.sessionData = {"accessToken" : self.accessToken} self.sessionKey = "fbuser" self.appId = "4873248102985" self.appSecret = "23049804w9rasdlfaj234094239lskdjds" self._doTraining() self._buildTestObj()
def setUp(self): self.robotFactory = testhelper.createSingletonMock(ai.RobotFactory) self.gameStatusRetriever = testhelper.createSingletonMock(retriever.GameStatusRetriever) self.turnRetriever = testhelper.createSingletonMock(retriever.TurnRetriever) self.players = ["1239804adjf", "32409afslk", "euchre_robot_random_0", "euchre_robot_random_1"] self.randomAI = testhelper.createSingletonMock(ai.RandomCardPlayerAI) self.gameObj = testhelper.createMock(model.GameModel) self.status = "" self.doTraining() self.buildTestObj()
def setUp(self): self.game = testhelper.createMock(euchre.Game) self.sequence = testhelper.createMock(euchre.Sequence) self.previousSequence = testhelper.createMock(euchre.Sequence) self.round = testhelper.createMock(euchre.Round) self.previousRound = testhelper.createMock(euchre.Round) self.trick = testhelper.createMock(euchre.Trick) self.trickLeaderRetriever = testhelper.createSingletonMock(retriever.TrickLeaderRetriever) self.winnerId = "7890" self.trump = euchre.SUIT_HEARTS self.cards = {"1234" : euchre.Card(euchre.SUIT_HEARTS, 10), "4567" : euchre.Card(euchre.SUIT_HEARTS, 12), "7890" : euchre.Card(euchre.SUIT_HEARTS, 11), "0123" : euchre.Card(euchre.SUIT_HEARTS, 14)} self.expectedResult = {"playedCards" : {}, "winnerId" : self.winnerId} for playerId, card in self.cards.iteritems(): self.expectedResult["playedCards"][playerId] = {"suit" : card.suit, "value" : card.value} when(self.game).getSequence().thenReturn(self.sequence) when(self.game).getPreviousSequence().thenReturn(None) when(self.sequence).getRound().thenReturn(self.round) when(self.previousSequence).getRound().thenReturn(self.previousRound) when(self.round).getPreviousTricks().thenReturn([{}, self.trick]) when(self.round).getTrump().thenReturn(self.trump) when(self.previousRound).getPreviousTricks().thenReturn([{}, {}, self.trick]) when(self.previousRound).getTrump().thenReturn(self.trump) when(self.trick).getPlayedCards().thenReturn(self.cards) when(self.trickLeaderRetriever).getLeaderFromTrick(self.trick, self.trump).thenReturn(self.winnerId) self._buildTestObj()