Skip to content

Commit

Permalink
Merge pull request #280 from bounswe/wiki_enhance_test
Browse files Browse the repository at this point in the history
Wikidata Searching Enhancement and Unit Tests
  • Loading branch information
damlakayikci authored Nov 18, 2024
2 parents ce7cdc6 + e9e120d commit 0289d8d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 70 deletions.
125 changes: 56 additions & 69 deletions django_project_491/django_app/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from django.test import TestCase
from .Utils.utils import run_code
from .views import wiki_result, wiki_search
from .views.utilization_views import wiki_result, wiki_search
from .models import User
from django.urls import reverse

Expand Down Expand Up @@ -33,75 +33,62 @@
# # self.assertEqual(response.status_code, 302) # Redirect to login page
#

# class TestSearchResult(TestCase):
# # def setUp(self):
# # # Create a sample user for testing
# # self.user = User.objects.create_user(
# # username='testuser',
# # email="test",
# # password='testpassword'
# # )
# # self.factory = RequestFactory()

# # test that the wiki_search returns correct labels even for multi-word strings
# def test_search_valid(self):

# # @login_required decorator requires a request object with a user attribute
# # self.client.login(username='testuser', email='test', password='testpassword')
# # request = self.factory.get('/result/1')
# # request.user = self.user

# request = None

# response = wiki_search(request, "Python Java")
# response_dict = json.loads(response.content)
# bindings = response_dict['results']['bindings']
# language_labels = [binding['languageLabel']['value'] for binding in bindings]
# self.assertTrue("Python 3" in language_labels)
# self.assertTrue("Java 21" in language_labels)

# # test that an invalid search does not crash and returns just empty bindings
# def test_search_invalid(self):
# # @login_required decorator requires a request object with a user attribute
# # self.client.login(username='testuser', email='test', password='testpassword')
# # request = self.factory.get('/result/1')
# # request.user = self.user

# request = None

# response = wiki_search(request, "this_is_not_a_language_1234@#")
# response_dict = json.loads(response.content)
# self.assertTrue(response_dict['results']['bindings'] == []) # empty bindings response expected

# # test the output of the wiki_result with a valid input
# def test_result_valid(self):
# # @login_required decorator requires a request object with a user attribute
# # self.client.login(username='testuser', email='test', password='testpassword')
# # request = self.factory.get('/result/1')
# # request.user = self.user

# request = None

# response = wiki_result(request, "Q15777") # C programming language
# response_dict = json.loads(response.content)
# self.assertTrue(response_dict['mainInfo'][0]['languageLabel']['value'] == 'C')
# self.assertTrue(response_dict['mainInfo'][0]['website']['value'] == 'https://www.iso.org/standard/74528.html')
# self.assertTrue(response_dict['wikipedia']['title'] == 'C (programming language)')

# def test_result_invalid(self):
# # @login_required decorator requires a request object with a user attribute
# # self.client.login(username='testuser', email='test', password='testpassword')
# # request = self.factory.get('/result/1')
# # request.user = self.user

# request = None

# response = wiki_result(request, "Qabc") # invalid wiki id
# response_dict = json.loads(response.content)
# self.assertTrue(response_dict['mainInfo'] == [])
# self.assertTrue(response_dict['wikipedia'] == [])
# self.assertTrue(response_dict['instances'] == [])

class TestSearchResult(TestCase):
def setUp(self):
# Create a test user
self.user = User.objects.create_user(
username='testuser',
email='[email protected]',
password='testpassword'
)
self.client.login(username='testuser', password='testpassword') # Log in the test user

def test_wiki_search_found(self):
search_strings = 'Python Java' # Valid search string

url = reverse('wiki_search', kwargs={'search_strings': search_strings})
response = self.client.get(url)

self.assertEqual(response.status_code, 200)
response_json = response.json()
self.assertGreater(len(response_json['results']), 0)
self.assertIn('Python', [page['languageLabel']['value'] for page in response_json['results']['bindings']])
self.assertIn('Java', [page['languageLabel']['value'] for page in response_json['results']['bindings']])

def test_search_invalid(self):
search_strings = 'this_is_not_a_language_1234' # Invalid search string

url = reverse('wiki_search', kwargs={'search_strings': search_strings})
response = self.client.get(url)

self.assertEqual(response.status_code, 200)
response_json = response.json()
self.assertTrue(response_json['results']['bindings'] == []) # Empty bindings response expected

def test_result_valid(self):
wiki_id = "Q15777" # C programming language

url = reverse('wiki_result', kwargs={'wiki_id': wiki_id})
response = self.client.get(url)

self.assertEqual(response.status_code, 200)
response_json = response.json()
self.assertTrue(response_json['mainInfo'][0]['languageLabel']['value'] == 'C')
self.assertTrue(response_json['mainInfo'][0]['website']['value'] == 'https://www.iso.org/standard/74528.html')
self.assertTrue(response_json['wikipedia']['title'] == 'C (programming language)')

def test_result_invalid(self):
wiki_id = "Qabc" # Invalid programming language

url = reverse('wiki_result', kwargs={'wiki_id': wiki_id})
response = self.client.get(url)

self.assertEqual(response.status_code, 200)
response_json = response.json()
self.assertTrue(response_json['mainInfo'] == [])
self.assertTrue(response_json['wikipedia'] == [])
self.assertTrue(response_json['instances'] == [])

class CodeExecutionTests(TestCase):
def setUp(self):
Expand Down
2 changes: 1 addition & 1 deletion django_project_491/django_app/views/utilization_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def wiki_search(request, search_strings):
SELECT DISTINCT ?language ?languageLabel
WHERE {{
# Limit to specific instance types
VALUES ?instanceType {{ wd:Q9143 wd:Q66747126 wd:Q28455561 wd:Q899523 wd:Q1268980 wd:Q21562092 wd:Q211496}}
VALUES ?instanceType {{ wd:Q9143 wd:Q66747126 wd:Q28455561 wd:Q899523 wd:Q1268980 wd:Q21562092 wd:Q211496 wd:Q12772052}}
# Filter by instance type
?language wdt:P31 ?instanceType.
Expand Down

0 comments on commit 0289d8d

Please sign in to comment.