1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
import pytest
from django.db import models
from psqlextra.fields import HStoreField
from psqlextra.query import ConflictAction
from .fake_model import get_fake_model
def test_on_conflict_nothing():
"""Tests whether simple insert NOTHING works correctly."""
model = get_fake_model(
{
"title": HStoreField(uniqueness=["key1"]),
"cookies": models.CharField(max_length=255, null=True),
}
)
# row does not conflict, new row should be created
obj1 = model.objects.on_conflict(
[("title", "key1")], ConflictAction.NOTHING
).insert_and_get(title={"key1": "beer"}, cookies="cheers")
obj1.refresh_from_db()
assert obj1.title["key1"] == "beer"
assert obj1.cookies == "cheers"
# row conflicts, no new row should be created
obj2 = model.objects.on_conflict(
[("title", "key1")], ConflictAction.NOTHING
).insert_and_get(title={"key1": "beer"}, cookies="choco")
assert not obj2
# assert that the 'cookies' field didn't change
obj1.refresh_from_db()
assert obj1.title["key1"] == "beer"
assert obj1.cookies == "cheers"
assert model.objects.count() == 1
def test_on_conflict_nothing_foreign_primary_key():
"""Tests whether simple insert NOTHING works correctly when the primary key
of a field is a foreign key with a custom name."""
referenced_model = get_fake_model({})
model = get_fake_model(
{
"parent": models.OneToOneField(
referenced_model, primary_key=True, on_delete=models.CASCADE
),
"cookies": models.CharField(max_length=255),
}
)
referenced_obj = referenced_model.objects.create()
# row does not conflict, new row should be created
obj1 = model.objects.on_conflict(
["parent_id"], ConflictAction.NOTHING
).insert_and_get(parent_id=referenced_obj.pk, cookies="cheers")
obj1.refresh_from_db()
assert obj1.parent == referenced_obj
assert obj1.cookies == "cheers"
# row conflicts, no new row should be created
obj2 = model.objects.on_conflict(
["parent_id"], ConflictAction.NOTHING
).insert_and_get(parent_id=referenced_obj.pk, cookies="choco")
assert not obj2
obj1.refresh_from_db()
assert obj1.cookies == "cheers"
assert model.objects.count() == 1
def test_on_conflict_nothing_foreign_key_by_object():
"""Tests whether simple insert NOTHING works correctly when the potentially
conflicting field is a foreign key specified as an object."""
other_model = get_fake_model({})
model = get_fake_model(
{
"other": models.OneToOneField(
other_model, on_delete=models.CASCADE
),
"data": models.CharField(max_length=255),
}
)
other_obj = other_model.objects.create()
# row does not conflict, new row should be created
obj1 = model.objects.on_conflict(
["other"], ConflictAction.NOTHING
).insert_and_get(other=other_obj, data="some data")
assert obj1.other == other_obj
assert obj1.data == "some data"
obj1.refresh_from_db()
assert obj1.other == other_obj
assert obj1.data == "some data"
with pytest.raises(ValueError):
(
model.objects.on_conflict(
["other"], ConflictAction.NOTHING
).insert_and_get(other=obj1)
)
# row conflicts, no new row should be created
obj2 = model.objects.on_conflict(
["other"], ConflictAction.NOTHING
).insert_and_get(other=other_obj, data="different data")
assert not obj2
obj1.refresh_from_db()
assert model.objects.count() == 1
assert obj1.other == other_obj
assert obj1.data == "some data"
def test_on_conflict_nothing_foreign_key_by_id():
"""Tests whether simple insert NOTHING works correctly when the potentially
conflicting field is a foreign key specified as an id."""
other_model = get_fake_model({})
model = get_fake_model(
{
"other": models.OneToOneField(
other_model, on_delete=models.CASCADE
),
"data": models.CharField(max_length=255),
}
)
other_obj = other_model.objects.create()
# row does not conflict, new row should be created
obj1 = model.objects.on_conflict(
["other_id"], ConflictAction.NOTHING
).insert_and_get(other_id=other_obj.pk, data="some data")
assert obj1.other == other_obj
assert obj1.data == "some data"
obj1.refresh_from_db()
assert obj1.other == other_obj
assert obj1.data == "some data"
# row conflicts, no new row should be created
obj2 = model.objects.on_conflict(
["other_id"], ConflictAction.NOTHING
).insert_and_get(other_id=other_obj.pk, data="different data")
assert not obj2
assert model.objects.count() == 1
obj1.refresh_from_db()
assert obj1.other == other_obj
assert obj1.data == "some data"
def test_on_conflict_nothing_duplicate_rows():
"""Tests whether duplicate rows are filtered out when doing a insert
NOTHING and no error is raised when the list of rows contains
duplicates."""
model = get_fake_model({"amount": models.IntegerField(unique=True)})
rows = [dict(amount=1), dict(amount=1)]
(
model.objects.on_conflict(
["amount"], ConflictAction.NOTHING
).bulk_insert(rows)
)
|