Beispiel #1
0
    def test_get_number_of_victories_no_xp_point(self):
        test_session = Session()
        user = User(id=14, name='Me')
        test_session.add(user)
        test_session.commit()

        result = asyncio.run(XpPoint.get_user_aggregated_points(user.id))

        self.assertIsNone(result)
    def test_get_number_of_victories_no_games(self):
        test_session = Session()
        user_1 = User(id=14, name='Me')
        user_2 = User(id=15, name='Them')
        test_session.add(user_1)
        test_session.commit()

        result = asyncio.run(ChessGame.get_number_of_victories(user_1.id))

        self.assertEqual(result, 0)
Beispiel #3
0
    def test_get_number_of_victories_many_servers(self):
        test_session = Session()
        user = User(id=14, name='Me')
        xp_point_1 = XpPoint()
        xp_point_1.user = user
        xp_point_1.server_id = 10
        xp_point_1.points = 140
        test_session.add(xp_point_1)
        xp_point_2 = XpPoint()
        xp_point_2.user = user
        xp_point_2.server_id = 12
        xp_point_2.points = 260
        test_session.add(xp_point_2)
        test_session.commit()

        result = asyncio.run(XpPoint.get_user_aggregated_points(user.id))

        self.assertEqual(result, 400)
    def test_get_number_of_victories_many_games(self):
        test_session = Session()
        user_1 = User(id=14, name='Me')
        user_2 = User(id=15, name='Them')
        chess_game_1 = ChessGame()
        chess_game_1.player1 = user_1
        chess_game_1.player2 = user_2
        chess_game_1.result = 1
        test_session.add(chess_game_1)
        chess_game_2 = ChessGame()
        chess_game_2.player1 = user_2
        chess_game_2.player2 = user_1
        chess_game_2.result = 1
        test_session.add(chess_game_2)
        chess_game_3 = ChessGame()
        chess_game_3.player1 = user_2
        chess_game_3.player2 = user_1
        chess_game_3.result = -1
        test_session.add(chess_game_3)
        test_session.commit()

        result = asyncio.run(ChessGame.get_number_of_victories(user_1.id))

        self.assertEqual(result, 2)
class TestPlanets(TestCase):
    def setUp(self):
        self.db_session = Session()

    def tearDown(self):
        clear_data(self.db_session)
        self.db_session.close()

    def test_list_of_planets_region_exists(self):
        for i in range(6):
            PlanetFactory(name='Planeta esperado' if i %
                          2 else 'Planeta não esperado',
                          region='Orla Exterior' if i % 2 else 'Núcleo')
        self.db_session.commit()

        result = asyncio.run(Planets().list_of_planets(region='Orla Exterior'))

        self.assertEqual(len(result), 3)
        self.assertEqual(result[0].name, 'Planeta esperado')
        self.assertEqual(result[1].name, 'Planeta esperado')
        self.assertEqual(result[2].name, 'Planeta esperado')

    def test_list_of_planets_region_does_not_exist(self):
        for i in range(6):
            PlanetFactory(region='Orla Exterior' if i % 2 else 'Núcleo')
        self.db_session.commit()

        result = asyncio.run(Planets().list_of_planets(region='Orla Média'))

        self.assertEqual(result, [])

    def test_list_of_planets_no_filters(self):
        for i in range(6):
            PlanetFactory()
        self.db_session.commit()

        result = asyncio.run(Planets().list_of_planets())

        self.assertEqual(len(result), 6)
