Beispiel #1
0
class TeamPresenterTestCase(TestCase):

    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user = User.objects.create_user('user', '', 'password')

        self.player = Player(user=self.user)
        self.player.save()

        self.team = Team(player=self.player, game=self.game)
        self.team.save()

    def test_from_team(self):
        presenter = TeamPresenter.from_team(team=self.team, game=self.game)

        self.assertEqual(presenter.player.username, self.user.username)
        self.assertTrue(presenter.is_next)
        self.assertEqual(presenter.winner, self.team.winner)
        self.assertEqual(presenter.alive, self.team.alive)
        self.assertEqual(len(presenter.tiles), GAME_SIZE)
        self.assertEqual(len(presenter.tiles[0]), GAME_SIZE)

    def test_make_tiles(self):
        tiles = TeamPresenter.make_tiles(team=self.team, game=self.game)

        self.assertEqual(len(tiles), GAME_SIZE)
        for i in range(0, GAME_SIZE):
            self.assertEqual(len(tiles[i]), GAME_SIZE)
Beispiel #2
0
    def test_ship_creation(self):
        """Test that Ship instances are created correctly."""
        game = Game()
        game.save()

        user = User(username='******', password='******')
        user.save()

        player = Player(user=user)
        player.save()

        team = Team(player=player, game=game)
        team.save()

        ship = Ship(
            team=team,
            x=0,
            y=0,
            length=3,
            direction=Ship.CARDINAL_DIRECTIONS['WEST']
        )

        self.assertTrue(isinstance(ship, Ship))
        self.assertEqual(
            str(ship),
            'Game 1 - user\'s 3L at (0, 0) facing West'
        )
    def setUp(self):
        self.game1 = Game()
        self.game2 = Game()
        self.game1.save()
        self.game2.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player1.save()
        self.player2.save()

        self.team_game1 = Team(
            player=self.player1,
            game=self.game1
        )
        self.team_game2 = Team(
            player=self.player1,
            game=self.game2,
            alive=False
        )
        self.team_game1.save()
        self.team_game2.save()
Beispiel #4
0
 def setUp(self):
     self.duke = School(abbrev="DUKE", name="Duke")
     self.maryland = School(abbrev="MARY", name="Maryland")
     self.season = Season(year=2012, start_date="2011-11-01", 
                          end_date="2012-04-15")
     self.duke_2012 = Team(school=self.duke, season=self.season)
     self.maryland_2012 = Team(school=self.maryland, season=self.season)
     self.game = Game(team=self.duke_2012, opponent=self.maryland_2012)
Beispiel #5
0
    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated():
            form = CreateGameForm(request.POST, max_players=MAX_PLAYERS)
            if form.is_valid():
                opponent_usernames = []
                for i in range(0, MAX_PLAYERS):
                    field_name = 'opponent_username_{}'.format(i)
                    opponent_usernames.append(form.cleaned_data[field_name])

                try:
                    opponent_users = []
                    for opponent_username in opponent_usernames:
                        if len(opponent_username) > 0:
                            opponent_users.append(
                                User.objects.get(username=opponent_username))
                except User.DoesNotExist:
                    error_message = 'User does not exist! '\
                        'Are you sure the username is correct?'
                    messages.error(request, error_message)
                    context = {'form': form}
                    return render(request, self.template_name, context)

                user_player = Player.objects.get(user=request.user)
                opponent_players = [
                    Player.objects.get(user=opponent_user)
                    for opponent_user in opponent_users
                ]

                # Create a game plus teams and ships for both players
                # Creation in Game -> Team -> Ships order is important
                # to satisfy ForeignKey dependencies.
                game = Game()
                game.save()
                user_team = Team(player=user_player, game=game, last_turn=-2)
                opponent_teams = [
                    Team(player=opponent_player, game=game, last_turn=-1)
                    for opponent_player in opponent_players
                ]
                user_team.save()
                for opponent_team in opponent_teams:
                    opponent_team.save()

                user_ships = make_ships(user_team, Ship.LENGTHS)
                for opponent_team in opponent_teams:
                    opponent_ships = make_ships(opponent_team, Ship.LENGTHS)
                    for user_ship in user_ships:
                        user_ship.save()
                    for opponent_ship in opponent_ships:
                        opponent_ship.save()

                return HttpResponseRedirect(reverse('game', args=[game.id]))
            else:
                messages.error(request, 'Invalid form.')
                context = {'form': form}
                return render(request, self.template_name, context)
        else:
            return HttpResponseRedirect('/login')
