Skip to content

Commit

Permalink
Update views.py
Browse files Browse the repository at this point in the history
  • Loading branch information
gebenalimert committed Nov 24, 2023
1 parent f775933 commit c51f4ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 9 additions & 0 deletions project/backend/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,14 @@ def test_get_removed_node(self):
response = self.client.get(self.node_url, data=data, format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_get_random_node_id(self):
url = reverse('get_random_node_id')
response = self.client.get(url , {'count':2})
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertIn('node_ids', data)
self.assertEqual(len(data['node_ids']), 2)

class TheoremGETAPITestCase(TestCase):
def setUp(self):
# Create a sample Theorem instance for testing
Expand Down Expand Up @@ -486,6 +494,7 @@ def test_get_workspace_from_id(self):
self.assertEqual(response.json()['workspace_title'], self.workspace.workspace_title)
self.assertEqual(response.json()['status'], 'workable')
self.assertEqual(response.json()['references'], [])
self.assertEqual(response.json()['semantic_tags'], [])
self.assertEqual(response.json()['pending_contributors'], [])
self.assertEqual(response.json()['num_approvals'], 0)
# self.assertEqual(response.json()['created_at'], self.workspace.created_at)
Expand Down
8 changes: 5 additions & 3 deletions project/backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ def get_workspace_from_id(request):
'workspace_entries': entries,
'status':status,
'num_approvals':workspace.num_approvals,
'semantic_tags':semantic_tags,
'contributors':contributors,
'pending_contributors':pending,
'references':references,
Expand Down Expand Up @@ -430,11 +431,12 @@ def create_workspace(request):

def get_random_node_id(request):
count = int(request.GET.get("count"))
node_ids = Node.node_id.all()
node_ids = [node['node_id'] for node in Node.objects.values('node_id')]
node_list = []
if count > len(node_ids):
return JsonResponse({'message': 'There are less nodes than requested number.'}, status=200)
for i in range(count):
while node_ids[index] not in node_list:
index = random.randint(0,len(node_ids))
index = random.randint(0,len(node_ids)-1)
node_list.append(node_ids[index])
return JsonResponse({'node_ids': node_list}, status=200)

Expand Down

0 comments on commit c51f4ff

Please sign in to comment.