Skip to content

Commit

Permalink
relation_creator fix: always fill in bron.typeURI and doel.typeURI
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvlaminck committed Dec 18, 2024
1 parent 1485d52 commit 7d333b9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 47 deletions.
83 changes: 55 additions & 28 deletions UnitTests/GeneralTests/RelationCreator_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from pathlib import Path

import pytest
from otlmow_model.OtlmowModel.Helpers.RelationCreator import create_relation

from UnitTests.TestModel.OtlmowModel.Classes.Onderdeel.AllCasesTestClass import AllCasesTestClass
from UnitTests.TestModel.OtlmowModel.Classes.Onderdeel.AnotherTestClass import AnotherTestClass
from UnitTests.TestModel.OtlmowModel.Classes.Onderdeel.Bevestiging import Bevestiging
from UnitTests.TestModel.OtlmowModel.Classes.Onderdeel.Voedt import Voedt

from otlmow_model.OtlmowModel.BaseClasses.OTLObject import dynamic_create_instance_from_ns_and_name
from otlmow_model.OtlmowModel.Classes.Onderdeel.HoortBij import HoortBij
from otlmow_model.OtlmowModel.Exceptions.CouldNotCreateRelationError import CouldNotCreateRelationError
from otlmow_model.OtlmowModel.Exceptions.RelationDeprecationWarning import RelationDeprecationWarning
from otlmow_model.OtlmowModel.Helpers.RelationCreator import create_relation


model_directory_path = Path(__file__).parent.parent / 'TestModel'
Expand All @@ -27,9 +28,41 @@ def test_create_valid_relation():
assert relation.bronAssetId.identificator == another.assetId.identificator
assert relation.doelAssetId.identificator == all_cases.assetId.identificator
assert relation.assetId.identificator == 'Bevestiging_-_another_-_all_cases'
assert relation.bron.typeURI == another.typeURI
assert relation.doel.typeURI == all_cases.typeURI


def test_create_relation_input_parameters(subtests):
def test_create_invalid_relation():
all_cases = AllCasesTestClass()
all_cases.assetId.identificator = 'all_cases'
another = AnotherTestClass()
another.assetId.identificator = 'another'

with pytest.raises(CouldNotCreateRelationError):
create_relation(source=another, target=all_cases, relation_type=Voedt)


def test_create_deprecated_relation():
all_cases = AllCasesTestClass()
all_cases.assetId.identificator = 'all_cases'
another = AnotherTestClass()
another.assetId.identificator = 'another'

with pytest.warns(RelationDeprecationWarning):
relation = create_relation(source=all_cases, target=another, relation_type=Voedt)

assert relation is not None


def test_create_valid_relation_without_assetIds():
all_cases = AllCasesTestClass()
another = AnotherTestClass()

with pytest.raises(AttributeError):
create_relation(source=another, target=all_cases, relation_type=Bevestiging)


def test_create_relation_different_input_parameters(subtests):
all_cases = AllCasesTestClass()
all_cases.assetId.identificator = 'all_cases'
another = AnotherTestClass()
Expand Down Expand Up @@ -90,6 +123,8 @@ def test_create_relation_input_parameters(subtests):
assert relation.bronAssetId.identificator == '00000000-0000-0000-0000-000000000000' \
'-b25kZXJkZWVsI0Fub3RoZXJUZXN0Q2xhc3M'
assert relation.doelAssetId.identificator == all_cases.assetId.identificator
assert relation.bron.typeURI == another.typeURI
assert relation.doel.typeURI == all_cases.typeURI

