Beispiel #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()
Beispiel #2
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)]

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

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

        self.bot.api_client.put.assert_not_called()
        self.bot.api_client.delete.assert_not_called()
Beispiel #3
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)
Beispiel #4
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()