コード例 #1
0
    async def save_profile_item(self, profile_item: ProfileItem, filename: str,
                                file_contents: bytes):
        """
        Saves given profile item with given image

        :param profile_item: Profile item to be persisted
        :type profile_item: ProfileItem
        :param filename: Image's file name
        :type filename: str
        :param file_contents: Image's contents
        :type file_contents: bytes
        :return: Created profile item's id
        :rtype: str
        """
        default_path = os.path.join(os.getcwd(), 'bot', 'images',
                                    'profile_items')
        directory_path = os.path.join(
            os.environ.get("PROFILE_ITEM_IMAGES_PATH", default_path),
            f'{str(profile_item.type)}s')
        os.makedirs(directory_path, exist_ok=True)

        profile_item.file_path = os.path.join(directory_path, filename)
        with open(profile_item.file_path, 'wb') as f:
            f.write(file_contents)

        return await ProfileItem.save(profile_item)
コード例 #2
0
    def build_profile_item(self, **args) -> ProfileItem:
        """
        Builds a profile item with given parameters

        :return: Profile item
        :rtype: ProfileItem
        """
        return ProfileItem(**args)
コード例 #3
0
    def test_save_profile_item(self):
        filename = 'wallpaper.png'
        with open(os.path.join('tests', 'support', filename), 'rb') as f:
            file_contents = f.read()

        profile_item = ProfileItem()
        profile_item.type = 'badge'
        profile_item.name = 'Some item'
        profile_item.price = 100

        expected_file_path = os.path.join('bot', 'images', 'profile_items',
                                          'badges', 'wallpaper.png')

        result = asyncio.run(Item().save_profile_item(profile_item, filename,
                                                      file_contents))

        self.assertIsNotNone(result)
        self.assertEqual(
            Session().query(ProfileItem).filter_by(id=result).count(), 1)
        self.assertTrue(os.path.exists(expected_file_path))
        with open(expected_file_path, 'rb') as f:
            self.assertEqual(f.read(), file_contents)
コード例 #4
0
 def test_validate_price_invalid_price(self):
     profile_item = ProfileItem()
     with self.assertRaises(ProfileItemException):
         profile_item.price = 'invalid'
     with self.assertRaises(ProfileItemException):
         profile_item.price = -100
コード例 #5
0
 def test_validate_price_valid_price(self):
     profile_item = ProfileItem()
     profile_item.price = 100
コード例 #6
0
 def test_validate_type_invalid_type(self):
     profile_item = ProfileItem()
     with self.assertRaises(ProfileItemException):
         profile_item.type = 'invalid'
コード例 #7
0
 def test_validate_type_valid_type(self):
     profile_item = ProfileItem()
     profile_item.type = 'badge'