forked from AdaGold/video-store-consumer-api
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcustomers_controller_test.rb
More file actions
204 lines (160 loc) · 5.49 KB
/
customers_controller_test.rb
File metadata and controls
204 lines (160 loc) · 5.49 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
require 'test_helper'
class CustomersControllerTest < ActionDispatch::IntegrationTest
describe "List customers" do
it "returns a JSON array" do
get customers_url
assert_response :success
@response.headers['Content-Type'].must_include 'json'
# Attempt to parse
data = JSON.parse @response.body
data.must_be_kind_of Array
end
it "should return many customer fields" do
get customers_url
assert_response :success
data = JSON.parse @response.body
data.each do |customer|
customer.must_include "id"
customer.must_include "name"
customer.must_include "registration_date"
customer.must_include "postal_code"
customer.must_include "phone"
customer.must_include "account_credit"
customer.must_include "movies_checked_out_count"
end
end
it "returns all customers when no query params are given" do
get customers_url
assert_response :success
data = JSON.parse @response.body
data.length.must_equal Customer.count
expected_names = {}
Customer.all.each do |customer|
expected_names[customer["name"]] = false
end
data.each do |customer|
expected_names[customer["name"]].must_equal false
expected_names[customer["name"]] = true
end
end
describe "sorting" do
it "can sort by name" do
get customers_url, params: { sort: 'name' }
assert_response :success
data = JSON.parse @response.body
data.length.must_equal Customer.count
# Verify sorted order
data.each_with_index do |customer, i|
if i + 1 >= data.length
break
end
customer['name'].must_be :<=, data[i+1]['name']
end
end
# it "can sort by registered_at" do
# get customers_url, params: { sort: 'registered_at' }
# assert_response :success
#
# data = JSON.parse @response.body
# data.length.must_equal Customer.count
#
# # Verify sorted order
# data.each_with_index do |customer, i|
# if i + 1 >= data.length
# break
# end
#
# DateTime.parse(customer['registered_at']).must_be :<=, DateTime.parse(data[i+1]['registered_at'])
# end
# end
it "can sort by postal_code" do
get customers_url, params: { sort: 'postal_code' }
assert_response :success
data = JSON.parse @response.body
data.length.must_equal Customer.count
# Verify sorted order
data.each_with_index do |customer, i|
if i + 1 >= data.length
break
end
customer['postal_code'].must_be :<=, data[i+1]['postal_code']
end
end
it "returns an error for an invalid sort field" do
get customers_url, params: { sort: 'gnome' }
assert_response :bad_request
data = JSON.parse @response.body
data.must_be_kind_of Hash
data.must_include 'errors'
data['errors'].must_include 'sort'
end
end
describe "pagination" do
it "can restrict entries per page" do
Customer.count.must_be :>, 2
get customers_url, params: { n: 2 }
assert_response :success
data = JSON.parse @response.body
data.length.must_equal 2
end
it "can return different pages" do
Customer.count.must_be :>, 2
get customers_url, params: { n: 2, p: 1 }
assert_response :success
page_one_data = JSON.parse @response.body
page_one_data.length.must_equal 2
page_one_ids = page_one_data.map { |c| c['id'] }
get customers_url, params: { n: 2, p: 2 }
assert_response :success
page_two_data = JSON.parse @response.body
page_two_data.each do |customer|
page_one_ids.wont_include customer['id']
end
end
it "Handles pagination and sorting" do
Customer.count.must_be :>, 2
# Get first page
get customers_url, params: { sort: 'name', n: 2, p: 1 }
assert_response :success
data = JSON.parse @response.body
data.length.must_equal 2
all_names = data.map { |c| c['name'] }
# Get second page
get customers_url, params: { sort: 'name', n: 2, p: 2 }
assert_response :success
data = JSON.parse @response.body
all_names += data.map { |c| c['name'] }
# Verify all data is sorted
all_names.each_with_index do |name, i|
break if i + 1 >= all_names.length
name.must_be :<=, all_names[i+1]
end
end
# it "errors on negative numbers" do
# get customers_url, params: { n: -2, p: 1 }
# assert_response :bad_request
#
# get customers_url, params: { n: 2, p: -1 }
# assert_response :bad_request
# end
it "returns an empty array past the end" do
Customer.count.must_be :<, 10000
# Get first page
get customers_url, params: { n: 10, p: 1001 }
assert_response :success
data = JSON.parse @response.body
data.length.must_equal 0
end
it "handles fewer entries left than page size" do
Customer.count.must_be :<, 10000
# Get first page
get customers_url, params: { n: 10000, p: 1 }
assert_response :success
data = JSON.parse @response.body
data.length.must_equal Customer.count
end
# it "requires a number" do
# end
end
end
end