4
4
from random import choice # noqa
5
5
from unittest .mock import patch
6
6
7
+ from django .db import connection
7
8
from django .utils .timezone import now
8
9
9
10
import pytest
34
35
"blog" : "https://joe.example.com" ,
35
36
"days_since_last_login" : 4 ,
36
37
"birth_time" : now (),
38
+ "data" : {"one" : 1 },
37
39
}
38
40
person_recipe = Recipe (Person , ** recipe_attrs )
39
41
user_recipe = Recipe (User )
@@ -68,6 +70,8 @@ def test_flat_model_make_recipe_with_the_correct_attributes(self):
68
70
assert person .appointment == recipe_attrs ["appointment" ]
69
71
assert person .blog == recipe_attrs ["blog" ]
70
72
assert person .days_since_last_login == recipe_attrs ["days_since_last_login" ]
73
+ assert person .data is not recipe_attrs ["data" ]
74
+ assert person .data == recipe_attrs ["data" ]
71
75
assert person .id is not None
72
76
73
77
def test_flat_model_prepare_recipe_with_the_correct_attributes (self ):
@@ -80,6 +84,8 @@ def test_flat_model_prepare_recipe_with_the_correct_attributes(self):
80
84
assert person .appointment == recipe_attrs ["appointment" ]
81
85
assert person .blog == recipe_attrs ["blog" ]
82
86
assert person .days_since_last_login == recipe_attrs ["days_since_last_login" ]
87
+ assert person .data is not recipe_attrs ["data" ]
88
+ assert person .data == recipe_attrs ["data" ]
83
89
assert person .id is None
84
90
85
91
def test_accepts_callable (self ):
@@ -171,6 +177,34 @@ def test_defining_recipes_str(self):
171
177
except AttributeError as e :
172
178
pytest .fail (f"{ e } " )
173
179
180
+ def test_recipe_dict_attribute_isolation (self ):
181
+ person1 = person_recipe .make ()
182
+ person2 = person_recipe .make ()
183
+ person2 .data ["two" ] = 2
184
+ person3 = person_recipe .make ()
185
+
186
+ # Mutation on instances must have no side effect on their recipe definition,
187
+ # or on other instances of the same recipe.
188
+ assert person1 .data == {"one" : 1 }
189
+ assert person2 .data == {"one" : 1 , "two" : 2 }
190
+ assert person3 .data == {"one" : 1 }
191
+
192
+ @pytest .mark .skipif (
193
+ connection .vendor != "postgresql" , reason = "PostgreSQL specific tests"
194
+ )
195
+ def test_recipe_list_attribute_isolation (self ):
196
+ pg_person_recipe = person_recipe .extend (acquaintances = [1 , 2 , 3 ])
197
+ person1 = pg_person_recipe .make ()
198
+ person2 = pg_person_recipe .make ()
199
+ person2 .acquaintances .append (4 )
200
+ person3 = pg_person_recipe .make ()
201
+
202
+ # Mutation on instances must have no side effect on their recipe definition,
203
+ # or on other instances of the same recipe.
204
+ assert person1 .acquaintances == [1 , 2 , 3 ]
205
+ assert person2 .acquaintances == [1 , 2 , 3 , 4 ]
206
+ assert person3 .acquaintances == [1 , 2 , 3 ]
207
+
174
208
175
209
@pytest .mark .django_db
176
210
class TestExecutingRecipes :
0 commit comments