Passed
Push — master ( 0124f0...487ec1 )
by Swen
06:39 queued 04:18
created

test_on_conflict_nothing_foreign_primary_key()   B

Complexity

Conditions 6

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
c 2
b 0
f 0
dl 0
loc 41
rs 7.5384
1
import pytest
2
from django.db import models
0 ignored issues
show
Configuration introduced by
The import django.db could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
3
4
from psqlextra.fields import HStoreField
5
from psqlextra.query import ConflictAction
6
7
from .util import get_fake_model
8
9 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
10
def test_on_conflict_nothing():
11
    """Tests whether simple insert NOTHING works correctly."""
12
13
    model = get_fake_model({
14
        'title': HStoreField(uniqueness=['key1']),
15
        'cookies': models.CharField(max_length=255, null=True)
16
    })
17
18
    obj1 = (
19
        model.objects
20
        .on_conflict([('title', 'key1')], ConflictAction.NOTHING)
21
        .insert_and_get(title={'key1': 'beer'}, cookies='cheers')
22
    )
23
24
    obj1.refresh_from_db()
25
    assert obj1.title['key1'] == 'beer'
26
    assert obj1.cookies == 'cheers'
27
28
    obj2 = (
29
        model.objects
30
        .on_conflict([('title', 'key1')], ConflictAction.NOTHING)
31
        .insert_and_get(title={'key1': 'beer'}, cookies='choco')
32
    )
33
34
    obj1.refresh_from_db()
35
    obj2.refresh_from_db()
36
37
    # assert that the 'cookies' field didn't change
38
    assert obj1.id == obj2.id
39
    assert obj1.title['key1'] == 'beer'
40
    assert obj1.cookies == 'cheers'
41
    assert obj2.title['key1'] == 'beer'
42
    assert obj2.cookies == 'cheers'
43
44
45
def test_on_conflict_nothing_foreign_primary_key():
46
    """
47
    Tests whether simple insert NOTHING works correctly when the primary key of
48
    a field is a foreign key with a custom name.
49
    """
50
51
    referenced_model = get_fake_model({})
52
53
    model = get_fake_model({
54
        'parent': models.OneToOneField(
55
            referenced_model,
56
            primary_key=True,
57
            on_delete=models.CASCADE,
58
        ),
59
        'cookies': models.CharField(max_length=255),
60
    })
61
62
    referenced_obj = referenced_model.objects.create()
63
64
    obj1 = (
65
        model.objects
66
        .on_conflict(['parent_id'], ConflictAction.NOTHING)
67
        .insert_and_get(parent_id=referenced_obj.pk, cookies='cheers')
68
    )
69
70
    obj1.refresh_from_db()
71
    assert obj1.parent == referenced_obj
72
    assert obj1.cookies == 'cheers'
73
74
    obj2 = (
75
        model.objects
76
        .on_conflict(['parent_id'], ConflictAction.NOTHING)
77
        .insert_and_get(parent_id=referenced_obj.pk, cookies='choco')
78
    )
79
80
    obj1.refresh_from_db()
81
    obj2.refresh_from_db()
82
83
    assert obj1.pk == obj2.pk
84
    assert obj1.cookies == 'cheers'
85
    assert obj2.cookies == 'cheers'
86
87
88 View Code Duplication
def test_on_conflict_nothing_foreign_key_by_object():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
89
    """
90
    Tests whether simple insert NOTHING works correctly when the potentially
91
    conflicting field is a foreign key specified as an object.
92
    """
93
94
    other_model = get_fake_model({})
95
96
    model = get_fake_model({
97
        'other': models.OneToOneField(
98
            other_model,
99
            on_delete=models.CASCADE,
100
        ),
101
        'data': models.CharField(max_length=255),
102
    })
103 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
104
    other_obj = other_model.objects.create()
105
106
    obj1 = (
107
        model.objects
108
        .on_conflict(['other'], ConflictAction.NOTHING)
109
        .insert_and_get(other=other_obj, data="some data")
110
    )
111
112
    assert obj1.other == other_obj
113
    assert obj1.data == "some data"
114
115
    obj1.refresh_from_db()
116
    assert obj1.other == other_obj
117
    assert obj1.data == "some data"
118
119
    with pytest.raises(ValueError):
120
        (
121
            model.objects
122
            .on_conflict(['other'], ConflictAction.NOTHING)
123
            .insert_and_get(other=obj1)
124
        )
125
126
    obj2 = (
127
        model.objects
128
        .on_conflict(['other'], ConflictAction.NOTHING)
129
        .insert_and_get(other=other_obj, data="different data")
130
    )
131
132
    assert obj2.other == other_obj
133
    assert obj2.data == "some data"
134
135
    obj1.refresh_from_db()
136
    obj2.refresh_from_db()
137
138
    # assert that the 'other' field didn't change
139
    assert obj1.id == obj2.id
140
    assert obj1.other == other_obj
141
    assert obj2.other == other_obj
142
    assert obj1.data == "some data"
143
    assert obj2.data == "some data"
144
145
146
def test_on_conflict_nothing_foreign_key_by_id():
147
    """
148
    Tests whether simple insert NOTHING works correctly when the potentially
149
    conflicting field is a foreign key specified as an id.
150
    """
151
152
    other_model = get_fake_model({})
153
154
    model = get_fake_model({
155
        'other': models.OneToOneField(
156
            other_model,
157
            on_delete=models.CASCADE,
158
        ),
159
        'data': models.CharField(max_length=255),
160
    })
161
162
    other_obj = other_model.objects.create()
163
164
    obj1 = (
165
        model.objects
166
        .on_conflict(['other_id'], ConflictAction.NOTHING)
167
        .insert_and_get(other_id=other_obj.pk, data="some data")
168
    )
169
170
    assert obj1.other == other_obj
171
    assert obj1.data == "some data"
172
173
    obj1.refresh_from_db()
174
    assert obj1.other == other_obj
175
    assert obj1.data == "some data"
176
177
    obj2 = (
178
        model.objects
179
        .on_conflict(['other_id'], ConflictAction.NOTHING)
180
        .insert_and_get(other_id=other_obj.pk, data="different data")
181
    )
182
183
    assert obj2.other == other_obj
184
    assert obj2.data == "some data"
185
186
    obj1.refresh_from_db()
187
    obj2.refresh_from_db()
188
189
    # assert that the 'other' field didn't change
190
    assert obj1.id == obj2.id
191
    assert obj1.other == other_obj
192
    assert obj2.other == other_obj
193
    assert obj1.data == "some data"
194
    assert obj2.data == "some data"
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...