diff options
| -rw-r--r-- | pydis_site/apps/api/models/bot/role.py | 15 | ||||
| -rw-r--r-- | pydis_site/apps/api/tests/test_roles.py | 42 | 
2 files changed, 49 insertions, 8 deletions
| diff --git a/pydis_site/apps/api/models/bot/role.py b/pydis_site/apps/api/models/bot/role.py index 836d9f27..58bbf8b4 100644 --- a/pydis_site/apps/api/models/bot/role.py +++ b/pydis_site/apps/api/models/bot/role.py @@ -7,7 +7,12 @@ from pydis_site.apps.api.models.utils import ModelReprMixin  class Role(ModelReprMixin, models.Model): -    """A role on our Discord server.""" +    """ +    A role on our Discord server. + +    The comparison operators <, <=, >, >=, ==, != act the same as they do with Role-objects of the +    discord.py library, see https://discordpy.readthedocs.io/en/latest/api.html#discord.Role +    """      id = models.BigIntegerField(          primary_key=True, @@ -49,10 +54,14 @@ class Role(ModelReprMixin, models.Model):          help_text="The position of the role in the role hierarchy of the Discord Guild."      ) -    def __str__(self): +    def __str__(self) -> str:          """Returns the name of the current role, for display purposes."""          return self.name -    def __lt__(self, other: Role): +    def __lt__(self, other: Role) -> bool:          """Compares the roles based on their position in the role hierarchy of the guild."""          return self.position < other.position + +    def __le__(self, other: Role) -> bool: +        """Compares the roles based on their position in the role hierarchy of the guild.""" +        return self.position <= other.position diff --git a/pydis_site/apps/api/tests/test_roles.py b/pydis_site/apps/api/tests/test_roles.py index 882083bb..0a6cea9e 100644 --- a/pydis_site/apps/api/tests/test_roles.py +++ b/pydis_site/apps/api/tests/test_roles.py @@ -28,6 +28,13 @@ class CreationTests(APISubdomainTestCase):              permissions=5,              position=0,          ) +        cls.lowest_position_duplicate = Role.objects.create( +            id=6, +            name="lowest position duplicate", +            colour=6, +            permissions=6, +            position=0, +        )      def _validate_roledict(self, role_dict: dict) -> None:          """Helper method to validate a dict representing a role.""" @@ -36,24 +43,49 @@ class CreationTests(APISubdomainTestCase):          attributes = ('id', 'name', 'colour', 'permissions', 'position')          self.assertTrue(all(attribute in role_dict for attribute in attributes)) -    def test_role_ordering(self): +    def test_role_ordering_lt(self):          """Tests the __lt__ comparisons based on role position in the hierarchy."""          self.assertTrue(self.everyone_role < self.developers_role)          self.assertFalse(self.developers_role > self.admins_role) -        top_role = max([self.developers_role, self.admins_role, self.everyone_role]) -        self.assertIs(top_role, self.admins_role) +    def test_role_ordering_le(self): +        """Tests the __le__ comparisons based on role position in the hierarchy.""" +        self.assertTrue(self.everyone_role <= self.developers_role) +        self.assertTrue(self.everyone_role <= self.lowest_position_duplicate) +        self.assertTrue(self.everyone_role >= self.lowest_position_duplicate) +        self.assertTrue(self.developers_role >= self.everyone_role) + +        self.assertFalse(self.developers_role >= self.admins_role) +        self.assertFalse(self.developers_role <= self.everyone_role) + +    def test_role_min_max_ordering(self): +        """Tests the `min` and `max` operations based on the role hierarchy.""" +        top_role_no_duplicates = max([self.developers_role, self.admins_role, self.everyone_role]) +        self.assertIs(top_role_no_duplicates, self.admins_role) + +        top_role_duplicates = max([self.developers_role, self.admins_role, self.admins_role]) +        self.assertIs(top_role_duplicates, self.admins_role) + +        bottom_role_no_duplicates = min( +            [self.developers_role, self.admins_role, self.everyone_role] +        ) +        self.assertIs(bottom_role_no_duplicates, self.everyone_role) + +        bottom_role_duplicates = min( +            [self.lowest_position_duplicate, self.admins_role, self.everyone_role] +        ) +        self.assertIs(bottom_role_duplicates, self.lowest_position_duplicate)      def test_role_list(self):          """Tests the GET list-view and validates the contents."""          url = reverse('bot:role-list', host='api')          response = self.client.get(url) -        self.assertContains(response, text="id", count=3, status_code=200) +        self.assertContains(response, text="id", count=4, status_code=200)          roles = response.json()          self.assertIsInstance(roles, list) -        self.assertEqual(len(roles), 3) +        self.assertEqual(len(roles), 4)          for role in roles:              self._validate_roledict(role) | 
