Skip to content

Commit ad5ef1d

Browse files
tests
1 parent 58ecb5b commit ad5ef1d

File tree

1 file changed

+73
-42
lines changed

1 file changed

+73
-42
lines changed

tests/integrations/django/test_db_query_data.py

Lines changed: 73 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from django import VERSION as DJANGO_VERSION
88
from django.db import connection, connections, transaction
9+
from django.contrib.auth.models import User
910

1011
try:
1112
from django.urls import reverse
@@ -526,22 +527,10 @@ def test_db_span_origin_executemany(sentry_init, client, capture_events):
526527
assert event["contexts"]["trace"]["origin"] == "manual"
527528
assert event["spans"][0]["origin"] == "auto.db.django"
528529

529-
commit_spans = [
530-
span
531-
for span in event["spans"]
532-
if span["data"].get(SPANDATA.DB_OPERATION) == DBOPERATION.COMMIT
533-
]
534-
assert len(commit_spans) == 1
535-
commit_span = commit_spans[0]
536-
assert commit_span["origin"] == "auto.db.django"
537-
538530

539531
@pytest.mark.forked
540-
@pytest_mark_django_db_decorator(transaction=True)
532+
@pytest_mark_django_db_decorator(transaction=True, databases=["postgres"])
541533
def test_db_no_autocommit_execute(sentry_init, client, capture_events):
542-
"""
543-
Verify we record a breadcrumb when opening a new database.
544-
"""
545534
sentry_init(
546535
integrations=[DjangoIntegration()],
547536
traces_sample_rate=1.0,
@@ -555,10 +544,13 @@ def test_db_no_autocommit_execute(sentry_init, client, capture_events):
555544

556545
events = capture_events()
557546

558-
client.get(reverse("postgres_select_orm_no_autocommit"))
547+
client.get(reverse("postgres_insert_orm_no_autocommit"))
559548

560549
(event,) = events
561550

551+
# Ensure operation is persisted
552+
assert User.objects.using("postgres").exists()
553+
562554
assert event["contexts"]["trace"]["origin"] == "auto.http.django"
563555

564556
for span in event["spans"]:
@@ -595,23 +587,46 @@ def test_db_no_autocommit_executemany(sentry_init, client, capture_events):
595587

596588
cursor = connection.cursor()
597589

598-
query = """UPDATE auth_user SET username = %s where id = %s;"""
590+
query = """INSERT INTO auth_user (
591+
password,
592+
is_superuser,
593+
username,
594+
first_name,
595+
last_name,
596+
email,
597+
is_staff,
598+
is_active,
599+
date_joined
600+
)
601+
VALUES ('password', false, %s, %s, %s, %s, false, true, %s);"""
602+
599603
query_list = (
600604
(
601-
"test1",
602-
1,
605+
"user1",
606+
"John",
607+
"Doe",
608+
609+
datetime(1970, 1, 1),
603610
),
604611
(
605-
"test2",
606-
2,
612+
"user2",
613+
"Max",
614+
"Mustermann",
615+
616+
datetime(1970, 1, 1),
607617
),
608618
)
609-
cursor.executemany(query, query_list)
610619

620+
transaction.set_autocommit(False)
621+
cursor.executemany(query, query_list)
611622
transaction.commit()
623+
transaction.set_autocommit(True)
612624

613625
(event,) = events
614626

627+
# Ensure operation is persisted
628+
assert User.objects.exists()
629+
615630
assert event["contexts"]["trace"]["origin"] == "manual"
616631
assert event["spans"][0]["origin"] == "auto.db.django"
617632

@@ -626,11 +641,8 @@ def test_db_no_autocommit_executemany(sentry_init, client, capture_events):
626641

627642

628643
@pytest.mark.forked
629-
@pytest_mark_django_db_decorator(transaction=True)
644+
@pytest_mark_django_db_decorator(transaction=True, databases=["postgres"])
630645
def test_db_atomic_execute(sentry_init, client, capture_events):
631-
"""
632-
Verify we record a breadcrumb when opening a new database.
633-
"""
634646
sentry_init(
635647
integrations=[DjangoIntegration()],
636648
send_default_pii=True,
@@ -645,12 +657,13 @@ def test_db_atomic_execute(sentry_init, client, capture_events):
645657

646658
events = capture_events()
647659

648-
with transaction.atomic():
649-
client.get(reverse("postgres_select_orm_atomic"))
650-
connections["postgres"].commit()
660+
client.get(reverse("postgres_insert_orm_atomic"))
651661

652662
(event,) = events
653663

664+
# Ensure operation is persisted
665+
assert User.objects.using("postgres").exists()
666+
654667
assert event["contexts"]["trace"]["origin"] == "auto.http.django"
655668

656669
commit_spans = [
@@ -666,9 +679,6 @@ def test_db_atomic_execute(sentry_init, client, capture_events):
666679
@pytest.mark.forked
667680
@pytest_mark_django_db_decorator(transaction=True)
668681
def test_db_atomic_executemany(sentry_init, client, capture_events):
669-
"""
670-
Verify we record a breadcrumb when opening a new database.
671-
"""
672682
sentry_init(
673683
integrations=[DjangoIntegration()],
674684
send_default_pii=True,
@@ -687,21 +697,42 @@ def test_db_atomic_executemany(sentry_init, client, capture_events):
687697
with transaction.atomic():
688698
cursor = connection.cursor()
689699

690-
query = """UPDATE auth_user SET username = %s where id = %s;"""
691-
query_list = (
692-
(
693-
"test1",
694-
1,
695-
),
696-
(
697-
"test2",
698-
2,
699-
),
700-
)
701-
cursor.executemany(query, query_list)
700+
query = """INSERT INTO auth_user (
701+
password,
702+
is_superuser,
703+
username,
704+
first_name,
705+
last_name,
706+
email,
707+
is_staff,
708+
is_active,
709+
date_joined
710+
)
711+
VALUES ('password', false, %s, %s, %s, %s, false, true, %s);"""
712+
713+
query_list = (
714+
(
715+
"user1",
716+
"John",
717+
"Doe",
718+
719+
datetime(1970, 1, 1),
720+
),
721+
(
722+
"user2",
723+
"Max",
724+
"Mustermann",
725+
726+
datetime(1970, 1, 1),
727+
),
728+
)
729+
cursor.executemany(query, query_list)
702730

703731
(event,) = events
704732

733+
# Ensure operation is persisted
734+
assert User.objects.exists()
735+
705736
assert event["contexts"]["trace"]["origin"] == "manual"
706737

707738
commit_spans = [

0 commit comments

Comments
 (0)