Skip to content

Commit b89d729

Browse files
author
Nicolas Rodriguez
committed
Add more tests
1 parent b7c04c5 commit b89d729

File tree

6 files changed

+314
-95
lines changed

6 files changed

+314
-95
lines changed

spec/ajax-datatables-rails/datatable/datatable_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
describe 'columns methods' do
5656
it 'should have 4 columns' do
57-
expect(datatable.columns.count).to eq(5)
57+
expect(datatable.columns.count).to eq(6)
5858
end
5959

6060
it 'child class' do
Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'spec_helper'
22

33
describe AjaxDatatablesRails::Base do
4-
54
let(:view) { double('view', params: sample_params) }
65
let(:datatable) { ReallyComplexDatatable.new(view) }
76

@@ -18,84 +17,4 @@
1817
expect(item[:last_name]).to eq 'Doe'
1918
end
2019
end
21-
22-
describe 'it can filter range records' do
23-
before(:each) do
24-
create(:user, username: 'johndoe', email: 'johndoe@example.com', last_name: 'Doe', created_at: '01/01/2000')
25-
create(:user, username: 'msmith', email: 'mary.smith@example.com', last_name: 'Smith', created_at: '01/02/2000')
26-
end
27-
28-
context 'when range is empty' do
29-
it 'should not filter records' do
30-
datatable.params[:columns]['4'][:search][:value] = '-'
31-
expect(datatable.data.size).to eq 2
32-
item = datatable.data.first
33-
expect(item[:last_name]).to eq 'Doe'
34-
end
35-
end
36-
37-
context 'when start date is filled' do
38-
it 'should filter records created after this date' do
39-
datatable.params[:columns]['4'][:search][:value] = '31/12/1999-'
40-
expect(datatable.data.size).to eq 2
41-
end
42-
end
43-
44-
context 'when end date is filled' do
45-
it 'should filter records created before this date' do
46-
datatable.params[:columns]['4'][:search][:value] = '-31/12/1999'
47-
expect(datatable.data.size).to eq 0
48-
end
49-
end
50-
51-
context 'when both date are filled' do
52-
it 'should filter records created between the range' do
53-
datatable.params[:columns]['4'][:search][:value] = '01/12/1999-15/01/2000'
54-
expect(datatable.data.size).to eq 1
55-
end
56-
end
57-
58-
context 'when another filter is active' do
59-
context 'when range is empty' do
60-
it 'should filter records' do
61-
datatable.params[:columns]['0'][:search][:value] = 'doe'
62-
datatable.params[:columns]['4'][:search][:value] = '-'
63-
expect(datatable.data.size).to eq 1
64-
item = datatable.data.first
65-
expect(item[:last_name]).to eq 'Doe'
66-
end
67-
end
68-
69-
context 'when start date is filled' do
70-
it 'should filter records' do
71-
datatable.params[:columns]['0'][:search][:value] = 'doe'
72-
datatable.params[:columns]['4'][:search][:value] = '01/12/1999-'
73-
expect(datatable.data.size).to eq 1
74-
item = datatable.data.first
75-
expect(item[:last_name]).to eq 'Doe'
76-
end
77-
end
78-
79-
context 'when end date is filled' do
80-
it 'should filter records' do
81-
datatable.params[:columns]['0'][:search][:value] = 'doe'
82-
datatable.params[:columns]['4'][:search][:value] = '-15/01/2000'
83-
expect(datatable.data.size).to eq 1
84-
item = datatable.data.first
85-
expect(item[:last_name]).to eq 'Doe'
86-
end
87-
end
88-
89-
context 'when both date are filled' do
90-
it 'should filter records' do
91-
datatable.params[:columns]['0'][:search][:value] = 'doe'
92-
datatable.params[:columns]['4'][:search][:value] = '01/12/1999-15/01/2000'
93-
expect(datatable.data.size).to eq 1
94-
item = datatable.data.first
95-
expect(item[:last_name]).to eq 'Doe'
96-
end
97-
end
98-
end
99-
end
100-
10120
end

spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb

Lines changed: 253 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
let(:datatable) { ComplexDatatable.new(view) }
77
let(:records) { User.all }
88

9-
before(:each) do
10-
create(:user, username: 'johndoe', email: 'johndoe@example.com')
11-
create(:user, username: 'msmith', email: 'mary.smith@example.com')
12-
end
13-
149
describe '#filter_records' do
1510
it 'requires a records collection as argument' do
1611
expect { datatable.send(:filter_records) }.to raise_error(ArgumentError)
@@ -30,6 +25,11 @@
3025
end
3126

3227
describe '#build_conditions_for_datatable' do
28+
before(:each) do
29+
create(:user, username: 'johndoe', email: 'johndoe@example.com')
30+
create(:user, username: 'msmith', email: 'mary.smith@example.com')
31+
end
32+
3333
it 'returns an Arel object' do
3434
datatable.params[:search] = { value: 'msmith' }
3535
result = datatable.send(:build_conditions_for_datatable)
@@ -69,6 +69,11 @@
6969
end
7070

