Skip to content

Commit 04df144

Browse files
committed
Added tests
Changes to be committed: modified: search/apis.py new file: tests/test_views/test_api_objects.py modified: tests/test_views/test_api_objects_search.py
1 parent 60da4fd commit 04df144

File tree

3 files changed

+92
-20
lines changed

3 files changed

+92
-20
lines changed

search/apis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SearchObjectsAPI(APIView):
5656
)
5757
],
5858
responses={
59-
200: ""
59+
200: "Search successfull"
6060
},
6161
tags=["BCO Management"],
6262
)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python3
2+
3+
"""Search the BCODB
4+
Tests for endpoint for use of query string based search.
5+
Four parameters are defined by this API:
6+
1. contents: Search in the contents of the BCO
7+
2. prefix: BCO Prefix to search
8+
3. owner_user: Search by BCO owner
9+
4. object_id: BCO object_id to search for
10+
"""
11+
12+
from django.test import TestCase
13+
from rest_framework.test import APIClient
14+
from rest_framework.authtoken.models import Token
15+
from django.contrib.auth.models import User
16+
from rest_framework.test import APITestCase
17+
18+
class ObjectsTestCase(APITestCase):
19+
fixtures = ['tests/fixtures/test_data']
20+
21+
def setUp(self):
22+
self.client = APIClient()
23+
24+
def test_search_contents(self):
25+
"""Search successfull. 200
26+
"""
27+
28+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
29+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
30+
response = self.client.get('http://localhost:8000/api/objects/?contents=review')
31+
self.assertEqual(response.status_code, 200)
32+
self.assertEqual(len(response.json()[0]), 12)
33+
34+
def test_search_prefix(self):
35+
"""Search successfull. 200
36+
"""
37+
38+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
39+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
40+
response = self.client.get('http://localhost:8000/api/objects/?prefix=TEST')
41+
self.assertEqual(response.status_code, 200)
42+
self.assertEqual(len(response.json()[0]), 2)
43+
44+
def test_search_owner_user(self):
45+
"""Search successfull. 200
46+
"""
47+
48+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
49+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
50+
response = self.client.get('http://localhost:8000/api/objects/?owner_user=test50')
51+
self.assertEqual(response.status_code, 200)
52+
self.assertEqual(len(response.json()[0]), 4)
53+
54+
def test_search_object_id(self):
55+
"""Search successfull. 200
56+
"""
57+
58+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
59+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
60+
response = self.client.get('http://localhost:8000/api/objects/?object_id=DRAFT')
61+
self.assertEqual(response.status_code, 200)
62+
self.assertEqual(len(response.json()[0]), 6)
63+
64+
def test_search_all(self):
65+
"""Search successfull. 200
66+
"""
67+
68+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
69+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
70+
response = self.client.get('http://localhost:8000/api/objects/?')
71+
self.assertEqual(response.status_code, 200)
72+
self.assertEqual(len(response.json()[0]), 12)
73+
74+
def test_search_multi_value(self):
75+
"""Search successfull. 200
76+
"""
77+
78+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
79+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
80+
response = self.client.get('http://localhost:8000/api/objects/?contents=HCV&contents=DRAFT')
81+
self.assertEqual(response.status_code, 200)
82+
self.assertEqual(len(response.json()[0]), 2)

tests/test_views/test_api_objects_search.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,18 @@
1111
from django.contrib.auth.models import User
1212
from rest_framework.test import APITestCase
1313

14+
#TODO: this needs refinement
1415
class ObjectsSearchTestCase(APITestCase):
1516

1617
fixtures = ['tests/fixtures/test_data']
1718
def setUp(self):
18-
1919
self.client = APIClient()
20-
# Checking if the user 'bco_api_user' already exists
21-
try:
22-
self.user = User.objects.get(username='bco_api_user')
23-
except User.DoesNotExist:
24-
self.user = User.objects.create_user(username='bco_api_user')
25-
26-
# Checking if user already has token, if not then creating one
27-
if not Token.objects.filter(user=self.user).exists():
28-
self.token = Token.objects.create(user=self.user)
29-
else:
30-
self.token = Token.objects.get(user=self.user)
3120

3221
def test_search_successful(self):
33-
# Test case for a successful search (status code: 200)
22+
"""Test case for a successful search (status code: 200)
23+
"""
3424

25+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
3526
data = {
3627
"POST_api_objects_search": [
3728
{
@@ -40,16 +31,15 @@ def test_search_successful(self):
4031
}
4132
]
4233
}
43-
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
34+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
4435
response = self.client.post("/api/objects/search/", data=data, format="json")
45-
4636
self.assertEqual(response.status_code, 200)
4737

48-
49-
5038
def test_prefix_not_found(self):
51-
# Test case for prefix not found (status code: 404)
39+
"""Test case for prefix not found (status code: 404)
40+
"""
5241

42+
token = Token.objects.get(user=User.objects.get(username='bco_api_user')).key
5343
data = {
5444
"POST_api_objects_search": [
5545
{
@@ -59,6 +49,6 @@ def test_prefix_not_found(self):
5949
]
6050
}
6151

62-
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
52+
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
6353
response = self.client.post("/api/objects/search/", data=data, format="json")
6454
self.assertEqual(response.status_code, 404)

0 commit comments

Comments
 (0)