relation = create_relation(target_typeURI=another.typeURI, target_uuid='00000000-0000-0000-0000-000000000000',
source=all_cases, relation_type=Bevestiging,
Expand All @@ -99,6 +134,8 @@ def test_create_relation_input_parameters(subtests):
assert relation.doelAssetId.identificator == '00000000-0000-0000-0000-000000000000' \
'-b25kZXJkZWVsI0Fub3RoZXJUZXN0Q2xhc3M'
assert relation.bronAssetId.identificator == all_cases.assetId.identificator
assert relation.bron.typeURI == all_cases.typeURI
assert relation.doel.typeURI == another.typeURI

with subtests.test(msg='creating relations using instances of objects'):
relation = create_relation(source=another, target=all_cases, relation_type=Bevestiging,
Expand All @@ -108,45 +145,35 @@ def test_create_relation_input_parameters(subtests):
assert relation.bronAssetId.identificator == another.assetId.identificator
assert relation.doelAssetId.identificator == all_cases.assetId.identificator
assert relation.assetId.identificator == 'Bevestiging_-_another_-_all_cases'
assert relation.bron.typeURI == another.typeURI
assert relation.doel.typeURI == all_cases.typeURI

with subtests.test(msg='real test'):
kast = dynamic_create_instance_from_ns_and_name(namespace='onderdeel', class_name='Wegkantkast')
uuid: str = '847a91b3-569d-4bae-87bf-7e148e8f7de9'
typeURI = 'https://lgc.data.wegenenverkeer.be/ns/installatie#Beheersys'
type_uri_beheersys = 'https://lgc.data.wegenenverkeer.be/ns/installatie#Beheersys'
kast.assetId.identificator = '0000'

relation = create_relation(source=kast, target_uuid=uuid, target_typeURI=typeURI,
relation = create_relation(source=kast, target_uuid=uuid, target_typeURI=type_uri_beheersys,
relation_type=HoortBij)
assert relation is not None
assert relation.doelAssetId.identificator == '847a91b3-569d-4bae-87bf-7e148e8f7de9-bGdjOmluc3RhbGxhdGllI0JlaGVlcnN5cw'
assert relation.assetId.identificator == 'HoortBij_-_0000_-_847a91b3-569d-4bae-87bf-7e148e8f7de9-bGdjOmluc3RhbGxhdGllI0JlaGVlcnN5cw'
assert relation.bron.typeURI == kast.typeURI
assert relation.doel.typeURI == type_uri_beheersys


def test_create_invalid_relation():
all_cases = AllCasesTestClass()
all_cases.assetId.identificator = 'all_cases'
another = AnotherTestClass()
another.assetId.identificator = 'another'

with pytest.raises(CouldNotCreateRelationError):
create_relation(source=another, target=all_cases, relation_type=Voedt)


def test_create_deprecated_relation():
def test_create_relation_legacy():
all_cases = AllCasesTestClass()
all_cases.assetId.identificator = 'all_cases'
another = AnotherTestClass()
another.assetId.identificator = 'another'

with pytest.warns(RelationDeprecationWarning):
relation = create_relation(source=all_cases, target=another, relation_type=Voedt)
legacy_asset_uuid = '00000000-0000-0000-0000-000000000000'
legacy_uri = 'https://lgc.data.wegenenverkeer.be/ns/installatie#Kast'

relation = create_relation(source_uuid=legacy_asset_uuid, source_typeURI=legacy_uri, target=all_cases,
relation_type=Voedt)
assert relation is not None


def test_create_valid_relation_without_assetIds():
all_cases = AllCasesTestClass()
another = AnotherTestClass()

with pytest.raises(AttributeError):
create_relation(source=another, target=all_cases, relation_type=Bevestiging)
assert relation.bronAssetId.identificator == '00000000-0000-0000-0000-000000000000-bGdjOmluc3RhbGxhdGllI0thc3Q'
assert relation.doelAssetId.identificator == 'all_cases'
assert relation.assetId.identificator == 'Voedt_-_00000000-0000-0000-0000-000000000000-bGdjOmluc3RhbGxhdGllI0thc3Q_-_all_cases'
assert relation.bron.typeURI == legacy_uri
assert relation.doel.typeURI == all_cases.typeURI
25 changes: 15 additions & 10 deletions UnitTests/TestModel/OtlmowModel/Helpers/RelationCreator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import warnings
from pathlib import Path
from typing import Type, Optional
from typing import Optional

from otlmow_model.OtlmowModel.BaseClasses.OTLObject import dynamic_create_instance_from_uri
from otlmow_model.OtlmowModel.BaseClasses.RelationInteractor import RelationInteractor
Expand All @@ -13,7 +13,7 @@
from otlmow_model.OtlmowModel.Helpers.RelationValidator import is_valid_relation


def create_relation(relation_type: Type[RelatieObject], source: Optional[RelationInteractor] = None,
def create_relation(relation_type: type[RelatieObject], source: Optional[RelationInteractor] = None,
target: Optional[RelationInteractor] = None,
source_uuid: Optional[str] = None, source_typeURI: Optional[str] = None,
target_uuid: Optional[str] = None, target_typeURI: Optional[str] = None,
Expand Down Expand Up @@ -121,20 +121,20 @@ def create_relation(relation_type: Type[RelatieObject], source: Optional[Relatio
raise AttributeError('In order to create a relation_type, the target needs to have a valid assetId '
'(target.assetId.identificator)')

relation_id = ''

if source_is_legacy:
relation_type.bronAssetId.identificator = source_aim_id
relation_type.bronAssetId.toegekendDoor = 'AWV'
relation_id += source_aim_id
relation_id = source_aim_id
else:
if source.typeURI == 'http://purl.org/dc/terms/Agent':
relation_type.bronAssetId.identificator = source.agentId.identificator
relation_type.bronAssetId.toegekendDoor = source.agentId.toegekendDoor
relation_id += source.agentId.identificator
relation_id = source.agentId.identificator
else:
relation_type.bronAssetId.identificator = source.assetId.identificator
relation_type.bronAssetId.toegekendDoor = source.assetId.toegekendDoor
relation_id += source.assetId.identificator
relation_id = source.assetId.identificator

relation_id += '_-_'

Expand All @@ -152,16 +152,21 @@ def create_relation(relation_type: Type[RelatieObject], source: Optional[Relatio
relation_type.doelAssetId.toegekendDoor = target.assetId.toegekendDoor
relation_id += target.assetId.identificator

relation_id += '_-_' + relation_type.__class__.__name__
relation_id = relation_type.__class__.__name__ + '_-_' + relation_id

relation_type.assetId.identificator = relation_id
relation_type.assetId.toegekendDoor = 'OTLMOW'

# TODO replace check with check for format of AIM id, if False, add typeURI
if relation_type.bronAssetId.toegekendDoor != 'AWV':
if source_is_legacy:
relation_type.bron.typeURI = source_typeURI
else:
relation_type.bron.typeURI = source.typeURI
if relation_type.doelAssetId.toegekendDoor != 'AWV':

if target_is_legacy:
relation_type.doel.typeURI = target_typeURI
else:
relation_type.doel.typeURI = target.typeURI

return relation_type


Expand Down
25 changes: 16 additions & 9 deletions otlmow_model/OtlmowModel/Helpers/RelationCreator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import warnings
from pathlib import Path
from typing import Type, Optional
from typing import Optional

from otlmow_model.OtlmowModel.BaseClasses.OTLObject import dynamic_create_instance_from_uri
from otlmow_model.OtlmowModel.BaseClasses.RelationInteractor import RelationInteractor
Expand All @@ -13,7 +13,7 @@
from otlmow_model.OtlmowModel.Helpers.RelationValidator import is_valid_relation


def create_relation(relation_type: Type[RelatieObject], source: Optional[RelationInteractor] = None,
def create_relation(relation_type: type[RelatieObject], source: Optional[RelationInteractor] = None,
target: Optional[RelationInteractor] = None,
source_uuid: Optional[str] = None, source_typeURI: Optional[str] = None,
target_uuid: Optional[str] = None, target_typeURI: Optional[str] = None,
Expand Down Expand Up @@ -121,20 +121,20 @@ def create_relation(relation_type: Type[RelatieObject], source: Optional[Relatio
raise AttributeError('In order to create a relation_type, the target needs to have a valid assetId '
'(target.assetId.identificator)')

relation_id = relation_type.__class__.__name__ + '_-_'

if source_is_legacy:
relation_type.bronAssetId.identificator = source_aim_id
relation_type.bronAssetId.toegekendDoor = 'AWV'
relation_id += source_aim_id
relation_id = source_aim_id
else:
if source.typeURI == 'http://purl.org/dc/terms/Agent':
relation_type.bronAssetId.identificator = source.agentId.identificator
relation_type.bronAssetId.toegekendDoor = source.agentId.toegekendDoor
relation_id += source.agentId.identificator
relation_id = source.agentId.identificator
else:
relation_type.bronAssetId.identificator = source.assetId.identificator
relation_type.bronAssetId.toegekendDoor = source.assetId.toegekendDoor
relation_id += source.assetId.identificator
relation_id = source.assetId.identificator

relation_id += '_-_'

Expand All @@ -152,14 +152,21 @@ def create_relation(relation_type: Type[RelatieObject], source: Optional[Relatio
relation_type.doelAssetId.toegekendDoor = target.assetId.toegekendDoor
relation_id += target.assetId.identificator

relation_id = relation_type.__class__.__name__ + '_-_' + relation_id

relation_type.assetId.identificator = relation_id
relation_type.assetId.toegekendDoor = 'OTLMOW'

# TODO replace check with check for format of AIM id, if False, add typeURI
if relation_type.bronAssetId.toegekendDoor != 'AWV':
if source_is_legacy:
relation_type.bron.typeURI = source_typeURI
else:
relation_type.bron.typeURI = source.typeURI
if relation_type.doelAssetId.toegekendDoor != 'AWV':

if target_is_legacy:
relation_type.doel.typeURI = target_typeURI
else:
relation_type.doel.typeURI = target.typeURI

return relation_type


Expand Down

0 comments on commit 7d333b9

Please sign in to comment.