Skip to content

Commit

Permalink
Fix test_checklicense_api (#531)
Browse files Browse the repository at this point in the history
* Fix test_checklicense_api

* Fix tests with new RDF files
  • Loading branch information
rtgdk authored Dec 21, 2024
1 parent 14d4ccf commit a98a07d
Show file tree
Hide file tree
Showing 4 changed files with 6,349 additions and 17 deletions.
22 changes: 14 additions & 8 deletions src/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from api.models import ValidateFileUpload,ConvertFileUpload,CompareFileUpload,SubmitLicenseModel
from django.conf import settings
import os
import json

def getExamplePath(filename):
return os.path.join(settings.EXAMPLES_DIR, filename)
Expand All @@ -55,7 +56,7 @@ def setUp(self):
u.is_staff = True
u.save()
self.tv_file = open(getExamplePath("SPDXTagExample-v2.0.spdx"))
self.rdf_file = open(getExamplePath("SPDXRdfExample-v2.0.rdf"))
self.rdf_file = open(getExamplePath("SPDXRdfExample-v2.2.spdx.rdf"))
self.invalid_tv_file = open(getExamplePath("SPDXTagExample-v2.0_invalid.spdx"))
self.invalid_rdf_file = open(getExamplePath("SPDXRdfExample-v2.0_invalid.rdf"))

Expand Down Expand Up @@ -225,8 +226,8 @@ def setUp(self):
u = User.objects.create_user(**self.credentials)
u.is_staff = True
u.save()
self.rdf_file = open(getExamplePath("SPDXRdfExample-v2.0.rdf"))
self.rdf_file2 = open(getExamplePath("SPDXRdfExample.rdf"))
self.rdf_file = open(getExamplePath("SPDXRdfExample-v2.2.spdx.rdf"))
self.rdf_file2 = open(getExamplePath("SPDXRdfExample-v2.3.spdx.rdf"))
self.tv_file = open(getExamplePath("SPDXTagExample-v2.0.spdx"))

def tearDown(self):
Expand Down Expand Up @@ -281,6 +282,7 @@ def setUp(self):
u = User.objects.create_user(**self.credentials)
u.is_staff = True
u.save()
self.license = "AFL-1.1"
self.license_file = open(getExamplePath("AFL-1.1.txt"))
self.other_file = open(getExamplePath("Other.txt"))

Expand All @@ -302,13 +304,17 @@ def test_checklicense_api(self):
""" Valid License File"""
resp3 = self.client.post(reverse("check_license-api"),{"file":self.license_file},format="multipart")
self.assertEqual(resp3.status_code,200)
self.assertEqual(resp3.data['owner'],User.objects.get_by_natural_key(self.username).id)
self.assertEqual(resp3.data["result"],"The following license ID(s) match: AFL-1.1")
result3 = json.loads(resp3.content)
self.assertEqual(result3['matched_license'], self.license)
self.assertEqual(result3['match_type'], 'Perfect match')
self.assertEqual(result3["all_matches"],{self.license: 1.0})
""" Other File"""
resp4 = self.client.post(reverse("check_license-api"),{"file":self.other_file},format="multipart")
self.assertEqual(resp4.data['owner'],User.objects.get_by_natural_key(self.username).id)
self.assertEqual(resp4.status_code,404)
self.assertEqual(resp4.data["result"],"There are no matching SPDX listed licenses")
self.assertEqual(resp4.status_code, 404)
result4 = json.loads(resp4.content)
self.assertEqual(result4['matched_license'], None)
self.assertEqual(result4['match_type'], 'No match')
self.assertEqual(result4['all_matches'], {})

def test_checklicense_without_argument(self):
self.client.login(username=self.username,password=self.password)
Expand Down
19 changes: 10 additions & 9 deletions src/app/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,22 @@ def license_compare_helper(request):
else :
filelist.append(myfile.name)
errorlist.append("No errors found")
except jpype.JavaException as ex :
except jpype.JException as ex:
""" Error raised by verifyclass.verifyRDFFile without exiting the application"""
erroroccurred = True
filelist.append(myfile.name)
errorlist.append(jpype.JavaException.message(ex))
except :
errorlist.append(jpype.JException.message(ex))
except Exception as ex:
""" Other Exceptions"""
erroroccurred = True
filelist.append(myfile.name)
errorlist.append(format_exc())
except :
except Exception as ex:
"""Invalid file extension"""
erroroccurred = True
filelist.append(myfile.name)
errorlist.append("Invalid file extension for "+filename+". Must be .xls, .xlsx, .xml, .json, .yaml, .spdx, .rdfxml")
errorlist.append(format_exc())
if (erroroccurred==False):
""" If no errors in any of the file,call the java function with parameters as list"""
try :
Expand Down Expand Up @@ -284,7 +285,7 @@ def ntia_check_helper(request):
result['context'] = context_dict
result['status'] = 404
return result
except:
except Exception as ex:
""" Other error raised """
if (request.is_ajax()):
ajaxdict = dict()
Expand Down Expand Up @@ -391,7 +392,7 @@ def license_validate_helper(request):
result['context'] = context_dict
result['status'] = 404
return result
except :
except Exception as ex:
""" Other error raised """
if (request.is_ajax()):
ajaxdict=dict()
Expand Down Expand Up @@ -458,7 +459,7 @@ def license_check_helper(request):
result['context'] = context_dict
result['status'] = 404
return result
except :
except Exception as ex:
""" Other exception raised """
if request.is_ajax():
ajaxdict = dict()
Expand Down Expand Up @@ -570,7 +571,7 @@ def license_convert_helper(request):
result['context'] = context_dict
result['status'] = 404
return result
except :
except Exception as ex:
""" Other error raised """
if (request.is_ajax()):
ajaxdict["type"] = "error"
Expand Down Expand Up @@ -625,7 +626,7 @@ def license_diff_helper(request):
data['context'] = data
data['status'] = 404
return data
except :
except Exception as ex:
""" Other exception raised """
if (request.is_ajax()):
ajaxdict = dict()
Expand Down
Loading

0 comments on commit a98a07d

Please sign in to comment.