7171
describe '#build_conditions_for_selected_columns' do
72+
before(:each) do
73+
create(:user, username: 'johndoe', email: 'johndoe@example.com')
74+
create(:user, username: 'msmith', email: 'mary.smith@example.com')
75+
end
76+
7277
context 'columns include search query' do
7378
before do
7479
datatable.params[:columns]['0'][:search][:value] = 'doe'
@@ -143,4 +148,247 @@
143148
end
144149
end
145150

151+
describe 'filter conditions' do
152+
let(:datatable) { ReallyComplexDatatable.new(view) }
153+
154+
describe 'it can filter records with condition :range' do
155+
before(:each) do
156+
create(:user, username: 'johndoe', email: 'johndoe@example.com', last_name: 'Doe', created_at: '01/01/2000')
157+
create(:user, username: 'msmith', email: 'mary.smith@example.com', last_name: 'Smith', created_at: '01/02/2000')
158+
end
159+
160+
context 'when range is empty' do
161+
it 'should not filter records' do
162+
datatable.params[:columns]['5'][:search][:value] = '-'
163+
expect(datatable.data.size).to eq 2
164+
item = datatable.data.first
165+
expect(item[:last_name]).to eq 'Doe'
166+
end
167+
end
168+
169+
context 'when start date is filled' do
170+
it 'should filter records created after this date' do
171+
datatable.params[:columns]['5'][:search][:value] = '31/12/1999-'
172+
expect(datatable.data.size).to eq 2
173+
end
174+
end
175+
176+
context 'when end date is filled' do
177+
it 'should filter records created before this date' do
178+
datatable.params[:columns]['5'][:search][:value] = '-31/12/1999'
179+
expect(datatable.data.size).to eq 0
180+
end
181+
end
182+
183+
context 'when both date are filled' do
184+
it 'should filter records created between the range' do
185+
datatable.params[:columns]['5'][:search][:value] = '01/12/1999-15/01/2000'
186+
expect(datatable.data.size).to eq 1
187+
end
188+
end
189+
190+
context 'when another filter is active' do
191+
context 'when range is empty' do
192+
it 'should filter records' do
193+
datatable.params[:columns]['0'][:search][:value] = 'doe'
194+
datatable.params[:columns]['5'][:search][:value] = '-'
195+
expect(datatable.data.size).to eq 1
196+
item = datatable.data.first
197+
expect(item[:last_name]).to eq 'Doe'
198+
end
199+
end
200+
201+
context 'when start date is filled' do
202+
it 'should filter records' do
203+
datatable.params[:columns]['0'][:search][:value] = 'doe'
204+
datatable.params[:columns]['5'][:search][:value] = '01/12/1999-'
205+
expect(datatable.data.size).to eq 1
206+
item = datatable.data.first
207+
expect(item[:last_name]).to eq 'Doe'
208+
end
209+
end
210+
211+
context 'when end date is filled' do
212+
it 'should filter records' do
213+
datatable.params[:columns]['0'][:search][:value] = 'doe'
214+
datatable.params[:columns]['5'][:search][:value] = '-15/01/2000'
215+
expect(datatable.data.size).to eq 1
216+
item = datatable.data.first
217+
expect(item[:last_name]).to eq 'Doe'
218+
end
219+
end
220+
221+
context 'when both date are filled' do
222+
it 'should filter records' do
223+
datatable.params[:columns]['0'][:search][:value] = 'doe'
224+
datatable.params[:columns]['5'][:search][:value] = '01/12/1999-15/01/2000'
225+
expect(datatable.data.size).to eq 1
226+
item = datatable.data.first
227+
expect(item[:last_name]).to eq 'Doe'
228+
end
229+
end
230+
end
231+
end
232+
233+
describe 'it can filter records with condition :start_with' do
234+
before(:each) do
235+
create(:user, first_name: 'John')
236+
create(:user, first_name: 'Mary')
237+
end
238+
239+
it 'should filter records matching' do
240+
datatable.params[:columns]['2'][:search][:value] = 'jo'
241+
expect(datatable.data.size).to eq 1
242+
item = datatable.data.first
243+
expect(item[:first_name]).to eq 'John'
244+
end
245+
end
246+
247+
describe 'it can filter records with condition :end_with' do
248+
before(:each) do
249+
create(:user, last_name: 'John')
250+
create(:user, last_name: 'Mary')
251+
end
252+
253+
it 'should filter records matching' do
254+
datatable.params[:columns]['3'][:search][:value] = 'ry'
255+
expect(datatable.data.size).to eq 1
256+
item = datatable.data.first
257+
expect(item[:last_name]).to eq 'Mary'
258+
end
259+
end
260+
261+
describe 'it can filter records with condition :null_value' do
262+
before(:each) do
263+
create(:user, first_name: 'john', email: 'foo@bar.com')
264+
create(:user, first_name: 'mary', email: nil)
265+
end
266+
267+
context 'when condition is NULL' do
268+
it 'should filter records matching' do
269+
datatable.params[:columns]['1'][:search][:value] = 'NULL'
270+
expect(datatable.data.size).to eq 1
271+
item = datatable.data.first
272+
expect(item[:first_name]).to eq 'mary'
273+
end
274+
end
275+
276+
context 'when condition is !NULL' do
277+
it 'should filter records matching' do
278+
datatable.params[:columns]['1'][:search][:value] = '!NULL'
279+
expect(datatable.data.size).to eq 1
280+
item = datatable.data.first
281+
expect(item[:first_name]).to eq 'john'
282+
end
283+
end
284+
end
285+
286+
describe 'it can filter records with condition :eq' do
287+
let(:datatable) { ReallyComplexDatatableEq.new(view) }
288+
289+
before(:each) do
290+
create(:user, first_name: 'john', post_id: 1)
291+
create(:user, first_name: 'mary', post_id: 2)
292+
end
293+
294+
it 'should filter records matching' do
295+
datatable.params[:columns]['4'][:search][:value] = 1
296+
expect(datatable.data.size).to eq 1
297+
item = datatable.data.first
298+
expect(item[:first_name]).to eq 'john'
299+
end
300+
end
301+
302+
describe 'it can filter records with condition :not_eq' do
303+
let(:datatable) { ReallyComplexDatatableNotEq.new(view) }
304+
305+
before(:each) do
306+
create(:user, first_name: 'john', post_id: 1)
307+
create(:user, first_name: 'mary', post_id: 2)
308+
end
309+
310+
it 'should filter records matching' do
311+
datatable.params[:columns]['4'][:search][:value] = 1
312+
expect(datatable.data.size).to eq 1
313+
item = datatable.data.first
314+
expect(item[:first_name]).to eq 'mary'
315+
end
316+
end
317+
318+
describe 'it can filter records with condition :lt' do
319+
let(:datatable) { ReallyComplexDatatableLt.new(view) }
320+
321+
before(:each) do
322+
create(:user, first_name: 'john', post_id: 1)
323+
create(:user, first_name: 'mary', post_id: 2)
324+
end
325+
326+
it 'should filter records matching' do
327+
datatable.params[:columns]['4'][:search][:value] = 2
328+
expect(datatable.data.size).to eq 1
329+
item = datatable.data.first
330+
expect(item[:first_name]).to eq 'john'
331+
end
332+
end
333+
334+
describe 'it can filter records with condition :gt' do
335+
let(:datatable) { ReallyComplexDatatableGt.new(view) }
336+
337+
before(:each) do
338+
create(:user, first_name: 'john', post_id: 1)
339+
create(:user, first_name: 'mary', post_id: 2)
340+
end
341+
342+
it 'should filter records matching' do
343+
datatable.params[:columns]['4'][:search][:value] = 1
344+
expect(datatable.data.size).to eq 1
345+
item = datatable.data.first
346+
expect(item[:first_name]).to eq 'mary'
347+
end
348+
end
349+
350+
describe 'it can filter records with condition :lteq' do
351+
let(:datatable) { ReallyComplexDatatableLteq.new(view) }
352+
353+
before(:each) do
354+
create(:user, first_name: 'john', post_id: 1)
355+
create(:user, first_name: 'mary', post_id: 2)
356+
end
357+
358+
it 'should filter records matching' do
359+
datatable.params[:columns]['4'][:search][:value] = 2
360+
expect(datatable.data.size).to eq 2
361+
end
362+
end
363+
364+
describe 'it can filter records with condition :gteq' do
365+
let(:datatable) { ReallyComplexDatatableGteq.new(view) }
366+
367+
before(:each) do
368+
create(:user, first_name: 'john', post_id: 1)
369+
create(:user, first_name: 'mary', post_id: 2)
370+
end
371+
372+
it 'should filter records matching' do
373+
datatable.params[:columns]['4'][:search][:value] = 1
374+
expect(datatable.data.size).to eq 2
375+
end
376+
end
377+
378+
describe 'it can filter records with condition :in' do
379+
let(:datatable) { ReallyComplexDatatableIn.new(view) }
380+
381+
before(:each) do
382+
create(:user, first_name: 'john', post_id: 1)
383+
create(:user, first_name: 'mary', post_id: 2)
384+
end
385+
386+
it 'should filter records matching' do
387+
datatable.params[:columns]['4'][:search][:value] = [1]
388+
expect(datatable.data.size).to eq 1
389+
item = datatable.data.first
390+
expect(item[:first_name]).to eq 'john'
391+
end
392+
end
393+
end
146394
end

spec/factories/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
f.email { Faker::Internet.email }
55
f.first_name { Faker::Name.first_name }
66
f.last_name { Faker::Name.last_name }
7+
f.post_id { ((1..100).to_a).sample }
78
end
89
end

0 commit comments

Comments
 (0)