Beispiel #6
0
class TestPalplatina(TestCase):
    @classmethod
    def setUpClass(cls):
        load_dotenv()

    def setUp(self):
        self.db_session = Session()

    def tearDown(self):
        clear_data(self.db_session)
        self.db_session.close()
        Session.remove()

    def test_give_daily_clear_for_new_daily(self):
        user = UserFactory(currency=150,
                           daily_last_collected_at=datetime.utcnow() -
                           timedelta(days=1))
        self.db_session.commit()

        result, user_actual = asyncio.run(Palplatina().give_daily(
            user.id, user.name))
        Session.remove()

        self.assertTrue(result)
        self.assertEqual(user_actual.currency, 450)
        self.assertEqual(
            self.db_session.query(User).get(user.id).currency, 450)

    def test_give_daily_new_user(self):
        result, user = asyncio.run(Palplatina().give_daily(14, "name"))

        self.assertTrue(result)
        self.assertEqual(user.currency, 300)
        self.assertEqual(user.name, "name")
        self.assertEqual(Session().query(User).get(14).currency, 300)

    def test_give_daily_not_clear_for_new_daily(self):
        user = UserFactory(currency=150,
                           daily_last_collected_at=datetime.utcnow())
        self.db_session.commit()

        result, user_actual = asyncio.run(Palplatina().give_daily(
            user.id, user.name))
        Session.remove()

        self.assertFalse(result)
        self.assertEqual(user_actual.currency, 150)
        self.assertEqual(
            self.db_session.query(User).get(user.id).currency, 150)

    def test_get_currency_user_exists(self):
        user = UserFactory(currency=150)
        self.db_session.commit()

        result = asyncio.run(Palplatina().get_currency(user.id))

        self.assertEqual(result, 150)

    def test_get_currency_user_doesnt_exist(self):
        result = asyncio.run(Palplatina().get_currency(14))

        self.assertEqual(result, 0)

    def test_buy_item_success(self):
        user = UserFactory(currency=150)
        profile_item = ProfileItemFactory(price=100)
        self.db_session.commit()

        result = asyncio.run(Palplatina().buy_item(user.id, profile_item.name))
        Session.remove()

        self.assertIsInstance(result, User)
        self.assertEqual(result.currency, 50)
        self.assertEqual(result.profile_items[0].profile_item.id,
                         profile_item.id)

        persisted_user = self.db_session.query(User).get(user.id)
        persisted_user_profile_items = persisted_user.profile_items
        self.assertEqual(persisted_user.currency, 50)
        self.assertEqual(len(persisted_user_profile_items), 1)
        self.assertEqual(persisted_user_profile_items[0].profile_item.id,
                         profile_item.id)

    def test_buy_item_not_enough_currency(self):
        user = UserFactory(currency=150)
        profile_item = ProfileItemFactory(price=200)
        self.db_session.commit()

        with self.assertRaises(NotEnoughCredits):
            asyncio.run(Palplatina().buy_item(user.id, profile_item.name))

        Session.remove()
        persisted_user = self.db_session.query(User).get(user.id)
        persisted_user_profile_items = persisted_user.profile_items
        self.assertEqual(persisted_user.currency, 150)
        self.assertEqual(len(persisted_user_profile_items), 0)

    def test_buy_item_item_already_bought(self):
        profile_item = ProfileItemFactory()
        user = UserFactory(currency=150)
        user.profile_items = [
            UserProfileItemFactory(profile_item=profile_item)
        ]
        self.db_session.commit()

        with self.assertRaises(AlreadyOwnsItem):
            asyncio.run(Palplatina().buy_item(user.id, profile_item.name))

        persisted_user = self.db_session.query(User).get(user.id)
        persisted_user_profile_items = persisted_user.profile_items
        self.assertEqual(persisted_user.currency, 150)
        self.assertEqual(len(persisted_user_profile_items), 1)
        self.assertEqual(persisted_user_profile_items[0].profile_item.id,
                         profile_item.id)

    def test_buy_item_item_not_found(self):
        user = UserFactory(currency=150)
        self.db_session.commit()

        with self.assertRaises(ItemNotFound):
            asyncio.run(Palplatina().buy_item(user.id, 'random'))

        Session.remove()
        persisted_user = self.db_session.query(User).get(user.id)
        persisted_user_profile_items = persisted_user.profile_items
        self.assertEqual(persisted_user.currency, 150)
        self.assertEqual(len(persisted_user_profile_items), 0)

    def test_buy_item_user_not_found(self):
        with self.assertRaises(ItemNotFound):
            asyncio.run(Palplatina().buy_item(14, 'random'))

    def test_get_available_items(self):
        ProfileItemFactory.reset_sequence()
        for i in range(12):
            ProfileItemFactory()
        for i in range(12):
            ProfileItemFactory(name=f'No match {i}')
        self.db_session.commit()

        result = asyncio.run(Palplatina().get_available_items('Profile item',
                                                              page=0))

        self.assertEqual(len(result), 2)
        self.assertEqual(len(result[0]), 9)
        self.assertEqual(result[1], 2)
        fetched_items_names = [item.name for item in result[0]]
        self.assertIn('Profile item 0', fetched_items_names)
        self.assertIn('Profile item 8', fetched_items_names)
        self.assertNotIn('Profile item 9', fetched_items_names)
        self.assertNotIn('Profile item 11', fetched_items_names)

    def test_get_user_items_user_has_items(self):
        user = UserFactory()
        ProfileItemFactory.reset_sequence()
        for i in range(2):
            profile_item = ProfileItemFactory()
            user.profile_items.append(
                UserProfileItemFactory(profile_item=profile_item))
        another_profile_item = ProfileItemFactory()
        self.db_session.commit()

        result = asyncio.run(Palplatina().get_user_items(user.id))

        self.assertEqual(len(result), 2)
        fetched_items_names = [item.profile_item.name for item in result]
        self.assertIn('Profile item 0', fetched_items_names)
        self.assertIn('Profile item 1', fetched_items_names)
        self.assertNotIn(another_profile_item.name, fetched_items_names)

    def test_get_user_items_user_has_no_items(self):
        user = UserFactory()
        self.db_session.commit()

        result = asyncio.run(Palplatina().get_user_items(user.id))

        self.assertEqual(len(result), 0)

    def test_get_user_items_user_not_found(self):
        result = asyncio.run(Palplatina().get_user_items(14))

        self.assertEqual(len(result), 0)

    def test_get_item_item_exists(self):
        profile_item = ProfileItemFactory()
        self.db_session.commit()

        result = asyncio.run(Palplatina().get_item(profile_item.name))

        self.assertEqual(result.id, profile_item.id)

    def test_get_item_item_does_not_exist(self):
        result = asyncio.run(Palplatina().get_item("item"))

        self.assertIsNone(result)

    def test_equip_item_user_has_item(self):
        user = UserFactory()
        profile_item = ProfileItemFactory()
        user.profile_items = [
            UserProfileItemFactory(equipped=False, profile_item=profile_item)
        ]
        self.db_session.commit()

        result = asyncio.run(Palplatina().equip_item(user.id,
                                                     profile_item.name))

        self.assertTrue(result.equipped)

        Session.remove()
        fetched_user_profile_item = self.db_session.query(UserProfileItem).get(
            (user.id, profile_item.id))
        self.assertTrue(fetched_user_profile_item.equipped)

    def test_equip_item_user_does_not_have_item(self):
        user = UserFactory()
        self.db_session.commit()

        with self.assertRaises(ItemNotFound):
            asyncio.run(Palplatina().equip_item(user.id, 'some item'))

    def test_equip_item_user_does_not_exist(self):
        with self.assertRaises(ItemNotFound):
            asyncio.run(Palplatina().equip_item(140, 'some item'))

    def test_unequip_item_user_has_item(self):
        user = UserFactory()
        profile_item = ProfileItemFactory()
        user.profile_items = [
            UserProfileItemFactory(equipped=True, profile_item=profile_item)
        ]
        self.db_session.commit()

        result = asyncio.run(Palplatina().unequip_item(user.id,
                                                       profile_item.name))

        self.assertFalse(result.equipped)

        Session.remove()
        fetched_user_profile_item = self.db_session.query(UserProfileItem).get(
            (user.id, profile_item.id))
        self.assertFalse(fetched_user_profile_item.equipped)

    def test_unequip_item_user_does_not_have_item(self):
        user = UserFactory()
        self.db_session.commit()

        with self.assertRaises(ItemNotFound):
            asyncio.run(Palplatina().unequip_item(user.id, 'some item'))

    def test_unequip_item_user_does_not_exist(self):
        with self.assertRaises(ItemNotFound):
            asyncio.run(Palplatina().unequip_item(140, 'some item'))
