From 113d93b2a9b2263ae6bc69a7d23852d1aa43d882 Mon Sep 17 00:00:00 2001 From: AndersToft20 Date: Wed, 29 Nov 2023 14:24:29 +0100 Subject: [PATCH] more tests --- relation_extraction/get_relations.py | 8 +++--- test/test_server/test_output.py | 39 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 test/test_server/test_output.py diff --git a/relation_extraction/get_relations.py b/relation_extraction/get_relations.py index 7ba4ed5..88c74ae 100644 --- a/relation_extraction/get_relations.py +++ b/relation_extraction/get_relations.py @@ -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) @@ -40,4 +39,5 @@ def extract_specific_relations(): return relations -extract_specific_relations() \ No newline at end of file +if __name__ == "__main__": + extract_specific_relations() \ No newline at end of file diff --git a/test/test_server/test_output.py b/test/test_server/test_output.py new file mode 100644 index 0000000..850bb1b --- /dev/null +++ b/test/test_server/test_output.py @@ -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() \ No newline at end of file