Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilize translate_field_to_slot() in appropriate places #6

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 16 additions & 32 deletions tests/test_gen_linkml.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,15 @@ def test_any_schema(self):
class Foo(BaseModel):
x: Any

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")
assert slot.range == "Any"

def test_none_schema(self):

class Foo(BaseModel):
x: None

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")
assert len(slot.notes) == 1
assert (
slot.notes[0] == f"{TRANSLATOR_PACKAGE}: LinkML does not have null values. "
Expand All @@ -204,8 +202,7 @@ def test_bool_schema(self):
class Foo(BaseModel):
x: bool

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")
assert slot.range == "boolean"

@pytest.mark.parametrize(
Expand All @@ -224,8 +221,7 @@ def test_int_schema(self, multiple_of, le, lt, ge, gt, expected_max, expected_mi
class Foo(BaseModel):
x: int = Field(..., multiple_of=multiple_of, le=le, ge=ge, lt=lt, gt=gt)

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")

assert slot.range == "integer"

Expand Down Expand Up @@ -256,8 +252,7 @@ class Foo(BaseModel):
gt=gt,
)

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")
verify_notes = partial(verify_str_lst, str_lst=slot.notes)

assert slot.range == "float"
Expand Down Expand Up @@ -298,8 +293,7 @@ class Foo(BaseModel):
decimal_places=decimal_places,
)

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")
verify_notes = partial(verify_str_lst, str_lst=slot.notes)

assert slot.range == "decimal"
Expand Down Expand Up @@ -361,8 +355,7 @@ class Foo(BaseModel):
),
]

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")
verify_notes = partial(verify_str_lst, str_lst=slot.notes)

assert slot.range == "string"
Expand Down Expand Up @@ -595,8 +588,7 @@ def test_literal_schema(
class Foo(BaseModel):
x: literal_specs

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")
verify_notes = partial(verify_str_lst, str_lst=slot.notes)

verify_notes(
Expand Down Expand Up @@ -659,8 +651,7 @@ def test_list_schema(
class Foo(BaseModel):
x: conlist(item_type, min_length=min_len, max_length=max_len)

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")

if is_item_type_list_type:
assert in_exactly_one_string("Translation is incomplete", slot.notes)
Expand All @@ -682,8 +673,7 @@ def test_dict_schema(self):
class Foo(BaseModel):
x: dict[int, str]

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")

assert in_exactly_one_string("`dict` types are yet to be supported", slot.notes)

Expand Down Expand Up @@ -726,8 +716,7 @@ def _test_function_schema(
class Foo(BaseModel):
x: Annotated[int, validator]

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")

assert in_exactly_one_string(
"Unable to translate the logic contained "
Expand Down Expand Up @@ -849,8 +838,7 @@ def test_nullable_schema(self, has_default):
class Foo(BaseModel):
x: Optional[int] = field_specs

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")
verify_notes = partial(verify_str_lst, str_lst=slot.notes)

verify_notes(
Expand Down Expand Up @@ -929,8 +917,7 @@ class Lizard(BaseModel):
class Foo(BaseModel):
pet: Union[Cat, Dog, Lizard] = Field(..., discriminator="pet_type")

field_schema = get_field_schema(Foo, "pet")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "pet")

assert in_exactly_one_string(
"Tagged union types are yet to be supported", slot.notes
Expand Down Expand Up @@ -966,8 +953,7 @@ class Bar(BaseModel):
class Foo(BaseModel):
x: Bar

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")

assert slot.range == "Bar"

Expand Down Expand Up @@ -1008,8 +994,7 @@ class Foo(BaseModel):
),
]

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")
verify_notes = partial(verify_str_lst, str_lst=slot.notes)

assert slot.range == "uri"
Expand Down Expand Up @@ -1043,8 +1028,7 @@ def test_uuid_schema(self, uuid_type, expected_pattern):
class Foo(BaseModel):
x: uuid_type

field_schema = get_field_schema(Foo, "x")
slot = SlotGenerator(field_schema).generate()
slot = translate_field_to_slot(Foo, "x")

assert slot.range == "string"
assert slot.pattern == expected_pattern