|
4 | 4 | from django.db import connection, models
|
5 | 5 | from django.db.models import F, Q
|
6 | 6 | from django.db.models.expressions import CombinedExpression, Value
|
| 7 | +from django.test.utils import CaptureQueriesContext |
7 | 8 |
|
8 | 9 | from psqlextra.expressions import ExcludedCol
|
9 | 10 | from psqlextra.fields import HStoreField
|
@@ -144,6 +145,35 @@ def test_upsert_with_update_condition():
|
144 | 145 | assert obj1.active
|
145 | 146 |
|
146 | 147 |
|
| 148 | +@pytest.mark.parametrize("update_condition_value", [0, False]) |
| 149 | +def test_upsert_with_update_condition_false(update_condition_value): |
| 150 | + """Tests that an expression can be used as an upsert update condition.""" |
| 151 | + |
| 152 | + model = get_fake_model( |
| 153 | + { |
| 154 | + "name": models.TextField(unique=True), |
| 155 | + "priority": models.IntegerField(), |
| 156 | + "active": models.BooleanField(), |
| 157 | + } |
| 158 | + ) |
| 159 | + |
| 160 | + obj1 = model.objects.create(name="joe", priority=1, active=False) |
| 161 | + |
| 162 | + with CaptureQueriesContext(connection) as ctx: |
| 163 | + upsert_result = model.objects.upsert( |
| 164 | + conflict_target=["name"], |
| 165 | + update_condition=update_condition_value, |
| 166 | + fields=dict(name="joe", priority=2, active=True), |
| 167 | + ) |
| 168 | + assert upsert_result is None |
| 169 | + assert len(ctx) == 1 |
| 170 | + assert 'ON CONFLICT ("name") DO NOTHING' in ctx[0]["sql"] |
| 171 | + |
| 172 | + obj1.refresh_from_db() |
| 173 | + assert obj1.priority == 1 |
| 174 | + assert not obj1.active |
| 175 | + |
| 176 | + |
147 | 177 | def test_upsert_with_update_values():
|
148 | 178 | """Tests that the default update values can be overriden with custom
|
149 | 179 | expressions."""
|
|
0 commit comments