From e3b537f6d63431e37f6942362b5855ff4449c9a7 Mon Sep 17 00:00:00 2001 From: Ahmed Bera Pay Date: Fri, 24 Nov 2023 11:50:43 +0300 Subject: [PATCH] Implement return of random node - Implemented the return of a random node when node_is not given as parameter to node GET. - Implemented unit tests --- project/backend/api/tests.py | 20 ++++++++++++++++++++ project/backend/api/views.py | 6 +++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/project/backend/api/tests.py b/project/backend/api/tests.py index a1e61861..7c436e6d 100644 --- a/project/backend/api/tests.py +++ b/project/backend/api/tests.py @@ -387,6 +387,26 @@ 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(self): + response = self.client.get(self.node_url) + + self.assertEqual(response.status_code, 200) + + self.assertContains(response, 'node_id') + self.assertContains(response, 'node_title') + self.assertContains(response, 'publish_date') + self.assertContains(response, 'is_valid') + self.assertContains(response, 'num_visits') + self.assertContains(response, 'theorem') + self.assertContains(response, 'contributors') + self.assertContains(response, 'reviewers') + self.assertContains(response, 'from_referenced_nodes') + self.assertContains(response, 'to_referenced_nodes') + self.assertContains(response, 'proofs') + self.assertContains(response, 'question_set') + self.assertContains(response, 'semantic_tags') + self.assertContains(response, 'annotations') + class TheoremGETAPITestCase(TestCase): def setUp(self): # Create a sample Theorem instance for testing diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 879a81d4..32158488 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -57,7 +57,11 @@ def get_object(self): class NodeAPIView(APIView): def get(self, request): - id = int(request.GET.get("node_id")) + id = request.GET.get("node_id") + if not id: + node_list = Node.objects.all() + return Response(NodeSerializer(node_list[random.randint(0, len(node_list)-1)]).data) + id = int(id) node = Node.objects.filter(node_id=id) if node.count() == 0: return JsonResponse(