@@ -132,29 +132,12 @@ def test_change_view_loads_normally(self):
132
132
)
133
133
134
134
135
- class TestBlobField (StorageOperationsMixin , TransactionTestCase ):
135
+ class TestBlobField (StorageOperationsMixin , TestCase ):
136
136
"""Inherits from transaction test case, because we use an on_commit
137
137
hook to move ingressed files once a database save has been made
138
138
139
- TODO REMOVE TRANSACTION TEST CASE
140
- as per https://code.djangoproject.com/ticket/30457
141
- ```
142
- with self.captureOnCommitCallbacks(execute=True) as callbacks:
143
- with transaction.atomic():
144
- transaction.on_commit(branch_1)
145
- ```
146
139
"""
147
140
148
- def test_validates_blob_exists_on_add (self ):
149
- """Ensure that a ValidationError is raised if the blob at _tmp_path does not exist"""
150
-
151
- form = BlobForm (data = {"blob" : {"name" : "thing" , "_tmp_path" : "something" }})
152
- form .is_valid ()
153
- self .assertEqual (
154
- form .errors ["blob" ],
155
- ["Upload incomplete or failed (no blob at 'something' in bucket 'example-media-assets')" ],
156
- )
157
-
158
141
def test_validates_name_is_present_on_add (self ):
159
142
"""Ensure that a ValidationError is raised if no 'name' property is present in the blob data"""
160
143
@@ -220,27 +203,32 @@ def test_validates_raises_on_add_blank_none(self):
220
203
["This field is required." ],
221
204
)
222
205
223
- def test_create_object_fails_with_missing_blob (self ):
224
- """Create an object but fail to copy the blob (because it's missing) then check that
225
- no database record was created"""
226
- with self .assertRaises (MissingBlobError ):
227
- obj = ExampleBlobFieldModel .objects .create (
228
- blob = {"_tmp_path" : f"_tmp/{ uuid4 ()} .txt" , "name" : "missing_blob.txt" }
229
- )
230
- obj .save ()
231
- count = ExampleBlobFieldModel .objects .count ()
232
- self .assertEqual (count , 0 )
233
-
234
206
@override_settings (GCP_STORAGE_OVERRIDE_BLOBFIELD_VALUE = True )
235
207
def test_create_object_succeeds_with_overridden_path (self ):
236
208
"""Through the ORM, we may need to create blobs directly at the destination"""
237
209
238
- blob_name = self ._prefix_blob_name ("create_object_succeeds_with_overridden_path.txt" )
239
- self ._create_test_blob (self .bucket , blob_name )
240
- ExampleBlobFieldModel .objects .create (blob = {"path" : blob_name })
210
+ with self .captureOnCommitCallbacks (execute = False ) as callbacks :
211
+ blob_name = self ._prefix_blob_name ("create_object_succeeds_with_overridden_path.txt" )
212
+ self ._create_test_blob (self .bucket , blob_name )
213
+ ExampleBlobFieldModel .objects .create (blob = {"path" : blob_name })
214
+
215
+ # There should be no callback executed when the blob field value is overwritten directly
216
+ self .assertEqual (len (callbacks ), 0 )
241
217
count = ExampleBlobFieldModel .objects .count ()
242
218
self .assertEqual (count , 1 )
243
219
220
+ def test_create_object_fails_with_missing_blob (self ):
221
+ """Create an object but fail to copy the blob (because it's missing) then check that
222
+ no database record was created"""
223
+ with self .assertRaises (MissingBlobError ):
224
+ with self .captureOnCommitCallbacks (execute = True ) as callbacks :
225
+ ExampleBlobFieldModel .objects .create (
226
+ blob = {"_tmp_path" : f"_tmp/{ uuid4 ()} .txt" , "name" : "missing_blob.txt" }
227
+ )
228
+ self .assertEqual (len (callbacks ), 1 )
229
+ count = ExampleBlobFieldModel .objects .count ()
230
+ self .assertEqual (count , 0 )
231
+
244
232
245
233
class TestBlankBlobField (BlobModelFactoryMixin , StorageOperationsMixin , TransactionTestCase ):
246
234
"""Inherits from transaction test case, because we use an on_commit
0 commit comments