1
1
import pytest
2
- import factory
3
2
4
3
import json
5
4
9
8
10
9
from rest_framework_json_api .renderers import JSONRenderer
11
10
12
- from example .models import Comment , Entry
11
+ from example .models import Comment , Entry , Blog
13
12
14
13
15
14
# serializers
@@ -80,10 +79,51 @@ def test_render_format_keys(settings):
80
79
assert result ['data' ]['attributes' ]['json-field' ] == {'json-key' : 'JsonValue' }
81
80
82
81
82
+ @pytest .mark .django_db
83
+ def test_blog_create (client ):
84
+
85
+ url = reverse ('drf-entry-blog-list' )
86
+ name = "Dummy Name"
87
+
88
+ request_data = {
89
+ 'data' : {
90
+ 'attributes' : {'name' : name },
91
+ 'type' : 'blogs'
92
+ },
93
+ }
94
+
95
+ resp = client .post (url , request_data )
96
+
97
+ # look for created blog in database
98
+ blog = Blog .objects .filter (name = name )
99
+
100
+ # check if blog exists in database
101
+ assert blog .exists ()
102
+ assert blog .count () == 1
103
+
104
+ # get created blog from database
105
+ blog = blog [0 ]
106
+
107
+ expected = {
108
+ 'data' : {
109
+ 'attributes' : {'name' : blog .name },
110
+ 'id' : '{}' .format (blog .id ),
111
+ 'links' : {'self' : 'http://testserver/blogs/{}' .format (blog .id )},
112
+ 'meta' : {'copyright' : 2018 },
113
+ 'relationships' : {'tags' : {'data' : []}},
114
+ 'type' : 'blogs'
115
+ },
116
+ 'meta' : {'apiDocs' : '/docs/api/blogs' }
117
+ }
118
+
119
+ assert resp .status_code == 201
120
+ assert resp .json () == expected
121
+
122
+
83
123
@pytest .mark .django_db
84
124
def test_get_object_gives_correct_blog (client , blog , entry ):
85
125
86
- url = reverse ('drf-entry-blog' , kwargs = {'entry_pk' : entry .id })
126
+ url = reverse ('drf-entry-blog-detail ' , kwargs = {'entry_pk' : entry .id })
87
127
resp = client .get (url )
88
128
expected = {
89
129
'data' : {
@@ -100,26 +140,49 @@ def test_get_object_gives_correct_blog(client, blog, entry):
100
140
assert got == expected
101
141
102
142
103
- # @pytest.mark.django_db
104
- # def test_get_object_updates_correct_blog(client, blog, entry):
105
- #
106
- # url = reverse('drf-entry-blog', kwargs={'entry_pk': entry.id})
107
- # new_name = blog.name + " update"
108
- # assert not new_name == blog.name
109
- #
110
- # resp = client.patch(url, {"name": new_name})
111
- # print(resp)
112
- #
113
- # expected = {
114
- # 'data': {
115
- # 'attributes': {'name': new_name},
116
- # 'id': '{}'.format(blog.id),
117
- # 'links': {'self': 'http://testserver/blogs/{}'.format(blog.id)},
118
- # 'meta': {'copyright': 2018},
119
- # 'relationships': {'tags': {'data': []}},
120
- # 'type': 'blogs'
121
- # },
122
- # 'meta': {'apiDocs': '/docs/api/blogs'}
123
- # }
124
- # got = resp.json()
125
- # assert got == expected
143
+ @pytest .mark .django_db
144
+ def test_get_object_patches_correct_blog (client , blog , entry ):
145
+
146
+ url = reverse ('drf-entry-blog-detail' , kwargs = {'entry_pk' : entry .id })
147
+ new_name = blog .name + " update"
148
+ assert not new_name == blog .name
149
+
150
+ request_data = {
151
+ 'data' : {
152
+ 'attributes' : {'name' : new_name },
153
+ 'id' : '{}' .format (blog .id ),
154
+ 'links' : {'self' : 'http://testserver/blogs/{}' .format (blog .id )},
155
+ 'meta' : {'copyright' : 2018 },
156
+ 'relationships' : {'tags' : {'data' : []}},
157
+ 'type' : 'blogs'
158
+ },
159
+ 'meta' : {'apiDocs' : '/docs/api/blogs' }
160
+ }
161
+
162
+ resp = client .patch (url , data = request_data )
163
+
164
+ assert resp .status_code == 200
165
+
166
+ expected = {
167
+ 'data' : {
168
+ 'attributes' : {'name' : new_name },
169
+ 'id' : '{}' .format (blog .id ),
170
+ 'links' : {'self' : 'http://testserver/blogs/{}' .format (blog .id )},
171
+ 'meta' : {'copyright' : 2018 },
172
+ 'relationships' : {'tags' : {'data' : []}},
173
+ 'type' : 'blogs'
174
+ },
175
+ 'meta' : {'apiDocs' : '/docs/api/blogs' }
176
+ }
177
+ got = resp .json ()
178
+ assert got == expected
179
+
180
+
181
+ @pytest .mark .django_db
182
+ def test_get_object_deletes_correct_blog (client , blog , entry ):
183
+
184
+ url = reverse ('drf-entry-blog-detail' , kwargs = {'entry_pk' : entry .id })
185
+
186
+ resp = client .delete (url )
187
+
188
+ assert resp .status_code == 204
0 commit comments