Beispiel #6
0
    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user = User.objects.create_user('user', '', 'password')

        self.player = Player(user=self.user)
        self.player.save()

        self.team = Team(player=self.player, game=self.game)
        self.team.save()
Beispiel #7
0
    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated():
            form = CreateGameForm(request.POST, max_players=MAX_PLAYERS)
            if form.is_valid():
                opponent_usernames = []
                for i in range(0, MAX_PLAYERS):
                    field_name = "opponent_username_{}".format(i)
                    opponent_usernames.append(form.cleaned_data[field_name])

                try:
                    opponent_users = []
                    for opponent_username in opponent_usernames:
                        if len(opponent_username) > 0:
                            opponent_users.append(User.objects.get(username=opponent_username))
                except User.DoesNotExist:
                    error_message = "User does not exist! " "Are you sure the username is correct?"
                    messages.error(request, error_message)
                    context = {"form": form}
                    return render(request, self.template_name, context)

                user_player = Player.objects.get(user=request.user)
                opponent_players = [Player.objects.get(user=opponent_user) for opponent_user in opponent_users]

                # Create a game plus teams and ships for both players
                # Creation in Game -> Team -> Ships order is important
                # to satisfy ForeignKey dependencies.
                game = Game()
                game.save()
                user_team = Team(player=user_player, game=game, last_turn=-2)
                opponent_teams = [
                    Team(player=opponent_player, game=game, last_turn=-1) for opponent_player in opponent_players
                ]
                user_team.save()
                for opponent_team in opponent_teams:
                    opponent_team.save()

                user_ships = make_ships(user_team, Ship.LENGTHS)
                for opponent_team in opponent_teams:
                    opponent_ships = make_ships(opponent_team, Ship.LENGTHS)
                    for user_ship in user_ships:
                        user_ship.save()
                    for opponent_ship in opponent_ships:
                        opponent_ship.save()

                return HttpResponseRedirect(reverse("game", args=[game.id]))
            else:
                messages.error(request, "Invalid form.")
                context = {"form": form}
                return render(request, self.template_name, context)
        else:
            return HttpResponseRedirect("/login")
