File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ from django .db import models
2+
3+ from psqlextra import HStoreField
4+ from psqlextra .query import ConflictAction
5+
6+ from .fake_model import get_fake_model
7+
8+
9+ def test_upsert ():
10+ """Tests whether simple upserts works correctly."""
11+
12+ model = get_fake_model ({
13+ 'title' : HStoreField (uniqueness = ['key1' ]),
14+ 'cookies' : models .CharField (max_length = 255 , null = True )
15+ })
16+
17+ obj1 = (
18+ model .objects
19+ .upsert_and_get (
20+ conflict_target = [('title' , 'key1' )],
21+ fields = dict (
22+ title = {'key1' : 'beer' },
23+ cookies = 'cheers'
24+ )
25+ )
26+ )
27+
28+ obj1 .refresh_from_db ()
29+ assert obj1 .title ['key1' ] == 'beer'
30+ assert obj1 .cookies == 'cheers'
31+
32+ obj2 = (
33+ model .objects
34+ .upsert_and_get (
35+ conflict_target = [('title' , 'key1' )],
36+ fields = dict (
37+ title = {'key1' : 'beer' },
38+ cookies = 'choco'
39+ )
40+ )
41+ )
42+
43+ obj1 .refresh_from_db ()
44+ obj2 .refresh_from_db ()
45+
46+ # assert both objects are the same
47+ assert obj1 .id == obj2 .id
48+ assert obj1 .title ['key1' ] == 'beer'
49+ assert obj1 .cookies == 'choco'
50+ assert obj2 .title ['key1' ] == 'beer'
51+ assert obj2 .cookies == 'choco'
You can’t perform that action at this time.
0 commit comments