|
6 | 6 | let(:datatable) { ComplexDatatable.new(view) }
|
7 | 7 | let(:records) { User.all }
|
8 | 8 |
|
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 |
| - |
14 | 9 | describe '#filter_records' do
|
15 | 10 | it 'requires a records collection as argument' do
|
16 | 11 | expect { datatable.send(:filter_records) }.to raise_error(ArgumentError)
|
|
30 | 25 | end
|
31 | 26 |
|
32 | 27 | 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 | + |
33 | 33 | it 'returns an Arel object' do
|
34 | 34 | datatable.params[:search] = { value: 'msmith' }
|
35 | 35 | result = datatable.send(:build_conditions_for_datatable)
|
|
69 | 69 | end
|
70 | 70 |
|
71 | 71 | 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 | + |
72 | 77 | context 'columns include search query' do
|
73 | 78 | before do
|
74 | 79 | datatable.params[:columns]['0'][:search][:value] = 'doe'
|
|
143 | 148 | end
|
144 | 149 | end
|
145 | 150 |
|
| 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 |
146 | 394 | end
|
0 commit comments