Passed
Push — master ( a064dd...c6686f )
by Swen
02:22
created

test_upsert_bulk_no_rows()   A

Complexity

Conditions 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
1
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...
2
3
from psqlextra.fields import HStoreField
4
5
from .util import get_fake_model
6
7
8
def test_upsert():
9
    """Tests whether simple upserts works correctly."""
10
11
    model = get_fake_model({
12
        'title': HStoreField(uniqueness=['key1']),
13
        'cookies': models.CharField(max_length=255, null=True)
14
    })
15
16
    obj1 = (
17
        model.objects
18
        .upsert_and_get(
19
            conflict_target=[('title', 'key1')],
20
            fields=dict(
21
                title={'key1': 'beer'},
22
                cookies='cheers'
23
            )
24
        )
25
    )
26
27
    obj1.refresh_from_db()
28
    assert obj1.title['key1'] == 'beer'
29
    assert obj1.cookies == 'cheers'
30
31
    obj2 = (
32
        model.objects
33
        .upsert_and_get(
34
            conflict_target=[('title', 'key1')],
35
            fields=dict(
36
                title={'key1': 'beer'},
37
                cookies='choco'
38
            )
39
        )
40
    )
41
42
    obj1.refresh_from_db()
43
    obj2.refresh_from_db()
44
45
    # assert both objects are the same
46
    assert obj1.id == obj2.id
47
    assert obj1.title['key1'] == 'beer'
48
    assert obj1.cookies == 'choco'
49
    assert obj2.title['key1'] == 'beer'
50
    assert obj2.cookies == 'choco'
51
52
53
def test_upsert_bulk():
54
    """Tests whether bulk_upsert works properly."""
55
56
    model = get_fake_model({
57
        'first_name': models.CharField(max_length=255, null=True, unique=True),
58
        'last_name': models.CharField(max_length=255, null=True)
59
    })
60
61
    model.objects.bulk_upsert(
62
        conflict_target=['first_name'],
63
        rows=[
64
            dict(first_name='Swen', last_name='Kooij'),
65
            dict(first_name='Henk', last_name='Test')
66
        ]
67
    )
68
69
    row_a = model.objects.get(first_name='Swen')
70
    row_b = model.objects.get(first_name='Henk')
71
72
    model.objects.bulk_upsert(
73
        conflict_target=['first_name'],
74
        rows=[
75
            dict(first_name='Swen', last_name='Test'),
76
            dict(first_name='Henk', last_name='Kooij')
77
        ]
78
    )
79
80
    row_a.refresh_from_db()
81
    assert row_a.last_name == 'Test'
82
83
    row_b.refresh_from_db()
84
    assert row_b.last_name == 'Kooij'
85
86
87
def test_upsert_bulk_no_rows():
88
    """Tests whether bulk_upsert doesn't crash when specifying
89
    no rows or a falsy value."""
90
91
    model = get_fake_model({
92
        'name': models.CharField(max_length=255, null=True, unique=True)
93
    })
94
95
    model.objects.bulk_upsert(
96
        conflict_target=['name'],
97
        rows=[]
98
    )
99
100
    model.objects.bulk_upsert(
101
        conflict_target=['name'],
102
        rows=None
103
    )
104