From eee7de9868f8e6a99961c2bd1e72d75fdc5f2ccd Mon Sep 17 00:00:00 2001 From: Isaac To Date: Sat, 9 Nov 2024 18:20:26 -0800 Subject: [PATCH] rf: utilize `translate_field_to_slot()` in appropriate places --- tests/test_gen_linkml.py | 48 ++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/tests/test_gen_linkml.py b/tests/test_gen_linkml.py index 4c13f0ba..66ec4709 100644 --- a/tests/test_gen_linkml.py +++ b/tests/test_gen_linkml.py @@ -182,8 +182,7 @@ 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): @@ -191,8 +190,7 @@ 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. " @@ -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( @@ -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" @@ -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" @@ -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" @@ -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" @@ -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( @@ -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) @@ -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) @@ -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 " @@ -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( @@ -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 @@ -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" @@ -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" @@ -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