diff options
| author | 2020-01-03 11:54:39 -0800 | |
|---|---|---|
| committer | 2020-02-12 10:07:50 -0800 | |
| commit | 155c4c7a1bb73ef42cf19ccacc612c7a5bc17201 (patch) | |
| tree | 0760cc28a55ba891e6ea0e1d8353cbb79b664edf /tests | |
| parent | Sync tests: test diff is empty when DB has a user not in the guild (diff) | |
Sync tests: add tests for API requests for syncing users
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/bot/cogs/sync/test_users.py | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/tests/bot/cogs/sync/test_users.py b/tests/bot/cogs/sync/test_users.py index dfb9ac405..7fc1b400f 100644 --- a/tests/bot/cogs/sync/test_users.py +++ b/tests/bot/cogs/sync/test_users.py @@ -1,7 +1,8 @@ import asyncio import unittest +from unittest import mock -from bot.cogs.sync.syncers import UserSyncer, _User +from bot.cogs.sync.syncers import UserSyncer, _Diff, _User from tests import helpers @@ -120,3 +121,41 @@ class UserSyncerDiffTests(unittest.TestCase): expected_diff = (set(), set(), None) self.assertEqual(actual_diff, expected_diff) + + +class UserSyncerSyncTests(unittest.TestCase): + """Tests for the API requests that sync roles.""" + + def setUp(self): + self.bot = helpers.MockBot() + self.syncer = UserSyncer(self.bot) + + 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) + asyncio.run(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() + + 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) + asyncio.run(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() |