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 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 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 test8(self): """ Piezas fuera del tablero """ 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() with self.assertRaises(ValidationError): game = Game(cat_user=self.users[0], mouse_user=self.users[1], cat2=id_cell) game.full_clean() with self.assertRaises(ValidationError): game = Game(cat_user=self.users[0], mouse_user=self.users[1], cat3=id_cell) game.full_clean() with self.assertRaises(ValidationError): game = Game(cat_user=self.users[0], mouse_user=self.users[1], cat4=id_cell) game.full_clean() with self.assertRaises(ValidationError): game = Game(cat_user=self.users[0], mouse_user=self.users[1], mouse=id_cell) game.full_clean()
def test6(self): """ Juegos sin jugador 1 """ for status in [ GameStatus.CREATED, GameStatus.ACTIVE, GameStatus.FINISHED ]: with self.assertRaises(ValidationError): game = Game(mouse_user=self.users[1], status=status) game.full_clean()
def test6(self): """ It is not valid to create a game without the cat player""" for status in [GameStatus.CREATED, GameStatus.ACTIVE, GameStatus.FINISHED]: with self.assertRaises(ValidationError): game = Game(mouse_user=self.users[1], status=status) game.full_clean()
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 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 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 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 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 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 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 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 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 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()
def test1(self): try: user1 = User.objects.get(id=10) except User.DoesNotExist: user1 = User.objects.create_user(id=10, username="******") self.assertEqual(user1.id, 10) try: user2 = User.objects.get(id=11) except User.DoesNotExist: user2 = User.objects.create_user(id=11, username="******") self.assertEqual(user2.id, 11) game = Game(cat_user=user1) game.full_clean() game.save() self.assertEqual(game.cat_user.id, 10) 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) move1 = Move.objects.create(game=playedgame, player=user1, origin=2, target=11) print(playedgame) self.assertEqual(game.cat2, 11) move2 = Move.objects.create(game=playedgame, player=user2, origin=59, target=52) print(playedgame) self.assertEqual(game.mouse, 52)
def test5(self): """It is possible to create a game using only the cat user and the status""" states = [ {"status": GameStatus.CREATED, "valid": True}, {"status": GameStatus.ACTIVE, "valid": False}, {"status": GameStatus.FINISHED, "valid": False} ] for state in states: try: game = Game(cat_user=self.users[0], status=state["status"]) game.full_clean() game.save() self.assertEqual(game.status, state["status"]) except ValidationError as err: with self.assertRaisesRegex(ValidationError, tests.MSG_ERROR_GAMESTATUS): if not state["valid"]: raise err
def test_query(): # Vemos is existe un usuario con id = 10 y, si no, lo creamos user10 = User.objects.get_or_create(id=10)[0] if user10.username == "": user10.username = '******' user10.save() # Vemos is existe un usuario con id = 11 y, si no, lo creamos user11 = User.objects.get_or_create(id=11)[0] if user11.username == "": user11.username = '******' user11.save() game = Game(cat_user=user10) game.full_clean() game.save() ids = [] games_only_one_user = Game.objects.filter(status=GameStatus.CREATED) print(" ---- Games with only one user ----") for g in games_only_one_user: ids.append(g.id) print(g) print(" ---- Añadimos el Ratón ----") ids.sort() game = Game.objects.filter(id=ids[0])[0] game.mouse_user = user11 game.full_clean() game.save() print(game) print(" ---- Movemos el cat2 a la posición 11 ----") move = Move.objects.create(game=game, player=user10, origin=game.cat2, target=11) game.full_clean() game.save() print(game) print(" ---- Movemos el Mouse a la posición 52 ----") move = Move.objects.create(game=game, player=user11, origin=game.mouse, target=52) game.full_clean() game.save() print(game)
def test5(self): """ Estados válidos con un jugador """ states = [{ "status": GameStatus.CREATED, "valid": True }, { "status": GameStatus.ACTIVE, "valid": False }, { "status": GameStatus.FINISHED, "valid": False }] for state in states: try: game = Game(cat_user=self.users[0], status=state["status"]) game.full_clean() game.save() self.assertEqual(game.status, state["status"]) except ValidationError as err: with self.assertRaisesRegex(ValidationError, tests.MSG_ERROR_GAMESTATUS): if not state["valid"]: raise err
try: user1 = User.objects.get(id=10) except User.DoesNotExist: user1 = User.objects.create_user(id=10, username="******", password="******") try: user2 = User.objects.get(id=11) except User.DoesNotExist: user2 = User.objects.create_user(id=11, username="******", password="******") game = Game(cat_user=user1) game.full_clean() game.save() query = Game.objects.filter(mouse_user=None) for juego in query: print(juego) print("\n\n") # query ordenando por id y pillando el primero query = Game.objects.filter(mouse_user=None).order_by("id") juego = query[0] juego.mouse_user = user2 juego.save()
def create_game_service(request): game = Game(cat_user=request.user) game.full_clean() game.save() return render(request, 'mouse_cat/new_game.html', {'game': game})