def test_register(self): """ Test players.register """ # test that the new player is actually added both in memory and in DB vbar() print("Test players.register") vbar() vprint( "We add a player and check that it is properly registered to the DB." ) vprint( "We also check that duplicate registering a player will not work.") # setup the test data self.setUp() players = Players() new_hash = "lEyycZ2UYZV0bX6ChdtSA5MGCmN3BrF1xoZG4TMRzEmwmpp" playerID = players.register("Dingo", new_hash)['playerID'] read_id = players.playersColl.find_one({'nickname': "Dingo"})['_id'] self.assertEqual(playerID, read_id) # replaced 'count' (deprecated) with 'count_documents' self.assertEqual(players.playersColl.count_documents({}), 7) # check the various fields of registered players pp = players.playersColl.find_one({'nickname': "Dingo"}) self.assertEqual(pp['_id'], playerID) self.assertEqual(pp['passwordHash'], new_hash) # check that it is impossible to register a duplicate nickname result = players.register("Daisy", new_hash) self.assertEqual(result['status'], "ko") self.assertEqual(result['reason'], "invalid nickname") # replaced 'count' (deprecated) with 'count_documents' self.assertEqual(players.playersColl.count_documents({}), 7) # summarize vprint(" We now have 7 players in the DB:") for pp in players.playersColl.find(): vprint(" " + pp['nickname']) # end of the test self.teardown(players)
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 test_deRegister(self): """ Test players.deRegister """ # setup the test data self.setUp() players = Players() # removes a player vbar() print("Test players.deRegister") vbar() vprint("We first register all 6 reference test players.") pp = players.playersColl.find_one({'nickname': "Donald"}) self.assertTrue(players.deRegister(pp['_id'])) # replaced 'count' (deprecated) with 'count_documents' self.assertEqual(players.playersColl.count_documents({}), 5) pp = players.playersColl.find_one({'nickname': "Donald"}) self.assertEqual(pp, None) vprint("We removed Donald and check that we have 5 players left.") for pp in players.playersColl.find(): vprint(" " + pp['nickname']) # end of the test self.teardown(players)
def test_serialize(self): """ Test players.serialize """ # build the reference data (without the passwrds) target = {'__class__': 'SetPlayers', 'players': []} for pp in refPlayers_Dict(): target['players'].append({ 'playerID': pp['playerID'], 'nickname': pp['nickname'], 'passwordHash': pp['passwordHash'], 'totalScore': pp['totalScore'], 'gameID': pp['gameID'] }) # setup the test data self.setUp() players = Players() gameID1 = ObjectId('57bf224df9a2f36dd206845a') gameID2 = ObjectId('57bf224df9a2f36dd206845b') players.enlist(players.getPlayerID("Daisy")['playerID'], gameID1) players.enlist(players.getPlayerID("Donald")['playerID'], gameID1) players.enlist(players.getPlayerID("Riri")['playerID'], gameID2) players.enlist(players.getPlayerID("Fifi")['playerID'], gameID2) players.enlist(players.getPlayerID("Loulou")['playerID'], gameID2) # runs the test vbar() print("Test players.serialize") vbar() vprint( "We compare the result of the 'serialize' method with the target which is:" ) vprint(target) # check that the class is equal result = players.serialize() self.assertTrue(playersDict_equality(target, result)) # end of the test self.teardown(players)
def test_changeHash(self): """ Test players.changeHash """ # setup the test data vbar() print("Test players.changeHash") vbar() vprint( "We change the hash of the players in the DB and compare the result with" ) vprint("the expected test data:") # test valid players self.setUp() players = Players() new_hash = "lEyycZ2UYZV0bX6ChdtSA5MGCmN3BrF1xoZG4TMRzEmwmpp" for pp in refPlayers(): playerID = pp['playerID'] result = players.changeHash(playerID, new_hash) valid = (result['status'] == "ok") and (result['passwordHash'] == new_hash) vprint(" " + pp['nickname'] + ": result = " + str(result)) self.assertTrue(valid) # test unknown player result = players.getHash(ObjectId()) valid = (result['status'] == "ko") and (result['reason'] == "unknown playerID") vprint(" Unknown player: result = " + str(result)) self.assertTrue(valid) # test invalid playerID result = players.getHash("invalid") valid = (result['status'] == "ko") and (result['reason'] == "invalid playerID") vprint(" Invalid playerID: result = " + str(result)) self.assertTrue(valid) # end of the test self.teardown(players)
def test_getNickname(self): """ Test players.getNickname """ # setup the test data self.setUp() players = Players() vbar() print("Test players.getNickname") vbar() vprint( "We collect the nickname of the players in the DB and compare with" ) vprint("the reference test data:") # test valid players for pp in refPlayers(): playerID = pp['playerID'] nickname_ref = pp['nickname'] result = players.getNickname(playerID) valid = (result['status'] == "ok") and (result['nickname'] == nickname_ref) vprint(" " + pp['nickname'] + ": result = " + str(result)) self.assertTrue(valid) # test unknown player result = players.getNickname(ObjectId()) valid = (result['status'] == "ko") and (result['reason'] == "unknown playerID") vprint(" Unknown player: result = " + str(result)) self.assertTrue(valid) # test invalid playerID result = players.getNickname("invalid") valid = (result['status'] == "ko") and (result['reason'] == "invalid playerID") vprint(" Invalid playerID: result = " + str(result)) self.assertTrue(valid) # end of the test self.teardown(players)
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 test_isNicknameAvailable(self): """ Test backend.isNicknameAvailable """ vbar() print("test backend.isNicknameAvailable") vbar() # initiate a backend backend = self.setUp() # check availability of nicknames before and after registering players vprint("Initiate a backend with no players registered, and check nickname availaiblity") vprint("before and after registering reference players:") for pp in refPlayers(True): # check the nickname is available before the player is registered answer = backend.isNicknameAvailable(pp['nickname']) self.assertEqual(answer['status'], "ok") vprint(" > " + pp['nickname'] + " is available") # register the player result = backend.registerPlayer(pp['nickname'], pp['passwordHash']) vprint(" - register " + pp['nickname'] + ": " + str(result['playerID'])) # check the nickname is not available after the player is registered answer = backend.isNicknameAvailable(pp['nickname']) self.assertEqual(answer['status'], "ko") vprint(" > " + pp['nickname'] + " is not available anymore")
def test__init__(self): """ Test Step.__init__ """ # setup the test data vbar() vprint("We build test data for testing the class Step") step_test = Step() # run the test vbar() print("Test Step.__init__") vbar() vprint("We build a new Step and check that all fields are empty.") self.assertEqual(step_test.turnCounter, 0) self.assertEqual(step_test.playerID, None) self.assertEqual(step_test.nickname, "") self.assertEqual(step_test.pick, []) self.assertEqual(step_test.table, []) self.assertEqual(step_test.used, []) self.assertEqual(step_test.set, [])
def test_validateSetFromTable(self): """ Test Step.validateSetFromTable """ # setup the test data [cardsets_ref, stepStart_ref] = self.setup() stepStartBis_ref = refSteps() # it reads the reference suite of couple (set / player) to be applied in # order to use the reference data on Steps. setandpl_ref = refSetsAndPlayers() # run the test vbar() print("Test Step.validateSetFromTable") vbar() vprint( "We run the method on two reference steps, and it should return") vprint("known answers each time.") # basic test, no need for additional test data vprint( " > We will check few Sets on cardset 0 /step 0 to see if they are " ) vprint(" validated or rejected, without populating the step:") result = stepStart_ref[0].validateSetFromTable(cardsets_ref[0], [0, 1, 2]) self.assertFalse(result) vprint(" [ 0, 1, 2] should be False => " + str(result)) result = stepStart_ref[0].validateSetFromTable(cardsets_ref[0], [9, 10, 11]) self.assertFalse(result) vprint(" [ 9,10,11] should be False => " + str(result)) result = stepStart_ref[0].validateSetFromTable(cardsets_ref[0], [1, 6, 11]) self.assertTrue(result) vprint(" [ 1, 6,11] should be True => " + str(result)) # here we populate the 'reference Start' steps: # => they become 'test StartBis' steps # First test with a invalid set of 3 cards: vprint( " > we will now propose valid sets with 'population' option activated," ) vprint( " and compare the outcome (so called 'stepStartBis') with reference data" ) vprint( " - Cardset 0 / step 0 - set [0,1,2] - populating with 'Donald'" ) player = setandpl_ref[0][0]['player'] result = stepStart_ref[1].validateSetFromTable(cardsets_ref[0], [0, 1, 2], True, player) self.assertFalse(result) self.assertEqual(stepStart_ref[0].playerID, None) self.assertEqual(stepStart_ref[0].nickname, "") self.assertEqual(stepStart_ref[0].set, []) vprint(" [ 0, 1, 2] should be False => " + str(result)) vprint(" so we check it was not populated:") vprint(" playerID = " + str(stepStart_ref[0].playerID)) vprint(" nickname = " + stepStart_ref[0].nickname) vprint(" set = " + str(stepStart_ref[0].set)) # second test with a valid set of 3 cards player = setandpl_ref[0][0]['player'] good_set = setandpl_ref[0][0]['set'] vprint(" - Cardset 0 / step 0 - set " + str(good_set) + " - populating with '" + str(player['nickname']) + "'") result = stepStart_ref[0].validateSetFromTable(cardsets_ref[0], good_set, True, player) self.assertTrue(result) self.assertTrue(step_equality(stepStart_ref[0], stepStartBis_ref[0][0])) vprint(" " + str(good_set) + " should be True => " + str(result)) vprint(" so we check it was populated") vprint(" playerID = " + str(stepStart_ref[0].playerID)) vprint(" nickname = " + stepStart_ref[0].nickname) vprint(" set = " + str(stepStart_ref[0].set)) # test with the second set of reference test data player = setandpl_ref[1][0]['player'] good_set = setandpl_ref[1][0]['set'] vprint(" - Cardset 1 / step 1 - set " + str(good_set) + " - populating with '" + str(player['nickname']) + "'") result = stepStart_ref[1].validateSetFromTable(cardsets_ref[1], good_set, True, player) self.assertTrue(result) self.assertTrue(step_equality(stepStart_ref[1], stepStartBis_ref[1][0])) vprint(" [ 0, 3, 9] should be True => " + str(result)) vprint(" so we check it was populated") vprint(" playerID = " + str(stepStart_ref[1].playerID)) vprint(" nickname = " + stepStart_ref[1].nickname) vprint(" set = " + str(stepStart_ref[1].set))
def test_isPlayerAvailableToPlay(self): """ Test players.isPlayerAvailableToPlay """ vbar() print("Test players.isPlayerAvailableToPlay") vbar() vprint( "We check which players are available to play and compare with the" ) vprint("reference data :") # setup the test data self.setUp() players = Players() id_Donald = players.getPlayerID("Donald")['playerID'] print("bogus: id_Donald =", id_Donald) id_Mickey = players.getPlayerID("Mickey")['playerID'] id_Riri = players.getPlayerID("Riri")['playerID'] id_Fifi = players.getPlayerID("Fifi")['playerID'] id_Loulou = players.getPlayerID("Loulou")['playerID'] id_Daisy = players.getPlayerID("Daisy")['playerID'] gameID1 = ObjectId() gameID2 = ObjectId() # first round of test players.enlist(id_Riri, gameID2) players.enlist(id_Fifi, gameID2) players.enlist(id_Loulou, gameID2) vprint(" > First round, only kids are playing:") result = players.isPlayerAvailableToPlay(id_Donald) print("Bogus: ", result) vprint(" Donald: " + str(result)) self.assertTrue(result['status'] == "ok") result = players.isPlayerAvailableToPlay(id_Mickey) vprint(" Mickey: " + str(result)) self.assertTrue(result['status'] == "ok") result = players.isPlayerAvailableToPlay(id_Riri) vprint(" Riri : " + str(result)) self.assertTrue(result['status'] == "ko" and result['reason'] == "player is not available") result = players.isPlayerAvailableToPlay(id_Fifi) vprint(" Fifi : " + str(result)) self.assertTrue(result['status'] == "ko" and result['reason'] == "player is not available") result = players.isPlayerAvailableToPlay(id_Loulou) vprint(" Loulou: " + str(result)) self.assertTrue(result['status'] == "ko" and result['reason'] == "player is not available") result = players.isPlayerAvailableToPlay(id_Daisy) vprint(" Daisy : " + str(result)) self.assertTrue(result['status'] == "ok") # second round of test players.enlist(id_Daisy, gameID1) players.enlist(id_Donald, gameID1) vprint(" > Second round, two parents are also playing:") result = players.isPlayerAvailableToPlay(id_Donald) vprint(" Donald: " + str(result)) self.assertTrue(result['status'] == "ko" and result['reason'] == "player is not available") result = players.isPlayerAvailableToPlay(id_Mickey) vprint(" Mickey: " + str(result)) self.assertTrue(result['status'] == "ok") result = players.isPlayerAvailableToPlay(id_Riri) vprint(" Riri : " + str(result)) self.assertTrue(result['status'] == "ko" and result['reason'] == "player is not available") result = players.isPlayerAvailableToPlay(id_Fifi) vprint(" Fifi : " + str(result)) self.assertTrue(result['status'] == "ko" and result['reason'] == "player is not available") result = players.isPlayerAvailableToPlay(id_Loulou) vprint(" Loulou: " + str(result)) self.assertTrue(result['status'] == "ko" and result['reason'] == "player is not available") result = players.isPlayerAvailableToPlay(id_Daisy) vprint(" Daisy : " + str(result)) self.assertTrue(result['status'] == "ko" and result['reason'] == "player is not available") # test inexistant or invalid playerID vprint(" > Third round, with invalid or unknown playerIDs:") result = players.isPlayerAvailableToPlay(ObjectId()) vprint(" Unknown player: " + str(result)) self.assertTrue(result['status'] == "ko" and result['reason'] == "unknown playerID") result = players.isPlayerAvailableToPlay("stupid") vprint(" Invalid playerID: " + str(result)) self.assertTrue(result['status'] == "ko" and result['reason'] == "invalid playerID") # end of the test self.teardown(players)
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)
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_stopGame(self): """ Test backend.stopGame """ vbar() print("test backend.stopGame") vbar() # initiate a backend and register reference players backend = self.setUp() vprint("We register the reference test players:") backend.ForTestOnly_RegisterRefPlayers() pp_test = refPlayers(True) # enlist a team of 5 players 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 soft- and hard-stop a game which is not finished => should fail result = backend.enlistTeam(list_pid) vprint("Enlist a team of 5 player: " + str(result['status']) + " (" + str(result['gameID']) + ")") vprint(" - games list is: " + str(backend.games)) gID = result['gameID'] # soft-stop the game vprint("We soft-stop the game which is not finished:") result = backend.stopGame(gID) vprint(" - status = " + result['status']) vprint(" - reason = " + result['reason']) self.assertEqual(result['status'], "ko") self.assertEqual(result['reason'], "game not finished") # check that the game is still live alive = False for gg in backend.games: if gg.getGameID() == gID: alive = True break vprint(" - games still is alive:" + str(alive)) self.assertTrue(alive) # hard-stop the game vprint("We hard-stop the game:") result = backend.stopGame(gID, True) # check that the game has been killed and the players available again. vprint(" - games list is:" + str(backend.games)) self.assertEqual(backend.games, []) for pp in backend.players.getPlayers(): nn = pp['nickname'] gID = pp['gameID'] vprint(" - " + nn + ": gameID = " + str(gID)) self.assertEqual(gID, None) self.assertEqual(result['status'], "ok") # we soft-stop a game which is finished (we force the 'gameFinished' # flag => should be ok vprint("We now soft-stop a game which is finished:") result = backend.enlistTeam(list_pid) vprint(" - enlist a team of 5 player: " + str(result['status']) + " (" + str(result['gameID']) + ")") vprint(" - games list is: " + str(backend.games)) gID = result['gameID'] # force the 'gameFinished' flag: for gg in backend.games: if gg.getGameID() == gID: gg.gameFinished = True break # soft-stop the game vprint(" - We soft-stop the game:") result = backend.stopGame(gID) # check that the game has been killed and the players available again. vprint(" - games list is:" + str(backend.games)) self.assertEqual(backend.games, []) for pp in backend.players.getPlayers(): nn = pp['nickname'] gID = pp['gameID'] vprint(" - " + nn + ": gameID = " + str(gID)) self.assertEqual(gID, None) self.assertEqual(result['status'], "ok") # try to stop a game which does not exist vprint("We stop a game which does not exist:") result = backend.stopGame(ObjectId(), True) vprint(" - status = " + result['status']) vprint(" - reason = " + result['reason']) self.assertEqual(result['status'], "ko") self.assertEqual(result['reason'], "game does not exist") # try to stop a game with an invalid ObjectId vprint("We stop a game which does not exist:") result = backend.stopGame('iamnotanobjectid', True) vprint(" - status = " + result['status']) vprint(" - reason = " + result['reason']) self.assertEqual(result['status'], "ko") self.assertEqual(result['reason'], "invalid GameID")
def test_enlistPlayer(self): """ Test backend.enlistPlayer """ vbar() print("test backend.enlistPlayer") vbar() # initiate a backend and register reference players backend = self.setUp() vprint("We register the reference test players:") backend.ForTestOnly_RegisterRefPlayers() pp_test = refPlayers(True) # enlist Donald and test the 'enlist' answer "wait" donald = pp_test[0] result = backend.enlistPlayer(donald['playerID']) status = result['status'] nbp = result['nb_players'] vprint(" enlist Donald : " + str(donald['playerID']) + " - " + status + " - " + str(nbp)) self.assertEqual(status, "wait") self.assertEqual(nbp, 1) # enlist Mickey and test the 'enlist' answer "wait" mickey = pp_test[1] result = backend.enlistPlayer(mickey['playerID']) status = result['status'] nbp = result['nb_players'] vprint(" enlist Mickey : " + str(mickey['playerID']) + " - " + status + " - " + str(nbp)) self.assertEqual(status, "wait") self.assertEqual(nbp, 2) # enlist Daisy and test the 'enlist' answer == "wait" daisy = pp_test[5] result = backend.enlistPlayer(daisy['playerID']) status = result['status'] nbp = result['nb_players'] vprint(" enlist Daisy : " + str(daisy['playerID']) + " - " + status + " - " + str(nbp)) self.assertEqual(status, "wait") self.assertEqual(nbp, 3) # enlist AGAIN Donald and test the 'enlist' answer "wait" result = backend.enlistPlayer(donald['playerID']) status = result['status'] nbp = result['nb_players'] vprint(" enlist Donald : " + str(donald['playerID']) + " - " + status + " - " + str(nbp)) self.assertEqual(status, "wait") self.assertEqual(nbp, 3) # enlist Riri and test the 'enlist' answer == gameID # i.e. this fourth player enlisting should start a new game riri = pp_test[2] pID = riri['playerID'] result = backend.enlistPlayer(pID) status = result['status'] #print("BOGUS 21: status =", status) gameID = result['gameID'] riri_db = backend.players.getPlayer(pID) gameID_db = riri_db['gameID'] vprint(" enlist Riri : " + str(pID) + " - " + status + " (" + str(gameID) + " == " + str(gameID_db) + ")") self.assertEqual(status, "ok") self.assertEqual(gameID_db, gameID) # enlist Fifi and test the 'enlist' answer == "wait" fifi = pp_test[3] pID = fifi['playerID'] result = backend.enlistPlayer(pID) status = result['status'] vprint(" enlist Fifi : " + str(pID) + " - " + status) self.assertEqual(status, "wait") # enlist AGAIN Mickey and test the 'enlist' answer gameID pID = mickey['playerID'] result = backend.enlistPlayer(pID) status = result['status'] gameID = result['gameID'] mickey_db = backend.players.getPlayer(pID) gameID_db = mickey_db['gameID'] vprint(" enlist again Mickey : " + str(pID) + " - " + status + " (" + str(gameID) + " == " + str(gameID_db) + ")") self.assertEqual(status, "ok") self.assertEqual(gameID, gameID_db) # enlist an unknown plauerID and test the 'enlist' answer 'invalid' result = backend.enlistPlayer(str(ObjectId())) status = result['status'] vprint(" enlist M. X (unknown to the server): " + status) self.assertEqual(status, "ko") # removes residual test data self.tearDown()
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)