From b8faa2df8a143c7e19ee1657d4768298e4f0f6ec Mon Sep 17 00:00:00 2001 From: grey-cat-1908 Date: Sun, 20 Mar 2022 16:41:11 +0300 Subject: [PATCH] refactor and add tests of guild converting --- melisa/__init__.py | 2 - melisa/models/guild/guild.py | 130 +++++++-------------------- tests/parsing/test_guilds_parsing.py | 76 ++++++++++++++++ 3 files changed, 108 insertions(+), 100 deletions(-) create mode 100644 tests/parsing/test_guilds_parsing.py diff --git a/melisa/__init__.py b/melisa/__init__.py index 141b049..d00d2a1 100644 --- a/melisa/__init__.py +++ b/melisa/__init__.py @@ -10,5 +10,3 @@ __title__ = "Melisa" __description__ = "Cache-configurable module to interact with the Discord API." __author__ = "MelisaDev" __license__ = "MIT" - - diff --git a/melisa/models/guild/guild.py b/melisa/models/guild/guild.py index 6da5ab0..2f90bb0 100644 --- a/melisa/models/guild/guild.py +++ b/melisa/models/guild/guild.py @@ -4,8 +4,8 @@ from __future__ import annotations from dataclasses import dataclass -from enum import IntEnum, Enum, Flag -from typing import Optional, Tuple, List, Literal +from enum import IntEnum, Enum +from typing import List, Any from ...utils import Snowflake from ...utils import APIModelBase @@ -27,7 +27,7 @@ class ExplicitContentFilterLevel(IntEnum): DISABLED = 0 MEMBERS_WITHOUT_ROLES = 1 - ALL_MEMBERS = 2 + ALL_MEMBERS = 2 def __int__(self): return self.value @@ -46,10 +46,10 @@ class MFALevel(IntEnum): class VerificationLevel(IntEnum): """""" - NONE = 0 - LOW = 1 - MEDIUM = 2 - HIGH = 3 + NONE = 0 + LOW = 1 + MEDIUM = 2 + HIGH = 3 VERY_HIGH = 4 def __int__(self): @@ -71,10 +71,10 @@ class GuildNSFWLevel(IntEnum): class PremiumTier(IntEnum): """""" - NONE = 0 - TIER_1 = 1 - TIER_2 = 2 - TIER_3 = 3 + NONE = 0 + TIER_1 = 1 + TIER_2 = 2 + TIER_3 = 3 def __int__(self): return self.value @@ -84,9 +84,9 @@ class SystemChannelFlags(IntEnum): """""" SUPPRESS_JOIN_NOTIFICATIONS = 1 << 0 - SUPPRESS_PREMIUM_SUBSCRIPTIONS = 1 << 1 - SUPPRESS_GUILD_REMINDER_NOTIFICATIONS = 1 << 2 - SUPPRESS_JOIN_NOTIFICATION_REPLIES = 1 << 3 + SUPPRESS_PREMIUM_SUBSCRIPTIONS = 1 << 1 + SUPPRESS_GUILD_REMINDER_NOTIFICATIONS = 1 << 2 + SUPPRESS_JOIN_NOTIFICATION_REPLIES = 1 << 3 def __int__(self): return self.value @@ -141,19 +141,27 @@ class Guild(APIModelBase): default_message_notifications: APINullable[int] = None explicit_content_filter: APINullable[int] = None features: APINullable[List[GuildFeatures]] = None - #TODO: Make a structure for emoji and role + roles: APINullable[List[Any]] = None + emojis: APINullable[List[Any]] = None + # TODO: Make a structures of emoji and role mfa_level: APINullable[int] = None application_id: APINullable[Snowflake] = None system_channel_id: APINullable[Snowflake] = None system_channel_flags: APINullable[int] = None rules_channel_id: APINullable[Snowflake] = None - #TODO: Deal with joined_at + joined_at: APINullable[int] = None + # TODO: Deal with joined_at large: APINullable[bool] = None unavailable: APINullable[bool] = None member_count: APINullable[int] = None - #TODO: Make a structure for voice_states, members, channels, threads, presences(?) + voice_states: APINullable[List[Any]] = None + members: APINullable[List[Any]] = None + channels: APINullable[List[Any]] = None + threads: APINullable[List[Any]] = None + presences: APINullable[List[Any]] = None + # TODO: Make a structure for voice_states, members, channels, threads, presences(?) max_presences: APINullable[int] = None max_members: APINullable[int] = None @@ -169,90 +177,16 @@ class Guild(APIModelBase): approximate_presence_count: APINullable[int] = None nsfw_level: APINullable[int] = None premium_progress_bar_enabled: APINullable[bool] = None - #TODO: Make a structure for welcome_screen, stage_instances, stickers and guild_scheduled_events + stage_instances: APINullable[List[Any]] = None + stickers: APINullable[List[Any]] = None + welcome_screen: APINullable[Any] = None + guild_scheduled_events: APINullable[List[Any]] = None - - async def get_guild(self, _id: id, with_counts: bool = False ): - """""" - - return await self._http.get( - f"/guild/{_id}", params = {"with_counts": "true" if with_counts else None}) - - - - @property - def default_message_notifications(self) -> Optional[DefaultMessageNotificationLevel]: - """""" - - return( - None - if self.default_message_notifications is None - else DefaultMessageNotificationLevel(self.default_message_notifications) - ) - - @property - def explicit_content_filter(self) -> Optional[ExplicitContentFilterLevel]: - """""" - - return( - None - if self.explicit_content_filter is None - else ExplicitContentFilterLevel(self.explicit_content_filter) - ) - - @property - def mfa_level(self) -> Optional[MFALevel]: - """""" - - return( - None - if self.mfa_level is None - else MFALevel(self.mfa_level) - ) - - @property - def verification_level(self) -> Optional[VerificationLevel]: - """""" - - return( - None - if self.verification_level is None - else VerificationLevel(self.verification_level) - ) - - @property - def nsfw_level(self) -> Optional[GuildNSFWLevel]: - """""" - - return( - None - if self.nsfw_level is None - else GuildNSFWLevel(self.nsfw_level) - ) - - @property - def premium_tier(self) -> Optional(PremiumTier): - """""" - - return( - None - if self.premium_tier is None - else PremiumTier(self.premium_tier) - ) - - @property - def system_channel_flags(self) -> Optional[SystemChannelFlags]: - """""" - - return( - None - if self.system_channel_flags is None - else SystemChannelFlags(self.system_channel_flags) - ) + # TODO: Make a structure for welcome_screen, stage_instances, + # stickers and guild_scheduled_events @dataclass(repr=False) class UnavailableGuild(APIModelBase): - id: APINullable[Snowflake] = None - unavailable: APINullable[bool] = True + unavailable: APINullable[bool] = True diff --git a/tests/parsing/test_guilds_parsing.py b/tests/parsing/test_guilds_parsing.py new file mode 100644 index 0000000..5863061 --- /dev/null +++ b/tests/parsing/test_guilds_parsing.py @@ -0,0 +1,76 @@ +from melisa.models import Guild + +guild_data = { + "id": "197038439483310086", + "name": "Discord Testers", + "icon": "f64c482b807da4f539cff778d174971c", + "description": "The official place to report Discord Bugs!", + "splash": None, + "discovery_splash": None, + "features": [ + "ANIMATED_ICON", + "VERIFIED", + "NEWS", + "VANITY_URL", + "DISCOVERABLE", + "MORE_EMOJI", + "INVITE_SPLASH", + "BANNER", + "COMMUNITY" + ], + "emojis": [], + "banner": "9b6439a7de04f1d26af92f84ac9e1e4a", + "owner_id": "73193882359173120", + "application_id": None, + "region": None, + "afk_channel_id": None, + "afk_timeout": 300, + "system_channel_id": None, + "widget_enabled": None, + "widget_channel_id": None, + "verification_level": 3, + "roles": [], + "default_message_notifications": 1, + "mfa_level": 1, + "explicit_content_filter": 2, + "max_presences": 40000, + "max_members": 250000, + "vanity_url_code": "discord-testers", + "premium_tier": 3, + "premium_subscription_count": 33, + "system_channel_flags": 0, + "preferred_locale": "en-US", + "rules_channel_id": "441688182833020939", + "public_updates_channel_id": "281283303326089216" +} + +parsed_model = Guild.from_dict(guild_data) + + +class TestGuildParsing: + def test_dict_to_model(self): + assert int(parsed_model.id) == 197038439483310086 + assert parsed_model.name == "Discord Testers" + assert parsed_model.features == [ + "ANIMATED_ICON", + "VERIFIED", + "NEWS", + "VANITY_URL", + "DISCOVERABLE", + "MORE_EMOJI", + "INVITE_SPLASH", + "BANNER", + "COMMUNITY" + ] + + def test_model_to_dict_without_none(self): + data = parsed_model.to_dict() + + new_guild_data = guild_data + + for i in data.keys(): + del new_guild_data[i] + + for j in new_guild_data.values(): + if j is not None: + raise TypeError