Skip to content

Commit b7ea7f5

Browse files
committed
Convert test_upsert to use new syntax
1 parent 0644227 commit b7ea7f5

File tree

1 file changed

+89
-40
lines changed

1 file changed

+89
-40
lines changed

tests/test_upsert.py

Lines changed: 89 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.test import TestCase
55

66
from psqlextra import HStoreField
7+
from psqlextra.query import ConflictAction
78

89
from .fake_model import get_fake_model
910

@@ -20,23 +21,29 @@ def test_simple(self):
2021
'cookies': models.CharField(max_length=255, null=True)
2122
})
2223

23-
obj = model.objects.upsert_and_get(
24-
conflict_target=[('title', 'key1')],
25-
fields=dict(
24+
obj = (
25+
model.objects
26+
.on_conflict(
27+
[('title', 'key1')],
28+
ConflictAction.UPDATE
29+
)
30+
.insert_and_get(
2631
title={'key1': 'beer'},
2732
cookies='cheers'
2833
)
2934
)
3035

31-
obj1 = model.objects.upsert_and_get(
32-
conflict_target=[('title', 'key1')],
33-
fields=dict(
36+
obj1 = (
37+
model.objects
38+
.on_conflict(
39+
[('title', 'key1')],
40+
ConflictAction.UPDATE
41+
)
42+
.insert_and_get(
3443
title={'key1': 'beer'}
3544
)
3645
)
3746

38-
obj1.refresh_from_db()
39-
4047
assert obj1.title['key1'] == 'beer'
4148
assert obj1.cookies == obj.cookies
4249

@@ -50,17 +57,25 @@ def test_auto_fields(self):
5057
'date_updated': models.DateTimeField(auto_now=True)
5158
})
5259

53-
obj1 = model.objects.upsert_and_get(
54-
conflict_target=['title'],
55-
fields=dict(
56-
title='beer'
60+
obj1 = (
61+
model.objects
62+
.on_conflict(
63+
['title'],
64+
ConflictAction.UPDATE
65+
)
66+
.insert_and_get(
67+
title={'beer'}
5768
)
5869
)
5970

60-
obj2 = model.objects.upsert_and_get(
61-
conflict_target=['title'],
62-
fields=dict(
63-
title='beer'
71+
obj2 = (
72+
model.objects
73+
.on_conflict(
74+
['title'],
75+
ConflictAction.UPDATE
76+
)
77+
.insert_and_get(
78+
title={'beer'}
6479
)
6580
)
6681

@@ -86,17 +101,25 @@ def test_foreign_key(self):
86101
'model1': models.ForeignKey(model1)
87102
})
88103

89-
model1_row = model1.objects.upsert_and_get(
90-
conflict_target=['name'],
91-
fields=dict(
104+
model1_row = (
105+
model1.objects
106+
.on_conflict(
107+
['name'],
108+
ConflictAction.UPDATE
109+
)
110+
.insert_and_get(
92111
name='item1'
93112
)
94113
)
95114

96115
# upsert by id, that should work
97-
model2.objects.upsert(
98-
conflict_target=['name'],
99-
fields=dict(
116+
model2_row = (
117+
model2.objects
118+
.on_conflict(
119+
['name'],
120+
ConflictAction.UPDATE
121+
)
122+
.insert_and_get(
100123
name='item1',
101124
model1_id=model1_row.id
102125
)
@@ -107,9 +130,13 @@ def test_foreign_key(self):
107130
assert model2_row.model1.id == model1_row.id
108131

109132
# upsert by object, that should also work
110-
model2.objects.upsert(
111-
conflict_target=['name'],
112-
fields=dict(
133+
model2_row = (
134+
model2.objects
135+
.on_conflict(
136+
['name'],
137+
ConflictAction.UPDATE
138+
)
139+
.insert_and_get(
113140
name='item2',
114141
model1=model1_row
115142
)
@@ -131,17 +158,27 @@ def test_get_partial(self):
131158
'updated_at': models.DateTimeField(auto_now=True),
132159
})
133160

134-
obj1 = model.objects.upsert_and_get(
135-
conflict_target=['title'],
136-
fields=dict(
161+
obj1 = (
162+
model.objects
163+
.on_conflict(
164+
['title'],
165+
ConflictAction.UPDATE
166+
)
167+
.insert_and_get(
137168
title='beer',
138169
purpose='for-sale'
139170
)
140171
)
141172

142-
obj2 = model.objects.upsert_and_get(
143-
conflict_target=['title'],
144-
fields=dict(title='beer')
173+
obj2 = (
174+
model.objects
175+
.on_conflict(
176+
['title'],
177+
ConflictAction.UPDATE
178+
)
179+
.insert_and_get(
180+
title='beer'
181+
)
145182
)
146183

147184
assert obj2.title == obj1.title
@@ -158,17 +195,25 @@ def test_invalid_conflict_target(self):
158195
})
159196

160197
with self.assertRaises(SuspiciousOperation):
161-
model.objects.upsert(
162-
conflict_target='cookie',
163-
fields=dict(
198+
(
199+
model.objects
200+
.on_conflict(
201+
['cookie'],
202+
ConflictAction.UPDATE
203+
)
204+
.insert(
164205
title='beer'
165206
)
166207
)
167208

168209
with self.assertRaises(SuspiciousOperation):
169-
model.objects.upsert(
170-
conflict_target=[None],
171-
fields=dict(
210+
(
211+
model.objects
212+
.on_conflict(
213+
[None],
214+
ConflictAction.UPDATE
215+
)
216+
.insert(
172217
title='beer'
173218
)
174219
)
@@ -196,9 +241,13 @@ def test_outdated_model(self):
196241
).format(table=model._meta.db_table))
197242

198243
# without proper handling, this would fail with a TypeError
199-
model.objects.upsert_and_get(
200-
conflict_target=['title'],
201-
fields=dict(
244+
(
245+
model.objects
246+
.on_conflict(
247+
['title'],
248+
ConflictAction.UPDATE
249+
).
250+
insert_and_get(
202251
title='beer'
203252
)
204253
)

0 commit comments

Comments
 (0)