class TestProfile(TestCase):
    @classmethod
    def setUpClass(cls):
        load_dotenv()

    def setUp(self):
        self.test_session = Session()

    def tearDown(self):
        clear_data(self.test_session)
        self.test_session.close()

    def test_get_user_profile_all_available_fields(self):
        user_1 = UserFactory(name='My very long name')
        user_2 = UserFactory()
        chess_game_1 = ChessGameFactory(player1=user_1, result=1)
        xp_point_1 = XpPointFactory(user=user_1, points=140)
        self.test_session.commit()
        astrology_bot = AstrologyChart()
        datetime = ('1997/08/10', '07:17', '-03:00')
        geopos = (-23.5506507, -46.6333824)
        chart = astrology_bot.calc_chart_raw(datetime, geopos)
        asyncio.run(astrology_bot.save_chart(user_1.id, chart))
        with open(os.path.join('tests', 'support', 'user_avatar.png'),
                  'rb') as f:
            user_avatar_bytes = f.read()

        result = asyncio.run(Profile().get_user_profile(user_1.id,
                                                        user_avatar_bytes,
                                                        lang='pt'))

        with open(
                os.path.join('tests', 'support',
                             'get_user_profile_all_fields.png'), 'rb') as f:
            self.assertEqual(result.getvalue(), f.read())

    def test_get_user_profile_user_has_badges(self):
        user_1 = UserFactory(name='Name')
        profile_badge_1 = ProfileItemFactory(
            file_path=os.path.join("tests", "support", "badge1.png"))
        profile_badge_2 = ProfileItemFactory(
            file_path=os.path.join("tests", "support", "badge2.png"))
        profile_badge_3 = ProfileItemFactory(
            file_path=os.path.join("tests", "support", "badge3.png"))
        profile_badge_4 = ProfileItemFactory(
            file_path=os.path.join("tests", "support", "badge1.png"))
        user_1.profile_items = [
            UserProfileItemFactory(profile_item=profile_badge_1),
            UserProfileItemFactory(profile_item=profile_badge_2),
            UserProfileItemFactory(profile_item=profile_badge_3),
            UserProfileItemFactory(profile_item=profile_badge_4,
                                   equipped=False),
        ]
        self.test_session.commit()
        with open(os.path.join('tests', 'support', 'user_avatar.png'),
                  'rb') as f:
            user_avatar_bytes = f.read()

        result = asyncio.run(Profile().get_user_profile(user_1.id,
                                                        user_avatar_bytes,
                                                        lang='pt'))

        with open(
                os.path.join('tests', 'support',
                             'get_user_profile_with_badges.png'), 'rb') as f:
            self.assertEqual(result.getvalue(), f.read())

    def test_get_user_profile_user_has_wallpaper(self):
        user_1 = UserFactory(name='Name')
        profile_badge = ProfileItemFactory(type='wallpaper',
                                           file_path=os.path.join(
                                               "tests", "support",
                                               "wallpaper.png"))
        user_1.profile_items = [
            UserProfileItemFactory(profile_item=profile_badge),
        ]
        self.test_session.commit()
        with open(os.path.join('tests', 'support', 'user_avatar.png'),
                  'rb') as f:
            user_avatar_bytes = f.read()

        result = asyncio.run(Profile().get_user_profile(user_1.id,
                                                        user_avatar_bytes,
                                                        lang='pt'))

        with open(
                os.path.join('tests', 'support',
                             'get_user_profile_with_wallpaper.png'),
                'rb') as f:
            self.assertEqual(result.getvalue(), f.read())

    def test_get_user_profile_user_has_profile_frame_color(self):
        user_1 = UserFactory(name='Name', profile_frame_color='#cccccc')
        self.test_session.commit()
        with open(os.path.join('tests', 'support', 'user_avatar.png'),
                  'rb') as f:
            user_avatar_bytes = f.read()

        result = asyncio.run(Profile().get_user_profile(user_1.id,
                                                        user_avatar_bytes,
                                                        lang='pt'))

        with open(
                os.path.join('tests', 'support',
                             'get_user_profile_with_frame_color.png'),
                'rb') as f:
            self.assertEqual(result.getvalue(), f.read())

    def test_get_user_profile_user_exists_no_info(self):
        user_1 = UserFactory(name='Name')
        self.test_session.commit()
        with open(os.path.join('tests', 'support', 'user_avatar.png'),
                  'rb') as f:
            user_avatar_bytes = f.read()

        result = asyncio.run(Profile().get_user_profile(user_1.id,
                                                        user_avatar_bytes,
                                                        lang='pt'))

        with open(
                os.path.join('tests', 'support',
                             'get_user_profile_no_info.png'), 'rb') as f:
            self.assertEqual(result.getvalue(), f.read())

    def test_get_user_profile_no_user(self):
        with open(os.path.join('tests', 'support', 'user_avatar.png'),
                  'rb') as f:
            user_avatar_bytes = f.read()

        result = asyncio.run(Profile().get_user_profile(14,
                                                        user_avatar_bytes,
                                                        lang='pt'))

        self.assertIsNone(result)

    def test_set_user_profile_frame_color_valid_color(self):
        user = UserFactory()
        self.test_session.commit()
        color = '#f7b1d8'

        result = asyncio.run(Profile().set_user_profile_frame_color(
            user.id, color))

        self.assertEqual(result.id, user.id)
        self.assertEqual(result.profile_frame_color, color)

        Session.remove()
        self.assertEqual(
            self.test_session.query(User).get(user.id).profile_frame_color,
            color)

    def test_set_user_profile_frame_color_invalid_color(self):
        user = UserFactory()
        self.test_session.commit()
        color = 'invalid color'

        with self.assertRaises(ValueError):
            asyncio.run(Profile().set_user_profile_frame_color(user.id, color))

        Session.remove()
        self.assertEqual(
            self.test_session.query(User).get(user.id).profile_frame_color,
            None)

    def test_set_user_profile_frame_color_user_does_not_exist(self):
        user_id = 140
        color = '#f7b1d8'

        result = asyncio.run(Profile().set_user_profile_frame_color(
            user_id, color))

        self.assertEqual(result.id, user_id)
        self.assertEqual(result.profile_frame_color, color)

        Session.remove()
        self.assertEqual(
            self.test_session.query(User).get(user_id).profile_frame_color,
            color)
