Пример #1
0
    async def test_sync_respects_confirmation_result(self):
        """The sync should abort if confirmation fails and continue if confirmed."""
        mock_message = helpers.MockMessage()
        subtests = (
            (True, mock_message),
            (False, None),
        )

        for confirmed, message in subtests:
            with self.subTest(confirmed=confirmed):
                self.syncer._sync.reset_mock()
                self.syncer._get_diff.reset_mock()

                diff = _Diff({1, 2, 3}, {4, 5}, None)
                self.syncer._get_diff.return_value = diff
                self.syncer._get_confirmation_result = mock.AsyncMock(
                    return_value=(confirmed, message))

                guild = helpers.MockGuild()
                await self.syncer.sync(guild)

                self.syncer._get_diff.assert_called_once_with(guild)
                self.syncer._get_confirmation_result.assert_called_once()

                if confirmed:
                    self.syncer._sync.assert_called_once_with(diff)
                else:
                    self.syncer._sync.assert_not_called()
Пример #2
0
    async def test_sync_updated_users(self):
        """Only PUT requests should be made with the correct payload."""
        diff = _Diff([], self.users, None)
        await UserSyncer._sync(diff)

        self.bot.api_client.patch.assert_any_call("bot/users/bulk_patch", json=diff.updated[:self.chunk_size])
        self.bot.api_client.patch.assert_any_call("bot/users/bulk_patch", json=diff.updated[self.chunk_size:])
        self.assertEqual(self.bot.api_client.patch.call_count, self.chunk_count)

        self.bot.api_client.post.assert_not_called()
        self.bot.api_client.delete.assert_not_called()
Пример #3
0
    async def test_sync_updated_users(self):
        """Only PUT requests should be made with the correct payload."""
        users = [fake_user(id=111), fake_user(id=222)]

        diff = _Diff([], users, None)
        await UserSyncer._sync(diff)

        self.bot.api_client.patch.assert_called_once_with(
            "bot/users/bulk_patch", json=diff.updated)

        self.bot.api_client.post.assert_not_called()
        self.bot.api_client.delete.assert_not_called()
Пример #4
0
    async def test_sync_created_users(self):
        """Only POST requests should be made with the correct payload."""
        users = [fake_user(id=111), fake_user(id=222)]

        diff = _Diff(users, [], None)
        await self.syncer._sync(diff)

        self.bot.api_client.post.assert_called_once_with("bot/users",
                                                         json=diff.created)

        self.bot.api_client.put.assert_not_called()
        self.bot.api_client.delete.assert_not_called()
Пример #5
0
    async def test_sync_deleted_roles(self):
        """Only DELETE requests should be made with the correct payload."""
        roles = [fake_role(id=111), fake_role(id=222)]

        role_tuples = {_Role(**role) for role in roles}
        diff = _Diff(set(), set(), role_tuples)
        await self.syncer._sync(diff)

        calls = [mock.call(f"bot/roles/{role['id']}") for role in roles]
        self.bot.api_client.delete.assert_has_calls(calls, any_order=True)
        self.assertEqual(self.bot.api_client.delete.call_count, len(roles))

        self.bot.api_client.post.assert_not_called()
        self.bot.api_client.put.assert_not_called()
Пример #6
0
    async def test_sync_created_roles(self):
        """Only POST requests should be made with the correct payload."""
        roles = [fake_role(id=111), fake_role(id=222)]

        role_tuples = {_Role(**role) for role in roles}
        diff = _Diff(role_tuples, set(), set())
        await RoleSyncer._sync(diff)

        calls = [mock.call("bot/roles", json=role) for role in roles]
        self.bot.api_client.post.assert_has_calls(calls, any_order=True)
        self.assertEqual(self.bot.api_client.post.call_count, len(roles))

        self.bot.api_client.put.assert_not_called()
        self.bot.api_client.delete.assert_not_called()
Пример #7
0
    async def test_sync_updated_users(self):
        """Only PUT requests should be made with the correct payload."""
        users = [fake_user(id=111), fake_user(id=222)]

        user_tuples = {_User(**user) for user in users}
        diff = _Diff(set(), user_tuples, None)
        await self.syncer._sync(diff)

        calls = [mock.call(f"bot/users/{user['id']}", json=user) for user in users]
        self.bot.api_client.put.assert_has_calls(calls, any_order=True)
        self.assertEqual(self.bot.api_client.put.call_count, len(users))

        self.bot.api_client.post.assert_not_called()
        self.bot.api_client.delete.assert_not_called()
Пример #8
0
    async def test_sync_diff_size(self):
        """The diff size should be correctly calculated."""
        subtests = (
            (6, _Diff({1, 2}, {3, 4}, {5, 6})),
            (5, _Diff({1, 2, 3}, None, {4, 5})),
            (0, _Diff(None, None, None)),
            (0, _Diff(set(), set(), set())),
        )

        for size, diff in subtests:
            with self.subTest(size=size, diff=diff):
                self.syncer._get_diff.reset_mock()
                self.syncer._get_diff.return_value = diff
                self.syncer._get_confirmation_result = mock.AsyncMock(
                    return_value=(False, None))

                guild = helpers.MockGuild()
                await self.syncer.sync(guild)

                self.syncer._get_diff.assert_called_once_with(guild)
                self.syncer._get_confirmation_result.assert_called_once()
                self.assertEqual(
                    self.syncer._get_confirmation_result.call_args[0][0], size)