forked from crowdint/rails3-jquery-autocomplete
/
active_record_test.rb
118 lines (99 loc) · 3.5 KB
/
active_record_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
end
end
end