Skip to content

Commit

Permalink
added tests and clean metakg parse endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkiBytes committed Dec 5, 2024
1 parent 3ce64f8 commit f4de5e9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/handlers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,6 @@ async def get(self, *args, **kwargs):
self.finish(res)

class MetaKGParserHandler(BaseHandler):
name="metakgparser"
kwargs = {
"GET": {
"url": {
Expand All @@ -713,6 +712,7 @@ def initialize(self, *args, **kwargs):
def get_filtered_api(self, api_dict):
"""Extract and return filtered API information."""
api_info = api_dict["api"]

# Default structure to preserve top-level keys
filtered_dict = {
"subject": api_dict.get("subject"),
Expand All @@ -721,6 +721,7 @@ def get_filtered_api(self, api_dict):
"subject_prefix": api_dict.get("subject_prefix"),
"object_prefix": api_dict.get("object_prefix"),
}

# case: bte=1, api_details=0
if self.args.bte == "1" and self.args.api_details == "0":
filtered_api = {
Expand All @@ -732,13 +733,16 @@ def get_filtered_api(self, api_dict):
),
"bte": api_info.get("bte", {}),
}

# case: bte=0, api_details=1
elif self.args.bte == "0" and self.args.api_details == "1":
api_info.pop("bte", None)
filtered_api = api_info

# case: api_details=1, bte=1
elif self.args.bte == "1" and self.args.api_details == "1":
filtered_api = api_info

# case: bte=0, api_details=0
else:
filtered_api = {
Expand All @@ -752,6 +756,10 @@ def get_filtered_api(self, api_dict):
# Add the filtered 'api' key to the preserved top-level structure
filtered_dict["api"] = filtered_api

# Remove 'bte' from 'api' if it exists
if "bte" in filtered_dict["api"]:
filtered_dict['bte'] = filtered_dict["api"].pop("bte", None)

return filtered_dict

def process_apis(self, apis):
Expand Down Expand Up @@ -796,7 +804,7 @@ async def get(self, *args, **kwargs):
}

self.set_header("Content-Type", "application/json")
self.write(json.dumps(response))
self.write(response)

async def post(self, *args, **kwargs):
try:
Expand All @@ -805,6 +813,8 @@ async def post(self, *args, **kwargs):
# Parse the JSON content
data = json.loads(body)
parser = MetaKGParser()
self.args.api_details = self.get_argument("api_details", "0")
self.args.bte = self.get_argument("bte", "0")
trapi_data = parser.get_TRAPI_metadatas(data=data)
nontrapi_data = parser.get_non_TRAPI_metadatas(data=data)
combined_data = trapi_data + nontrapi_data
Expand All @@ -821,8 +831,7 @@ async def post(self, *args, **kwargs):
}

self.set_header("Content-Type", "application/json")
self.write(json.dumps(response))
self.write(response)

except json.JSONDecodeError:
self.set_status(400)
self.write({"error": "Invalid JSON format"})
raise ValueError("Invalid JSON content in request body.")
80 changes: 80 additions & 0 deletions src/tests/_utils/metakg/integration/parser/parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import unittest
import requests
import json


class TestAPI(unittest.TestCase):
URL_EXAMPLE = "https://raw.githubusercontent.com/NCATS-Tangerine/translator-api-registry/master/mygene.info/openapi_full.yml"

def setUp(self):
self.headers = {"Content-Type": "application/json"}
with open('/Users/nacosta/Documents/smartAPI/WORKING_BRANCH/add-metakg-endpoint/smartAPI/src/metadata_content.json', 'r') as file:
self.data = json.load(file)

# POST Tests
def test_post_metakg_parse_api_details_1_bte_1(self):
url = "http://localhost:8000/api/metakg/parse?api_details=1&bte=1"
response = requests.post(url, headers=self.headers, json=self.data)

Check warning on line 17 in src/tests/_utils/metakg/integration/parser/parse.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/tests/_utils/metakg/integration/parser/parse.py#L17

Requests call without timeout
json_response = response.json()
self.assertEqual(response.status_code, 200)
self.assertIn('api', json_response['hits'][0].keys())
self.assertIn('bte', json_response['hits'][0].keys())

def test_post_metakg_parse_api_details_0_bte_1(self):
url = "http://localhost:8000/api/metakg/parse?api_details=0&bte=1"
response = requests.post(url, headers=self.headers, json=self.data)
json_response = response.json()
self.assertEqual(response.status_code, 200)
self.assertIn('bte', json_response['hits'][0].keys())

def test_post_metakg_parse_api_details_1_bte_0(self):
url = "http://localhost:8000/api/metakg/parse?api_details=1&bte=0"
response = requests.post(url, headers=self.headers, json=self.data)
json_response = response.json()
self.assertEqual(response.status_code, 200)
self.assertIn('api', json_response['hits'][0].keys())
self.assertNotIn('bte', json_response['hits'][0].keys())

def test_post_metakg_parse_api_details_0_bte_0(self):
url = "http://localhost:8000/api/metakg/parse?api_details=0&bte=0"
response = requests.post(url, headers=self.headers, json=self.data)
json_response = response.json()
self.assertEqual(response.status_code, 200)
self.assertNotIn('bte', json_response['hits'][0].keys())
self.assertIn('subject', json_response['hits'][0].keys())

# GET Tests
def test_get_metakg_parse_api_details_1_bte_1(self):
url = f"http://localhost:8000/api/metakg/parse?url={self.URL_EXAMPLE}&api_details=1&bte=1"
response = requests.get(url)

Check warning on line 49 in src/tests/_utils/metakg/integration/parser/parse.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/tests/_utils/metakg/integration/parser/parse.py#L49

Requests call without timeout
json_response = response.json()
self.assertEqual(response.status_code, 200)
self.assertIn('api', json_response['hits'][0].keys())
self.assertIn('bte', json_response['hits'][0].keys())

def test_get_metakg_parse_api_details_0_bte_1(self):
url = f"http://localhost:8000/api/metakg/parse?url={self.URL_EXAMPLE}&api_details=0&bte=1"
response = requests.get(url)
json_response = response.json()
self.assertEqual(response.status_code, 200)
self.assertIn('bte', json_response['hits'][0].keys())

def test_get_metakg_parse_api_details_1_bte_0(self):
url = f"http://localhost:8000/api/metakg/parse?url={self.URL_EXAMPLE}&api_details=1&bte=0"
response = requests.get(url)
json_response = response.json()
self.assertEqual(response.status_code, 200)
self.assertIn('api', json_response['hits'][0].keys())
self.assertNotIn('bte', json_response['hits'][0].keys())

def test_get_metakg_parse_api_details_0_bte_0(self):
url = f"http://localhost:8000/api/metakg/parse?url={self.URL_EXAMPLE}&api_details=0&bte=0"
response = requests.get(url)
json_response = response.json()
self.assertEqual(response.status_code, 200)
self.assertNotIn('bte', json_response['hits'][0].keys())
self.assertIn('subject', json_response['hits'][0].keys())


if __name__ == "__main__":
unittest.main()

0 comments on commit f4de5e9

Please sign in to comment.