@@ -34,10 +34,14 @@ class ResourceTestUtils[
3434 delegating directly to the base class if not changing default behavior.
3535
3636 Type Parameters:
37- ResourceEntity: The SQLAlchemy entity class that implements EntityProtocl.
38- ResourceData: The Pydantic model representing the resource's data object used to create entities.
39- OtherModels: Additional Pydantic models that may be used in assertions,
40- represented by a union if multiple models are applicable. Primarily used for typing in the assert_matches method.
37+ - ResourceEntity: The SQLAlchemy entity class that implements EntityBase.
38+ - Note: the default implementation expects `from_model`
39+ and `to_model` methods to be present. However, these methods are not required in the type definition to allow for flexibility,
40+ expecting a subclass to override `next_entity` and/or `entity_to_dict` as needed if these methods are not present.
41+ - ResourceData: The Pydantic model representing the resource's data object used to create entities.
42+ - OtherModels: Additional Pydantic models that may be used in assertions
43+ - Represented by a union if multiple models are applicable.
44+ - Primarily used for typing in the assert_matches method.
4145
4246 Attributes:
4347 session (AsyncSession): The SQLAlchemy async database session.
@@ -163,7 +167,13 @@ async def next_entity(self, **overrides: Any) -> ResourceEntity:
163167 **overrides: Fields to override in the generated entity.
164168 """
165169 data = await self .next_data (** overrides )
166- return self ._ResourceEntity .from_model (data )
170+ if not hasattr (self ._ResourceEntity , "from_model" ) or not callable (
171+ getattr (self ._ResourceEntity , "from_model" )
172+ ):
173+ raise AttributeError (
174+ f"{ self ._ResourceEntity .__name__ } must implement a 'from_model' classmethod"
175+ )
176+ return self ._ResourceEntity .from_model (data ) # type: ignore
167177
168178 async def create_many (self , * , i : int , ** overrides : Any ) -> list [ResourceEntity ]:
169179 """Create multiple resource entities in the database, applying any overrides to every entity.
@@ -196,7 +206,13 @@ async def get_all(self) -> list[ResourceEntity]:
196206
197207 def entity_to_dict (self , entity : ResourceEntity ) -> dict :
198208 """Convert a resource entity to a dict via its model representation."""
199- return entity .to_model ().model_dump ()
209+ if not hasattr (self ._ResourceEntity , "to_model" ) or not callable (
210+ getattr (self ._ResourceEntity , "to_model" )
211+ ):
212+ raise AttributeError (
213+ f"{ self ._ResourceEntity .__name__ } must implement a 'to_model' method"
214+ )
215+ return entity .to_model ().model_dump () # type: ignore
200216
201217 def assert_matches (
202218 self ,
0 commit comments