Skip to content

Commit af7a9ea

Browse files
committed
TST: Fix race condition from transaction handler
1 parent cc67a25 commit af7a9ea

File tree

1 file changed

+20
-32
lines changed

1 file changed

+20
-32
lines changed

tests/test_storage_fields.py

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,12 @@ def test_change_view_loads_normally(self):
132132
)
133133

134134

135-
class TestBlobField(StorageOperationsMixin, TransactionTestCase):
135+
class TestBlobField(StorageOperationsMixin, TestCase):
136136
"""Inherits from transaction test case, because we use an on_commit
137137
hook to move ingressed files once a database save has been made
138138
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-
```
146139
"""
147140

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-
158141
def test_validates_name_is_present_on_add(self):
159142
"""Ensure that a ValidationError is raised if no 'name' property is present in the blob data"""
160143

@@ -220,27 +203,32 @@ def test_validates_raises_on_add_blank_none(self):
220203
["This field is required."],
221204
)
222205

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-
234206
@override_settings(GCP_STORAGE_OVERRIDE_BLOBFIELD_VALUE=True)
235207
def test_create_object_succeeds_with_overridden_path(self):
236208
"""Through the ORM, we may need to create blobs directly at the destination"""
237209

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)
241217
count = ExampleBlobFieldModel.objects.count()
242218
self.assertEqual(count, 1)
243219

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+
244232

245233
class TestBlankBlobField(BlobModelFactoryMixin, StorageOperationsMixin, TransactionTestCase):
246234
"""Inherits from transaction test case, because we use an on_commit

0 commit comments

Comments
 (0)