🎨 add get_versionless_id() helper (and test)

This commit is contained in:
Ondřej Samohel 2024-11-08 09:46:47 +01:00
parent b9e430e2a5
commit db5d997ce7
No known key found for this signature in database
GPG key ID: 02376E18990A97C6
2 changed files with 33 additions and 0 deletions

View file

@ -94,6 +94,16 @@ class TraitBase(ABC, BaseModel):
match = re.search(version_regex, str(cls.id))
return int(match[1]) if match else None
@classmethod
def get_versionless_id(cls) -> str:
"""Get trait ID without version.
Returns:
str: Trait ID without version.
"""
return re.sub(r"\.v\d+$", "", str(cls.id))
class Representation:
"""Representation of products.
@ -416,6 +426,9 @@ class Representation:
bool: True if the representations are equal, False otherwise.
"""
if self.representation_id != other.representation_id:
return False
if not isinstance(other, Representation):
return False

View file

@ -252,6 +252,26 @@ def test_get_version_from_id() -> None:
assert TestMimeType(mime_type="foo/bar").get_version() is None
def test_get_versionless_id() -> None:
"""Test getting versionless trait ID."""
assert Image().get_versionless_id() == "ayon.2d.Image"
class TestOverscan(Overscan):
id = "ayon.2d.Overscan.v2"
assert TestOverscan(
left=0,
right=0,
top=0,
bottom=0
).get_versionless_id() == "ayon.2d.Overscan"
class TestMimeType(MimeType):
id = "ayon.content.MimeType"
assert TestMimeType(mime_type="foo/bar").get_versionless_id() == \
"ayon.content.MimeType"
def test_from_dict() -> None:
"""Test creating representation from dictionary."""