Beispiel #8
0
class IsTeamNextTestCase(TestCase):
    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')
        self.user3 = User.objects.create_user('user3', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player3 = Player(user=self.user3)
        self.player1.save()
        self.player2.save()
        self.player3.save()

        self.team1 = Team(player=self.player1, game=self.game, last_turn=1)
        self.team2 = Team(player=self.player2, game=self.game, last_turn=2)
        self.team3 = Team(player=self.player3, game=self.game, last_turn=3)
        self.team1.save()
        self.team2.save()
        self.team3.save()

    def test_team_is_next(self):
        self.assertTrue(is_team_next(self.team1, self.game))

    def test_team_is_not_next(self):
        self.assertFalse(is_team_next(self.team2, self.game))
        self.assertFalse(is_team_next(self.team3, self.game))

    def test_non_alive_team(self):
        self.team1.alive = False
        self.team1.save()

        self.assertTrue(is_team_next(self.team2, self.game))
Beispiel #9
0
    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player1.save()
        self.player2.save()

        self.team1 = Team(player=self.player1, game=self.game)
        self.team2 = Team(player=self.player2, game=self.game)
        self.team1.save()
        self.team2.save()
    def setUp(self):
        self.user = User.objects.create_user("user1", "", "password")
        self.player = Player(user=self.user)
        self.player.save()

        self.winning_game = Game()
        self.losing_game = Game()
        self.in_progress_game = Game()
        self.winning_game.save()
        self.losing_game.save()
        self.in_progress_game.save()

        self.winning_team = Team(game=self.winning_game, player=self.player, alive=True, winner=True)
        self.losing_team = Team(game=self.losing_game, player=self.player, alive=False, winner=False)
        self.in_progress_team = Team(game=self.in_progress_game, player=self.player, alive=True, winner=False)
        self.winning_team.save()
        self.losing_team.save()
        self.in_progress_team.save()
Beispiel #11
0
    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player1.save()
        self.player2.save()

        self.team1 = Team(player=self.player1, game=self.game)
        self.team2 = Team(player=self.player2, game=self.game)
        self.team1.save()
        self.team2.save()

        self.ship = Ship(
            team=self.team2,
            x=3,
            y=3,
            length=3,
            direction=Ship.CARDINAL_DIRECTIONS['SOUTH']
        )
        self.ship.save()

        self.shot_miss = Shot(
            game=self.game,
            attacking_team=self.team1,
            defending_team=self.team2,
            x=2,
            y=3
        )
        self.shot_miss.save()

        self.shot_hit = Shot(
            game=self.game,
            attacking_team=self.team1,
            defending_team=self.team2,
            x=3,
            y=5
        )
        self.shot_hit.save()
Beispiel #12
0
def fetch_all_team():
    league = 'bl1/'
    season = '2016'
    url_endpoint = URL_TEAM_ALL
    url = url_endpoint + league + season
    resp = requests.get(url)
    teams_json = resp.json()
    for team in teams_json:
        Team(id=team['TeamId'], name=team['TeamName'],
             alias=team['ShortName']).save()
    return
Beispiel #13
0
    def setUp(self):
        self.game1 = Game()
        self.game2 = Game()
        self.game1.save()
        self.game2.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player1.save()
        self.player2.save()

        self.team_game1 = Team(player=self.player1, game=self.game1)
        self.team_game2 = Team(player=self.player1,
                               game=self.game2,
                               alive=False)
        self.team_game1.save()
        self.team_game2.save()
Beispiel #14
0
    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')
        self.user3 = User.objects.create_user('user3', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player3 = Player(user=self.user3)
        self.player1.save()
        self.player2.save()
        self.player3.save()

        self.team1 = Team(player=self.player1, game=self.game, last_turn=1)
        self.team2 = Team(player=self.player2, game=self.game, last_turn=2)
        self.team3 = Team(player=self.player3, game=self.game, last_turn=3)
        self.team1.save()
        self.team2.save()
        self.team3.save()
    def setUp(self):
        self.user = User.objects.create_user('user1', '', 'password')
        self.player = Player(user=self.user)
        self.player.save()

        self.winning_game = Game()
        self.losing_game = Game()
        self.in_progress_game = Game()
        self.winning_game.save()
        self.losing_game.save()
        self.in_progress_game.save()

        self.winning_team = Team(
            game=self.winning_game,
            player=self.player,
            alive=True,
            winner=True
        )
        self.losing_team = Team(
            game=self.losing_game,
            player=self.player,
            alive=False,
            winner=False
        )
        self.in_progress_team = Team(
            game=self.in_progress_game,
            player=self.player,
            alive=True,
            winner=False
        )
        self.winning_team.save()
        self.losing_team.save()
        self.in_progress_team.save()
class PlayerPresenterTestCase(TestCase):
    def setUp(self):
        self.user = User.objects.create_user("user1", "", "password")
        self.player = Player(user=self.user)
        self.player.save()

        self.winning_game = Game()
        self.losing_game = Game()
        self.in_progress_game = Game()
        self.winning_game.save()
        self.losing_game.save()
        self.in_progress_game.save()

        self.winning_team = Team(game=self.winning_game, player=self.player, alive=True, winner=True)
        self.losing_team = Team(game=self.losing_game, player=self.player, alive=False, winner=False)
        self.in_progress_team = Team(game=self.in_progress_game, player=self.player, alive=True, winner=False)
        self.winning_team.save()
        self.losing_team.save()
        self.in_progress_team.save()

    def test_from_player(self):
        presenter = PlayerPresenter.from_player(self.player)

        self.assertEqual(presenter.username, self.user.username)
        self.assertEqual(presenter.win_count, 1)
        self.assertEqual(presenter.loss_count, 1)
        self.assertEqual(presenter.in_progress_count, 1)
Beispiel #17
0
    def test_team_creation(self):
        """Test that Team instances are created correctly."""
        game = Game()
        game.save()

        user = User.objects.create_user('user', '', 'password')

        player = Player(user=user)
        player.save()

        team = Team(player=player, game=game)

        self.assertTrue(isinstance(team, Team))
        self.assertEqual(str(team), 'Game 1 - user (last_turn=0)')
Beispiel #18
0
class GamePresenterTestCase(TestCase):

    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player1.save()
        self.player2.save()

        self.team1 = Team(player=self.player1, game=self.game)
        self.team2 = Team(player=self.player2, game=self.game)
        self.team1.save()
        self.team2.save()

    def test_from_game(self):
        presenter = GamePresenter.from_game(self.game)

        self.assertEqual(presenter.id, self.game.id)
        self.assertEqual(len(presenter.teams), 2)
Beispiel #19
0
    def test_game_creation(self):
        """Test that Game instances are created correctly."""
        game = Game()
        game.save()

        user1 = User.objects.create_user('user1', '', 'password')
        user2 = User.objects.create_user('user2', '', 'password')

        player1 = Player(user=user1)
        player2 = Player(user=user2)
        player1.save()
        player2.save()

        team1 = Team(player=player1, game=game)
        team2 = Team(player=player2, game=game)
        team1.save()
        team2.save()

        self.assertTrue(isinstance(game, Game))
        self.assertEqual(str(game), '1 - user1 user2')
Beispiel #20
0
    def test_shot_creation(self):
        """Test that Shot instances are created correctly."""
        game = Game()
        game.save()

        attacking_user = User.objects.create_user(
            'attacking_user',
            '',
            'password'
        )
        defending_user = User.objects.create_user(
            'defending_user',
            '',
            'password'
        )
        attacking_user.save()
        defending_user.save()

        attacking_player = Player(user=attacking_user)
        defending_player = Player(user=defending_user)
        attacking_player.save()
        defending_player.save()

        attacking_team = Team(player=attacking_player, game=game)
        defending_team = Team(player=defending_player, game=game)
        attacking_team.save()
        defending_team.save()

        shot = Shot(
            game=game,
            attacking_team=attacking_team,
            defending_team=defending_team,
            x=0,
            y=0
        )

        self.assertTrue(isinstance(shot, Shot))
        self.assertEqual(
            str(shot),
            'Game 1 - attacking_user attacked defending_user (0, 0)'
        )
Beispiel #21
0
def create_league_tour(league_instance):
    """Round-robin Tournaments Algorithm
    - Create the neccessary Teams
    - Apply the round-robin algorithm to create required Fixtures
    """
    number_of_teams = league_instance.team_number
    player_per_team = league_instance.player_per_team
    teams = []

    for idx in range(number_of_teams):

        def _player_name(play_id):
            name = "LT{league_id}-T{team_id}-Player_{play_id}".format(
                league_id=league_instance.id,
                team_id=idx,
                play_id=play_id)
            return name

        players = [Player.objects.create(name=_player_name(playid)) for playid in range(player_per_team)]
        team = Team(player_a=players[0], tour=league_instance, name='LT{}-T{}'.format(league_instance.id, idx))
        team.player_b = players[1] if len(players) > 2 else None
        team.player_c = players[2] if len(players) > 3 else None
        team.player_d = players[3] if len(players) == 4 else None
        team.save()

        teams.append(team)

    games = []
    fixtures = []

    def create_game_and_fixture(match, round):
        home_team = match[0]
        away_team = match[1]
        game = Game.objects.create(home_team=home_team, away_team=away_team)
        games.append(game)
        fixture = Fixture.objects.create(game=game, tour=league_instance, round=round+1)
        fixtures.append(fixture)

    round_robin_scheduler(teams, create_game_and_fixture)

    return league_instance, teams, games, fixtures
Beispiel #22
0
class IsTeamNextTestCase(TestCase):

    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')
        self.user3 = User.objects.create_user('user3', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player3 = Player(user=self.user3)
        self.player1.save()
        self.player2.save()
        self.player3.save()

        self.team1 = Team(player=self.player1, game=self.game, last_turn=1)
        self.team2 = Team(player=self.player2, game=self.game, last_turn=2)
        self.team3 = Team(player=self.player3, game=self.game, last_turn=3)
        self.team1.save()
        self.team2.save()
        self.team3.save()

    def test_team_is_next(self):
        self.assertTrue(is_team_next(self.team1, self.game))

    def test_team_is_not_next(self):
        self.assertFalse(is_team_next(self.team2, self.game))
        self.assertFalse(is_team_next(self.team3, self.game))

    def test_non_alive_team(self):
        self.team1.alive = False
        self.team1.save()

        self.assertTrue(is_team_next(self.team2, self.game))
class PlayerPresenterTestCase(TestCase):

    def setUp(self):
        self.user = User.objects.create_user('user1', '', 'password')
        self.player = Player(user=self.user)
        self.player.save()

        self.winning_game = Game()
        self.losing_game = Game()
        self.in_progress_game = Game()
        self.winning_game.save()
        self.losing_game.save()
        self.in_progress_game.save()

        self.winning_team = Team(
            game=self.winning_game,
            player=self.player,
            alive=True,
            winner=True
        )
        self.losing_team = Team(
            game=self.losing_game,
            player=self.player,
            alive=False,
            winner=False
        )
        self.in_progress_team = Team(
            game=self.in_progress_game,
            player=self.player,
            alive=True,
            winner=False
        )
        self.winning_team.save()
        self.losing_team.save()
        self.in_progress_team.save()

    def test_from_player(self):
        presenter = PlayerPresenter.from_player(self.player)

        self.assertEqual(presenter.username, self.user.username)
        self.assertEqual(presenter.win_count, 1)
        self.assertEqual(presenter.loss_count, 1)
        self.assertEqual(presenter.in_progress_count, 1)
Beispiel #24
0
    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')
        self.user3 = User.objects.create_user('user3', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player3 = Player(user=self.user3)
        self.player1.save()
        self.player2.save()
        self.player3.save()

        self.team1 = Team(player=self.player1, game=self.game, last_turn=1)
        self.team2 = Team(player=self.player2, game=self.game, last_turn=2)
        self.team3 = Team(player=self.player3, game=self.game, last_turn=3)
        self.team1.save()
        self.team2.save()
        self.team3.save()
Beispiel #25
0
class HomeViewTestCase(TestCase):
    def setUp(self):
        self.game1 = Game()
        self.game2 = Game()
        self.game1.save()
        self.game2.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player1.save()
        self.player2.save()

        self.team_game1 = Team(player=self.player1, game=self.game1)
        self.team_game2 = Team(player=self.player1,
                               game=self.game2,
                               alive=False)
        self.team_game1.save()
        self.team_game2.save()

    def test_logged_out(self):
        url = reverse('home')
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 200)
        pq = PyQuery(resp.content)

        self.assertIn('You\'re not logged in', pq('h3').text())
        links = [e.attr('href') for e in pq('.container a').items()]
        self.assertIn(reverse('login'), links)
        self.assertIn(reverse('signup'), links)

    def test_logged_in_with_matches(self):
        self.client.login(username=self.user1.username, password='******')

        url = reverse('home')
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 200)
        pq = PyQuery(resp.content)

        # Assert links to 'Create New Game' plus ongoing games are shown
        self.assertIn('Your Active Games', pq('h3').text())
        links = [e.attr('href') for e in pq('.container a').items()]
        self.assertIn(reverse('game', args=[self.game1.id]), links)
        self.assertNotIn(reverse('game', args=[self.game2.id]), links)
        self.assertIn(reverse('create_game'), links)

    def test_logged_in_without_matches(self):
        self.client.login(username=self.user2.username, password='******')

        url = reverse('home')
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 200)
        pq = PyQuery(resp.content)

        # Assert only link to 'Create New Game' is shown
        self.assertEqual(len(pq('.container a')), 1)
        links = [e.attr('href') for e in pq('.container a').items()]
        self.assertIn(reverse('create_game'), links)
Beispiel #26
0
class AttackViewTestCase(TestCase):
    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')
        self.user3 = User.objects.create_user('user3', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player3 = Player(user=self.user3)
        self.player1.save()
        self.player2.save()
        self.player3.save()

        self.team1 = Team(player=self.player1, game=self.game)
        self.team2 = Team(player=self.player2, game=self.game)
        self.team1.save()
        self.team2.save()

    def test_post_logged_out(self):
        url = reverse('attack', args=[self.game.id])
        resp = self.client.post(url, {
            'target_x': 0,
            'target_y': 0,
            'target_team': self.team2.id,
        })

        self.assertEqual(resp.status_code, 302)
        self.assertRedirects(resp, reverse('login'))

    def test_post_non_existent_game(self):
        self.client.login(username=self.user3.username, password='******')

        url = reverse('attack', args=[2])
        resp = self.client.post(url, {
            'target_x': 0,
            'target_y': 0,
            'target_team': self.team2.id,
        })

        self.assertEqual(resp.status_code, 404)

    def test_post_logged_in_not_playing(self):
        self.client.login(username=self.user3.username, password='******')

        url = reverse('attack', args=[self.game.id])
        resp = self.client.post(url, {
            'target_x': 0,
            'target_y': 0,
            'target_team': self.team2.id,
        })

        self.assertEqual(resp.status_code, 404)

    def test_post_logged_in_playing_not_turn(self):
        self.client.login(username=self.user2.username, password='******')

        url = reverse('attack', args=[self.game.id])
        resp = self.client.post(url, {
            'target_x': 0,
            'target_y': 0,
            'target_team': self.team2.id,
        },
                                follow=True)

        game_url = 'http://testserver{}'.format(
            reverse('game', args=[self.game.id]))
        self.assertIn((game_url, 302), resp.redirect_chain)

        pq = PyQuery(resp.content)

        # Assert error is shown
        self.assertEqual(len(pq('.alert-danger')), 1)
        self.assertIn('It\'s not your turn', pq('.alert-danger').text())

    def test_post_logged_in_playing_miss(self):
        self.client.login(username=self.user1.username, password='******')

        url = reverse('attack', args=[self.game.id])
        resp = self.client.post(url, {
            'target_x': 0,
            'target_y': 0,
            'target_team': self.team2.id,
        },
                                follow=True)

        game_url = 'http://testserver{}'.format(
            reverse('game', args=[self.game.id]))
        self.assertIn((game_url, 302), resp.redirect_chain)

        pq = PyQuery(resp.content)

        # Assert error is shown
        self.assertEqual(len(pq('.alert-warning')), 1)
        self.assertIn('Miss', pq('.alert-warning').text())

    def test_post_logged_in_playing_duplicate(self):
        self.client.login(username=self.user1.username, password='******')

        shot = Shot(game=self.game,
                    attacking_team=self.team1,
                    defending_team=self.team2,
                    x=0,
                    y=0)
        shot.save()

        url = reverse('attack', args=[self.game.id])
        resp = self.client.post(url, {
            'target_x': 0,
            'target_y': 0,
            'target_team': self.team2.id,
        },
                                follow=True)

        game_url = 'http://testserver{}'.format(
            reverse('game', args=[self.game.id]))
        self.assertIn((game_url, 302), resp.redirect_chain)

        pq = PyQuery(resp.content)

        # Assert error is shown
        self.assertEqual(len(pq('.alert-danger')), 1)
        self.assertIn('You\'ve already shot there', pq('.alert-danger').text())

    def test_post_logged_in_playing_hit(self):
        self.client.login(username=self.user1.username, password='******')

        ship = Ship(team=self.team2,
                    x=1,
                    y=1,
                    length=2,
                    direction=Ship.CARDINAL_DIRECTIONS['SOUTH'])
        ship.save()

        url = reverse('attack', args=[self.game.id])
        resp = self.client.post(url, {
            'target_x': 1,
            'target_y': 1,
            'target_team': self.team2.id,
        },
                                follow=True)

        game_url = 'http://testserver{}'.format(
            reverse('game', args=[self.game.id]))
        self.assertIn((game_url, 302), resp.redirect_chain)

        pq = PyQuery(resp.content)

        # Assert error is shown
        self.assertEqual(len(pq('.alert-success')), 1)
        self.assertIn('Hit', pq('.alert-success').text())

    def test_post_logged_in_playing_defeat(self):
        self.client.login(username=self.user1.username, password='******')

        ship = Ship(team=self.team2,
                    x=1,
                    y=1,
                    length=1,
                    direction=Ship.CARDINAL_DIRECTIONS['SOUTH'])
        ship.save()

        url = reverse('attack', args=[self.game.id])
        resp = self.client.post(url, {
            'target_x': 1,
            'target_y': 1,
            'target_team': self.team2.id,
        },
                                follow=True)

        game_url = 'http://testserver{}'.format(
            reverse('game', args=[self.game.id]))
        self.assertIn((game_url, 302), resp.redirect_chain)

        pq = PyQuery(resp.content)

        # Assert error is shown
        self.assertEqual(len(pq('.alert-success')), 2)
        self.assertIn('Hit', pq('.alert-success').text())
        self.assertIn('You defeated user2', pq('.alert-success').text())
Beispiel #27
0
class GameViewTestCase(TestCase):
    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')
        self.user3 = User.objects.create_user('user3', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player3 = Player(user=self.user3)
        self.player1.save()
        self.player2.save()
        self.player3.save()

        self.team1 = Team(player=self.player1, game=self.game)
        self.team2 = Team(player=self.player2, game=self.game)
        self.team1.save()
        self.team2.save()

    def test_non_existant_game(self):
        url = reverse('game', args=[2])
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 404)

    def test_logged_out(self):
        url = reverse('game', args=[self.game.id])
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 404)

    def test_logged_in_playing_current_turn(self):
        self.client.login(username=self.user1.username, password='******')

        url = reverse('game', args=[self.game.id])
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 200)
        pq = PyQuery(resp.content)

        # Assert we have one board for each player
        self.assertEqual(len(pq('.board')), 2)
        self.assertIn('user1\'s board', pq('.board h3').text())
        self.assertIn('user2\'s board', pq('.board h3').text())
        # Assert the player is given an attack form
        self.assertEqual(len(pq('#attack-form')), 1)

    def test_logged_in_playing_not_current_turn(self):
        self.client.login(username=self.user2.username, password='******')

        url = reverse('game', args=[self.game.id])
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 200)
        pq = PyQuery(resp.content)

        # Assert we have one board for each player
        self.assertEqual(len(pq('.board')), 2)
        self.assertIn('user1\'s board', pq('.board h3').text())
        self.assertIn('user2\'s board', pq('.board h3').text())
        # Assert the player is not given an attack form
        self.assertEqual(len(pq('#attack-form')), 0)

    def test_logged_in_not_playing(self):
        self.client.login(username=self.user3.username, password='******')

        url = reverse('game', args=[self.game.id])
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 404)
class HomeViewTestCase(TestCase):

    def setUp(self):
        self.game1 = Game()
        self.game2 = Game()
        self.game1.save()
        self.game2.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player1.save()
        self.player2.save()

        self.team_game1 = Team(
            player=self.player1,
            game=self.game1
        )
        self.team_game2 = Team(
            player=self.player1,
            game=self.game2,
            alive=False
        )
        self.team_game1.save()
        self.team_game2.save()

    def test_logged_out(self):
        url = reverse('home')
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 200)
        pq = PyQuery(resp.content)

        self.assertIn('You\'re not logged in', pq('h3').text())
        links = [e.attr('href') for e in pq('.container a').items()]
        self.assertIn(reverse('login'), links)
        self.assertIn(reverse('signup'), links)

    def test_logged_in_with_matches(self):
        self.client.login(
            username=self.user1.username,
            password='******'
        )

        url = reverse('home')
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 200)
        pq = PyQuery(resp.content)

        # Assert links to 'Create New Game' plus ongoing games are shown
        self.assertIn('Your Active Games', pq('h3').text())
        links = [e.attr('href') for e in pq('.container a').items()]
        self.assertIn(reverse('game', args=[self.game1.id]), links)
        self.assertNotIn(reverse('game', args=[self.game2.id]), links)
        self.assertIn(reverse('create_game'), links)

    def test_logged_in_without_matches(self):
        self.client.login(
            username=self.user2.username,
            password='******'
        )

        url = reverse('home')
        resp = self.client.get(url)

        self.assertEqual(resp.status_code, 200)
        pq = PyQuery(resp.content)

        # Assert only link to 'Create New Game' is shown
        self.assertEqual(len(pq('.container a')), 1)
        links = [e.attr('href') for e in pq('.container a').items()]
        self.assertIn(reverse('create_game'), links)
