diff --git a/server/models/__init__.py b/server/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server/service_protocols/fs_service_protocol/models.py b/server/models/file_metadata.py similarity index 100% rename from server/service_protocols/fs_service_protocol/models.py rename to server/models/file_metadata.py diff --git a/server/service_protocols/ontology_service_protocol/models.py b/server/models/ontology.py similarity index 98% rename from server/service_protocols/ontology_service_protocol/models.py rename to server/models/ontology.py index 9f6ffcf..8a9a9a0 100644 --- a/server/service_protocols/ontology_service_protocol/models.py +++ b/server/models/ontology.py @@ -1,7 +1,7 @@ from dataclasses import dataclass, field from enum import StrEnum -from server.service_protocols.fs_service_protocol.models import ( +from server.models.file_metadata import ( FileMetadata, ) diff --git a/server/service_protocols/workspace_service_protocol/models.py b/server/models/workspace.py similarity index 88% rename from server/service_protocols/workspace_service_protocol/models.py rename to server/models/workspace.py index 0fb9120..dfb23ec 100644 --- a/server/service_protocols/workspace_service_protocol/models.py +++ b/server/models/workspace.py @@ -1,6 +1,6 @@ from dataclasses import dataclass -from server.service_protocols.workspace_metadata_service_protocol.models import ( +from server.models.workspace_metadata import ( WorkspaceMetadataModel, ) diff --git a/server/service_protocols/workspace_metadata_service_protocol/models.py b/server/models/workspace_metadata.py similarity index 100% rename from server/service_protocols/workspace_metadata_service_protocol/models.py rename to server/models/workspace_metadata.py diff --git a/server/service_protocols/fs_service_protocol/__init__.py b/server/service_protocols/fs_service_protocol/__init__.py index 747e0e9..76cd122 100644 --- a/server/service_protocols/fs_service_protocol/__init__.py +++ b/server/service_protocols/fs_service_protocol/__init__.py @@ -1,6 +1,6 @@ from typing import Protocol -from server.service_protocols.fs_service_protocol.models import ( +from server.models.file_metadata import ( FileMetadata, ) diff --git a/server/service_protocols/ontology_service_protocol/__init__.py b/server/service_protocols/ontology_service_protocol/__init__.py index 1de1495..c7b72e0 100644 --- a/server/service_protocols/ontology_service_protocol/__init__.py +++ b/server/service_protocols/ontology_service_protocol/__init__.py @@ -1,9 +1,9 @@ from typing import Protocol -from server.service_protocols.fs_service_protocol.models import ( +from server.models.file_metadata import ( FileMetadata, ) -from server.service_protocols.ontology_service_protocol.models import ( +from server.models.ontology import ( Ontology, ) diff --git a/server/service_protocols/workspace_metadata_service_protocol/__init__.py b/server/service_protocols/workspace_metadata_service_protocol/__init__.py index 70474ad..a003eff 100644 --- a/server/service_protocols/workspace_metadata_service_protocol/__init__.py +++ b/server/service_protocols/workspace_metadata_service_protocol/__init__.py @@ -1,6 +1,6 @@ from typing import Protocol -from server.service_protocols.workspace_metadata_service_protocol.models import ( +from server.models.workspace_metadata import ( WorkspaceMetadataModel, ) from server.services.core.sqlite_db_service.tables.workspace_metadata import ( diff --git a/server/service_protocols/workspace_service_protocol/__init__.py b/server/service_protocols/workspace_service_protocol/__init__.py index 89a73d9..78f320c 100644 --- a/server/service_protocols/workspace_service_protocol/__init__.py +++ b/server/service_protocols/workspace_service_protocol/__init__.py @@ -1,6 +1,6 @@ from typing import Protocol -from server.service_protocols.workspace_service_protocol.models import ( +from server.models.workspace import ( WorkspaceModel, ) diff --git a/server/services/core/workspace_metadata_service.py b/server/services/core/workspace_metadata_service.py index 3f3e793..1839386 100644 --- a/server/services/core/workspace_metadata_service.py +++ b/server/services/core/workspace_metadata_service.py @@ -7,7 +7,7 @@ from server.const.err_enums import ErrCodes from server.exceptions import ServerException -from server.service_protocols.workspace_metadata_service_protocol.models import ( +from server.models.workspace_metadata import ( WorkspaceMetadataModel, ) from server.services.core.sqlite_db_service import DBService diff --git a/server/utils/ontology_indexer.py b/server/utils/ontology_indexer.py index 257996b..2f084cf 100644 --- a/server/utils/ontology_indexer.py +++ b/server/utils/ontology_indexer.py @@ -4,8 +4,8 @@ from rdflib import Graph, Literal from rdflib.query import ResultRow -from server.service_protocols.ontology_service_protocol import ( - models, +from server.models import ( + ontology, ) @@ -84,8 +84,8 @@ class OntologyIndexer: def _create_literal_from_rdflib_literal( self, rdflib_literal: Literal - ) -> models.Literal: - return models.Literal( + ) -> ontology.Literal: + return ontology.Literal( value=rdflib_literal.value, language=rdflib_literal.language or "en", datatype=rdflib_literal.datatype or "", @@ -93,7 +93,7 @@ def _create_literal_from_rdflib_literal( def get_classes( self, ontology_uri: str, g: Graph - ) -> list[models.Class]: + ) -> list[ontology.Class]: """ Get classes from the ontology @@ -182,9 +182,9 @@ def _update_class_labels_and_descriptions( def _create_class_models( self, ontology_uri: str, classes: dict - ) -> list[models.Class]: + ) -> list[ontology.Class]: return [ - models.Class( + ontology.Class( belongs_to=ontology_uri, full_uri=str(class_uri), label=class_data["label"], @@ -197,7 +197,7 @@ def _create_class_models( def get_properties( self, ontology_uri: str, g: Graph - ) -> list[models.Property]: + ) -> list[ontology.Property]: """ Get properties from the ontology @@ -299,9 +299,9 @@ def _update_property_labels_and_descriptions( def _create_property_models( self, ontology_uri: str, properties: dict - ) -> list[models.Property]: + ) -> list[ontology.Property]: return [ - models.Property( + ontology.Property( belongs_to=str(ontology_uri), full_uri=str(property_uri), label=property_data["label"], @@ -320,14 +320,14 @@ def _create_property_models( def _get_property_type( self, property_type: str - ) -> models.PropertyType: + ) -> ontology.PropertyType: match property_type: case "http://www.w3.org/2002/07/owl#ObjectProperty": - return models.PropertyType.OBJECT + return ontology.PropertyType.OBJECT case "http://www.w3.org/2002/07/owl#DatatypeProperty": - return models.PropertyType.DATATYPE + return ontology.PropertyType.DATATYPE case "http://www.w3.org/2002/07/owl#AnnotationProperty": - return models.PropertyType.ANNOTATION + return ontology.PropertyType.ANNOTATION case _: raise ValueError( f"Unknown property type: {property_type}" @@ -335,7 +335,7 @@ def _get_property_type( def get_individuals( self, ontology_uri: str, g: Graph - ) -> list[models.Individual]: + ) -> list[ontology.Individual]: """ Get individuals from the ontology @@ -410,9 +410,9 @@ def _update_individual_labels_and_descriptions( def _create_individual_models( self, ontology_uri: str, individuals: dict - ) -> list[models.Individual]: + ) -> list[ontology.Individual]: return [ - models.Individual( + ontology.Individual( belongs_to=ontology_uri, full_uri=str(individual_uri), label=individual_data["label"], diff --git a/test/services/core/sqlite_db_service_test.py b/test/services/core/sqlite_db_service_test.py deleted file mode 100644 index 6aac024..0000000 --- a/test/services/core/sqlite_db_service_test.py +++ /dev/null @@ -1,38 +0,0 @@ -import unittest -from pathlib import Path -from unittest.mock import patch - -from server.services.core.sqlite_db_service import DBService - - -class TestDBServiceInit(unittest.TestCase): - @patch( - "server.services.core.sqlite_db_service.create_engine" - ) - @patch( - "server.services.core.sqlite_db_service.Base.metadata.create_all" - ) - def test_init( - self, mock_create_all, mock_create_engine - ): - # Arrange - app_dir = Path("/fake/path") - expected_db_path = f"sqlite:///{(app_dir / 'data' / 'db.sqlite').absolute()}" - - # Act - db_service = DBService(APP_DIR=app_dir) - - # Assert - self.assertEqual( - db_service._db_path, expected_db_path - ) - mock_create_engine.assert_called_once_with( - expected_db_path - ) - mock_create_all.assert_called_once_with( - mock_create_engine.return_value - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/test/services/core/workspace_metadata_service_test.py b/test/services/core/workspace_metadata_service_test.py index bb4f86f..5bf0aab 100644 --- a/test/services/core/workspace_metadata_service_test.py +++ b/test/services/core/workspace_metadata_service_test.py @@ -1,12 +1,11 @@ import unittest -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch from server.const.err_enums import ErrCodes from server.exceptions import ServerException -from server.service_protocols.workspace_metadata_service_protocol.models import ( - WorkspaceMetadataModel, -) +from server.services.core.sqlite_db_service import DBService from server.services.core.sqlite_db_service.tables.workspace_metadata import ( + WorkspaceMetadata, WorkspaceType, ) from server.services.core.workspace_metadata_service import ( @@ -16,133 +15,101 @@ class TestWorkspaceMetadataService(unittest.TestCase): def setUp(self): - self.db_service_mock = MagicMock() - self.workspace_metadata_service = ( - WorkspaceMetadataService(self.db_service_mock) - ) - self.workspace_metadata_model = ( - WorkspaceMetadataModel( - uuid="uuid", - name="name", - description="description", - type=WorkspaceType.LOCAL, - location="location", - enabled_features=["feature1", "feature2"], - ) - ) - self.workspace_metadata_model_table = ( - self.workspace_metadata_model.to_table() + self.db_service = MagicMock(spec=DBService) + self.service = WorkspaceMetadataService( + self.db_service ) - def test_get_workspaces(self): + @patch( + "server.services.core.workspace_metadata_service.uuid4" + ) + def test_create_workspace_metadata(self, mock_uuid4): + mock_uuid4.return_value.hex = "test-uuid" session_mock = MagicMock() - self.db_service_mock.get_session.return_value.__enter__.return_value = session_mock - session_mock.query.return_value.all.return_value = [ - self.workspace_metadata_model_table - ] + self.db_service.get_session.return_value.__enter__.return_value = session_mock - result = ( - self.workspace_metadata_service.get_workspaces() + self.service.create_workspace_metadata( + name="Test Workspace", + description="Test Description", + type=WorkspaceType.LOCAL, + location="/test/location", ) - self.assertEqual(len(result), 1) - self.db_service_mock.get_session.assert_called_once() - session_mock.query.assert_called_once() - session_mock.query.return_value.all.assert_called_once() + session_mock.add.assert_called_once() + session_mock.commit.assert_called_once() - def test_create_workspace_metadata(self): + def test_get_workspaces(self): session_mock = MagicMock() - self.db_service_mock.get_session.return_value.__enter__.return_value = session_mock + self.db_service.get_session.return_value.__enter__.return_value = session_mock + session_mock.query.return_value.all.return_value = [] - self.workspace_metadata_service.create_workspace_metadata( - self.workspace_metadata_model - ) + result = self.service.get_workspaces() - self.db_service_mock.get_session.assert_called_once() - session_mock.add.assert_called_once() - session_mock.commit.assert_called_once() + self.assertEqual(result, []) + session_mock.query.assert_called_once_with( + WorkspaceMetadata + ) - def test_update_workspace_metadata_success(self): + def test_update_workspace_metadata(self): session_mock = MagicMock() - self.db_service_mock.get_session.return_value.__enter__.return_value = session_mock - session_mock.execute.return_value.one_or_none.return_value = self.workspace_metadata_model + self.db_service.get_session.return_value.__enter__.return_value = session_mock + session_mock.execute.return_value.one_or_none.return_value = WorkspaceMetadata( + uuid="test-uuid" + ) - self.workspace_metadata_service.update_workspace_metadata( - "uuid", self.workspace_metadata_model + self.service.update_workspace_metadata( + uuid="test-uuid", + name="Updated Name", + description="Updated Description", ) - self.db_service_mock.get_session.assert_called_once() - session_mock.execute.assert_called_once() - session_mock.merge.assert_called_once() session_mock.commit.assert_called_once() def test_update_workspace_metadata_not_found(self): session_mock = MagicMock() - self.db_service_mock.get_session.return_value.__enter__.return_value = session_mock + self.db_service.get_session.return_value.__enter__.return_value = session_mock session_mock.execute.return_value.one_or_none.return_value = None with self.assertRaises(ServerException) as context: - self.workspace_metadata_service.update_workspace_metadata( - "uuid", self.workspace_metadata_model + self.service.update_workspace_metadata( + uuid="test-uuid", + name="Updated Name", + description="Updated Description", ) self.assertEqual( context.exception.code, ErrCodes.WORKSPACE_METADATA_NOT_FOUND, ) - self.db_service_mock.get_session.assert_called_once() - session_mock.execute.assert_called_once() - def test_update_workspace_metadata_illegal_update(self): + def test_delete_workspace_metadata(self): session_mock = MagicMock() - self.db_service_mock.get_session.return_value.__enter__.return_value = session_mock - session_mock.execute.return_value.one_or_none.return_value = self.workspace_metadata_model - self.workspace_metadata_model.uuid = ( - "different_uuid" + self.db_service.get_session.return_value.__enter__.return_value = session_mock + session_mock.execute.return_value.one_or_none.return_value = WorkspaceMetadata( + uuid="test-uuid" ) - with self.assertRaises(ServerException) as context: - self.workspace_metadata_service.update_workspace_metadata( - "uuid", self.workspace_metadata_model - ) - - self.assertEqual( - context.exception.code, - ErrCodes.WORKSPACE_METADATA_ILLEGAL_UPDATE_OPERATION, - ) - self.db_service_mock.get_session.assert_called_once() - session_mock.execute.assert_called_once() - - def test_delete_workspace_metadata_success(self): - session_mock = MagicMock() - self.db_service_mock.get_session.return_value.__enter__.return_value = session_mock - session_mock.execute.return_value.one_or_none.return_value = self.workspace_metadata_model - - self.workspace_metadata_service.delete_workspace_metadata( - "uuid" + self.service.delete_workspace_metadata( + uuid="test-uuid" ) - self.db_service_mock.get_session.assert_called_once() - session_mock.execute.assert_called_once() - session_mock.delete.assert_called_once() + session_mock.execute.assert_called() session_mock.commit.assert_called_once() def test_delete_workspace_metadata_not_found(self): session_mock = MagicMock() - self.db_service_mock.get_session.return_value.__enter__.return_value = session_mock + self.db_service.get_session.return_value.__enter__.return_value = session_mock session_mock.execute.return_value.one_or_none.return_value = None with self.assertRaises(ServerException) as context: - self.workspace_metadata_service.delete_workspace_metadata( - "uuid" + self.service.delete_workspace_metadata( + uuid="test-uuid" ) self.assertEqual( context.exception.code, ErrCodes.WORKSPACE_METADATA_NOT_FOUND, ) - self.db_service_mock.get_session.assert_called_once() - session_mock.execute.assert_called_once() if __name__ == "__main__": diff --git a/test/utils/ontology_indexer_test.py b/test/utils/ontology_indexer_test.py index ed86880..c82378c 100644 --- a/test/utils/ontology_indexer_test.py +++ b/test/utils/ontology_indexer_test.py @@ -2,7 +2,7 @@ from rdflib import Graph -from server.service_protocols.ontology_service_protocol.models import ( +from server.models.ontology import ( Class, ) from server.utils.ontology_indexer import OntologyIndexer