Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersToft20 committed Nov 29, 2023
1 parent 8631c14 commit 113d93b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
8 changes: 4 additions & 4 deletions relation_extraction/get_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ def extract_specific_relations_offline():
relations = set()
with open(ontology_file_path, 'r', encoding='utf-8', errors='ignore') as file:
lines = file.readlines()
i = 0
for i, line in enumerate(lines):
line = line.strip()
# Check if the line starts with a colon and the next lines contain the specified pattern
if line.startswith(":") and i+1 < len(lines) and "a rdf:Property, owl:ObjectProperty ;" in lines[i+1]:
if line.startswith(":") and i+1 <= len(lines) and "a rdf:Property, owl:ObjectProperty ;" in lines[i+1]:
relation = line.split()[0] # Extracting the relation name
relation = relation[1:] # Remove colon
relations.add(relation)
i += 1


return sorted(relations)

Expand All @@ -40,4 +39,5 @@ def extract_specific_relations():

return relations

extract_specific_relations()
if __name__ == "__main__":
extract_specific_relations()
39 changes: 39 additions & 0 deletions test/test_server/test_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
from server.server import *
from unittest.mock import patch, Mock, MagicMock, mock_open
from relation_extraction.get_relations import *

class TestGetRelations(unittest.TestCase):

@patch('requests.get')
def test_extract_specific_relations(self, mock_get):
response = {
"triples": [
{"s": {"Value": "http://dbpedia.org/ontology/test"}},
{"s": {"Value": "http://dbpedia.org/ontology/another_test"}}
]
}

mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = response
mock_get.return_value.text.return_value = "request response"

relations = extract_specific_relations()

self.assertEqual(len(relations), 2)
self.assertEqual(relations[0], "test")
self.assertEqual(relations[1], "another_test")
mock_get.assert_called_once_with(url='http://130.225.57.13/knox-api/triples', params={'g': 'http://knox_ontology', 's': 'http://dbpedia.org/ontology/', 'o': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property'}, headers={'Access-Authorization': 'internal_key'})


@patch("builtins.open", new_callable=mock_open, read_data=":testline\na rdf:Property, owl:ObjectProperty ;")
def test_extract_specific_relations_offline(self, mock_open):
res = extract_specific_relations_offline()
mock_open.assert_called_once()

self.assertEqual(res, ["testline"])



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

0 comments on commit 113d93b

Please sign in to comment.