|
1 | 1 | require 'test_helper'
|
2 | 2 |
|
3 | 3 | module Rails3JQueryAutocomplete
|
4 |
| - module Orm |
5 |
| - class ActiveRecordTest < Test::Unit::TestCase |
6 |
| - include Rails3JQueryAutocomplete::Orm::ActiveRecord |
7 |
| - |
8 |
| - context "#get_autocomplete_order" do |
9 |
| - context 'order is specified' do |
10 |
| - should 'returns that order option' do |
11 |
| - assert_equal "field ASC", get_autocomplete_order(:field, {:order => 'field ASC'}) |
12 |
| - end |
13 |
| - end |
14 |
| - |
15 |
| - context 'no order is specified' do |
16 |
| - should 'return the order clause by the field ASC' do |
17 |
| - assert_equal "field ASC", get_autocomplete_order(:field, {}) |
18 |
| - end |
19 |
| - |
20 |
| - context 'a different model is specified' do |
21 |
| - should 'return the order clause by the table_name.field ASC' do |
22 |
| - model = Object.new |
23 |
| - mock(model).table_name { 'table_name' } |
24 |
| - assert_equal "table_name.field ASC", get_autocomplete_order(:field, {}, model) |
25 |
| - end |
26 |
| - end |
27 |
| - end |
28 |
| - end |
29 |
| - |
30 |
| - context '#get_autocomplete_items' do |
31 |
| - should 'retrieve the items from ActiveRecord' do |
32 |
| - class Dog ; end |
33 |
| - |
34 |
| - model = Dog |
35 |
| - scoped = [] |
36 |
| - whered = [] |
37 |
| - term = 'query' |
38 |
| - method = :field |
39 |
| - |
40 |
| - options = { |
41 |
| - :model => model, |
42 |
| - :term => term, |
43 |
| - :method => method, |
44 |
| - :options => {} |
45 |
| - } |
46 |
| - |
47 |
| - mock(self).get_autocomplete_limit(anything) { 10 } |
48 |
| - mock(self).get_autocomplete_order(anything, anything, anything) { "order ASC" } |
49 |
| - mock(self).get_autocomplete_select_clause(model, method, {}) { ["field"] } |
50 |
| - mock(self).get_autocomplete_where_clause(model, term, method, {}) { ["WHERE something"] } |
51 |
| - mock(model).table_name.times(any_times) { 'model_table_name' } |
52 |
| - |
53 |
| - mock(model).scoped { model } |
54 |
| - mock(model).select(["field"]) { model } |
55 |
| - mock(model).where(["WHERE something"]).mock!.limit(10).mock!. |
56 |
| - order("order ASC") { 1 } |
57 |
| - |
58 |
| - assert_equal 1, get_autocomplete_items(options) |
59 |
| - end |
60 |
| - end |
61 |
| - |
62 |
| - context '#get_autocomplete_select_clause' do |
63 |
| - setup do |
64 |
| - @model = Object.new |
65 |
| - mock(@model).table_name { 'table_name' } |
66 |
| - mock(@model).primary_key { 'id' } |
67 |
| - end |
68 |
| - |
69 |
| - should 'create a select clause' do |
70 |
| - assert_equal ["table_name.id", "table_name.method"], |
71 |
| - get_autocomplete_select_clause(@model, :method, {}) |
72 |
| - end |
73 |
| - |
74 |
| - context 'with extra options' do |
75 |
| - should 'return those extra fields on the clause' do |
76 |
| - options = {:extra_data => ['table_name.created_at']} |
77 |
| - |
78 |
| - assert_equal ["table_name.id", "table_name.method", "table_name.created_at"], |
79 |
| - get_autocomplete_select_clause(@model, :method, options) |
80 |
| - end |
81 |
| - end |
82 |
| - end |
83 |
| - |
84 |
| - context '#get_autocomplete_where_clause' do |
85 |
| - setup do |
86 |
| - @model = Object.new |
87 |
| - mock(@model).table_name { 'table_name' } |
88 |
| - |
89 |
| - @term = 'query' |
90 |
| - @options = {} |
91 |
| - @method = :method |
92 |
| - end |
93 |
| - |
94 |
| - context 'Not Postgres' do |
95 |
| - should 'return options for where' do |
96 |
| - mock(self).postgres? { false } |
97 |
| - assert_equal ["LOWER(table_name.method) LIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options) |
98 |
| - end |
99 |
| - end |
100 |
| - |
101 |
| - context 'Postgres' do |
102 |
| - should 'return options for where with ILIKE' do |
103 |
| - mock(self).postgres? { true } |
104 |
| - assert_equal ["LOWER(table_name.method) ILIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options) |
105 |
| - end |
106 |
| - end |
107 |
| - |
108 |
| - context 'full search' do |
109 |
| - should 'return options for where with the term sourrounded by %%' do |
110 |
| - mock(self).postgres? { false } |
111 |
| - @options[:full] = true |
112 |
| - assert_equal ["LOWER(table_name.method) LIKE ?", "%query%"], get_autocomplete_where_clause(@model, @term, @method, @options) |
113 |
| - end |
114 |
| - end |
115 |
| - end |
116 |
| - |
117 |
| - context '#postgres?' do |
118 |
| - should 'return nil if PGconn is not defined' do |
119 |
| - assert_nil self.postgres? |
120 |
| - end |
121 |
| - |
122 |
| - should 'return true if PGconn is defined' do |
123 |
| - class ::PGconn ; end |
124 |
| - |
125 |
| - assert self.postgres? |
126 |
| - end |
127 |
| - end |
128 |
| - end |
129 |
| - end |
| 4 | + module Orm |
| 5 | + class ActiveRecordTest < Test::Unit::TestCase |
| 6 | + include Rails3JQueryAutocomplete::Orm::ActiveRecord |
| 7 | + |
| 8 | + context "#get_autocomplete_order" do |
| 9 | + context 'order is specified' do |
| 10 | + should 'returns that order option' do |
| 11 | + assert_equal "field ASC", get_autocomplete_order(:field, {:order => 'field ASC'}) |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + context 'no order is specified' do |
| 16 | + should 'return the order clause by the field ASC' do |
| 17 | + assert_equal "field ASC", get_autocomplete_order(:field, {}) |
| 18 | + end |
| 19 | + |
| 20 | + context 'a different model is specified' do |
| 21 | + should 'return the order clause by the table_name.field ASC' do |
| 22 | + model = Object.new |
| 23 | + mock(model).table_name { 'table_name' } |
| 24 | + assert_equal "table_name.field ASC", get_autocomplete_order(:field, {}, model) |
| 25 | + end |
| 26 | + end |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + context '#get_autocomplete_items' do |
| 31 | + should 'retrieve the items from ActiveRecord' do |
| 32 | + class Dog ; end |
| 33 | + |
| 34 | + model = Dog |
| 35 | + scoped = [] |
| 36 | + whered = [] |
| 37 | + term = 'query' |
| 38 | + method = :field |
| 39 | + |
| 40 | + options = { |
| 41 | + :model => model, |
| 42 | + :term => term, |
| 43 | + :method => method, |
| 44 | + :options => {} |
| 45 | + } |
| 46 | + |
| 47 | + mock(self).get_autocomplete_limit(anything) { 10 } |
| 48 | + mock(self).get_autocomplete_order(anything, anything, anything) { "order ASC" } |
| 49 | + mock(self).get_autocomplete_select_clause(model, method, {}) { ["field"] } |
| 50 | + mock(self).get_autocomplete_where_clause(model, term, method, {}) { ["WHERE something"] } |
| 51 | + mock(model).table_name.times(any_times) { 'model_table_name' } |
| 52 | + |
| 53 | + mock(model).scoped { model } |
| 54 | + mock(model).select(["field"]) { model } |
| 55 | + mock(model).where(["WHERE something"]).mock!.limit(10).mock!. |
| 56 | + order("order ASC") { 1 } |
| 57 | + |
| 58 | + assert_equal 1, get_autocomplete_items(options) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + context '#get_autocomplete_select_clause' do |
| 63 | + setup do |
| 64 | + @model = Object.new |
| 65 | + mock(@model).table_name { 'table_name' } |
| 66 | + mock(@model).primary_key { 'id' } |
| 67 | + end |
| 68 | + |
| 69 | + should 'create a select clause' do |
| 70 | + assert_equal ["table_name.id", "table_name.method"], |
| 71 | + get_autocomplete_select_clause(@model, :method, {}) |
| 72 | + end |
| 73 | + |
| 74 | + context 'with extra options' do |
| 75 | + should 'return those extra fields on the clause' do |
| 76 | + options = {:extra_data => ['table_name.created_at']} |
| 77 | + |
| 78 | + assert_equal ["table_name.id", "table_name.method", "table_name.created_at"], |
| 79 | + get_autocomplete_select_clause(@model, :method, options) |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context '#get_autocomplete_where_clause' do |
| 85 | + setup do |
| 86 | + @model = Object.new |
| 87 | + mock(@model).table_name { 'table_name' } |
| 88 | + |
| 89 | + @term = 'query' |
| 90 | + @options = {} |
| 91 | + @method = :method |
| 92 | + end |
| 93 | + |
| 94 | + context 'Not Postgres' do |
| 95 | + should 'return options for where' do |
| 96 | + mock(self).postgres? { false } |
| 97 | + assert_equal ["LOWER(table_name.method) LIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + context 'Postgres' do |
| 102 | + should 'return options for where with ILIKE' do |
| 103 | + mock(self).postgres? { true } |
| 104 | + assert_equal ["LOWER(table_name.method) ILIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options) |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + context 'full search' do |
| 109 | + should 'return options for where with the term sourrounded by %%' do |
| 110 | + mock(self).postgres? { false } |
| 111 | + @options[:full] = true |
| 112 | + assert_equal ["LOWER(table_name.method) LIKE ?", "%query%"], get_autocomplete_where_clause(@model, @term, @method, @options) |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + context '#postgres?' do |
| 118 | + should 'return nil if PGconn is not defined' do |
| 119 | + assert_nil self.postgres? |
| 120 | + end |
| 121 | + |
| 122 | + should 'return true if PGconn is defined' do |
| 123 | + class ::PGconn ; end |
| 124 | + |
| 125 | + assert self.postgres? |
| 126 | + end |
| 127 | + end |
| 128 | + end |
| 129 | + end |
130 | 130 | end
|
0 commit comments