diff --git a/project/backend/api/tests.py b/project/backend/api/tests.py index a1e61861..b7278c8a 100644 --- a/project/backend/api/tests.py +++ b/project/backend/api/tests.py @@ -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 @@ -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) diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 879a81d4..c0f6d489 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -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, @@ -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)