Skip to content

Commit 34142d1

Browse files
committed
Add tests for foreign key as id
To ensure that this works too.
1 parent 0776cd2 commit 34142d1

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

tests/test_on_conflict_nothing.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def test_on_conflict_nothing():
4242
assert obj2.cookies == 'cheers'
4343

4444

45-
def test_on_conflict_nothing_foreign_key():
45+
def test_on_conflict_nothing_foreign_key_by_object():
4646
"""
4747
Tests whether simple insert NOTHING works correctly when the potentially
48-
conflicting field is a foreign key.
48+
conflicting field is a foreign key specified as an object.
4949
"""
5050

5151
other_model = get_fake_model({})
@@ -98,3 +98,54 @@ def test_on_conflict_nothing_foreign_key():
9898
assert obj2.other == other_obj
9999
assert obj1.data == "some data"
100100
assert obj2.data == "some data"
101+
102+
103+
def test_on_conflict_nothing_foreign_key_by_id():
104+
"""
105+
Tests whether simple insert NOTHING works correctly when the potentially
106+
conflicting field is a foreign key specified as an id.
107+
"""
108+
109+
other_model = get_fake_model({})
110+
111+
model = get_fake_model({
112+
'other': models.OneToOneField(
113+
other_model,
114+
on_delete=models.CASCADE,
115+
),
116+
'data': models.CharField(max_length=255),
117+
})
118+
119+
other_obj = other_model.objects.create()
120+
121+
obj1 = (
122+
model.objects
123+
.on_conflict(['other_id'], ConflictAction.NOTHING)
124+
.insert_and_get(other_id=other_obj.pk, data="some data")
125+
)
126+
127+
assert obj1.other == other_obj
128+
assert obj1.data == "some data"
129+
130+
obj1.refresh_from_db()
131+
assert obj1.other == other_obj
132+
assert obj1.data == "some data"
133+
134+
obj2 = (
135+
model.objects
136+
.on_conflict(['other_id'], ConflictAction.NOTHING)
137+
.insert_and_get(other_id=other_obj.pk, data="different data")
138+
)
139+
140+
assert obj2.other == other_obj
141+
assert obj2.data == "some data"
142+
143+
obj1.refresh_from_db()
144+
obj2.refresh_from_db()
145+
146+
# assert that the 'other' field didn't change
147+
assert obj1.id == obj2.id
148+
assert obj1.other == other_obj
149+
assert obj2.other == other_obj
150+
assert obj1.data == "some data"
151+
assert obj2.data == "some data"

tests/test_on_conflict_update.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def test_on_conflict_update():
4242
assert obj2.cookies == 'choco'
4343

4444

45-
def test_on_conflict_update_foreign_key():
45+
def test_on_conflict_update_foreign_key_by_object():
4646
"""
4747
Tests whether simple upsert works correctly when the conflicting field is a
48-
foreign key.
48+
foreign key specified as an object.
4949
"""
5050

5151
other_model = get_fake_model({})
@@ -98,3 +98,54 @@ def test_on_conflict_update_foreign_key():
9898
assert obj2.other == other_obj
9999
assert obj1.data == "different data"
100100
assert obj2.data == "different data"
101+
102+
103+
def test_on_conflict_update_foreign_key_by_id():
104+
"""
105+
Tests whether simple upsert works correctly when the conflicting field is a
106+
foreign key specified as an id.
107+
"""
108+
109+
other_model = get_fake_model({})
110+
111+
model = get_fake_model({
112+
'other': models.OneToOneField(
113+
other_model,
114+
on_delete=models.CASCADE,
115+
),
116+
'data': models.CharField(max_length=255),
117+
})
118+
119+
other_obj = other_model.objects.create()
120+
121+
obj1 = (
122+
model.objects
123+
.on_conflict(['other_id'], ConflictAction.UPDATE)
124+
.insert_and_get(other_id=other_obj.pk, data="some data")
125+
)
126+
127+
assert obj1.other == other_obj
128+
assert obj1.data == "some data"
129+
130+
obj1.refresh_from_db()
131+
assert obj1.other == other_obj
132+
assert obj1.data == "some data"
133+
134+
obj2 = (
135+
model.objects
136+
.on_conflict(['other_id'], ConflictAction.UPDATE)
137+
.insert_and_get(other_id=other_obj.pk, data="different data")
138+
)
139+
140+
assert obj2.other == other_obj
141+
assert obj2.data == "different data"
142+
143+
obj1.refresh_from_db()
144+
obj2.refresh_from_db()
145+
146+
# assert that the 'other' field didn't change
147+
assert obj1.id == obj2.id
148+
assert obj1.other == other_obj
149+
assert obj2.other == other_obj
150+
assert obj1.data == "different data"
151+
assert obj2.data == "different data"

0 commit comments

Comments
 (0)