-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathfiltering_spec.rb
More file actions
125 lines (110 loc) · 3.68 KB
/
filtering_spec.rb
File metadata and controls
125 lines (110 loc) · 3.68 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
require 'spec_helper'
RSpec.describe UsersController, type: :request do
describe '#extract_attributes_and_predicate' do
context 'mixed attributes (and/or)' do
it 'extracts ANDs' do
attributes, predicates = JSONAPI::Filtering
.extract_attributes_and_predicates('attr1_and_attr2_eq')
expect(attributes).to eq(['attr1', 'attr2'])
expect(predicates.size).to eq(1)
expect(predicates[0].name).to eq('eq')
end
end
context 'mixed predicates' do
it 'extracts in order' do
attributes, predicates = JSONAPI::Filtering
.extract_attributes_and_predicates('attr1_sum_eq')
expect(attributes).to eq(['attr1'])
expect(predicates.size).to eq(2)
expect(predicates[0].name).to eq('sum')
expect(predicates[1].name).to eq('eq')
end
end
context 'with an invalidate predicate' do
context 'with default configuration' do
it 'does not return any predicates' do
attributes, predicates = JSONAPI::Filtering
.extract_attributes_and_predicates('attr1_eqx')
expect(attributes).to eq(['attr1_eqx'])
expect(predicates.size).to eq(0)
end
end
context 'with ignore_unknown_conditions as false' do
before do
Ransack.configure { |c| c.ignore_unknown_conditions = false }
end
after do
Ransack.configure { |c| c.ignore_unknown_conditions = true }
end
it 'raises an error' do
expect do
JSONAPI::Filtering.extract_attributes_and_predicates('attr1_eqx')
end.to raise_error ArgumentError
end
end
end
end
describe 'GET /users' do
let!(:user) { }
let(:params) { }
before do
get(users_path, params: params, headers: jsonapi_headers)
end
context 'with users' do
let(:first_user) { create_user }
let(:second_user) { create_note.user }
let(:third_user) { create_user }
let(:users) { [first_user, second_user, third_user] }
let(:user) { users.last }
context 'returns filtered users' do
let(:params) do
{
filter: {
last_name_or_first_name_cont_any: (
"#{third_user.first_name[0..5]}%,#{self.class.name}"
)
}
}
end
it do
expect(response).to have_http_status(:ok)
expect(response_json['data'].size).to eq(1)
expect(response_json['data'][0]).to have_id(third_user.id.to_s)
end
context 'with a comma' do
let(:params) do
third_user.update(first_name: third_user.first_name + ',')
{
filter: { first_name_eq: third_user.first_name }
}
end
it do
expect(response).to have_http_status(:ok)
expect(response_json['data'].size).to eq(1)
expect(response_json['data'][0]).to have_id(third_user.id.to_s)
end
end
end
context 'returns sorted users by notes quantity sum' do
let(:params) do
{ sort: '-notes_quantity_sum' }
end
it do
expect(response).to have_http_status(:ok)
expect(response_json['data'].size).to eq(3)
expect(response_json['data'][0]).to have_id(second_user.id.to_s)
end
end
context 'returns sorted users by notes' do
let(:params) do
{ sort: '-notes_created_at' }
end
it do
expect(response).to have_http_status(:ok)
expect(response_json['data'].size).to eq(3)
expect(response_json['data'][0]).to have_id(second_user.id.to_s)
end
end
end
end
end