|
8 | 8 | from ..fake_model import get_fake_model |
9 | 9 |
|
10 | 10 |
|
11 | | -@pytest.mark.django_db() |
12 | 11 | @pytest.mark.benchmark() |
13 | | -class TestUpsert(TestCase): |
| 12 | +def test_upsert_traditional(benchmark): |
| 13 | + model = get_fake_model({ |
| 14 | + 'field': models.CharField(max_length=255, unique=True) |
| 15 | + }) |
14 | 16 |
|
15 | | - @pytest.mark.benchmark() |
16 | | - @staticmethod |
17 | | - def test_upsert_traditional(benchmark): |
18 | | - model = get_fake_model({ |
19 | | - 'field': models.CharField(max_length=255, unique=True) |
20 | | - }) |
| 17 | + random_value = str(uuid.uuid4())[:8] |
| 18 | + model.objects.create(field=random_value) |
21 | 19 |
|
22 | | - random_value = str(uuid.uuid4())[:8] |
23 | | - model.objects.create(field=random_value) |
| 20 | + def _traditional_upsert(model, random_value): |
| 21 | + """Performs a concurrency safe upsert |
| 22 | + the traditional way.""" |
24 | 23 |
|
25 | | - def _traditional_upsert(model, random_value): |
26 | | - """Performs a concurrency safe upsert |
27 | | - the traditional way.""" |
| 24 | + try: |
28 | 25 |
|
29 | | - try: |
| 26 | + with transaction.atomic(): |
| 27 | + return model.objects.create(field=random_value) |
| 28 | + except IntegrityError: |
| 29 | + model.objects.update(field=random_value) |
| 30 | + return model.objects.get(field=random_value) |
30 | 31 |
|
31 | | - with transaction.atomic(): |
32 | | - return model.objects.create(field=random_value) |
33 | | - except IntegrityError: |
34 | | - model.objects.update(field=random_value) |
35 | | - return model.objects.get(field=random_value) |
| 32 | + benchmark(_traditional_upsert, model, random_value) |
36 | 33 |
|
37 | | - benchmark(_traditional_upsert, model, random_value) |
38 | 34 |
|
39 | | - @pytest.mark.benchmark() |
40 | | - @staticmethod |
41 | | - def test_upsert_native(benchmark): |
42 | | - model = get_fake_model({ |
43 | | - 'field': models.CharField(max_length=255, unique=True) |
44 | | - }) |
| 35 | +@pytest.mark.benchmark() |
| 36 | +def test_upsert_native(benchmark): |
| 37 | + model = get_fake_model({ |
| 38 | + 'field': models.CharField(max_length=255, unique=True) |
| 39 | + }) |
45 | 40 |
|
46 | | - random_value = str(uuid.uuid4())[:8] |
47 | | - model.objects.create(field=random_value) |
| 41 | + random_value = str(uuid.uuid4())[:8] |
| 42 | + model.objects.create(field=random_value) |
48 | 43 |
|
49 | | - def _native_upsert(model, random_value): |
50 | | - """Performs a concurrency safe upsert |
51 | | - using the native PostgreSQL upsert.""" |
| 44 | + def _native_upsert(model, random_value): |
| 45 | + """Performs a concurrency safe upsert |
| 46 | + using the native PostgreSQL upsert.""" |
52 | 47 |
|
53 | | - return model.objects.upsert_and_get( |
54 | | - conflict_target=['field'], |
55 | | - fields=dict(field=random_value) |
56 | | - ) |
| 48 | + return model.objects.upsert_and_get( |
| 49 | + conflict_target=['field'], |
| 50 | + fields=dict(field=random_value) |
| 51 | + ) |
57 | 52 |
|
58 | | - benchmark(_native_upsert, model, random_value) |
| 53 | + benchmark(_native_upsert, model, random_value) |
0 commit comments