-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #280 from bounswe/wiki_enhance_test
Wikidata Searching Enhancement and Unit Tests
- Loading branch information
Showing
2 changed files
with
57 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
@@ -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): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters