From 74da2c50b370ef0d40bd795cc51d854e718e3f01 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Tue, 19 Mar 2024 21:56:42 -0500 Subject: [PATCH] Adding searchable false test case --- CHANGELOG.md | 2 ++ .../datatable/column_spec.rb | 8 +++++ .../datatable/datatable_spec.rb | 4 +-- .../orm/active_record_filter_records_spec.rb | 35 ++++++++++++------- spec/support/datatables/complex_datatable.rb | 2 ++ spec/support/helpers/params.rb | 6 ++++ spec/support/models/user.rb | 8 +++++ 7 files changed, 51 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f915a5e..ca0e88a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # CHANGELOG +* Implementing `searchable: false` tests + ## 1.5.0 (2024-04-08) * Drop support of Rails 5.2 diff --git a/spec/ajax-datatables-rails/datatable/column_spec.rb b/spec/ajax-datatables-rails/datatable/column_spec.rb index 772694a..69738ab 100644 --- a/spec/ajax-datatables-rails/datatable/column_spec.rb +++ b/spec/ajax-datatables-rails/datatable/column_spec.rb @@ -128,6 +128,14 @@ end end + describe 'unsearchable column' do + let(:column) { datatable.datatable.columns.find{ |c| c.data == 'email_hash' } } + + it 'is not searchable' do + expect(column.searchable?).to eql(false) + end + end + describe '#formatter' do let(:datatable) { DatatableWithFormater.new(sample_params) } let(:column) { datatable.datatable.columns.find { |c| c.data == 'last_name' } } diff --git a/spec/ajax-datatables-rails/datatable/datatable_spec.rb b/spec/ajax-datatables-rails/datatable/datatable_spec.rb index 6d26628..7e8f977 100644 --- a/spec/ajax-datatables-rails/datatable/datatable_spec.rb +++ b/spec/ajax-datatables-rails/datatable/datatable_spec.rb @@ -40,8 +40,8 @@ end shared_examples 'columns methods' do - it 'has 7 columns' do - expect(datatable.columns.count).to eq(7) + it 'has 8 columns' do + expect(datatable.columns.count).to eq(8) end it 'child class' do diff --git a/spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb b/spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb index 6ee96bb..8f59020 100644 --- a/spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb +++ b/spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb @@ -12,12 +12,23 @@ expect { datatable.filter_records }.to raise_error(ArgumentError) end - it 'performs a simple search first' do - datatable.params[:search] = { value: 'msmith' } - expect(datatable).to receive(:build_conditions_for_datatable) - datatable.filter_records(records) + context 'with simple search' do + before do + datatable.params[:search] = { value: 'msmith' } + end + + it 'performs a simple search first' do + expect(datatable).to receive(:build_conditions_for_datatable) + datatable.filter_records(records) + end + + it 'does not search unsearchable fields' do + criteria = datatable.filter_records(records) + expect(criteria.to_sql).not_to include('email_hash') + end end + it 'performs a composite search second' do datatable.params[:search] = { value: '' } expect(datatable).to receive(:build_conditions_for_selected_columns) @@ -246,7 +257,7 @@ context 'when range is empty' do it 'does not filter records' do - datatable.params[:columns]['6'][:search][:value] = '-' + datatable.params[:columns]['7'][:search][:value] = '-' expect(datatable.data.size).to eq 2 item = datatable.data.first expect(item[:last_name]).to eq 'Doe' @@ -255,21 +266,21 @@ context 'when start date is filled' do it 'filters records created after this date' do - datatable.params[:columns]['6'][:search][:value] = '31/12/1999-' + datatable.params[:columns]['7'][:search][:value] = '31/12/1999-' expect(datatable.data.size).to eq 2 end end context 'when end date is filled' do it 'filters records created before this date' do - datatable.params[:columns]['6'][:search][:value] = '-31/12/1999' + datatable.params[:columns]['7'][:search][:value] = '-31/12/1999' expect(datatable.data.size).to eq 0 end end context 'when both date are filled' do it 'filters records created between the range' do - datatable.params[:columns]['6'][:search][:value] = '01/12/1999-15/01/2000' + datatable.params[:columns]['7'][:search][:value] = '01/12/1999-15/01/2000' expect(datatable.data.size).to eq 1 end end @@ -278,7 +289,7 @@ context 'when range is empty' do it 'filters records' do datatable.params[:columns]['0'][:search][:value] = 'doe' - datatable.params[:columns]['6'][:search][:value] = '-' + datatable.params[:columns]['7'][:search][:value] = '-' expect(datatable.data.size).to eq 1 item = datatable.data.first expect(item[:last_name]).to eq 'Doe' @@ -288,7 +299,7 @@ context 'when start date is filled' do it 'filters records' do datatable.params[:columns]['0'][:search][:value] = 'doe' - datatable.params[:columns]['6'][:search][:value] = '01/12/1999-' + datatable.params[:columns]['7'][:search][:value] = '01/12/1999-' expect(datatable.data.size).to eq 1 item = datatable.data.first expect(item[:last_name]).to eq 'Doe' @@ -298,7 +309,7 @@ context 'when end date is filled' do it 'filters records' do datatable.params[:columns]['0'][:search][:value] = 'doe' - datatable.params[:columns]['6'][:search][:value] = '-15/01/2000' + datatable.params[:columns]['7'][:search][:value] = '-15/01/2000' expect(datatable.data.size).to eq 1 item = datatable.data.first expect(item[:last_name]).to eq 'Doe' @@ -308,7 +319,7 @@ context 'when both date are filled' do it 'filters records' do datatable.params[:columns]['0'][:search][:value] = 'doe' - datatable.params[:columns]['6'][:search][:value] = '01/12/1999-15/01/2000' + datatable.params[:columns]['7'][:search][:value] = '01/12/1999-15/01/2000' expect(datatable.data.size).to eq 1 item = datatable.data.first expect(item[:last_name]).to eq 'Doe' diff --git a/spec/support/datatables/complex_datatable.rb b/spec/support/datatables/complex_datatable.rb index 6969f74..ece7ee4 100644 --- a/spec/support/datatables/complex_datatable.rb +++ b/spec/support/datatables/complex_datatable.rb @@ -9,6 +9,7 @@ def view_columns last_name: { source: 'User.last_name' }, full_name: { source: 'full_name' }, post_id: { source: 'User.post_id', orderable: false }, + email_hash: { source: 'email_hash', searchable: false }, created_at: { source: 'User.created_at' }, } end @@ -22,6 +23,7 @@ def data last_name: record.last_name, full_name: record.full_name, post_id: record.post_id, + email_hash: record.email_hash, created_at: record.created_at, } end diff --git a/spec/support/helpers/params.rb b/spec/support/helpers/params.rb index 406b038..dff2e79 100644 --- a/spec/support/helpers/params.rb +++ b/spec/support/helpers/params.rb @@ -43,6 +43,12 @@ def sample_params } }, '6' => { + 'data' => 'email_hash', 'name' => '', 'searchable' => 'false', 'orderable' => 'true', + 'search' => { + 'value' => '', 'regex' => 'false' + } + }, + '7' => { 'data' => 'created_at', 'name' => '', 'searchable' => 'true', 'orderable' => 'true', 'search' => { 'value' => '', 'regex' => 'false' diff --git a/spec/support/models/user.rb b/spec/support/models/user.rb index 7ad18b2..887efd9 100644 --- a/spec/support/models/user.rb +++ b/spec/support/models/user.rb @@ -1,7 +1,15 @@ # frozen_string_literal: true +require 'digest' + class User < ActiveRecord::Base def full_name "#{first_name} #{last_name}" end + + def email_hash + return nil if email.nil? + + Digest::SHA256.hexdigest email + end end