forked from applegrew/django-select2
-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtest_views.py
115 lines (97 loc) · 4.28 KB
/
test_views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import json
from django.urls import reverse
from django.utils.encoding import smart_str
from django_select2.cache import cache
from django_select2.forms import ModelSelect2Widget
from tests.testapp.forms import (
AlbumModelSelect2WidgetForm,
ArtistCustomTitleWidget,
CityForm,
)
from tests.testapp.models import Genre
class TestAutoResponseView:
def test_get(self, client, artists):
artist = artists[0]
form = AlbumModelSelect2WidgetForm()
assert form.as_p()
field_id = form.fields["artist"].widget.field_id
url = reverse("django_select2:auto-json")
response = client.get(url, {"field_id": field_id, "term": artist.title})
assert response.status_code == 200
data = json.loads(response.content.decode("utf-8"))
assert data["results"]
assert {"id": artist.pk, "text": smart_str(artist)} in data["results"]
def test_no_field_id(self, client, artists):
artist = artists[0]
url = reverse("django_select2:auto-json")
response = client.get(url, {"term": artist.title})
assert response.status_code == 404
def test_wrong_field_id(self, client, artists):
artist = artists[0]
url = reverse("django_select2:auto-json")
response = client.get(url, {"field_id": 123, "term": artist.title})
assert response.status_code == 404
def test_field_id_not_found(self, client, artists):
artist = artists[0]
field_id = "not-exists"
url = reverse("django_select2:auto-json")
response = client.get(url, {"field_id": field_id, "term": artist.title})
assert response.status_code == 404
def test_pagination(self, genres, client):
url = reverse("django_select2:auto-json")
widget = ModelSelect2Widget(
max_results=10, model=Genre, search_fields=["title__icontains"]
)
widget.render("name", None)
field_id = widget.field_id
response = client.get(url, {"field_id": field_id, "term": ""})
assert response.status_code == 200
data = json.loads(response.content.decode("utf-8"))
assert data["more"] is True
response = client.get(url, {"field_id": field_id, "term": "", "page": 1000})
assert response.status_code == 404
response = client.get(url, {"field_id": field_id, "term": "", "page": "last"})
assert response.status_code == 200
data = json.loads(response.content.decode("utf-8"))
assert data["more"] is False
def test_label_from_instance(self, artists, client):
url = reverse("django_select2:auto-json")
form = AlbumModelSelect2WidgetForm()
form.fields["artist"].widget = ArtistCustomTitleWidget()
assert form.as_p()
field_id = form.fields["artist"].widget.field_id
artist = artists[0]
response = client.get(url, {"field_id": field_id, "term": artist.title})
assert response.status_code == 200
data = json.loads(response.content.decode("utf-8"))
assert data["results"]
assert {"id": artist.pk, "text": smart_str(artist.title.upper())} in data[
"results"
]
def test_result_from_instance(self, cities, client):
url = reverse("django_select2:auto-json")
form = CityForm()
assert form.as_p()
field_id = form.fields["city"].widget.field_id
city = cities[0]
response = client.get(url, {"field_id": field_id, "term": city.name})
assert response.status_code == 200
data = json.loads(response.content.decode("utf-8"))
assert data["results"]
assert {
"id": city.pk,
"text": smart_str(city),
"country": smart_str(city.country),
} in data["results"]
def test_url_check(self, client, artists):
artist = artists[0]
form = AlbumModelSelect2WidgetForm()
assert form.as_p()
field_id = form.fields["artist"].widget.field_id
cache_key = form.fields["artist"].widget._get_cache_key()
widget_dict = cache.get(cache_key)
widget_dict["url"] = "yet/another/url"
cache.set(cache_key, widget_dict)
url = reverse("django_select2:auto-json")
response = client.get(url, {"field_id": field_id, "term": artist.title})
assert response.status_code == 404