Skip to content

Commit 11a4194

Browse files
committed
Refactor .values() tests into separate cases
Add failing test case for selecting individual hstore keys through a foreign key relationship.
1 parent 5780fd1 commit 11a4194

File tree

2 files changed

+73
-34
lines changed

2 files changed

+73
-34
lines changed

tests/test_query.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,6 @@
66
from .fake_model import get_fake_model
77

88

9-
def test_values_hstore_key():
10-
"""Tests whether selecting only certain hstore keys using the
11-
query set's .values() method works properly."""
12-
13-
model_fk = get_fake_model({
14-
'first_name': models.CharField(max_length=255),
15-
'last_name': models.CharField(max_length=255)
16-
})
17-
18-
model = get_fake_model({
19-
'title': HStoreField(),
20-
'fk': models.ForeignKey(model_fk)
21-
})
22-
23-
fk = model_fk.objects.create(first_name='Swen', last_name='Kooij')
24-
obj = model.objects.create(title={'en': 'english', 'ar': 'arabic'}, fk=fk)
25-
26-
# ensure that selecting only certain keys from a hstore field works
27-
result = list(model.objects.values('title__en', 'fk__first_name', 'title__ar'))[0]
28-
assert result['title__en'] == obj.title['en']
29-
assert result['title__ar'] == obj.title['ar']
30-
31-
# make sure that selecting the whole hstore field works properly
32-
result = list(model.objects.values('fk__first_name', 'title'))[0]
33-
assert result['title'] == obj.title
34-
35-
# make sure .values_list() also works properly
36-
result = list(model.objects.values_list('title__en', 'title__ar'))[0]
37-
assert result[0] == obj.title['en']
38-
assert result[1] == obj.title['ar']
39-
40-
result = list(model.objects.values_list('title__en', 'title__ar'))[0]
41-
42-
439
def test_annotate_hstore_key_ref():
4410
"""Tests whether annotating using a :see:HStoreRef expression
4511
works correctly.

tests/test_query_values.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import pytest
2+
3+
from collections import namedtuple
4+
5+
from django.db.models import CharField, ForeignKey
6+
7+
from psqlextra import HStoreField
8+
from psqlextra.expressions import HStoreRef
9+
10+
from .fake_model import get_fake_model
11+
12+
13+
@pytest.fixture
14+
def model():
15+
"""Test models, where the first model has a foreign
16+
key relationship to the second."""
17+
18+
return get_fake_model({
19+
'title': HStoreField(),
20+
})
21+
22+
23+
@pytest.fixture
24+
def modelobj(model):
25+
"""Data for the test models, one row per model."""
26+
27+
return model.objects.create(title={'en': 'english', 'ar': 'arabic'})
28+
29+
30+
def test_values_hstore(model, modelobj):
31+
"""Tests that selecting all the keys properly works
32+
and returns a :see:LocalizedValue instance."""
33+
34+
result = list(model.objects.values('title'))[0]
35+
assert result['title'] == modelobj.title
36+
37+
38+
def test_values_hstore_key(model, modelobj):
39+
"""Tests whether selecting a single key from a :see:HStoreField
40+
using the query set's .values() works properly."""
41+
42+
result = list(model.objects.values('title__en', 'title__ar'))[0]
43+
assert result['title__en'] == modelobj.title['en']
44+
assert result['title__ar'] == modelobj.title['ar']
45+
46+
47+
def test_values_list_hstore_key(model, modelobj):
48+
"""Tests that selecting a single key from a :see:HStoreField
49+
using the query set's .values_list() works properly."""
50+
51+
result = list(model.objects.values_list('title__en', 'title__ar'))[0]
52+
assert result[0] == modelobj.title['en']
53+
assert result[1] == modelobj.title['ar']
54+
55+
56+
def test_values_hstore_key_through_fk():
57+
"""Tests whether selecting a single key from a :see:HStoreField
58+
using the query set's .values() works properly when there's a
59+
foreign key relationship involved."""
60+
61+
fmodel = get_fake_model({
62+
'name': HStoreField()
63+
})
64+
65+
model = get_fake_model({
66+
'fk': ForeignKey(fmodel)
67+
})
68+
69+
fobj = fmodel.objects.create(name={'en': 'swen', 'ar': 'arabic swen'})
70+
obj = model.objects.create(fk=fobj)
71+
72+
result = list(model.objects.values('fk__name__ar'))[0]
73+
assert result['fk__name__ar'] == fobj.name['ar']

0 commit comments

Comments
 (0)