1+ from unittest import IsolatedAsyncioTestCase
2+
13from fastapi_startkit .masoniteorm .collection import Collection
24from fastapi_startkit .masoniteorm .models .model import Model
35
4- from ..fixtures .model import User
6+ from ..fixtures .model import Articles , Profile , User
57from ..sqlite .test_case import TestCase
68
79
@@ -17,6 +19,54 @@ async def test_serialize_with_on_the_fly_appends(self):
1719 self .assertTrue (isinstance (serialized , list ))
1820 self .assertTrue (len (serialized ) > 0 )
1921
22+ async def test_load_has_one_relationship (self ):
23+ users = await User .all ()
24+
25+ result = await users .load ("profile" )
26+
27+ self .assertIs (result , users )
28+ admin = users .where ("email" , "admin@admin.com" ).first ()
29+ self .assertIn ("profile" , admin ._relationships )
30+ self .assertIsInstance (admin .profile , Profile )
31+
32+ async def test_load_has_many_relationship (self ):
33+ users = await User .all ()
34+
35+ result = await users .load ("articles" )
36+
37+ self .assertIs (result , users )
38+ admin = users .where ("email" , "admin@admin.com" ).first ()
39+ self .assertIn ("articles" , admin ._relationships )
40+ self .assertEqual (len (admin .articles ), 1 )
41+ self .assertIsInstance (admin .articles [0 ], Articles )
42+
43+ async def test_load_multiple_relationships_at_once (self ):
44+ users = await User .all ()
45+
46+ await users .load ("profile" , "articles" )
47+
48+ admin = users .where ("email" , "admin@admin.com" ).first ()
49+ self .assertIn ("profile" , admin ._relationships )
50+ self .assertIn ("articles" , admin ._relationships )
51+
52+ async def test_load_without_matching_related_records (self ):
53+ # Jane (id 2) has no articles, so get_related returns an empty
54+ # collection and no relation is registered.
55+ users = await User .where ("id" , 2 ).get ()
56+
57+ result = await users .load ("articles" )
58+
59+ self .assertIs (result , users )
60+ self .assertNotIn ("articles" , users .first ()._relationships )
61+
62+ async def test_load_on_empty_collection_returns_self (self ):
63+ users = await User .where ("id" , 9999 ).get ()
64+ self .assertTrue (users .is_empty ())
65+
66+ result = await users .load ("profile" )
67+
68+ self .assertIs (result , users )
69+
2070 def test_take (self ):
2171 collection = Collection ([1 , 2 , 3 , 4 ])
2272 self .assertEqual (collection .take (2 ), [1 , 2 ])
@@ -694,3 +744,43 @@ def test_eq(self):
694744 self .assertTrue (collection == other )
695745 different = Collection ([1 , 2 , 3 ])
696746 self .assertFalse (collection == different )
747+
748+
749+ class _StubRelationship :
750+ """Relationship whose get_related returns a non-Collection result."""
751+
752+ async def get_related (self , query , relation ):
753+ return {"id" : 99 }
754+
755+ def map_related (self , related_result ):
756+ return related_result
757+
758+
759+ class _StubModel :
760+ stub = _StubRelationship ()
761+
762+ def __init__ (self ):
763+ self .relations = {}
764+
765+ def add_relation (self , data ):
766+ self .relations .update (data )
767+
768+
769+ class TestCollectionLoad (IsolatedAsyncioTestCase ):
770+ """Covers the load() paths that don't require a database."""
771+
772+ async def test_load_on_empty_collection_is_noop (self ):
773+ collection = Collection ([])
774+ self .assertIs (await collection .load ("stub" ), collection )
775+
776+ async def test_load_registers_non_collection_result_via_add_relation (self ):
777+ collection = Collection ([_StubModel (), _StubModel ()])
778+
779+ result = await collection .load ("stub" )
780+
781+ self .assertIs (result , collection )
782+ for model in collection :
783+ self .assertEqual (model .relations ["stub" ], {"id" : 99 })
784+
785+ def test_with_relationship_autoloading_is_noop (self ):
786+ self .assertIsNone (Collection ([]).with_relationship_autoloading ())
0 commit comments