Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return random off topic names which are Active only #644

Merged
merged 4 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions pydis_site/apps/api/tests/test_off_topic_channel_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def setUpTestData(cls):
cls.test_name_3 = OffTopicChannelName.objects.create(
name="frozen-with-iceman", used=True, active=False
)
cls.test_name_4 = OffTopicChannelName.objects.create(
name="xith-is-cool", used=True, active=True
)

def test_returns_name_in_list(self):
"""Return all off-topic channel names."""
Expand All @@ -86,28 +89,46 @@ def test_returns_name_in_list(self):
{
self.test_name.name,
self.test_name_2.name,
self.test_name_3.name
self.test_name_3.name,
self.test_name_4.name
}
)

def test_returns_two_items_with_random_items_param_set_to_2(self):
"""Return not-used name instead used."""
def test_returns_two_active_items_with_random_items_param_set_to_2(self):
"""Return not-used active names instead used."""
url = reverse('api:bot:offtopicchannelname-list')
response = self.client.get(f'{url}?random_items=2')

self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()), 2)
self.assertEqual(set(response.json()), {self.test_name.name, self.test_name_2.name})
self.assertTrue(
all(
item in (self.test_name.name, self.test_name_2.name, self.test_name_4.name)
for item in response.json()
)
)

def test_returns_three_active_items_with_random_items_param_set_to_3(self):
"""Return not-used active names instead used."""
url = reverse('api:bot:offtopicchannelname-list')
response = self.client.get(f'{url}?random_items=3')

self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()), 3)
self.assertEqual(
set(response.json()),
{self.test_name.name, self.test_name_2.name, self.test_name_4.name}
)

def test_running_out_of_names_with_random_parameter(self):
"""Reset names `used` parameter to `False` when running out of names."""
"""Reset names `used` parameter to `False` when running out of active names."""
url = reverse('api:bot:offtopicchannelname-list')
response = self.client.get(f'{url}?random_items=3')

self.assertEqual(response.status_code, 200)
self.assertEqual(
set(response.json()),
{self.test_name.name, self.test_name_2.name, self.test_name_3.name}
{self.test_name.name, self.test_name_2.name, self.test_name_4.name}
)

def test_returns_inactive_ot_names(self):
Expand All @@ -129,7 +150,7 @@ def test_returns_active_ot_names(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(
set(response.json()),
{self.test_name.name, self.test_name_2.name}
{self.test_name.name, self.test_name_2.name, self.test_name_4.name}
)


Expand Down
3 changes: 1 addition & 2 deletions pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def list(self, request: Request, *args, **kwargs) -> Response:
'random_items': ["Must be a positive integer."]
})

queryset = self.queryset.order_by('used', '?')[:random_count]
queryset = self.queryset.filter(active=True).order_by('used', '?')[:random_count]

# When any name is used in our listing then this means we reached end of round
# and we need to reset all other names `used` to False
Expand All @@ -133,7 +133,6 @@ def list(self, request: Request, *args, **kwargs) -> Response:
return Response(serialized.data)

params = {}

if active_param := request.query_params.get("active"):
params["active"] = active_param.lower() == "true"

Expand Down