class TestServers(TestCase):
    @classmethod
    def setUpClass(cls):
        load_dotenv()

    def setUp(self):
        self.test_session = Session()
        cache.server_configs = {}

    def tearDown(self):
        clear_data(self.test_session)
        self.test_session.close()

    def test_load_configs(self):
        server_config_1 = ServerConfigFactory(language='en')
        server_config_2 = ServerConfigFactory(language='jp')
        server_config_3 = ServerConfigFactory(language='pt')
        server_config_3.autoreply_configs = [ServerConfigAutoreplyFactory()]
        self.test_session.commit()

        self.assertEqual(cache.server_configs, {})

        result = asyncio.run(cache.load_configs())

        self.assertIsNone(result)
        self.assertIn(server_config_1.id, cache.server_configs)
        self.assertIn(server_config_2.id, cache.server_configs)
        self.assertIn(server_config_3.id, cache.server_configs)

        self.assertEqual(
            cache.server_configs.get(server_config_1.id).id,
            server_config_1.id)
        self.assertEqual(
            cache.server_configs.get(server_config_2.id).id,
            server_config_2.id)
        self.assertEqual(
            cache.server_configs.get(server_config_3.id).id,
            server_config_3.id)

        server_config_3_autoreply_config = cache.server_configs.get(
            server_config_3.id).autoreply_configs
        self.assertEqual(len(server_config_3_autoreply_config), 1)
        self.assertEqual(server_config_3_autoreply_config[0].server_config_id,
                         server_config_3.id)

    def test_get_config_server_config_exists(self):
        server_config_1 = ServerConfigFactory()
        cache.server_configs = {server_config_1.id: server_config_1}

        result = cache.get_config(server_config_1.id)

        self.assertEqual(result, server_config_1)

    def test_get_config_server_config_does_not_exist(self):
        result = cache.get_config(10)

        self.assertIsNone(result)

    def test_update_config_creates_new_server_config(self):
        server_id = 14
        server_language = 'pt'
        result = asyncio.run(
            cache.update_config(server_id, language=server_language))

        self.assertIsInstance(result, ServerConfig)
        self.assertEqual(result.id, server_id)
        self.assertEqual(result.language, server_language)
        self.assertEqual(result.autoreply_configs, [])

        fetched_server_config = self.test_session.query(ServerConfig).first()
        self.assertEqual(fetched_server_config.id, server_id)
        self.assertEqual(fetched_server_config.language, server_language)

    def test_update_config_updates_existing_server_config(self):
        server_config = ServerConfigFactory(language='en')
        self.test_session.commit()

        new_server_language = 'pt'
        result = asyncio.run(
            cache.update_config(server_config.id,
                                language=new_server_language))
        Session.remove()

        self.assertIsInstance(result, ServerConfig)
        self.assertEqual(result.id, server_config.id)
        self.assertEqual(result.language, new_server_language)

        fetched_server_config = self.test_session.query(ServerConfig).get(
            server_config.id)
        self.assertEqual(fetched_server_config.id, server_config.id)
        self.assertEqual(fetched_server_config.language, new_server_language)

    def test_get_autoreply_to_message_message_starts_with(self):
        server_config = ServerConfigFactory()
        server_config_autoreply_starts_with = ServerConfigAutoreplyFactory(
            message_regex='^hello')
        server_config_autoreply_ends_with = ServerConfigAutoreplyFactory(
            message_regex='.*bye$')
        server_config_autoreply_has = ServerConfigAutoreplyFactory(
            message_regex='.*wait.*')
        server_config.autoreply_configs = [
            server_config_autoreply_starts_with,
            server_config_autoreply_ends_with, server_config_autoreply_has
        ]
        cache.server_configs = {server_config.id: server_config}

        result = cache.get_autoreply_to_message(server_config.id,
                                                'hello there')

        self.assertIsNotNone(result)
        self.assertEqual(result.id, server_config_autoreply_starts_with.id)

    def test_get_autoreply_to_message_message_ends_with(self):
        server_config = ServerConfigFactory()
        server_config_autoreply_starts_with = ServerConfigAutoreplyFactory(
            message_regex='^hello')
        server_config_autoreply_ends_with = ServerConfigAutoreplyFactory(
            message_regex='.*bye$')
        server_config_autoreply_has = ServerConfigAutoreplyFactory(
            message_regex='.*wait.*')
        server_config.autoreply_configs = [
            server_config_autoreply_starts_with,
            server_config_autoreply_ends_with, server_config_autoreply_has
        ]
        cache.server_configs = {server_config.id: server_config}

        result = cache.get_autoreply_to_message(server_config.id, 'ok, bye')

        self.assertIsNotNone(result)
        self.assertEqual(result.id, server_config_autoreply_ends_with.id)

    def test_get_autoreply_to_message_message_has(self):
        server_config = ServerConfigFactory()
        server_config_autoreply_starts_with = ServerConfigAutoreplyFactory(
            message_regex='^hello')
        server_config_autoreply_ends_with = ServerConfigAutoreplyFactory(
            message_regex='.*bye$')
        server_config_autoreply_has = ServerConfigAutoreplyFactory(
            message_regex='.*wait.*')
        server_config.autoreply_configs = [
            server_config_autoreply_starts_with,
            server_config_autoreply_ends_with, server_config_autoreply_has
        ]
        cache.server_configs = {server_config.id: server_config}

        result = cache.get_autoreply_to_message(server_config.id,
                                                'please, wait for me')

        self.assertIsNotNone(result)
        self.assertEqual(result.id, server_config_autoreply_has.id)

    def test_get_autoreply_to_message_no_match(self):
        server_config = ServerConfigFactory()
        server_config_autoreply_starts_with = ServerConfigAutoreplyFactory(
            message_regex='^hello')
        server_config_autoreply_ends_with = ServerConfigAutoreplyFactory(
            message_regex='.*bye$')
        server_config_autoreply_has = ServerConfigAutoreplyFactory(
            message_regex='.*wait.*')
        server_config.autoreply_configs = [
            server_config_autoreply_starts_with,
            server_config_autoreply_ends_with, server_config_autoreply_has
        ]
        cache.server_configs = {server_config.id: server_config}

        result = cache.get_autoreply_to_message(server_config.id, 'nopee')

        self.assertIsNone(result)

    def test_get_reply(self):
        server_config = ServerConfigFactory()
        server_config_autoreply = ServerConfigAutoreplyFactory(
            message_regex='tão sábi(\\w) (.*)',
            reply='Já ouviu a história de Darth \\2, \\1 sábi\\1?')
        server_config.autoreply_configs = [server_config_autoreply]
        message = 'Tão sábia Lele'

        result = server_config_autoreply.get_reply(message)

        self.assertEqual(result, 'Já ouviu a história de Darth Lele, a sábia?')