11from tests .base import DBTestCase
2- from tests .example_app .tables import Manager
2+ from tests .example_app .tables import Band , Manager
33
44
55class TestToDict (DBTestCase ):
@@ -11,7 +11,26 @@ def test_to_dict(self):
1111
1212 instance = Manager .objects ().first ().run_sync ()
1313 dictionary = instance .to_dict ()
14- self .assertEqual (dictionary , {"id" : 1 , "name" : "Guido" })
14+ self .assertDictEqual (dictionary , {"id" : 1 , "name" : "Guido" })
15+
16+ def test_nested (self ):
17+ """
18+ Make sure that `to_dict` works correctly, when the object contains
19+ nested objects.
20+ """
21+ self .insert_row ()
22+
23+ instance = Band .objects (Band .manager ).first ().run_sync ()
24+ dictionary = instance .to_dict ()
25+ self .assertDictEqual (
26+ dictionary ,
27+ {
28+ "id" : 1 ,
29+ "name" : "Pythonistas" ,
30+ "manager" : {"id" : 1 , "name" : "Guido" },
31+ "popularity" : 1000 ,
32+ },
33+ )
1534
1635 def test_filter_rows (self ):
1736 """
@@ -21,9 +40,26 @@ def test_filter_rows(self):
2140
2241 instance = Manager .objects ().first ().run_sync ()
2342 dictionary = instance .to_dict (Manager .name )
24- self .assertEqual (dictionary , {"name" : "Guido" })
43+ self .assertDictEqual (dictionary , {"name" : "Guido" })
44+
45+ def test_nested_filter (self ):
46+ """
47+ Make sure that `to_dict` works correctly with nested objects and
48+ filtering.
49+ """
50+ self .insert_row ()
51+
52+ instance = Band .objects (Band .manager ).first ().run_sync ()
53+ dictionary = instance .to_dict (Band .name , Band .manager .id )
54+ self .assertDictEqual (
55+ dictionary ,
56+ {
57+ "name" : "Pythonistas" ,
58+ "manager" : {"id" : 1 },
59+ },
60+ )
2561
26- def test_to_dict_aliases (self ):
62+ def test_aliases (self ):
2763 """
2864 Make sure that `to_dict` works correctly with aliases.
2965 """
@@ -33,4 +69,4 @@ def test_to_dict_aliases(self):
3369 dictionary = instance .to_dict (
3470 Manager .id , Manager .name .as_alias ("title" )
3571 )
36- self .assertEqual (dictionary , {"id" : 1 , "title" : "Guido" })
72+ self .assertDictEqual (dictionary , {"id" : 1 , "title" : "Guido" })
0 commit comments