Пример #1
0
    def test_creating_new_user_with_existing_name_failsr(
            self, main_menu_factory, database_connection):
        old_apina = UserDao(database_connection).get_by_name("apina")
        start = time.time()
        events = [
            MockEvent(pygame.key.key_code("return"), "?"),
            MockEvent(pygame.key.key_code("a"), "a"),
            MockEvent(pygame.key.key_code("p"), "p"),
            MockEvent(pygame.key.key_code("i"), "i"),
            MockEvent(pygame.key.key_code("n"), "n"),
            MockEvent(pygame.key.key_code("a"), "a"),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("return"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?")
        ]
        event_times = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 7]

        # should only quit at 7 seconds (second escape) if the
        # creation was unsuccessful

        assert (time.time() - start - 7) < 0.5

        event_handler = EventHandlerMock(events=events,
                                         event_times=event_times)
        main_menu = main_menu_factory(event_handler)
        main_menu.run()
        new_apina = UserDao(database_connection).get_by_name("apina")
        assert old_apina.id == new_apina.id
Пример #2
0
    def test_game_organizer_quits_within_1s_of_esc(self, game_factory_factory,
                                                   game_organizer_factory):

        start_time = time.time()
        events = [MockEvent(pygame.key.key_code("escape"), "")]
        event_times = [2]
        event_handler = EventHandlerMock(events=events,
                                         event_times=event_times)
        game_factory = game_factory_factory(1, event_handler)
        game = game_factory.game()
        game_organizer_factory(event_handler).organize(game)
Пример #3
0
 def test_creating_erasing_new_user_text(self, main_menu_factory,
                                         database_connection):
     events = [
         MockEvent(pygame.key.key_code("return"), ""),
         MockEvent(pygame.key.key_code("a"), "a"),
         MockEvent(pygame.key.key_code("b"), "b"),
         MockEvent(pygame.key.key_code("c"), "c"),
         MockEvent(pygame.key.key_code("backspace"), "?"),
         MockEvent(pygame.key.key_code("a"), "a"),
         MockEvent(pygame.key.key_code("down"), "?"),
         MockEvent(pygame.key.key_code("return"), "?"),
         MockEvent(pygame.key.key_code("escape"), "?"),
         MockEvent(pygame.key.key_code("escape"), "?")
     ]
     event_times = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
     event_handler = EventHandlerMock(events=events,
                                      event_times=event_times)
     main_menu = main_menu_factory(event_handler)
     main_menu.run()
     user_dao = UserDao(database_connection)
     assert user_dao.get_by_name("aba") is not None
Пример #4
0
 def test_user_name_field_ignores_whitespaces(self, main_menu_factory,
                                              database_connection):
     events = [
         MockEvent(pygame.key.key_code("return"), "?"),
         MockEvent(pygame.key.key_code("space"), " "),
         MockEvent(pygame.key.key_code("b"), "b"),
         MockEvent(pygame.key.key_code("c"), "c"),
         MockEvent(pygame.key.key_code("down"), "?"),
         MockEvent(pygame.key.key_code("return"), "?"),
         MockEvent(pygame.key.key_code("escape"), "?"),
         MockEvent(pygame.key.key_code("escape"), "?")
     ]
     event_times = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4]
     event_handler = EventHandlerMock(events=events,
                                      event_times=event_times)
     main_menu = main_menu_factory(event_handler)
     main_menu.run()
     user_dao = UserDao(database_connection)
     assert user_dao.get_by_name("bc") is not None
Пример #5
0
    def test_game_organizer_saves_results_to_database(self,
                                                      game_factory_factory,
                                                      game_organizer_factory,
                                                      stats_dao):

        # setup player 1 shooting player 2
        keys = []
        times = []

        keys.append(
            PressedKeys(
                [pygame.key.key_code("q"),
                 pygame.key.key_code("right shift")]))
        times.append(0)
        keys.append(PressedKeys([]))
        times.append(0.9)

        # for quitting the results view
        events = [MockEvent(pygame.key.key_code("escape"), "")]
        event_times = [2]
        event_handler = EventHandlerMock(keys,
                                         times,
                                         events=events,
                                         event_times=event_times)
        game_factory = game_factory_factory(1, event_handler)
        game_factory.user_selectors[1].next()
        game = game_factory.game()
        game_organizer_factory(event_handler).organize(game)

        top_scorers = stats_dao.get_top_scorers(2)
        summary_table = top_scorers.get_summary_table()
        assert list(summary_table["name"].values) == ["apina", "banaani"]
        assert list(summary_table["total score"].values) == [100, -20]
        assert list(summary_table["rounds"].values) == [1, 1]
        assert list(summary_table["kills"].values) == [1, 0]
        assert list(summary_table["deaths"].values) == [0, 1]
Пример #6
0
    def test_added_users_replace_default_players_when_database_starts_empty(
            self, main_menu_factory_empty_database, database_connection):
        events = [
            MockEvent(pygame.key.key_code("return"), "?"),
            MockEvent(pygame.key.key_code("a"), "a"),
            MockEvent(pygame.key.key_code("b"), "b"),
            MockEvent(pygame.key.key_code("c"), "c"),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("return"), "?"),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("return"), ""),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("return"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?")
        ]
        event_times = list(numpy.linspace(0, 10, len(events)))
        event_handler = EventHandlerMock(events=events,
                                         event_times=event_times)
        main_menu = main_menu_factory_empty_database(event_handler)
        main_menu.run()
        stats_dao = StatsDao(database_connection)

        top_scorers = stats_dao.get_top_scorers(2)
        names = top_scorers.get_summary_table()["name"].values
        assert len(names) == 1
        assert "abc" in names
Пример #7
0
    def test_game_works_with_empty_database_but_doesnt_save_results(
            self, main_menu_factory_empty_database, database_connection):
        events = [
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("return"), ""),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("return"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?")
        ]
        event_times = list(numpy.linspace(0, 10, len(events)))
        event_handler = EventHandlerMock(events=events,
                                         event_times=event_times)
        main_menu = main_menu_factory_empty_database(event_handler)
        main_menu.run()
        stats_dao = StatsDao(database_connection)

        top_scorers = stats_dao.get_top_scorers(2)
        names = top_scorers.get_summary_table()["name"].values
        assert len(names) == 0
Пример #8
0
    def test_running_game_saves_statistics_for_correct_users(
            self, main_menu_factory, database_connection):
        events = [
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("return"), ""),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("right"), "?"),  # select 3 players
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("right"), "?"),  # banaani
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("right"), "?"),  # banaani
            MockEvent(pygame.key.key_code("right"), "?"),  # cembalo
            MockEvent(pygame.key.key_code("down"), "?"),
            MockEvent(pygame.key.key_code("return"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?"),
            MockEvent(pygame.key.key_code("escape"), "?")
        ]
        event_times = list(numpy.linspace(0, 12, len(events)))
        event_handler = EventHandlerMock(events=events,
                                         event_times=event_times)
        main_menu = main_menu_factory(event_handler)
        main_menu.run()
        stats_dao = StatsDao(database_connection)

        top_scorers = stats_dao.get_top_scorers(3)
        names = top_scorers.get_summary_table()["name"].values
        assert len(names) == 3
        assert "apina" in names
        assert "banaani" in names
        assert "cembalo" in names