Beispiel #29
0
class TilePresenterTestCase(TestCase):

    def setUp(self):
        self.game = Game()
        self.game.save()

        self.user1 = User.objects.create_user('user1', '', 'password')
        self.user2 = User.objects.create_user('user2', '', 'password')

        self.player1 = Player(user=self.user1)
        self.player2 = Player(user=self.user2)
        self.player1.save()
        self.player2.save()

        self.team1 = Team(player=self.player1, game=self.game)
        self.team2 = Team(player=self.player2, game=self.game)
        self.team1.save()
        self.team2.save()

        self.ship = Ship(
            team=self.team2,
            x=3,
            y=3,
            length=3,
            direction=Ship.CARDINAL_DIRECTIONS['SOUTH']
        )
        self.ship.save()

        self.shot_miss = Shot(
            game=self.game,
            attacking_team=self.team1,
            defending_team=self.team2,
            x=2,
            y=3
        )
        self.shot_miss.save()

        self.shot_hit = Shot(
            game=self.game,
            attacking_team=self.team1,
            defending_team=self.team2,
            x=3,
            y=5
        )
        self.shot_hit.save()

    def test_from_team(self):
        presenter = TilePresenter.from_team(
            x=0,
            y=1,
            team=self.team2,
            game=self.game
        )

        self.assertEqual(presenter.x, 0)
        self.assertEqual(presenter.y, 1)
        self.assertEqual(presenter.name, 'A1')
        self.assertTrue(presenter.is_empty)
        self.assertFalse(presenter.is_hit)

    def test_from_team_with_miss(self):
        presenter = TilePresenter.from_team(
            x=2,
            y=3,
            team=self.team2,
            game=self.game
        )

        self.assertEqual(presenter.x, 2)
        self.assertEqual(presenter.y, 3)
        self.assertEqual(presenter.name, 'C3')
        self.assertTrue(presenter.is_empty)
        self.assertTrue(presenter.is_hit)

    def test_from_team_with_ship(self):
        presenter = TilePresenter.from_team(
            x=3,
            y=4,
            team=self.team2,
            game=self.game
        )

        self.assertEqual(presenter.x, 3)
        self.assertEqual(presenter.y, 4)
        self.assertEqual(presenter.name, 'D4')
        self.assertFalse(presenter.is_empty)
        self.assertFalse(presenter.is_hit)

    def test_from_team_with_hit_ship(self):
        presenter = TilePresenter.from_team(
            x=3,
            y=5,
            team=self.team2,
            game=self.game
        )

        self.assertEqual(presenter.x, 3)
        self.assertEqual(presenter.y, 5)
        self.assertEqual(presenter.name, 'D5')
        self.assertFalse(presenter.is_empty)
        self.assertTrue(presenter.is_hit)