From 8046fef7f79cd6da9433584e1526ee573fc5651c Mon Sep 17 00:00:00 2001 From: davfsa Date: Fri, 21 Aug 2020 17:40:21 +0200 Subject: [PATCH] New tests - Rename `format_*` arg `format` to `ext` --- hikari/applications.py | 24 +-- hikari/channels.py | 8 +- hikari/guilds.py | 72 ++++---- hikari/invites.py | 16 +- hikari/users.py | 22 +-- hikari/webhooks.py | 8 +- pytest.ini | 16 -- tests/hikari/test_applications.py | 12 +- tests/hikari/test_channels.py | 2 +- tests/hikari/test_guilds.py | 294 ++++++++++++++++++++++++++++-- tests/hikari/test_users.py | 16 +- 11 files changed, 363 insertions(+), 127 deletions(-) diff --git a/hikari/applications.py b/hikari/applications.py index b2e90ea630..07205c9569 100644 --- a/hikari/applications.py +++ b/hikari/applications.py @@ -334,13 +334,13 @@ def icon_url(self) -> typing.Optional[files.URL]: return self.format_icon() # noinspection PyShadowingBuiltins - def format_icon(self, *, format: str = "png", size: int = 4096) -> typing.Optional[files.URL]: + def format_icon(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]: """Generate the icon for this team if set. Parameters ---------- - format : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg` and `webp`. size : builtins.int The size to set for the URL, defaults to `4096`. Can be any power @@ -361,7 +361,7 @@ def format_icon(self, *, format: str = "png", size: int = 4096) -> typing.Option return None return routes.CDN_TEAM_ICON.compile_to_file( - constants.CDN_URL, team_id=self.id, hash=self.icon_hash, size=size, file_format=format, + constants.CDN_URL, team_id=self.id, hash=self.icon_hash, size=size, file_format=ext, ) @@ -455,13 +455,13 @@ def icon_url(self) -> typing.Optional[files.URL]: return self.format_icon() # noinspection PyShadowingBuiltins - def format_icon(self, *, format: str = "png", size: int = 4096) -> typing.Optional[files.URL]: + def format_icon(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]: """Generate the icon for this application. Parameters ---------- - format : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg` and `webp`. size : builtins.int The size to set for the URL, defaults to `4096`. @@ -482,7 +482,7 @@ def format_icon(self, *, format: str = "png", size: int = 4096) -> typing.Option return None return routes.CDN_APPLICATION_ICON.compile_to_file( - constants.CDN_URL, application_id=self.id, hash=self.icon_hash, size=size, file_format=format, + constants.CDN_URL, application_id=self.id, hash=self.icon_hash, size=size, file_format=ext, ) @property @@ -497,13 +497,13 @@ def cover_image_url(self) -> typing.Optional[files.URL]: return self.format_cover_image() # noinspection PyShadowingBuiltins - def format_cover_image(self, *, format: str = "png", size: int = 4096) -> typing.Optional[files.URL]: + def format_cover_image(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]: """Generate the cover image used in the store, if set. Parameters ---------- - format : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg` and `webp`. size : builtins.int The size to set for the URL, defaults to `4096`. @@ -524,5 +524,5 @@ def format_cover_image(self, *, format: str = "png", size: int = 4096) -> typing return None return routes.CDN_APPLICATION_COVER.compile_to_file( - constants.CDN_URL, application_id=self.id, hash=self.cover_image_hash, size=size, file_format=format, + constants.CDN_URL, application_id=self.id, hash=self.cover_image_hash, size=size, file_format=ext, ) diff --git a/hikari/channels.py b/hikari/channels.py index c658838646..e29b5a874a 100644 --- a/hikari/channels.py +++ b/hikari/channels.py @@ -456,13 +456,13 @@ def icon_url(self) -> typing.Optional[files.URL]: return self.format_icon() # noinspection PyShadowingBuiltins - def format_icon(self, *, format: str = "png", size: int = 4096) -> typing.Optional[files.URL]: + def format_icon(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]: """Generate the icon for this DM, if set. Parameters ---------- - format : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg` and `webp`. size : builtins.int The size to set for the URL, defaults to `4096`. @@ -482,7 +482,7 @@ def format_icon(self, *, format: str = "png", size: int = 4096) -> typing.Option return None return routes.CDN_CHANNEL_ICON.compile_to_file( - constants.CDN_URL, channel_id=self.id, hash=self.icon_hash, size=size, file_format=format, + constants.CDN_URL, channel_id=self.id, hash=self.icon_hash, size=size, file_format=ext, ) diff --git a/hikari/guilds.py b/hikari/guilds.py index d51c557c5f..711c4493f3 100644 --- a/hikari/guilds.py +++ b/hikari/guilds.py @@ -365,12 +365,12 @@ def flags(self) -> users.UserFlag: return self.user.flags @property - def avatar(self) -> files.URL: - return self.user.avatar + def avatar_url(self) -> files.URL: + return self.user.avatar_url # noinspection PyShadowingBuiltins - def format_avatar(self, *, format: typing.Optional[str] = None, size: int = 4096) -> typing.Optional[files.URL]: - return self.user.format_avatar(format=format, size=size) + def format_avatar(self, *, ext: typing.Optional[str] = None, size: int = 4096) -> typing.Optional[files.URL]: + return self.user.format_avatar(ext=ext, size=size) @property def default_avatar(self) -> files.URL: @@ -645,18 +645,18 @@ def icon_url(self) -> typing.Optional[files.URL]: """Icon for the guild, if set; otherwise `builtins.None`.""" return self.format_icon() - def format_icon(self, *, format_: typing.Optional[str] = None, size: int = 4096) -> typing.Optional[files.URL]: + def format_icon(self, *, ext: typing.Optional[str] = None, size: int = 4096) -> typing.Optional[files.URL]: """Generate the guild's icon, if set. Parameters ---------- - format_ : typing.Optional[builtins.str] - The format to use for this URL, defaults to `png` or `gif`. + ext : typing.Optional[builtins.str] + The extension to use for this URL, defaults to `png` or `gif`. Supports `png`, `jpeg`, `jpg`, `webp` and `gif` (when animated). - If `builtins.None`, then the correct default format is determined - based on whether the icon is animated or not. + If `builtins.None`, then the correct default extension is + determined based on whether the icon is animated or not. size : builtins.int The size to set for the URL, defaults to `4096`. Can be any power of two between 16 and 4096. @@ -674,14 +674,14 @@ def format_icon(self, *, format_: typing.Optional[str] = None, size: int = 4096) if self.icon_hash is None: return None - if format_ is None: + if ext is None: if self.icon_hash.startswith("a_"): - format_ = "gif" + ext = "gif" else: - format_ = "png" + ext = "png" return routes.CDN_GUILD_ICON.compile_to_file( - constants.CDN_URL, guild_id=self.id, hash=self.icon_hash, size=size, file_format=format_, + constants.CDN_URL, guild_id=self.id, hash=self.icon_hash, size=size, file_format=ext, ) @@ -712,13 +712,13 @@ def splash_url(self) -> typing.Optional[files.URL]: """Splash for the guild, if set.""" return self.format_splash() - def format_splash(self, *, format_: str = "png", size: int = 4096) -> typing.Optional[files.URL]: + def format_splash(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]: """Generate the guild's splash image, if set. Parameters ---------- - format_ : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg` and `webp`. size : builtins.int The size to set for the URL, defaults to `4096`. @@ -738,21 +738,21 @@ def format_splash(self, *, format_: str = "png", size: int = 4096) -> typing.Opt return None return routes.CDN_GUILD_SPLASH.compile_to_file( - constants.CDN_URL, guild_id=self.id, hash=self.splash_hash, size=size, file_format=format_, + constants.CDN_URL, guild_id=self.id, hash=self.splash_hash, size=size, file_format=ext, ) @property - def discovery_splash(self) -> typing.Optional[files.URL]: + def discovery_splash_url(self) -> typing.Optional[files.URL]: """Discovery splash for the guild, if set.""" return self.format_discovery_splash() - def format_discovery_splash(self, *, format_: str = "png", size: int = 4096) -> typing.Optional[files.URL]: + def format_discovery_splash(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]: """Generate the guild's discovery splash image, if set. Parameters ---------- - format_ : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg` and `webp`. size : builtins.int The size to set for the URL, defaults to `4096`. @@ -772,7 +772,7 @@ def format_discovery_splash(self, *, format_: str = "png", size: int = 4096) -> return None return routes.CDN_GUILD_DISCOVERY_SPLASH.compile_to_file( - constants.CDN_URL, guild_id=self.id, hash=self.discovery_splash_hash, size=size, file_format=format_, + constants.CDN_URL, guild_id=self.id, hash=self.discovery_splash_hash, size=size, file_format=ext, ) @@ -952,13 +952,13 @@ def splash_url(self) -> typing.Optional[files.URL]: """Splash for the guild, if set.""" return self.format_splash() - def format_splash(self, *, format_: str = "png", size: int = 4096) -> typing.Optional[files.URL]: + def format_splash(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]: """Generate the guild's splash image, if set. Parameters ---------- - format_ : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg` and `webp`. size : builtins.int The size to set for the URL, defaults to `4096`. @@ -978,21 +978,21 @@ def format_splash(self, *, format_: str = "png", size: int = 4096) -> typing.Opt return None return routes.CDN_GUILD_SPLASH.compile_to_file( - constants.CDN_URL, guild_id=self.id, hash=self.splash_hash, size=size, file_format=format_, + constants.CDN_URL, guild_id=self.id, hash=self.splash_hash, size=size, file_format=ext, ) @property - def discovery_splash(self) -> typing.Optional[files.URL]: + def discovery_splash_url(self) -> typing.Optional[files.URL]: """Discovery splash for the guild, if set.""" return self.format_discovery_splash() - def format_discovery_splash(self, *, format_: str = "png", size: int = 4096) -> typing.Optional[files.URL]: + def format_discovery_splash(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]: """Generate the guild's discovery splash image, if set. Parameters ---------- - format_ : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg` and `webp`. size : builtins.int The size to set for the URL, defaults to `4096`. @@ -1012,21 +1012,21 @@ def format_discovery_splash(self, *, format_: str = "png", size: int = 4096) -> return None return routes.CDN_GUILD_DISCOVERY_SPLASH.compile_to_file( - constants.CDN_URL, guild_id=self.id, hash=self.discovery_splash_hash, size=size, file_format=format_, + constants.CDN_URL, guild_id=self.id, hash=self.discovery_splash_hash, size=size, file_format=ext, ) @property - def banner(self) -> typing.Optional[files.URL]: + def banner_url(self) -> typing.Optional[files.URL]: """Banner for the guild, if set.""" return self.format_banner() - def format_banner(self, *, format_: str = "png", size: int = 4096) -> typing.Optional[files.URL]: + def format_banner(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]: """Generate the guild's banner image, if set. Parameters ---------- - format_ : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg` and `webp`. size : builtins.int The size to set for the URL, defaults to `4096`. @@ -1046,7 +1046,7 @@ def format_banner(self, *, format_: str = "png", size: int = 4096) -> typing.Opt return None return routes.CDN_GUILD_BANNER.compile_to_file( - constants.CDN_URL, guild_id=self.id, hash=self.banner_hash, size=size, file_format=format_, + constants.CDN_URL, guild_id=self.id, hash=self.banner_hash, size=size, file_format=ext, ) diff --git a/hikari/invites.py b/hikari/invites.py index 564fa72a40..1ac251ad88 100644 --- a/hikari/invites.py +++ b/hikari/invites.py @@ -136,13 +136,13 @@ def splash_url(self) -> typing.Optional[files.URL]: """Splash for the guild, if set.""" return self.format_splash() - def format_splash(self, *, format_: str = "png", size: int = 4096) -> typing.Optional[files.URL]: + def format_splash(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]: """Generate the guild's splash image, if set. Parameters ---------- - format_ : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg` and `webp`. size : builtins.int The size to set for the URL, defaults to `4096`. @@ -162,7 +162,7 @@ def format_splash(self, *, format_: str = "png", size: int = 4096) -> typing.Opt return None return routes.CDN_GUILD_SPLASH.compile_to_file( - constants.CDN_URL, guild_id=self.id, hash=self.splash_hash, size=size, file_format=format_, + constants.CDN_URL, guild_id=self.id, hash=self.splash_hash, size=size, file_format=ext, ) @property @@ -170,13 +170,13 @@ def banner(self) -> typing.Optional[files.URL]: """Banner for the guild, if set.""" return self.format_banner() - def format_banner(self, *, format_: str = "png", size: int = 4096) -> typing.Optional[files.URL]: + def format_banner(self, *, ext: str = "png", size: int = 4096) -> typing.Optional[files.URL]: """Generate the guild's banner image, if set. Parameters ---------- - format_ : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg` and `webp`. size : builtins.int The size to set for the URL, defaults to `4096`. @@ -196,7 +196,7 @@ def format_banner(self, *, format_: str = "png", size: int = 4096) -> typing.Opt return None return routes.CDN_GUILD_BANNER.compile_to_file( - constants.CDN_URL, guild_id=self.id, hash=self.banner_hash, size=size, file_format=format_, + constants.CDN_URL, guild_id=self.id, hash=self.banner_hash, size=size, file_format=ext, ) diff --git a/hikari/users.py b/hikari/users.py index 9863a59b0e..c7e5cf4703 100644 --- a/hikari/users.py +++ b/hikari/users.py @@ -239,12 +239,12 @@ def mention(self) -> str: """ @property - def avatar(self) -> files.URL: + def avatar_url(self) -> files.URL: """Avatar for the user, or the default avatar if not set.""" return self.format_avatar() or self.default_avatar # noinspection PyShadowingBuiltins - def format_avatar(self, *, format: typing.Optional[str] = None, size: int = 4096) -> typing.Optional[files.URL]: + def format_avatar(self, *, ext: typing.Optional[str] = None, size: int = 4096) -> typing.Optional[files.URL]: """Generate the avatar for this user, if set. If no custom avatar is set, this returns `builtins.None`. You can then @@ -253,14 +253,14 @@ def format_avatar(self, *, format: typing.Optional[str] = None, size: int = 4096 Parameters ---------- - format : typing.Optional[builtins.str] - The format to use for this URL, defaults to `png` or `gif`. + ext : typing.Optional[builtins.str] + The ext to use for this URL, defaults to `png` or `gif`. Supports `png`, `jpeg`, `jpg`, `webp` and `gif` (when animated). Will be ignored for default avatars which can only be `png`. - If `builtins.None`, then the correct default format is determined - based on whether the icon is animated or not. + If `builtins.None`, then the correct default extension is + determined based on whether the icon is animated or not. size : builtins.int The size to set for the URL, defaults to `4096`. Can be any power of two between 16 and 4096. @@ -279,16 +279,14 @@ def format_avatar(self, *, format: typing.Optional[str] = None, size: int = 4096 if self.avatar_hash is None: return None - if format is None: + if ext is None: if self.avatar_hash.startswith("a_"): - # Ignore the fact this shadows `format`, as it is the parameter - # name, which shadows it anyway. - format = "gif" # noqa: A001 shadowing builtin + ext = "gif" else: - format = "png" # noqa: A001 shadowing builtin + ext = "png" return routes.CDN_USER_AVATAR.compile_to_file( - constants.CDN_URL, user_id=self.id, hash=self.avatar_hash, size=size, file_format=format, + constants.CDN_URL, user_id=self.id, hash=self.avatar_hash, size=size, file_format=ext, ) @property diff --git a/hikari/webhooks.py b/hikari/webhooks.py index 37f3042436..b10c2889a3 100644 --- a/hikari/webhooks.py +++ b/hikari/webhooks.py @@ -414,7 +414,7 @@ def default_avatar(self) -> files_.URL: return routes.CDN_DEFAULT_USER_AVATAR.compile_to_file(constants.CDN_URL, discriminator=0, file_format="png",) # noinspection PyShadowingBuiltins - def format_avatar(self, format: str = "png", size: int = 4096) -> typing.Optional[files_.URL]: + def format_avatar(self, ext: str = "png", size: int = 4096) -> typing.Optional[files_.URL]: """Generate the avatar URL for this webhook's custom avatar if set. If no avatar is specified, return `None`. In this case, you should @@ -422,8 +422,8 @@ def format_avatar(self, format: str = "png", size: int = 4096) -> typing.Optiona Parameters ---------- - format : builtins.str - The format to use for this URL, defaults to `png`. + ext : builtins.str + The extension to use for this URL, defaults to `png`. Supports `png`, `jpeg`, `jpg`, `webp`. This will be ignored for default avatars which can only be `png`. size : builtins.int @@ -446,5 +446,5 @@ def format_avatar(self, format: str = "png", size: int = 4096) -> typing.Optiona return None return routes.CDN_USER_AVATAR.compile_to_file( - constants.CDN_URL, user_id=self.id, hash=self.avatar_hash, size=size, file_format=format, + constants.CDN_URL, user_id=self.id, hash=self.avatar_hash, size=size, file_format=ext, ) diff --git a/pytest.ini b/pytest.ini index f16a8400d7..4077041a8c 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,20 +1,4 @@ [pytest] -markers = - auditlog: Mark this test as being part of the Audit Log API. - channel: Mark this test as being part of the Channel API. - gateway: Mark this test as being part of the Gateway API. - guild: Mark this test as being part of the Guild API. - invite: Mark this test as being part of the Invite API. - model: Mark this test as testing a model. - oauth2: Mark this test as being part of the OAuth2 API. - slow: Mark this test as being relatively slow to evaluate. - user: Mark this test as being part of the User API. - voice: Mark this test as being part of the Voice API. - webhook: Mark this test as being part of the Webhook API. - emoji: Mark this test as being part of the AbstractEmoji API. - orm: Mark this test as being part of the ORM. - integration: Mark this test as an integration test. - xfail_strict = true norecursedirs = docs *.egg-info .git tasks .nox .pytest_cache .venv venv public ci diff --git a/tests/hikari/test_applications.py b/tests/hikari/test_applications.py index ebcd054982..a519a85a88 100644 --- a/tests/hikari/test_applications.py +++ b/tests/hikari/test_applications.py @@ -80,7 +80,7 @@ def test_format_icon_when_hash_is_None(self, model): with mock.patch.object( routes, "CDN_TEAM_ICON", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) ) as route: - assert model.format_icon(format="jpeg", size=1) is None + assert model.format_icon(ext="jpeg", size=1) is None route.compile_to_file.assert_not_called() @@ -88,7 +88,7 @@ def test_format_icon_when_hash_is_not_None(self, model): with mock.patch.object( routes, "CDN_TEAM_ICON", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) ) as route: - assert model.format_icon(format="jpeg", size=1) == "file" + assert model.format_icon(ext="jpeg", size=1) == "file" route.compile_to_file.assert_called_once_with( constants.CDN_URL, team_id=123, hash="ahashicon", size=1, file_format="jpeg" @@ -114,7 +114,7 @@ def test_format_icon_when_hash_is_None(self, model): with mock.patch.object( routes, "CDN_APPLICATION_ICON", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) ) as route: - assert model.format_icon(format="jpeg", size=1) is None + assert model.format_icon(ext="jpeg", size=1) is None route.compile_to_file.assert_not_called() @@ -122,7 +122,7 @@ def test_format_icon_when_hash_is_not_None(self, model): with mock.patch.object( routes, "CDN_APPLICATION_ICON", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) ) as route: - assert model.format_icon(format="jpeg", size=1) == "file" + assert model.format_icon(ext="jpeg", size=1) == "file" route.compile_to_file.assert_called_once_with( constants.CDN_URL, application_id=123, hash="ahashicon", size=1, file_format="jpeg" @@ -141,7 +141,7 @@ def test_format_cover_image_when_hash_is_None(self, model): with mock.patch.object( routes, "CDN_APPLICATION_COVER", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) ) as route: - assert model.format_cover_image(format="jpeg", size=1) is None + assert model.format_cover_image(ext="jpeg", size=1) is None route.compile_to_file.assert_not_called() @@ -149,7 +149,7 @@ def test_format_cover_image_when_hash_is_not_None(self, model): with mock.patch.object( routes, "CDN_APPLICATION_COVER", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) ) as route: - assert model.format_cover_image(format="jpeg", size=1) == "file" + assert model.format_cover_image(ext="jpeg", size=1) == "file" route.compile_to_file.assert_called_once_with( constants.CDN_URL, application_id=123, hash="ahashcover", size=1, file_format="jpeg" diff --git a/tests/hikari/test_channels.py b/tests/hikari/test_channels.py index f8774926c3..8bf61f92df 100644 --- a/tests/hikari/test_channels.py +++ b/tests/hikari/test_channels.py @@ -154,7 +154,7 @@ def test_GroupDMChannel_icon_url(): def test_GroupDMChannel_format_icon(): mock_channel = mock.Mock(channels.GroupPrivateTextChannel, id=123, icon_hash="456abc") - assert channels.GroupPrivateTextChannel.format_icon(mock_channel, format="jpeg", size=16) == files.URL( + assert channels.GroupPrivateTextChannel.format_icon(mock_channel, ext="jpeg", size=16) == files.URL( "https://cdn.discordapp.com/channel-icons/123/456abc.jpeg?size=16" ) diff --git a/tests/hikari/test_guilds.py b/tests/hikari/test_guilds.py index 1c064c4f30..c6c694b0c9 100644 --- a/tests/hikari/test_guilds.py +++ b/tests/hikari/test_guilds.py @@ -19,9 +19,12 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import mock +import pytest from hikari import guilds from hikari import users +from hikari.utilities import constants +from hikari.utilities import routes from tests.hikari import hikari_test_helpers @@ -60,22 +63,6 @@ def test_GuildVerificationLevel_str_operator(): assert str(level) == "NONE" -def test_Member_display_name_property_when_nickname_set(): - member = hikari_test_helpers.stub_class(guilds.Member, user=object(), nickname="davb") - assert member.display_name == "davb" - - -def test_Member_display_name_property_when_nickname_not_set(): - member = hikari_test_helpers.stub_class(guilds.Member, user=mock.Mock(users.User, username="davfsa"), nickname=None) - assert member.display_name == "davfsa" - - -def test_Member_str_operator(): - mock_user = mock.Mock(users.User, __str__=mock.Mock(return_value="thomm.o#8637")) - mock_member = mock.Mock(guilds.Member, user=mock_user) - assert guilds.Member.__str__(mock_member) == "thomm.o#8637" - - def test_PartialRole_str_operator(): mock_role = mock.Mock(guilds.Role) mock_role.name = "The Big Cool" @@ -94,7 +81,274 @@ def test_PartialIntegration_str_operator(): assert guilds.PartialIntegration.__str__(mock_integration) == "not an integration" -def test_PartialGuild_str_operator(): - mock_guild = mock.Mock(guilds.PartialGuild) - mock_guild.name = "hikari" - assert guilds.PartialGuild.__str__(mock_guild) == "hikari" +def test_Role_colour_property(): + role_obj = hikari_test_helpers.stub_class(guilds.Role, color="color") + assert role_obj.colour == "color" + + +class TestMember: + @pytest.fixture + def obj(self): + return hikari_test_helpers.stub_class( + guilds.Member, + user=mock.Mock( + users.User, + id=123, + username="davfsa", + discriminator="0001", + avatar_url="avatar", + avatar_hash="a_12asfdjk1213", + default_avatar="default avatar", + format_avatar=mock.Mock(return_value="formated avatar"), + is_bot=False, + is_system=True, + flags="flags", + mention="<@123>", + __str__=mock.Mock(return_value="davfsa#0001"), + ), + nickname="davb", + ) + + def test_str_operator(self, obj): + assert guilds.Member.__str__(obj) == "davfsa#0001" + + def test_id_property(self, obj): + assert obj.id == 123 + + def test_id_setter_property(self, obj): + with pytest.raises(TypeError): + obj.id = 456 + + def test_username_property(self, obj): + assert obj.username == "davfsa" + + def test_discriminator_property(self, obj): + assert obj.discriminator == "0001" + + def test_avatar_hash_property(self, obj): + assert obj.avatar_hash == "a_12asfdjk1213" + + def test_is_bot_property(self, obj): + assert obj.is_bot is False + + def test_is_system_property(self, obj): + assert obj.is_system is True + + def test_flags_property(self, obj): + assert obj.flags == "flags" + + def test_avatar_url_property(self, obj): + assert obj.avatar_url == "avatar" + + def test_format_avatar(self, obj): + assert obj.format_avatar(ext="png", size=1) == "formated avatar" + obj.user.format_avatar.assert_called_once_with(ext="png", size=1) + + def test_default_avatar_property(self, obj): + assert obj.default_avatar == "default avatar" + + def test_display_name_property_when_nickname(self, obj): + assert obj.display_name == "davb" + + def test_display_name_property_when_no_nickname(self, obj): + obj.nickname = None + assert obj.display_name == "davfsa" + + def test_mention_property_when_nickname(self, obj): + assert obj.mention == "<@!123>" + + def test_mention_property_when_no_nickname(self, obj): + obj.nickname = None + assert obj.mention == "<@123>" + + +class TestPartialGuild: + @pytest.fixture + def obj(self): + return hikari_test_helpers.stub_class( + guilds.PartialGuild, name="hikari", id=1234567890, app=mock.Mock(shard_count=4), icon_hash=None + ) + + def test_str_operator(self, obj): + assert str(obj) == "hikari" + + def test_shard_id_property(self, obj): + assert obj.shard_id == 2 + + @pytest.mark.parametrize("error", [TypeError, AttributeError, NameError]) + def test_shard_id_property_when_error(self, error, obj): + obj.app.shard_count = mock.Mock(side_effect=error) + + assert obj.shard_id is None + + def test_icon_url(self, obj): + icon = object() + + with mock.patch.object(guilds.PartialGuild, "format_icon", return_value=icon): + assert obj.icon_url is icon + + def test_format_icon_when_no_hash(self, obj): + obj.icon_hash = None + + assert obj.format_icon(ext="png", size=2) is None + + def test_format_icon_when_format_is_None_and_avatar_hash_is_for_gif(self, obj): + obj.icon_hash = "a_18dnf8dfbakfdh" + + with mock.patch.object( + routes, "CDN_GUILD_ICON", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) + ) as route: + assert obj.format_icon(ext=None, size=2) == "file" + + route.compile_to_file.assert_called_once_with( + constants.CDN_URL, guild_id=1234567890, hash="a_18dnf8dfbakfdh", size=2, file_format="gif", + ) + + def test_format_icon_when_format_is_None_and_avatar_hash_is_not_for_gif(self, obj): + obj.icon_hash = "18dnf8dfbakfdh" + + with mock.patch.object( + routes, "CDN_GUILD_ICON", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) + ) as route: + assert obj.format_icon(ext=None, size=2) == "file" + + route.compile_to_file.assert_called_once_with( + constants.CDN_URL, guild_id=1234567890, hash="18dnf8dfbakfdh", size=2, file_format="png", + ) + + def test_format_icon_with_all_args(self, obj): + obj.icon_hash = "18dnf8dfbakfdh" + + with mock.patch.object( + routes, "CDN_GUILD_ICON", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) + ) as route: + assert obj.format_icon(ext="url", size=2) == "file" + + route.compile_to_file.assert_called_once_with( + constants.CDN_URL, guild_id=1234567890, hash="18dnf8dfbakfdh", size=2, file_format="url", + ) + + +class TestGuildPreview: + @pytest.fixture + def obj(self): + return hikari_test_helpers.stub_class(guilds.GuildPreview, id=123, splash_hash=None, discovery_splash_hash=None) + + def test_splash_url(self, obj): + splash = object() + + with mock.patch.object(guilds.GuildPreview, "format_splash", return_value=splash): + assert obj.splash_url is splash + + def test_format_splash_when_hash(self, obj): + obj.splash_hash = "18dnf8dfbakfdh" + + with mock.patch.object( + routes, "CDN_GUILD_SPLASH", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) + ) as route: + assert obj.format_splash(ext="url", size=2) == "file" + + route.compile_to_file.assert_called_once_with( + constants.CDN_URL, guild_id=123, hash="18dnf8dfbakfdh", size=2, file_format="url", + ) + + def test_format_splash_when_no_hash(self, obj): + assert obj.format_splash(ext="png", size=2) is None + + def test_discovery_splash_url(self, obj): + discovery_splash = object() + + with mock.patch.object(guilds.GuildPreview, "format_discovery_splash", return_value=discovery_splash): + assert obj.discovery_splash_url is discovery_splash + + def test_format_discovery_splash_when_hash(self, obj): + obj.discovery_splash_hash = "18dnf8dfbakfdh" + + with mock.patch.object( + routes, "CDN_GUILD_DISCOVERY_SPLASH", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) + ) as route: + assert obj.format_discovery_splash(ext="url", size=2) == "file" + + route.compile_to_file.assert_called_once_with( + constants.CDN_URL, guild_id=123, hash="18dnf8dfbakfdh", size=2, file_format="url", + ) + + def test_format_discovery_splash_when_no_hash(self, obj): + assert obj.format_discovery_splash(ext="png", size=2) is None + + +class TestGuild: + @pytest.fixture + def obj(self): + class StubGuild(guilds.Guild): + emojis = None + get_emoji = None + get_role = None + roles = None + + return hikari_test_helpers.stub_class( + StubGuild, id=123, splash_hash=None, discovery_splash_hash=None, banner_hash=None + ) + + def test_splash_url(self, obj): + splash = object() + + with mock.patch.object(guilds.Guild, "format_splash", return_value=splash): + assert obj.splash_url is splash + + def test_format_splash_when_hash(self, obj): + obj.splash_hash = "18dnf8dfbakfdh" + + with mock.patch.object( + routes, "CDN_GUILD_SPLASH", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) + ) as route: + assert obj.format_splash(ext="url", size=2) == "file" + + route.compile_to_file.assert_called_once_with( + constants.CDN_URL, guild_id=123, hash="18dnf8dfbakfdh", size=2, file_format="url", + ) + + def test_format_splash_when_no_hash(self, obj): + assert obj.format_splash(ext="png", size=2) is None + + def test_discovery_splash_url(self, obj): + discovery_splash = object() + + with mock.patch.object(guilds.Guild, "format_discovery_splash", return_value=discovery_splash): + assert obj.discovery_splash_url is discovery_splash + + def test_format_discovery_splash_when_hash(self, obj): + obj.discovery_splash_hash = "18dnf8dfbakfdh" + + with mock.patch.object( + routes, "CDN_GUILD_DISCOVERY_SPLASH", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) + ) as route: + assert obj.format_discovery_splash(ext="url", size=2) == "file" + + route.compile_to_file.assert_called_once_with( + constants.CDN_URL, guild_id=123, hash="18dnf8dfbakfdh", size=2, file_format="url", + ) + + def test_format_discovery_splash_when_no_hash(self, obj): + assert obj.format_discovery_splash(ext="png", size=2) is None + + def test_banner_url(self, obj): + banner = object() + + with mock.patch.object(guilds.Guild, "format_banner", return_value=banner): + assert obj.banner_url is banner + + def test_format_banner_when_hash(self, obj): + obj.banner_hash = "18dnf8dfbakfdh" + + with mock.patch.object( + routes, "CDN_GUILD_BANNER", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) + ) as route: + assert obj.format_banner(ext="url", size=2) == "file" + + route.compile_to_file.assert_called_once_with( + constants.CDN_URL, guild_id=123, hash="18dnf8dfbakfdh", size=2, file_format="url", + ) + + def test_format_banner_when_no_hash(self, obj): + assert obj.format_banner(ext="png", size=2) is None diff --git a/tests/hikari/test_users.py b/tests/hikari/test_users.py index 0c55f7c223..8a4f5c6aa3 100644 --- a/tests/hikari/test_users.py +++ b/tests/hikari/test_users.py @@ -54,24 +54,24 @@ class StubUser(users.User): return StubUser() - def test_avatar_when_hash(self, obj): + def test_avatar_url_when_hash(self, obj): avatar = object() obj.format_avatar = mock.Mock(return_value=avatar) with mock.patch.object(users.User, "default_avatar", new=None): - assert obj.avatar is avatar + assert obj.avatar_url is avatar - def test_avatar_when_no_hash(self, obj): + def test_avatar_url_when_no_hash(self, obj): avatar = object() obj.format_avatar = mock.Mock(return_value=None) with mock.patch.object(users.User, "default_avatar", new=avatar): - assert obj.avatar is avatar + assert obj.avatar_url is avatar def test_format_avatar_when_no_hash(self, obj): obj.avatar_hash = None - assert obj.format_avatar(format="png", size=2) is None + assert obj.format_avatar(ext="png", size=2) is None def test_format_avatar_when_format_is_None_and_avatar_hash_is_for_gif(self, obj): obj.avatar_hash = "a_18dnf8dfbakfdh" @@ -79,7 +79,7 @@ def test_format_avatar_when_format_is_None_and_avatar_hash_is_for_gif(self, obj) with mock.patch.object( routes, "CDN_USER_AVATAR", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) ) as route: - assert obj.format_avatar(format=None, size=2) == "file" + assert obj.format_avatar(ext=None, size=2) == "file" route.compile_to_file.assert_called_once_with( constants.CDN_URL, user_id=123, hash="a_18dnf8dfbakfdh", size=2, file_format="gif", @@ -91,7 +91,7 @@ def test_format_avatar_when_format_is_None_and_avatar_hash_is_not_for_gif(self, with mock.patch.object( routes, "CDN_USER_AVATAR", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) ) as route: - assert obj.format_avatar(format=None, size=2) == "file" + assert obj.format_avatar(ext=None, size=2) == "file" route.compile_to_file.assert_called_once_with( constants.CDN_URL, user_id=123, hash="18dnf8dfbakfdh", size=2, file_format="png", @@ -103,7 +103,7 @@ def test_format_avatar_with_all_args(self, obj): with mock.patch.object( routes, "CDN_USER_AVATAR", new=mock.Mock(compile_to_file=mock.Mock(return_value="file")) ) as route: - assert obj.format_avatar(format="url", size=2) == "file" + assert obj.format_avatar(ext="url", size=2) == "file" route.compile_to_file.assert_called_once_with( constants.CDN_URL, user_id=123, hash="18dnf8dfbakfdh", size=2, file_format="url",