77
77
Restaurant ,
78
78
Street ,
79
79
TestHistoricParticipanToHistoricOrganization ,
80
+ TestOrganizationWithHistory ,
80
81
TestParticipantToHistoricOrganization ,
81
82
TrackedAbstractBaseA ,
82
83
TrackedConcreteBase ,
@@ -103,24 +104,37 @@ def test_update_change_reason_with_excluded_fields(self):
103
104
class HistoryTrackedModelTestInfo :
104
105
model : Type [Model ]
105
106
history_manager_name : Optional [str ]
107
+ init_kwargs : dict
106
108
107
109
def __init__ (
108
110
self ,
109
111
model : Type [Model ],
110
112
history_manager_name : Optional [str ] = "history" ,
113
+ ** init_kwargs ,
111
114
):
112
115
self .model = model
113
116
self .history_manager_name = history_manager_name
117
+ self .init_kwargs = init_kwargs
114
118
115
119
116
120
class GetHistoryManagerAndModelHelpersTestCase (TestCase ):
117
121
@classmethod
118
122
def setUpClass (cls ):
119
123
super ().setUpClass ()
120
124
125
+ user = User .objects .create (username = "user" )
126
+ poll_kwargs = {"pub_date" : timezone .now ()}
127
+ poll = Poll .objects .create (** poll_kwargs )
128
+ choice_kwargs = {"poll" : poll , "votes" : 0 }
129
+ choice = Choice .objects .create (** choice_kwargs )
130
+ place = Place .objects .create ()
131
+ org_kwarg = {
132
+ "organization" : TestOrganizationWithHistory .objects .create (),
133
+ }
134
+
121
135
H = HistoryTrackedModelTestInfo
122
136
cls .history_tracked_models = [
123
- H (Choice ),
137
+ H (Choice , ** choice_kwargs ),
124
138
H (ConcreteAttr ),
125
139
H (ConcreteExternal ),
126
140
H (ConcreteUtil ),
@@ -135,23 +149,23 @@ def setUpClass(cls):
135
149
H (OverrideModelNameAsCallable ),
136
150
H (OverrideModelNameRegisterMethod1 ),
137
151
H (OverrideModelNameUsingBaseModel1 ),
138
- H (Poll ),
139
- H (PollChildBookWithManyToMany ),
140
- H (PollWithAlternativeManager ),
141
- H (PollWithCustomManager ),
142
- H (PollWithExcludedFKField ),
152
+ H (Poll , ** poll_kwargs ),
153
+ H (PollChildBookWithManyToMany , ** poll_kwargs ),
154
+ H (PollWithAlternativeManager , ** poll_kwargs ),
155
+ H (PollWithCustomManager , ** poll_kwargs ),
156
+ H (PollWithExcludedFKField , place = place , ** poll_kwargs ),
143
157
H (PollWithHistoricalSessionAttr ),
144
- H (PollWithManyToMany ),
145
- H (PollWithManyToManyCustomHistoryID ),
146
- H (PollWithManyToManyWithIPAddress ),
147
- H (PollWithQuerySetCustomizations ),
158
+ H (PollWithManyToMany , ** poll_kwargs ),
159
+ H (PollWithManyToManyCustomHistoryID , ** poll_kwargs ),
160
+ H (PollWithManyToManyWithIPAddress , ** poll_kwargs ),
161
+ H (PollWithQuerySetCustomizations , ** poll_kwargs ),
148
162
H (PollWithSelfManyToMany ),
149
- H (Restaurant , "updates" ),
150
- H (TestHistoricParticipanToHistoricOrganization ),
163
+ H (Restaurant , "updates" , rating = 0 ),
164
+ H (TestHistoricParticipanToHistoricOrganization , ** org_kwarg ),
151
165
H (TrackedConcreteBase ),
152
166
H (TrackedWithAbstractBase ),
153
167
H (TrackedWithConcreteBase ),
154
- H (Voter ),
168
+ H (Voter , user = user , choice = choice ),
155
169
H (external .ExternalModel ),
156
170
H (external .ExternalModelRegistered , "histories" ),
157
171
H (external .Poll ),
@@ -161,11 +175,11 @@ def setUpClass(cls):
161
175
H (AbstractModelCallable1 , None ),
162
176
H (BaseModel , None ),
163
177
H (FirstLevelInheritedModel , None ),
164
- H (HardbackBook , None ),
178
+ H (HardbackBook , None , isbn = "123" , price = 0 ),
165
179
H (Place , None ),
166
- H (PollParentWithManyToMany , None ),
167
- H (Profile , None ),
168
- H (TestParticipantToHistoricOrganization , None ),
180
+ H (PollParentWithManyToMany , None , ** poll_kwargs ),
181
+ H (Profile , None , date_of_birth = timezone . now (). date () ),
182
+ H (TestParticipantToHistoricOrganization , None , ** org_kwarg ),
169
183
H (TrackedAbstractBaseA , None ),
170
184
]
171
185
@@ -191,12 +205,25 @@ def assert_history_manager(history_manager, info: HistoryTrackedModelTestInfo):
191
205
manager = get_history_manager_for_model (model )
192
206
assert_history_manager (manager , model_info )
193
207
208
+ # Passing a model instance should also work
209
+ instance = model (** model_info .init_kwargs )
210
+ instance .save ()
211
+ manager = get_history_manager_for_model (instance )
212
+ assert_history_manager (manager , model_info )
213
+
194
214
for model_info in self .models_without_history_manager :
195
215
with self .subTest (model_info = model_info ):
196
216
model = model_info .model
197
217
with self .assertRaises (NotHistoricalModelError ):
198
218
get_history_manager_for_model (model )
199
219
220
+ # The same error should be raised if passing a model instance
221
+ if not model ._meta .abstract :
222
+ instance = model (** model_info .init_kwargs )
223
+ instance .save ()
224
+ with self .assertRaises (NotHistoricalModelError ):
225
+ get_history_manager_for_model (instance )
226
+
200
227
def test__get_history_model_for_model (self ):
201
228
"""Test that ``get_history_model_for_model()`` returns the expected value
202
229
for various models."""
@@ -207,12 +234,25 @@ def test__get_history_model_for_model(self):
207
234
self .assertTrue (issubclass (historical_model , HistoricalChanges ))
208
235
self .assertEqual (historical_model .instance_type , model )
209
236
237
+ # Passing a model instance should also work
238
+ instance = model (** model_info .init_kwargs )
239
+ instance .save ()
240
+ historical_model_from_instance = get_history_model_for_model (instance )
241
+ self .assertEqual (historical_model_from_instance , historical_model )
242
+
210
243
for model_info in self .models_without_history_manager :
211
244
with self .subTest (model_info = model_info ):
212
245
model = model_info .model
213
246
with self .assertRaises (NotHistoricalModelError ):
214
247
get_history_model_for_model (model )
215
248
249
+ # The same error should be raised if passing a model instance
250
+ if not model ._meta .abstract :
251
+ instance = model (** model_info .init_kwargs )
252
+ instance .save ()
253
+ with self .assertRaises (NotHistoricalModelError ):
254
+ get_history_model_for_model (instance )
255
+
216
256
def test__get_pk_name (self ):
217
257
"""Test that ``get_pk_name()`` returns the expected value for various models."""
218
258
self .assertEqual (get_pk_name (Poll ), "id" )
0 commit comments