|
| 1 | +import django |
1 | 2 | import pytest
|
2 | 3 |
|
3 | 4 | from django.core.exceptions import SuspiciousOperation
|
4 | 5 | from django.db import connection, models
|
| 6 | +from django.utils import timezone |
5 | 7 |
|
6 | 8 | from psqlextra.fields import HStoreField
|
7 | 9 | from psqlextra.models import PostgresModel
|
@@ -412,3 +414,39 @@ def test_bulk_return_models(conflict_action):
|
412 | 414 | for index, obj in enumerate(objs, 1):
|
413 | 415 | assert isinstance(obj, model)
|
414 | 416 | assert obj.id == index
|
| 417 | + |
| 418 | + |
| 419 | +@pytest.mark.skipif( |
| 420 | + django.VERSION < (3, 1), |
| 421 | + reason="Django < 3.1 doesn't implement JSONField", |
| 422 | +) |
| 423 | +@pytest.mark.parametrize("conflict_action", ConflictAction.all()) |
| 424 | +def test_bulk_return_models_converters(conflict_action): |
| 425 | + """Tests whether converters are properly applied when using |
| 426 | + return_model=True.""" |
| 427 | + |
| 428 | + model = get_fake_model( |
| 429 | + { |
| 430 | + "name": models.TextField(unique=True), |
| 431 | + "data": models.JSONField(unique=True), |
| 432 | + "updated_at": models.DateTimeField(), |
| 433 | + } |
| 434 | + ) |
| 435 | + |
| 436 | + now = timezone.now() |
| 437 | + |
| 438 | + rows = [ |
| 439 | + dict(name="John Smith", data={"a": 1}, updated_at=now.isoformat()), |
| 440 | + dict(name="Jane Doe", data={"b": 2}, updated_at=now), |
| 441 | + ] |
| 442 | + |
| 443 | + objs = model.objects.on_conflict(["name"], conflict_action).bulk_insert( |
| 444 | + rows, return_model=True |
| 445 | + ) |
| 446 | + |
| 447 | + for index, (obj, row) in enumerate(zip(objs, rows), 1): |
| 448 | + assert isinstance(obj, model) |
| 449 | + assert obj.id == index |
| 450 | + assert obj.name == row["name"] |
| 451 | + assert obj.data == row["data"] |
| 452 | + assert obj.updated_at == now |
0 commit comments