def test_proposeSet(self): """ Test backend.proposeSet """ vbar() print("Test backend.proposeSet") vbar() # initialize test data, launch a game with 5 players vprint("We reconstitute the reference game 0 at turn 0, and we will play the") vprint("whole game according to the reference path. We then check that the") vprint("resulting history is compliant.") backend = self.setUp() backend.ForTestOnly_EnlistRefPlayers() backend.ForTestOnly_LoadRefGame(0) # rewind the game back to turn 0 # we know - since the backend was reseted, that the new game is # backend.game[0] => we set the games index i at 0 i = 0 backend.ForTestOnly_GetBackToTurn(0, 0) # the game is now ready for the test case j = 0 while (backend.games[i].getGameFinished() == False): pID = ObjectId(refGames_Dict()[0]['steps'][j]['playerID']) pnn = refGames_Dict()[0]['steps'][j]['nickname'] setlist = refGames_Dict()[0]['steps'][j]['set'] for k in range(0,3): setlist[k] = int(setlist[k]) result = backend.proposeSet(pID, setlist) vprint(" - turn " + str(backend.games[i].turnCounter).zfill(2) + ": " + pnn + " propose " + str(setlist) + " => " + result['status']) self.assertEqual(result['status'], "ok") j += 1 self.assertTrue(backend.games[i].gameFinished) self.assertTrue(gameRef_compliant(backend.games[i], 0, " "))
def test_deserialize(self): """ Test Step.deserialize """ # setup the test data dictStart_ref = self.refStepStarts_Dict() [cardsets_ref, stepStarts_ref] = self.setup() dictStartBis_ref0 = refGames_Dict()[0]['steps'][0] dictStartBis_ref1 = refGames_Dict()[1]['steps'][0] stepStartBis_ref = refSteps() step_test = Step() # run the test vbar() print("Test Step.deserialize") vbar() vprint( "We compare the steps produced with the deserialize method with") vprint("reference steps, and check the output is correct") # test with the first series of Steps vprint() step_test.deserialize(dictStart_ref[0]) vprint(" > Cardets 0 - Step 0 - stepStart:") vprint(" Target: " + stepStarts_ref[0].toString(cardsets_ref[0], " ")) vprint(" Result: " + step_test.toString(cardsets_ref[0], " ")) self.assertTrue(step_equality(step_test, stepStarts_ref[0])) vprint() step_test.deserialize(dictStartBis_ref0) vprint(" > Cardets 0 - Step 0 - stepStartBis:") vprint(" Target: " + stepStartBis_ref[0][0].toString(cardsets_ref[0], " ")) vprint(" Result: " + step_test.toString(cardsets_ref[0], " ")) self.assertTrue(step_equality(step_test, stepStartBis_ref[0][0]))
def test_deserialize(self): """ Test the deserialize method """ # setup the test data cards_ref = self.setup() cards_test = [] cards_test.append(CardSet()) cards_test.append(CardSet()) # runs the test vbar() vprint("Test cardset.deserialize", True) vbar() vprint( "We compare cardsets created from reference dictionaries with reference" ) vprint("cardsets.") vprint( " > Cardset 0: reference followed by test (first 6 cards only)") dict_ref = refGames_Dict()[0]['cardset'] cards_test[0].deserialize(dict_ref) vprint(cards_ref[0].displayCardList(cardsList(6), 6, " ")) vprint(cards_test[0].displayCardList(cardsList(6), 6, " ")) self.assertEqual(cards_test[0].cards, cards_ref[0].cards) vprint( " > Cardset 1: reference followed by test cardset (first 6 cards only)" ) dict_ref = refGames_Dict()[1]['cardset'] cards_test[1].deserialize(dict_ref) vprint(cards_ref[1].displayCardList(cardsList(6), 6, " ")) vprint(cards_test[1].displayCardList(cardsList(6), 6, " ")) self.assertEqual(cards_test[1].cards, cards_ref[1].cards) # end of the test self.teardown()
def test_serialize(self): """ Test Step.serialize """ # setup the test data dictStart_ref = self.refStepStarts_Dict() [cardsets_ref, stepStarts_ref] = self.setup() dictStartBis_ref0 = refGames_Dict()[0]['steps'][0] dictStartBis_ref1 = refGames_Dict()[1]['steps'][0] stepStartBis_ref = refSteps() dictSecond_ref = self.refStepSecond_Dict() stepSeconds_ref = self.refStepSecond() # run the test vbar() print("Test Step.serialize") vbar() vprint( "We compare the dictionaries produced with the serialize method") vprint("with reference dictionaries, and check the output is correct") # test with the first series of Steps vprint() step_test = stepStarts_ref[0] dict_test = step_test.serialize() vprint(" > Cardets 0 - Step 0 - stepStart:") vprint(" Target: " + str(dictStart_ref[0])) vprint(" Result: " + str(dict_test)) self.assertTrue(stepDict_equality(dict_test, dictStart_ref[0])) step_test = stepStartBis_ref[0][0] dict_test = step_test.serialize() vprint(" > Cardets 0 - Step 0 - stepStartBis:") vprint(" Target: " + str(dictStartBis_ref0)) vprint(" Result: " + str(dict_test)) self.assertTrue(stepDict_equality(dict_test, dictStartBis_ref0)) step_test = stepSeconds_ref[0] dict_test = step_test.serialize() vprint(" > Cardets 0 - Step 0 - stepSecond:") vprint(" Target: " + str(dictSecond_ref[0])) vprint(" Result: " + str(dict_test)) self.assertTrue(stepDict_equality(dict_test, dictSecond_ref[0])) # test with the second series of Steps vprint() step_test = stepStarts_ref[1] dict_test = step_test.serialize() vprint(" > Cardets 1 - Step 1 - stepStart:") vprint(" Target: " + str(dictStart_ref[1])) vprint(" Result: " + str(dict_test)) self.assertTrue(stepDict_equality(dict_test, dictStart_ref[1])) step_test = stepStartBis_ref[1][0] dict_test = step_test.serialize() vprint(" > Cardets 1 - Step 1 - stepStartBis:") vprint(" Target: " + str(dictStartBis_ref1)) vprint(" Result: " + str(dict_test)) self.assertTrue(stepDict_equality(dict_test, dictStartBis_ref1)) step_test = stepSeconds_ref[1] dict_test = step_test.serialize() vprint(" > Cardets 1 - Step 1 - stepSecond:") vprint(" Target: " + str(dictSecond_ref[1])) vprint(" Result: " + str(dict_test)) self.assertTrue(stepDict_equality(dict_test, dictSecond_ref[1]))
def refSetsAndPlayers(): """ This function returns two lists of reference 'valid sets of 3 cards', 'refSet 0' and 'refSet 1', corresponding respectively to 'Cardset 0' and 'Cardset 1', to enable the test of a whole game with known reference data. """ setsAndPlayers_lists = [] for i in range(0, 2): source = refGames_Dict()[i]['steps'] setsAndPlayers_lists.append([]) for j in range(0, len(source)): set_int = [] for k in source[j]['set']: set_int.append(int(k)) playerID = source[j]['playerID'] if playerID == 'None': playerID = None else: playerID = ObjectId(playerID) nickname = source[j]['nickname'] setsAndPlayers_lists[i].append({ 'set': set_int, 'player': { 'playerID': playerID, 'nickname': nickname } }) return setsAndPlayers_lists
def test_Reset(self): """ Test backend.reset """ vbar() print("test backend.reset") vbar() # initiate a backend and populate it partially vprint("Initiate a backend and partially populate it:") backend = self.setUp() backend.players.register("Superman", "hash_superman") backend.players.register("Ironman", "hash_ironman") backend.players.register("Spiderman", "hash_spiderman") backend.players.register("Batman", "hash_batman") vprint(" - players:" + str(backend.players.getPlayers())) gg = Game(backend.players.getPlayers()) gg.deserialize(refGames_Dict()[0]) backend.games.append(gg) # reset the backend result = backend.reset() # check that the backend was actually reseted vprint("After reset, we check the backend:") self.assertEqual(backend.games, []) vprint(" - players:" + str(backend.players.getPlayers())) self.assertEqual(backend.players.getPlayers(), []) vprint(" - playersWaitingList:" + str(backend.playersWaitingList)) self.assertEqual(backend.playersWaitingList, []) vprint(" - nextgameID :" + str(backend.nextGameID)) self.assertEqual(backend.nextGameID, None) self.assertEqual(result['status'], "reset") # tear down the test data self.tearDown()
def test_getHistory(self): """ Test backend.getHistory """ vbar() print("Test backend.getHistory") vbar() # initialize test data, launch a game with 5 players backend = self.setUp() vprint("We register the reference test players:") backend.ForTestOnly_RegisterRefPlayers() pp_test = refPlayers(True) list_pid = [{'playerID': pp_test[0]['playerID']}, {'playerID': pp_test[1]['playerID']}, {'playerID': pp_test[2]['playerID']}, {'playerID': pp_test[3]['playerID']}, {'playerID': pp_test[4]['playerID']}] result = backend.enlistTeam(list_pid) gID = result['gameID'] # identify the right game and overwrite it with a reference game # (cardset 0, refGame 0, finished, 25 turns) vprint("We load the reference game 0 (full and finished).") for j in range(0, len(backend.games)): if str(backend.games[j].getGameID()) == str(gID): i = j break backend.games[i].deserialize(refGames_Dict()[0]) # First we collect the history of the full game vprint("We ask for the full history:") result = backend.getHistory(ObjectId('57b9bec5124e9b2d2503b72b')) status = result['status'] vprint(" - status: " + status) valid = gameRef_compliant(backend.games[i], 0, " ") vprint(" - game compliant: " + str(valid))
def gameSetup(test_data_index): """ Initialize a game from test data: the 'test_data_index' (0 or 1) points at the data set to be used from 'test_utilities' for: - players - cardset We then use the reference 'set proposal history' (in 'test_utilities' in order to go through the game and compare the progress against reference data available from 'refSteps'. """ # Connection to the MongoDB server # read the players, register them and initiate a game. players = refPlayers(True) temp_players = Players() for pp in players: temp_players.register(pp['nickname'], pp['passwordHash']) partie = Game(players) # overwrite the gameID with the reference test data partie.gameID = ObjectId(refGameHeader_start()[test_data_index]['gameID']) # Overwrite the cardset with reference test data cards_dict = refGames_Dict()[test_data_index]['cardset'] partie.cards.deserialize(cards_dict) # Force 'Steps' to take into account this new cardset. partie.steps[0].start(partie.cards) # The game is ready to start. return partie
def test_oidIsValid(self): """ This method will test that the reference playerID and gameID are recognized as valid ObjectIds, and that other strings are not. """ vbar() print("Test constants.oidIsValid") vbar() # check that playerIDs are tested ok (in both formats) vprint( "We check that playerIDs for all reference test players are recognized:" ) for pp in refPlayers(): self.assertTrue(oidIsValid(pp['playerID'])) vprint(" > " + pp['nickname'] + "'s playerID is recognized ok.") for pp in refPlayers_Dict(): self.assertTrue(oidIsValid(pp['playerID'])) vprint(" > " + pp['nickname'] + "'s stringified playerID is recognized ok.") vprint("We check that the reference game's GameID are recognized:") for i in range(0, 2): gameID = refGames_Dict()[i]['gameID'] self.assertTrue(oidIsValid(gameID)) vprint(" > game " + str(i) + " stringified gameID is recognized ok.") # We now test that random strings are not recognized as valid ObjectIds. vprint("We test that 'false' ObjectIds are not recognized:") wrong_list = ["REzcozienz34", "d*zdojEZFFE", "#`{#]^rdgrgre"] for pp in wrong_list: self.assertFalse(oidIsValid(pp)) vprint(" > " + pp + " is not recognized")
def refCardsets(): """ This function returns a list of 3 CardSets which are test references: - Cardset 0: interesting because there is no valid set of 3 cards in the first 12 cards of the cardset. The Table must be build with the 13th card in the pick, and move the 12th card at the end of the Pick. - Cardset 1: *** TO BE CONFIRMED *** jointly with the 'refSet 0' series, will get to a final table with only 6 cards. - Cardset 2: show a non-randomized cardset, usefull for testing the CardSet class. """ # loads the test data into CardSets Dict = refGames_Dict() cardsets_ref = [] cardsets_ref.append(CardSet()) # cardset 0 cardsets_ref.append(CardSet()) # cardset 1 cardsets_ref.append(CardSet()) # cardset init # overwrite the cardsets 0 and 1 with reference data read from refGamesDict for i in range(0, 2): cc = cardsets_ref[i].cards for code in Dict[i]['cardset']['cards']: k = int(code[:2]) c = int(code[3]) s = int(code[4]) f = int(code[5]) n = int(code[6]) cc[k] = [c, s, f, n] # overwrite the cardset init Dict = { '__class__': 'SetCardset', 'cards': [ '00-0000', '01-0001', '02-0002', '03-0010', '04-0011', '05-0012', '06-0020', '07-0021', '08-0022', '09-0100', '10-0101', '11-0102', '12-0110', '13-0111', '14-0112', '15-0120', '16-0121', '17-0122', '18-0200', '19-0201', '20-0202', '21-0210', '22-0211', '23-0212', '24-0220', '25-0221', '26-0222', '27-1000', '28-1001', '29-1002', '30-1010', '31-1011', '32-1012', '33-1020', '34-1021', '35-1022', '36-1100', '37-1101', '38-1102', '39-1110', '40-1111', '41-1112', '42-1120', '43-1121', '44-1122', '45-1200', '46-1201', '47-1202', '48-1210', '49-1211', '50-1212', '51-1220', '52-1221', '53-1222', '54-2000', '55-2001', '56-2002', '57-2010', '58-2011', '59-2012', '60-2020', '61-2021', '62-2022', '63-2100', '64-2101', '65-2102', '66-2110', '67-2111', '68-2112', '69-2120', '70-2121', '71-2122', '72-2200', '73-2201', '74-2202', '75-2210', '76-2211', '77-2212', '78-2220', '79-2221', '80-2222' ] } cc = cardsets_ref[2].cards for code in Dict['cards']: k = int(code[:2]) c = int(code[3]) s = int(code[4]) f = int(code[5]) n = int(code[6]) cc[k] = [c, s, f, n] # returns the 3 filled cardsets. return cardsets_ref
def refGameHeader_turnN(n): """ This function returns 2 reference Game headers, enabling to pass the 'game_equality' properly at the indicated turn 'n' """ return [ # Header for the reference test data 0 { 'gameID': refGames_Dict()[0]['gameID'], 'gameFinished': 'False', 'turnCounter': str(n) }, # Header for the reference test data 1 { 'gameID': refGames_Dict()[1]['gameID'], 'gameFinished': 'False', 'turnCounter': str(n) } ]
def test_serialize(self): """ Test the serialize method """ # setup the test data cards_ref = self.setup() # runs the test vbar() vprint("Test cardset.serialize", True) vbar() dict_test_0 = cards_ref[0].serialize() dict_test_1 = cards_ref[1].serialize() vprint( "We compare the reference cardset dictionary with the one produced by serialize method:" ) vprint(" Cardset 0: " + str(dict_test_0)) vprint(" Cardset 1: " + str(dict_test_1)) self.assertEqual(refGames_Dict()[0]['cardset'], dict_test_0) self.assertEqual(refGames_Dict()[1]['cardset'], dict_test_1) # end of the test self.teardown()
def gameProgress(game, test_data_index): """ Takes the game in a current status, and make it progress with one step by using the reference test data (the series 0 or 1 being pointed by the 'test_data_index' argument. """ # check if we can still iterate or if the game is finished according to # the test data mx_turn = int(refGames_Dict()[test_data_index]['turnCounter']) + 1 turn = game.turnCounter if turn < mx_turn: # read the reference Step and apply step_dict = refGames_Dict()[test_data_index]['steps'][turn] next_set = [] for i in step_dict['set']: next_set.append(int(i)) if step_dict['playerID'] == 'None': next_playerID = None else: next_playerID = ObjectId(step_dict['playerID']) game.receiveSetProposal(next_playerID, next_set)
def refSteps(): """ Populate a reference 'list of lists of Steps', from the reference 'list of lists of Step dictionaries'. """ steps_list_of_lists = [] for i in range(0, 2): # get the list of dictionaries dicts_list = refGames_Dict()[i]['steps'] # build a list of steps steps_list = [] for j in range(0, len(dicts_list)): temp = Step() stepDict_to_Step(dicts_list[j], temp) steps_list.append(temp) steps_list_of_lists.append(steps_list) return steps_list_of_lists
def ForTestOnly_GetBackToTurn(self, test_data_index, target_turn): """ FOR TEST PURPOSE ONLY. This method enable to roll the reference played loaded with previous method back to the turn N. """ # rewind the game back to turn 'target_turn' # we know - since the backend was reseted, that the new game is # backend.game[0] => we set the games index i at 0 i = 0 nb_turn_max = self.games[i].turnCounter target_turn = min(target_turn, nb_turn_max) target_turn = max(0, target_turn) original_turn = self.games[i].turnCounter if (target_turn < original_turn): refGames = refGames_Dict()[test_data_index] # adapts the generic details self.games[i].gameFinished = False self.games[i].turnCounter = target_turn # removes the 'future' steps j = original_turn while j > target_turn: del(self.games[i].steps[j]) j -= 1 # resets the 'playerID', nickname' and 'set' to empty if the game is # not finished self.games[i].steps[target_turn].playerID = None self.games[i].steps[target_turn].nickname = "" self.games[i].steps[target_turn].set = [] # set the player's points as from the reference test data # The only way to do so is actually to replay the game and add points to # the players accordingly. for pp in self.games[0].players: pp['points'] = 0 for j in range(0,target_turn): pID_str = refGames['steps'][j]['playerID'] for pp in self.games[0].players: if str(pp['playerID']) == pID_str: pp['points'] += pointsPerStep result = {'status': "ok"} else: result = {'status': "ko"} return result
def test_getPoints(self): """ Test game.getPoints """ vbar() print("Test game.getPoints") vbar() # load the reference data for test_data_index in range(0, 2): ref_playerPoints = refGames_Dict()[test_data_index]['players'] vprint(" Cardset " + str(test_data_index) + ":") # check that all points are set at 0 at the start partie = self.setup(test_data_index) test_playerPoints = partie.getPoints() valid = True vprint(" > start of game:") for pp in test_playerPoints: valid = valid and (pp['points'] == 0) vprint(" - '" + str(pp['playerID']) + "': " + str(pp['points']) + " -> compliant: " + str(valid)) vprint(" Overall: result is compliant: " + str(valid)) self.assertTrue(valid) # run the game to the end and check the points partie = self.setupAndProgress(test_data_index, 30) test_playerPoints = partie.getPoints() valid = True vprint(" > end of game:") for pp in test_playerPoints: # find the points of the corresponding player in reference data for pp_ref in ref_playerPoints: if str(pp['playerID']) == str(pp_ref['playerID']): points = int(pp_ref['points']) break # test the points are equal valid = valid and (pp['points'] == points) vprint(" - '" + str(pp['playerID']) + "': " + str(pp['points']) + "/" + str(points) + " -> compliant: " + str(valid)) vprint(" Overall: result is compliant: " + str(valid)) self.assertTrue(valid)
def test_deserialize(self): """ Test game.deserialize """ vbar() print("Test game.deserialize") vbar() vprint( "We overwrite a game with the 'game.deserialize' method, using the" ) vprint( "reference dictionaries, and compare the outcome with reference games:" ) for test_data_index in range(0, 2): # build test data ref_dict = refGames_Dict()[test_data_index] test_game = self.setup(test_data_index) test_game.deserialize(ref_dict) test_dict = test_game.serialize() # compare various sections of the dictionaries vprint(" > Game " + str(test_data_index) + ":") result = (test_dict['gameID'] == ref_dict['gameID']) vprint(" gameID: " + str(result)) self.assertTrue(result) result = (test_dict['gameFinished'] == ref_dict['gameFinished']) vprint(" gameFinished: " + str(result)) self.assertTrue(result) result = (test_dict['turnCounter'] == ref_dict['turnCounter']) vprint(" turnCounter: " + str(result)) self.assertTrue(result) result = (test_dict['cardset'] == ref_dict['cardset']) vprint(" cardset: " + str(result)) self.assertTrue(result) for i in range(0, len(test_dict['steps'])): result = stepDict_equality(test_dict['steps'][i], ref_dict['steps'][i]) vprint(" steps " + str(i) + ": " + str(result)) self.assertTrue(result)
def ForTestOnly_LoadRefGame(self, test_data_index): """ FOR TEST PURPOSE ONLY. This method initiate the reference test game corresponding to the index passed as argument. The game is fully played and is finished. """ # cleans the DB and registers the reference players if test_data_index in (0,1): # initiate a new game and overwrite it with reference test data self.reset() self.ForTestOnly_RegisterRefPlayers() # initialize a game (just to have a game object available self.games.append(Game(refPlayers(True))) # override this game with the reference test data self.games[0].deserialize(refGames_Dict()[test_data_index]) gID = self.games[0].getGameID() for pp in self.players.getPlayers(): self.players.delist(pp['playerID']) self.players.enlist(pp['playerID'], gID) result = {'status': "ok", 'gameID': gID} else: result = {'status': "ko", 'reason': "wrong test_data_index"} return result
def test_serialize(self): """ Test game.serialize """ vbar() print("Test game.serialize") vbar() # build the test data vprint( "We compare the output of the 'game.serialize' method with reference" ) vprint("dictionaries.") for test_data_index in range(0, 2): # build the data partie = self.setupAndProgress(test_data_index, 30) test_dict = partie.serialize() ref_dict = refGames_Dict()[test_data_index] # compare various sections of the dictionaries vprint(" > Game " + str(test_data_index) + ":") result1 = (test_dict['gameID'] == ref_dict['gameID']) vprint(" gameID: " + str(result1)) self.assertTrue(result1) result2 = (test_dict['gameFinished'] == ref_dict['gameFinished']) vprint(" gameFinished: " + str(result2)) self.assertTrue(result2) result3 = (test_dict['turnCounter'] == ref_dict['turnCounter']) vprint(" turnCounter: " + str(result3)) self.assertTrue(result3) result4 = (test_dict['cardset'] == ref_dict['cardset']) vprint(" cardset: " + str(result4)) self.assertTrue(result4) result5 = (test_dict['steps'] == ref_dict['steps']) vprint(" steps: " + str(result5)) self.assertTrue(result5) vprint(" ---------------") result = result1 and result2 and result3 and result4 and result5 vprint(" Global: " + str(result))
def refGameHeader_Finished(): """ This function returns 2 reference Game headers, enabling to pass the 'game_equality' properly """ return [ # Header for the reference test data 0 { 'gameID': refGames_Dict()[0]['gameID'], 'gameFinished': refGames_Dict()[0]['gameFinished'], 'turnCounter': refGames_Dict()[0]['turnCounter'] }, # Header for the reference test data 1 { 'gameID': refGames_Dict()[1]['gameID'], 'gameFinished': refGames_Dict()[1]['gameFinished'], 'turnCounter': refGames_Dict()[1]['turnCounter'] } ]
def gameRef_compliant(game, index, tab=" "): """ This function returns True if the two games show the same generic details, the same players, the same cardsets and the same steps. """ # loads the reference Cardset from test_cardset import refCardsets # loads the relevant reference data temp_dict = refGameHeader_Finished()[index] generic2 = { 'gameID': ObjectId(temp_dict['gameID']), 'turnCounter': int(temp_dict['turnCounter']), 'gameFinished': (temp_dict['gameFinished'] == 'True') } players2 = refGames_Dict()[index]['players'] for pp in players2: pp['playerID'] = ObjectId(pp['playerID']) pp['points'] = int(pp['points']) cardset2 = refCardsets()[index] steps2 = refSteps()[index] # set the validity flags valid_generic = valid_players = valid_cardset = valid_steps = False # compare the generic details valid_generic = (game.gameID == generic2['gameID']) and \ (game.turnCounter == generic2['turnCounter']) and \ (game.gameFinished == generic2['gameFinished']) vprint(tab + "generic details: " + str(valid_generic)) # compare the players if valid_generic: valid_players = (len(game.players) == len(players2)) for p1 in game.players: # find the same player in the reference data for p2 in players2: if (str(p1['playerID']) == str(p2['playerID'])): valid_players = valid_players and (p1['nickname'] == p2['nickname']) valid_players = valid_players and (int(p1['points']) == int(p2['points'])) break vprint(tab + "players: " + str(valid_players)) # compare the cardsets if valid_players: valid_cardset = cardset_equality(game.cards, cardset2) vprint(tab + "cardsets: " + str(valid_cardset)) # compare the steps if valid_cardset: valid_steps = (len(game.steps) == len(steps2) == game.turnCounter + 1) msg = tab vprint(tab + "steps:") for i in range(0, game.turnCounter + 1): step1 = game.steps[i] step2 = steps2[i] valid_steps = valid_steps and step_equality(step1, step2) msg += " step " + str(i).zfill(2) + ": " + str(valid_steps) if (i + 1) % 6 == 0: msg += "\n" + tab vprint(msg) valid = valid_generic and valid_players and valid_cardset and valid_steps return valid
def test__init__(self): """ Test game.__init__ """ vbar() print("Test game.__init__") vbar() # first try to initialize a game with wrong playerIDs # populate the DB with reference players playersColl = getPlayersColl() playersColl.drop() for pp in refPlayers(): playersColl.insert_one({ '_id': pp['playerID'], 'nickname': pp['nickname'], 'totalScore': pp['totalScore'], 'gameID': None }) # try to initiate a game with different players + 1 wrong player players = [] pp = refPlayers()[0] players.append({ 'playerID': pp['playerID'], 'nickname': pp['nickname'] }) pp = refPlayers()[2] players.append({ 'playerID': pp['playerID'], 'nickname': pp['nickname'] }) pp = refPlayers()[3] players.append({ 'playerID': pp['playerID'], 'nickname': pp['nickname'] }) pp = refPlayers()[4] players.append({ 'playerID': pp['playerID'], 'nickname': pp['nickname'] }) players.append({'playerID': ObjectId(), 'nickname': "batman"}) try: partie = Game(players) except invalidPlayerID: vprint(" - could not initialize a game: playerIDs are invadid.") # second initialize successfully a game and test various values # build the reference starting for the game from the test data test_data_index = 0 partie = self.setup(test_data_index) cards_ref = refCardsets()[test_data_index] steps_ref = refSteps()[test_data_index] vprint( "We start with a first iteration: we push one set and compare the") vprint("full status of the game with target:") # compare status with target ref_gameID = ObjectId(refGameHeader_start()[test_data_index]['gameID']) result = (partie.gameID == ref_gameID) vprint(" > gameID is compliant: " + str(result)) self.assertTrue(result) result = cardset_equality(partie.cards, cards_ref) vprint(" > cardset is compliant: " + str(result)) self.assertTrue(result) # we can compare with reference test data only steps which are in a # 'definitive' status (i.e. a valid Set has been proposed, the game took # it to build the next Step (here: Step 1) and store the Step 0 in its # 'final' status. Our reference data enable to compare Step only in # their 'final' status'. # Here, we have proposed a valid set and the game built the Step 1: # - Step 0 is in a 'final' status => we compare it with reference data # - Step 1 is in an intermediate status: cannot be compared yet. # We fetch from reference data the next player and the next Set: nextset_dict = refGames_Dict()[test_data_index]['steps'][0]['set'] next_set = [] for ss in nextset_dict: next_set.append(int(ss)) next_playerID = refGames_Dict( )[test_data_index]['steps'][0]['playerID'] next_playerID = ObjectId(next_playerID) # The player proposes the Set: partie.receiveSetProposal(next_playerID, next_set) # We now can compare Step 0 with reference test data. result = step_equality(partie.steps[0], steps_ref[0]) vprint(" > Step[0] is compliant: " + str(result)) self.assertTrue(result)
def test_getDetails(self): """ tests backend.getDetails """ vbar() print("Test backend.getDetails") vbar() # initialize test data, launch a game with 5 players backend = self.setUp() vprint("We register the reference test players:") backend.ForTestOnly_RegisterRefPlayers() pp_test = refPlayers(True) list_pid = [{'playerID': pp_test[0]['playerID']}, {'playerID': pp_test[1]['playerID']}, {'playerID': pp_test[2]['playerID']}, {'playerID': pp_test[3]['playerID']}, {'playerID': pp_test[4]['playerID']}] # we enlist 5 players => succeed and return the gameID enlisted = backend.enlistTeam(list_pid) # build the target against which the test data will be compared gID = enlisted['gameID'] target_cardset = refGames_Dict()[0]['cardset'] target = { '__class__': 'SetGameDetails', 'gameFinished': 'False', 'turnCounter': '0', 'players': [], 'cardset': target_cardset, 'gameID': str(gID) } for pp in refPlayers(): if pp['nickname'] != "Daisy": target['players'].append({ 'playerID': str(pp['playerID']), 'nickname': pp['nickname'], 'passwordHash': pp['passwordHash'], 'points': '0' }) # override the newly created game with the reference test data backend.games[0].cards.deserialize(target_cardset) backend.games[0].gameID = gID vprint("Enlist a team of 5 player: " + str(enlisted['status']) + " (" + str(gID) + ")") # request the details of the game result = backend.getDetails(gID) # check that the result is compliant valid = True valid1 = (target['gameID'] == result['gameID']) vprint(" - 'gameID' are similar: " + str(valid)) valid2 = (target['turnCounter'] == result['turnCounter']) vprint(" - 'turnCounters' are similar: " + str(valid)) valid3 = (target['gameFinished'] == result['gameFinished']) vprint(" - 'gameFinished' are similar: " + str(valid)) valid4 = (len(target['players']) == len(result['players'])) valid5 = True for pp in target['players']: valid5 = valid5 and (pp in result['players']) vprint(" - 'players' are similar: " + str(valid5)) valid6 = cardsetDict_equality(target['cardset'], result['cardset']) vprint(" - 'cardset' are similar: " + str(valid6)) valid = valid1 and valid2 and valid3 and valid4 and valid5 and valid6 vprint(" -> the result is compliant: " + str(valid)) self.assertTrue(valid)
def test_getStep(self): """ Test backend.getStep """ vbar() print("Test backend.getStep") vbar() # initialize reference test data, launch a game with 5 players backend = self.setUp() vprint("We register the reference test players:") backend.ForTestOnly_RegisterRefPlayers() pp_test = refPlayers(True) list_pid = [{'playerID': pp_test[0]['playerID']}, {'playerID': pp_test[1]['playerID']}, {'playerID': pp_test[2]['playerID']}, {'playerID': pp_test[3]['playerID']}, {'playerID': pp_test[4]['playerID']}] result = backend.enlistTeam(list_pid) gID = result['gameID'] # identify the right game and overwrite it with a reference game # (cardset 0, refGame 0, finished, 25 turns) vprint("We reconstitute the reference game 0 at turn 9.") for j in range(0, len(backend.games)): if str(backend.games[j].getGameID()) == str(gID): i = j break backend.games[i].deserialize(refGames_Dict()[0]) # rewind the game back to turn 9 backend.games[i].turnCounter = 9 j = 25 while j > 9: del(backend.games[i].steps[j]) j -= 1 backend.games[i].steps[9].set = [] target = refGames_Dict()[0]['steps'][9] target['set'] = [] # the game is now ready for the test case vprint("We ask for the Step 9:") result = backend.getStep(ObjectId('57b9bec5124e9b2d2503b72b')) status = result['status'] step_dict = result['step'] vprint(" - status: " + status) vprint(" - step 9: " + str(step_dict)) valid = (status == "ok") and stepDict_equality(step_dict, target) vprint(" Result compliant: " + str(valid)) self.assertTrue(valid) # We check that invalid gameID are discarded vprint("We ask for the Step 9 of an invalid gameID:") result = backend.getStep('tzagb9b2d2503b72b') valid = (result['status'] == "ko") vprint(" - status: " + result['status']) vprint(" - reason: " + result['reason']) valid = valid and (result['reason'] == "invalid gameID") vprint(" Result compliant: " + str(valid)) self.assertTrue(valid) vprint("We ask for the Step 9 of an unkown gameID:") result = backend.getStep(ObjectId()) valid = (result['status'] == "ko") vprint(" - status: " + result['status']) vprint(" - reason: " + result['reason']) valid = valid and (result['reason'] == "game does not exist") vprint(" Result compliant: " + str(valid)) self.assertTrue(valid)