Skip to content

Commit

Permalink
The Postgres query differences are now treated at
Browse files Browse the repository at this point in the history
model level. This commits just adds the tests.

The actual commit for this change was merged from
a pull request by @ljulien
  • Loading branch information
David Padilla committed Feb 6, 2012
1 parent bd1c788 commit 8e97e28
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/rails3-jquery-autocomplete/orm/active_record.rb
Expand Up @@ -43,8 +43,8 @@ def get_autocomplete_where_clause(model, term, method, options)
["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]
end

def postgres? model
# Figure out if this particular model uses the PostgreSQL adapter
def postgres?(model)
# Figure out if this particular model uses the PostgreSQL adapter
model.connection.class.to_s.match(/PostgreSQLAdapter/)
end
end
Expand Down
33 changes: 24 additions & 9 deletions test/lib/rails3-jquery-autocomplete/orm/active_record_test.rb
Expand Up @@ -84,7 +84,7 @@ class Dog ; end
context '#get_autocomplete_where_clause' do
setup do
@model = Object.new
mock(@model).table_name { 'table_name' }
mock(@model).table_name { 'table_name' }

@term = 'query'
@options = {}
Expand All @@ -93,36 +93,51 @@ class Dog ; end

context 'Not Postgres' do
should 'return options for where' do
mock(self).postgres? { false }
mock(self).postgres?(@model) { 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 }
mock(self).postgres?(@model) { 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 }
mock(self).postgres?(@model) { 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?
setup do
@model = stub
end

should 'return true if PGconn is defined' do
class ::PGconn ; end
context 'the connection class is not postgres' do
setup do
mock(@model).connection { stub }
end

assert self.postgres?
should 'return nil if the connection class matches PostgreSQLAdapter' do
assert_nil self.postgres?(@model)
end
end

context 'the connection class matches PostgreSQLAdapter' do
setup do
class PostgreSQLAdapter; end
mock(@model).connection { PostgreSQLAdapter.new }
end

should 'return true' do
assert self.postgres?(@model)
end
end
end
end
Expand Down

0 comments on commit 8e97e28

Please sign in to comment.