def test8(self): """ Check if it is possible to place a cat/mouse out of the board. Creation must return an exception. That is, models should check if cat is in a valid square. """ for id_cell in [Game.MIN_CELL - 1, Game.MAX_CELL + 1]: with self.assertRaises(ValidationError): game = Game(cat_user=self.users[0], mouse_user=self.users[1], cat1=id_cell) game.full_clean() game.save() with self.assertRaises(ValidationError): game = Game(cat_user=self.users[0], mouse_user=self.users[1], cat2=id_cell) game.full_clean() game.save() with self.assertRaises(ValidationError): game = Game(cat_user=self.users[0], mouse_user=self.users[1], cat3=id_cell) game.full_clean() game.save() with self.assertRaises(ValidationError): game = Game(cat_user=self.users[0], mouse_user=self.users[1], cat4=id_cell) game.full_clean() game.save() with self.assertRaises(ValidationError): game = Game(cat_user=self.users[0], mouse_user=self.users[1], mouse=id_cell) game.full_clean() game.save()
def test2(self): """ check if moves are possible in non active games""" game = Game(cat_user=self.users[0]) game.save() with self.assertRaisesRegex(ValidationError, tests.MSG_ERROR_MOVE): Move.objects.create(game=game, player=self.users[0], origin=0, target=9)
def test10(self): """ OPTIONAL: Check if it is possible to place a cat/mouse in a black square. Creation must return an exception. That is, models should check if cat is in a valid square. """ # 26, 44, 62, 7, 56 = black cells with self.assertRaisesRegex(ValidationError, tests.MSG_ERROR_INVALID_CELL): game = Game(cat_user=self.users[0], cat1=26) game = Game(cat_user=self.users[0], mouse_user=self.users[1], cat1=26) game.full_clean() game.save() with self.assertRaisesRegex(ValidationError, tests.MSG_ERROR_INVALID_CELL): game = Game(cat_user=self.users[0], cat2=44) game.full_clean() game.save() with self.assertRaisesRegex(ValidationError, tests.MSG_ERROR_INVALID_CELL): game = Game(cat_user=self.users[0], cat3=62) game.full_clean() game.save() with self.assertRaisesRegex(ValidationError, tests.MSG_ERROR_INVALID_CELL): game = Game(cat_user=self.users[0], cat4=7) game.full_clean() game.save() with self.assertRaisesRegex(ValidationError, tests.MSG_ERROR_INVALID_CELL): game = Game(cat_user=self.users[0], mouse=56) game.full_clean() game.save()
def test11(self): """ Check if __str__ function is available. If Game created (but not active) only cat information is printed If game is actived then cat and mouse position are given [X] vs [ ] report if it is the cat/mouse player turn. """ game = Game(id=0, cat_user=self.users[0]) self.assertEqual(str(game), "(0, Created)\tCat [X]" " cat_user_test(0, 2, 4, 6)") game.mouse_user = self.users[1] game.status = GameStatus.ACTIVE game.save() self.assertEqual( str(game), "(0, Active)\tCat [X] cat_user_test(0, 2, 4, 6)" " --- Mouse [ ] mouse_user_test(59)") game.cat_turn = False self.assertEqual( str(game), "(0, Active)\tCat [ ] " "cat_user_test(0, 2, 4, 6) --- Mouse [X] mouse_user_test(59)") game.status = GameStatus.FINISHED game.save() self.assertEqual( str(game), "(0, Finished)\tCat [ ]" " cat_user_test(0, 2, 4, 6) --- Mouse [X] mouse_user_test(59)" )
def populate(): try: user1 = User.objects.get(id=10) except User.DoesNotExist: user1 = User.objects.create_user(id=10, username="******") try: user2 = User.objects.get(id=11) except User.DoesNotExist: user2 = User.objects.create_user(id=11, username="******") game = Game(cat_user=user1) game.full_clean() game.save() gamelist = [] gamelisttemp = Game.objects.all() for game in gamelisttemp: if (game.mouse_user is None): gamelist.append(game) print(gamelist) playedgame = min(gamelist, key=attrgetter('id')) playedgame.mouse_user = user2 game.full_clean() game.save() print(playedgame) Move.objects.create(game=playedgame, player=user1, origin=2, target=11) print(playedgame) Move.objects.create(game=playedgame, player=user2, origin=59, target=52) print(playedgame)
def test11(self): """ Conversiones a string """ game = Game(id=0, cat_user=self.users[0]) self.assertEqual(str(game), "(0, Created)\tCat [X] cat_user_test(0, 2, 4, 6)") game.mouse_user = self.users[1] game.status = GameStatus.ACTIVE game.save() self.assertEqual( str(game), "(0, Active)\tCat [X] cat_user_test(0, 2, 4, 6) --- Mouse [ ] mouse_user_test(59)" ) game.cat_turn = False self.assertEqual( str(game), "(0, Active)\tCat [ ] cat_user_test(0, 2, 4, 6) --- Mouse [X] mouse_user_test(59)" ) game.status = GameStatus.FINISHED game.save() self.assertEqual( str(game), "(0, Finished)\tCat [ ] cat_user_test(0, 2, 4, 6) --- Mouse [X] mouse_user_test(59)" )
def create_game(request): user = request.user game = Game(cat_user=user) game.save() return render(request, "mouse_cat/new_game.html", {'game': game})
def test9(self): """ Relaciones inversas entre User y Game""" self.assertEqual(self.users[0].games_as_cat.count(), 0) self.assertEqual(self.users[1].games_as_mouse.count(), 0) self.assertEqual( User.objects.filter(games_as_cat__cat_user__username=self.users[0]. username).count(), 0) self.assertEqual( User.objects.filter(games_as_mouse__mouse_user__username=self. users[1].username).count(), 0) game = Game(cat_user=self.users[0], mouse_user=self.users[1]) game.save() self.assertEqual(self.users[0].games_as_cat.count(), 1) self.assertEqual(self.users[1].games_as_mouse.count(), 1) self.assertEqual( User.objects.filter(games_as_cat__cat_user__username=self.users[0]. username).count(), 1) self.assertEqual( User.objects.filter(games_as_mouse__mouse_user__username=self. users[1].username).count(), 1) game = Game(cat_user=self.users[0]) game.save() self.assertEqual(self.users[0].games_as_cat.count(), 2) self.assertEqual(self.users[1].games_as_mouse.count(), 1) self.assertEqual( User.objects.filter(games_as_cat__cat_user__username=self.users[0]. username).count(), 2) self.assertEqual( User.objects.filter(games_as_mouse__mouse_user__username=self. users[1].username).count(), 1)
def create_game_service(request): game = Game() game.cat_user = request.user game.save() context_dict = {} context_dict['game'] = game return render(request, 'mouse_cat/new_game.html', context_dict)
def test2(self): """ OPTIONAL: If game is created with both cat user and mouse user then status=activated game""" game = Game(cat_user=self.users[0], mouse_user=self.users[1]) game.save() self.assertEqual(self.get_array_positions(game), [0, 2, 4, 6, 59]) self.assertTrue(game.cat_turn) self.assertEqual(game.status, GameStatus.ACTIVE)
def test2(self): """ Optional """ """ Crear juego válido con dos jugadores """ game = Game(cat_user=self.users[0], mouse_user=self.users[1]) game.save() self.assertEqual(self.get_array_positions(game), [0, 2, 4, 6, 59]) self.assertTrue(game.cat_turn) self.assertEqual(game.status, GameStatus.ACTIVE)
def test3(self): """ Comprobar que el juego se termina correctamente cuando el ratón es encerrado en una esquina """ game = Game(cat_user=self.users[0]) game.full_clean() game.mouse_user = self.users[1] game.cat2 = 54 game.mouse = 63 game.save() self.assertEqual(game.status, GameStatus.FINISHED)
def test3(self): """ Optional """ """ Transición de creado a activo al añadir el segundo jugador """ game = Game(cat_user=self.users[0]) game.save() self.assertEqual(game.status, GameStatus.CREATED) game.mouse_user = self.users[1] game.save() self.assertEqual(game.status, GameStatus.ACTIVE)
def test1(self): """ Crear juego válido con un único jugador """ game = Game(cat_user=self.users[0]) game.full_clean() game.save() self.assertIsNone(game.mouse_user) self.assertEqual(self.get_array_positions(game), [0, 2, 4, 6, 59]) self.assertTrue(game.cat_turn) self.assertEqual(game.status, GameStatus.CREATED)
def create_game(request): # Author: Sergio Galán # Creamos el juego en cuestión game = Game(cat_user=request.user) game.save() context_dict = {} # Pasamos el juego creado a la template context_dict['game'] = game return render(request, "mouse_cat/new_game.html", context_dict)
def test3(self): """ OPTIONAL: If mouse_user is added to a CREATED game then sttus became ACTIVE""" game = Game(cat_user=self.users[0]) game.save() self.assertEqual(game.status, GameStatus.CREATED) game.mouse_user = self.users[1] game.save() self.assertEqual(game.status, GameStatus.ACTIVE)
def test1(self): def get_game_id(game): return game.id """ Comprobar si existe un usuario con id=10 y si no existe crearlo """ try: user = User.objects.get(id=10) except User.DoesNotExist: user = User.objects.create_user(username="******", password="******", id=10) self.users.append(user) """ Comprobar si existe un usuario con id=11 y si no existe crearlo """ try: user = User.objects.get(id=11) except User.DoesNotExist: user = User.objects.create_user(username="******", password="******", id=11) self.users.append(user) """ Crear un juego y asignarselo al usuario con id=10 """ game = Game(cat_user=User.objects.get(id=10)) #game.full_clean() game.save() """ Buscar todos los juegos con un solo usuario asignado """ print(game) oneplayergames = [] for item in Game.objects.all(): if item.cat_user != None: if item.mouse_user == None: oneplayergames.append(item) else: if item.mouse_user != None: oneplayergames.append(item) print(oneplayergames) """ Unir al usuario con id=11 al juego con menor id encontrado en el paso anterior y comenzar la partida """ oneplayergames.sort(key=get_game_id) game = oneplayergames[0] game.mouse_user = User.objects.get(id=11) game.save() print(game) """ En la partida seleccionada, mover el segundo gato pasandolo de la posicion 2a la 11 """ Move.objects.create(game=game, player=User.objects.get(id=10), origin=2, target=11) print(game) """ En la partida seleccionada, mover el raton de la posicion 59 a la 52 """ Move.objects.create(game=game, player=User.objects.get(id=11), origin=59, target=52) print(game) """
def test5(self): """ Comprobar que el juego no se termina cuando el ratón tiene aún una salida""" game = Game(cat_user=self.users[0]) game.full_clean() game.mouse_user = self.users[1] game.cat1 = 38 game.cat2 = 52 game.cat3 = 54 game.mouse = 45 game.save() self.assertEqual(game.status, GameStatus.ACTIVE)
def test8(self): """ Comprobar que el juego se termina correctamente cuando el ratón ha superado a los gatos""" game = Game(cat_user=self.users[0]) game.full_clean() game.mouse_user = self.users[1] game.cat1 = 36 game.cat2 = 38 game.cat3 = 52 game.cat4 = 54 game.mouse = 20 game.save() self.assertEqual(game.status, GameStatus.FINISHED)
def test1(self): """ Comprobar que el juego se encuentra activo al empezar """ game = Game(cat_user=self.users[0]) game.full_clean() game.save() self.assertIsNone(game.mouse_user) self.assertTrue(game.cat_turn) self.assertEqual(game.status, GameStatus.CREATED) game.mouse_user = self.users[1] game.save() self.assertEqual(game.status, GameStatus.ACTIVE) self.assertEqual(self.get_array_positions(game), [0, 2, 4, 6, 59])
def test1(self): """ Create Game with test. Test default values for cats, mouse and status""" game = Game(cat_user=self.users[0]) game.full_clean() # this function force validation to execute # very likely is not important at this step # reinforce constrains defined for forms game.save() self.assertIsNone(game.mouse_user) self.assertEqual(self.get_array_positions(game), [0, 2, 4, 6, 59]) self.assertTrue(game.cat_turn) self.assertEqual(game.status, GameStatus.CREATED)
def test6(self): """ Comprobar que el juego se termina correctamente cuando el ratón está completamente rodeado""" game = Game(cat_user=self.users[0]) game.full_clean() game.mouse_user = self.users[1] game.cat1 = 36 game.cat2 = 38 game.cat3 = 52 game.cat4 = 54 game.mouse = 45 game.save() self.assertEqual(game.status, GameStatus.FINISHED)
def test7(self): """ Comprobar que el juego no se termina cuando el ratón ha superado a la mitad de los gatos""" game = Game(cat_user=self.users[0]) game.full_clean() game.mouse_user = self.users[1] game.cat1 = 34 game.cat2 = 38 game.cat3 = 52 game.cat4 = 54 game.mouse = 45 game.save() self.assertEqual(game.status, GameStatus.ACTIVE)
def test7(self): """ Model test """ """ Validación de celdas válidas """ # MIN_CELL = 0, MAX_CELL = 63 for id_cell in [Game.MIN_CELL, Game.MAX_CELL]: game = Game(cat_user=self.users[0], cat1=id_cell, cat2=id_cell, cat3=id_cell, cat4=id_cell, mouse=id_cell) game.full_clean() game.save()
def test2(self): """ Comprobar que el juego se encuentra activo tras una serie de movimientos """ game = Game(cat_user=self.users[0]) game.full_clean() game.mouse_user = self.users[1] game.cat2 += 9 game.cat3 += 9 game.cat4 += 9 game.mouse -= 9 game.save() self.assertEqual(game.status, GameStatus.ACTIVE) self.assertEqual(self.get_array_positions(game), [0, 2 + 9, 4 + 9, 6 + 9, 59 - 9])
def test0(self): """ Validación de la actualización del juego al ganar el gato""" game = Game(cat_user=self.user1) game.save() game.cat1 = 41 game.cat2 = 50 game.mouse = 48 game.mouse_user = self.user2 game.save() move = Move(origin=50, target=57, player=self.user1, game=game) move.save() self.assertEqual(game.status, GameStatus.FINISHED)
def test4(self): """Test status attribute""" """ROB note valid is never used""" states = [ {"status": GameStatus.ACTIVE, "valid": True}, {"status": GameStatus.FINISHED, "valid": True} ] for state in states: game = Game(cat_user=self.users[0], mouse_user=self.users[1], status=state["status"]) game.full_clean() game.save() self.assertEqual(game.status, state["status"])
def create_ai_game_service(request): game = Game() game.cat_user = request.user if User.objects.filter(username="******").exists(): game.mouse_user = User.objects.filter(username="******").first() else: game.mouse_user = User.objects.create_user('Bot', '', '0i80t81d16s810') game.save() context_dict = {} context_dict['game'] = game return select_game_service(request, game_id=game.id)
def test4(self): """ Estados válidos de juegos con dos jugadores """ states = [{ "status": GameStatus.ACTIVE, "valid": True }, { "status": GameStatus.FINISHED, "valid": True }] for state in states: game = Game(cat_user=self.users[0], mouse_user=self.users[1], status=state["status"]) game.full_clean() game.save() self.assertEqual(game.status, state["status"])
def test7(self): """ # check board limits, notice that all # cats should not be in the same possition # varaibles Game.MIN_CELL and game.MAX_CELL # should be defined. This test may fail if code to reinforce constrain cat1 != cat2 is implemented """ for id_cell in [Game.MIN_CELL, Game.MAX_CELL]: game = Game( cat_user=self.users[0], cat1=id_cell, cat2=id_cell, cat3=id_cell, cat4=id_cell, mouse=id_cell) game.full_clean() # force Django validation Code game.save()