Skip to content

Commit

Permalink
update path tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KerstenBreuer committed Dec 13, 2023
1 parent 612f625 commit 74afe01
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@
# limitations under the License.
#

"""Test reference utils."""
"""Test the path module."""

from contextlib import nullcontext

import pytest
from pydantic import BaseModel

from metldata.builtin_transformations.infer_references.path.path import ReferencePath
from metldata.builtin_transformations.infer_references.path.path_elements import (
ReferencePathElement,
ReferencePathElementType,
from metldata.schemapack_.builtin_transformations.infer_relations.path.path import (
RelationPath,
)
from metldata.schemapack_.builtin_transformations.infer_relations.path.path_elements import (
RelationPathElement,
RelationPathElementType,
)


Expand All @@ -34,10 +36,10 @@
(
"class_a(class_b)>class_b",
[
ReferencePathElement(
type_=ReferencePathElementType.ACTIVE,
RelationPathElement(
type_=RelationPathElementType.ACTIVE,
source="class_a",
slot="class_b",
property="class_b",
target="class_b",
)
],
Expand All @@ -49,10 +51,10 @@
(class_b) >
class_b""", # containing whitespaces
[
ReferencePathElement(
type_=ReferencePathElementType.ACTIVE,
RelationPathElement(
type_=RelationPathElementType.ACTIVE,
source="class_a",
slot="class_b",
property="class_b",
target="class_b",
)
],
Expand All @@ -62,10 +64,10 @@
(
"class_a<(class_a)class_b",
[
ReferencePathElement(
type_=ReferencePathElementType.PASSIVE,
RelationPathElement(
type_=RelationPathElementType.PASSIVE,
source="class_a",
slot="class_a",
property="class_a",
target="class_b",
)
],
Expand All @@ -75,16 +77,16 @@
(
"class_a(class_b)>class_b(class_c)>class_c",
[
ReferencePathElement(
type_=ReferencePathElementType.ACTIVE,
RelationPathElement(
type_=RelationPathElementType.ACTIVE,
source="class_a",
slot="class_b",
property="class_b",
target="class_b",
),
ReferencePathElement(
type_=ReferencePathElementType.ACTIVE,
RelationPathElement(
type_=RelationPathElementType.ACTIVE,
source="class_b",
slot="class_c",
property="class_c",
target="class_c",
),
],
Expand All @@ -94,16 +96,16 @@
(
"class_a(class_b)>class_b<(class_b)class_c",
[
ReferencePathElement(
type_=ReferencePathElementType.ACTIVE,
RelationPathElement(
type_=RelationPathElementType.ACTIVE,
source="class_a",
slot="class_b",
property="class_b",
target="class_b",
),
ReferencePathElement(
type_=ReferencePathElementType.PASSIVE,
RelationPathElement(
type_=RelationPathElementType.PASSIVE,
source="class_b",
slot="class_b",
property="class_b",
target="class_c",
),
],
Expand All @@ -114,13 +116,13 @@
)
def test_reference_path(
path_str: str,
expected_elements: ReferencePathElement,
expected_elements: RelationPathElement,
expected_source: str,
expected_target: str,
):
"""Test the ReferencePath class."""
"""Test the RelationPath class."""

observed_path = ReferencePath(path_str=path_str)
observed_path = RelationPath(path_str=path_str)
assert observed_path.elements == expected_elements
assert observed_path.source == expected_source
assert observed_path.target == expected_target
Expand All @@ -138,16 +140,16 @@ def test_reference_path(
],
)
def test_reference_path_pydantic(path_str: str, is_valid: bool):
"""Test the ReferencePath class when used with pydantic."""
"""Test the RelationPath class when used with pydantic."""

class ExampleModel(BaseModel):
"""Some example model."""

path: ReferencePath
path: RelationPath

with nullcontext() if is_valid else pytest.raises(ValueError):
observed_path = ExampleModel(path=path_str).path # type: ignore

if is_valid:
expected_path = ReferencePath(path_str=path_str)
expected_path = RelationPath(path_str=path_str)
assert observed_path == expected_path
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
# limitations under the License.
#

"""Test reference utils."""
"""Test the path_str module."""

from contextlib import nullcontext
from typing import Optional

import pytest

from metldata.builtin_transformations.infer_references.path.path_elements import (
ReferencePathElement,
ReferencePathElementType,
from metldata.schemapack_.builtin_transformations.infer_relations.path.path_elements import (
RelationPathElement,
RelationPathElementType,
)
from metldata.builtin_transformations.infer_references.path.path_str import (
from metldata.schemapack_.builtin_transformations.infer_relations.path.path_str import (
ValidationError,
extract_first_element,
get_element_components,
Expand Down Expand Up @@ -188,34 +188,37 @@ def test_validate_string_element(string_element: str, is_valid: bool):
@pytest.mark.parametrize(
"string_element, expected_type",
[
("class_a(class_b)>class_b", ReferencePathElementType.ACTIVE),
("class_a<(class_a)class_b", ReferencePathElementType.PASSIVE),
("class_a(class_b)>class_b", RelationPathElementType.ACTIVE),
("class_a<(class_a)class_b", RelationPathElementType.PASSIVE),
],
)
def test_get_element_type(string_element: str, expected_type: ReferencePathElementType):
def test_get_element_type(string_element: str, expected_type: RelationPathElementType):
"""Test the get_element_type method."""

observed_type = get_element_type(string_element=string_element)
assert observed_type == expected_type


@pytest.mark.parametrize(
"string_element, expected_source, expected_slot, expected_target",
"string_element, expected_source, expected_property, expected_target",
[
("class_a(class_b)>class_b", "class_a", "class_b", "class_b"),
("class_a<(class_a)class_b", "class_a", "class_a", "class_b"),
],
)
def test_get_element_components(
string_element: str, expected_source: str, expected_slot: str, expected_target: str
string_element: str,
expected_source: str,
expected_property: str,
expected_target: str,
):
"""Test the get_element_components method."""

observed_source, observed_slot, observed_target = get_element_components(
observed_source, observed_property, observed_target = get_element_components(
string_element=string_element
)
assert observed_source == expected_source
assert observed_slot == expected_slot
assert observed_property == expected_property
assert observed_target == expected_target


Expand All @@ -224,26 +227,26 @@ def test_get_element_components(
[
(
"class_a(class_b)>class_b",
ReferencePathElement(
type_=ReferencePathElementType.ACTIVE,
RelationPathElement(
type_=RelationPathElementType.ACTIVE,
source="class_a",
slot="class_b",
property="class_b",
target="class_b",
),
),
(
"class_a<(class_a)class_b",
ReferencePathElement(
type_=ReferencePathElementType.PASSIVE,
RelationPathElement(
type_=RelationPathElementType.PASSIVE,
source="class_a",
slot="class_a",
property="class_a",
target="class_b",
),
),
],
)
def test_string_element_to_object(
string_element: str, expected_object: ReferencePathElement
string_element: str, expected_object: RelationPathElement
):
"""Test the string_element_to_object method."""

Expand All @@ -257,63 +260,63 @@ def test_string_element_to_object(
(
"class_a(class_b)>class_b",
[
ReferencePathElement(
type_=ReferencePathElementType.ACTIVE,
RelationPathElement(
type_=RelationPathElementType.ACTIVE,
source="class_a",
slot="class_b",
property="class_b",
target="class_b",
)
],
),
(
"class_a<(class_a)class_b",
[
ReferencePathElement(
type_=ReferencePathElementType.PASSIVE,
RelationPathElement(
type_=RelationPathElementType.PASSIVE,
source="class_a",
slot="class_a",
property="class_a",
target="class_b",
)
],
),
(
"class_a(class_b)>class_b(class_c)>class_c",
[
ReferencePathElement(
type_=ReferencePathElementType.ACTIVE,
RelationPathElement(
type_=RelationPathElementType.ACTIVE,
source="class_a",
slot="class_b",
property="class_b",
target="class_b",
),
ReferencePathElement(
type_=ReferencePathElementType.ACTIVE,
RelationPathElement(
type_=RelationPathElementType.ACTIVE,
source="class_b",
slot="class_c",
property="class_c",
target="class_c",
),
],
),
(
"class_a(class_b)>class_b<(class_b)class_c",
[
ReferencePathElement(
type_=ReferencePathElementType.ACTIVE,
RelationPathElement(
type_=RelationPathElementType.ACTIVE,
source="class_a",
slot="class_b",
property="class_b",
target="class_b",
),
ReferencePathElement(
type_=ReferencePathElementType.PASSIVE,
RelationPathElement(
type_=RelationPathElementType.PASSIVE,
source="class_b",
slot="class_b",
property="class_b",
target="class_c",
),
],
),
],
)
def test_path_str_to_object_elements(
path_str: str, expected_elements: ReferencePathElement
path_str: str, expected_elements: RelationPathElement
):
"""Test the path_str_to_object_elements method."""

Expand Down

0 comments on commit 74afe01

Please sign in to comment.