forked from crowdint/rails3-jquery-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Padilla
committed
Sep 21, 2011
1 parent
6effc6e
commit 58d629b
Showing
1 changed file
with
126 additions
and
126 deletions.
There are no files selected for viewing
252 changes: 126 additions & 126 deletions
252
test/lib/rails3-jquery-autocomplete/orm/active_record_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,130 @@ | ||
require 'test_helper' | ||
|
||
module Rails3JQueryAutocomplete | ||
module Orm | ||
class ActiveRecordTest < Test::Unit::TestCase | ||
include Rails3JQueryAutocomplete::Orm::ActiveRecord | ||
|
||
context "#get_autocomplete_order" do | ||
context 'order is specified' do | ||
should 'returns that order option' do | ||
assert_equal "field ASC", get_autocomplete_order(:field, {:order => 'field ASC'}) | ||
end | ||
end | ||
|
||
context 'no order is specified' do | ||
should 'return the order clause by the field ASC' do | ||
assert_equal "field ASC", get_autocomplete_order(:field, {}) | ||
end | ||
|
||
context 'a different model is specified' do | ||
should 'return the order clause by the table_name.field ASC' do | ||
model = Object.new | ||
mock(model).table_name { 'table_name' } | ||
assert_equal "table_name.field ASC", get_autocomplete_order(:field, {}, model) | ||
end | ||
end | ||
end | ||
end | ||
|
||
context '#get_autocomplete_items' do | ||
should 'retrieve the items from ActiveRecord' do | ||
class Dog ; end | ||
|
||
model = Dog | ||
scoped = [] | ||
whered = [] | ||
term = 'query' | ||
method = :field | ||
|
||
options = { | ||
:model => model, | ||
:term => term, | ||
:method => method, | ||
:options => {} | ||
} | ||
|
||
mock(self).get_autocomplete_limit(anything) { 10 } | ||
mock(self).get_autocomplete_order(anything, anything, anything) { "order ASC" } | ||
mock(self).get_autocomplete_select_clause(model, method, {}) { ["field"] } | ||
mock(self).get_autocomplete_where_clause(model, term, method, {}) { ["WHERE something"] } | ||
mock(model).table_name.times(any_times) { 'model_table_name' } | ||
|
||
mock(model).scoped { model } | ||
mock(model).select(["field"]) { model } | ||
mock(model).where(["WHERE something"]).mock!.limit(10).mock!. | ||
order("order ASC") { 1 } | ||
|
||
assert_equal 1, get_autocomplete_items(options) | ||
end | ||
end | ||
|
||
context '#get_autocomplete_select_clause' do | ||
setup do | ||
@model = Object.new | ||
mock(@model).table_name { 'table_name' } | ||
mock(@model).primary_key { 'id' } | ||
end | ||
|
||
should 'create a select clause' do | ||
assert_equal ["table_name.id", "table_name.method"], | ||
get_autocomplete_select_clause(@model, :method, {}) | ||
end | ||
|
||
context 'with extra options' do | ||
should 'return those extra fields on the clause' do | ||
options = {:extra_data => ['table_name.created_at']} | ||
|
||
assert_equal ["table_name.id", "table_name.method", "table_name.created_at"], | ||
get_autocomplete_select_clause(@model, :method, options) | ||
end | ||
end | ||
end | ||
|
||
context '#get_autocomplete_where_clause' do | ||
setup do | ||
@model = Object.new | ||
mock(@model).table_name { 'table_name' } | ||
|
||
@term = 'query' | ||
@options = {} | ||
@method = :method | ||
end | ||
|
||
context 'Not Postgres' do | ||
should 'return options for where' do | ||
mock(self).postgres? { false } | ||
assert_equal ["LOWER(table_name.method) LIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options) | ||
end | ||
end | ||
|
||
context 'Postgres' do | ||
should 'return options for where with ILIKE' do | ||
mock(self).postgres? { true } | ||
assert_equal ["LOWER(table_name.method) ILIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options) | ||
end | ||
end | ||
|
||
context 'full search' do | ||
should 'return options for where with the term sourrounded by %%' do | ||
mock(self).postgres? { false } | ||
@options[:full] = true | ||
assert_equal ["LOWER(table_name.method) LIKE ?", "%query%"], get_autocomplete_where_clause(@model, @term, @method, @options) | ||
end | ||
end | ||
end | ||
|
||
context '#postgres?' do | ||
should 'return nil if PGconn is not defined' do | ||
assert_nil self.postgres? | ||
end | ||
|
||
should 'return true if PGconn is defined' do | ||
class ::PGconn ; end | ||
|
||
assert self.postgres? | ||
end | ||
end | ||
end | ||
end | ||
module Orm | ||
class ActiveRecordTest < Test::Unit::TestCase | ||
include Rails3JQueryAutocomplete::Orm::ActiveRecord | ||
|
||
context "#get_autocomplete_order" do | ||
context 'order is specified' do | ||
should 'returns that order option' do | ||
assert_equal "field ASC", get_autocomplete_order(:field, {:order => 'field ASC'}) | ||
end | ||
end | ||
|
||
context 'no order is specified' do | ||
should 'return the order clause by the field ASC' do | ||
assert_equal "field ASC", get_autocomplete_order(:field, {}) | ||
end | ||
|
||
context 'a different model is specified' do | ||
should 'return the order clause by the table_name.field ASC' do | ||
model = Object.new | ||
mock(model).table_name { 'table_name' } | ||
assert_equal "table_name.field ASC", get_autocomplete_order(:field, {}, model) | ||
end | ||
end | ||
end | ||
end | ||
|
||
context '#get_autocomplete_items' do | ||
should 'retrieve the items from ActiveRecord' do | ||
class Dog ; end | ||
|
||
model = Dog | ||
scoped = [] | ||
whered = [] | ||
term = 'query' | ||
method = :field | ||
|
||
options = { | ||
:model => model, | ||
:term => term, | ||
:method => method, | ||
:options => {} | ||
} | ||
|
||
mock(self).get_autocomplete_limit(anything) { 10 } | ||
mock(self).get_autocomplete_order(anything, anything, anything) { "order ASC" } | ||
mock(self).get_autocomplete_select_clause(model, method, {}) { ["field"] } | ||
mock(self).get_autocomplete_where_clause(model, term, method, {}) { ["WHERE something"] } | ||
mock(model).table_name.times(any_times) { 'model_table_name' } | ||
|
||
mock(model).scoped { model } | ||
mock(model).select(["field"]) { model } | ||
mock(model).where(["WHERE something"]).mock!.limit(10).mock!. | ||
order("order ASC") { 1 } | ||
|
||
assert_equal 1, get_autocomplete_items(options) | ||
end | ||
end | ||
|
||
context '#get_autocomplete_select_clause' do | ||
setup do | ||
@model = Object.new | ||
mock(@model).table_name { 'table_name' } | ||
mock(@model).primary_key { 'id' } | ||
end | ||
|
||
should 'create a select clause' do | ||
assert_equal ["table_name.id", "table_name.method"], | ||
get_autocomplete_select_clause(@model, :method, {}) | ||
end | ||
|
||
context 'with extra options' do | ||
should 'return those extra fields on the clause' do | ||
options = {:extra_data => ['table_name.created_at']} | ||
|
||
assert_equal ["table_name.id", "table_name.method", "table_name.created_at"], | ||
get_autocomplete_select_clause(@model, :method, options) | ||
end | ||
end | ||
end | ||
|
||
context '#get_autocomplete_where_clause' do | ||
setup do | ||
@model = Object.new | ||
mock(@model).table_name { 'table_name' } | ||
|
||
@term = 'query' | ||
@options = {} | ||
@method = :method | ||
end | ||
|
||
context 'Not Postgres' do | ||
should 'return options for where' do | ||
mock(self).postgres? { false } | ||
assert_equal ["LOWER(table_name.method) LIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options) | ||
end | ||
end | ||
|
||
context 'Postgres' do | ||
should 'return options for where with ILIKE' do | ||
mock(self).postgres? { true } | ||
assert_equal ["LOWER(table_name.method) ILIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options) | ||
end | ||
end | ||
|
||
context 'full search' do | ||
should 'return options for where with the term sourrounded by %%' do | ||
mock(self).postgres? { false } | ||
@options[:full] = true | ||
assert_equal ["LOWER(table_name.method) LIKE ?", "%query%"], get_autocomplete_where_clause(@model, @term, @method, @options) | ||
end | ||
end | ||
end | ||
|
||
context '#postgres?' do | ||
should 'return nil if PGconn is not defined' do | ||
assert_nil self.postgres? | ||
end | ||
|
||
should 'return true if PGconn is defined' do | ||
class ::PGconn ; end | ||
|
||
assert self.postgres? | ||
end | ||
end | ||
end | ||
end | ||
end |