wvanbergen-scoped_search 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +13 -0
- data/{README → README.rdoc} +9 -4
- data/Rakefile +38 -20
- data/TODO.rdoc +6 -0
- data/tasks/github-gem.rake +90 -13
- data/test/database.yml +17 -0
- data/test/integration/api_test.rb +52 -0
- data/test/lib/test_models.rb +148 -0
- data/test/lib/test_schema.rb +66 -0
- data/test/test_helper.rb +30 -233
- data/test/{query_conditions_builder_test.rb → unit/query_conditions_builder_test.rb} +2 -2
- data/test/{query_language_test.rb → unit/query_language_test.rb} +2 -2
- data/test/unit/search_for_test.rb +122 -0
- metadata +32 -20
- data/CHANGELOG +0 -12
- data/README.textile +0 -76
- data/TODO +0 -15
- data/tasks/test.rake +0 -32
- data/test/search_for_test.rb +0 -166
data/test/search_for_test.rb
DELETED
@@ -1,166 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper'
|
2
|
-
|
3
|
-
class ScopedSearchTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
def setup
|
6
|
-
case ENV['DATABASE']
|
7
|
-
when 'mysql'
|
8
|
-
create_mysql_connection
|
9
|
-
when 'postgresql'
|
10
|
-
create_postgresql_connection
|
11
|
-
else 'sqlite3'
|
12
|
-
create_sqlite3_connection
|
13
|
-
end
|
14
|
-
InitialSchema.up
|
15
|
-
SearchTestModel.create_corpus!
|
16
|
-
Group.create_corpus!
|
17
|
-
Location.create_corpus!
|
18
|
-
Address.create_corpus!
|
19
|
-
User.create_corpus!
|
20
|
-
Client.create_corpus!
|
21
|
-
Office.create_corpus!
|
22
|
-
Note.create_corpus!
|
23
|
-
end
|
24
|
-
|
25
|
-
def teardown
|
26
|
-
InitialSchema.down
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_enabling
|
30
|
-
assert !SearchTestModel.respond_to?(:search_for)
|
31
|
-
SearchTestModel.searchable_on :string_field, :text_field, :date_field
|
32
|
-
assert SearchTestModel.respond_to?(:search_for)
|
33
|
-
|
34
|
-
assert_equal ActiveRecord::NamedScope::Scope, SearchTestModel.search_for('test').class
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_search_only_fields
|
38
|
-
SearchTestModel.searchable_on :only => [:string_field, :text_field, :date_field]
|
39
|
-
assert SearchTestModel.respond_to?(:search_for)
|
40
|
-
assert_equal SearchTestModel.scoped_search_fields.size, 3
|
41
|
-
assert SearchTestModel.scoped_search_fields.include?(:string_field)
|
42
|
-
assert SearchTestModel.scoped_search_fields.include?(:text_field)
|
43
|
-
assert SearchTestModel.scoped_search_fields.include?(:date_field)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_search_except_fields
|
47
|
-
SearchTestModel.searchable_on :except => [:id, :ignored_field, :created_at, :updated_at]
|
48
|
-
assert SearchTestModel.respond_to?(:search_for)
|
49
|
-
assert_equal SearchTestModel.scoped_search_fields.size, 3
|
50
|
-
assert SearchTestModel.scoped_search_fields.include?(:string_field)
|
51
|
-
assert SearchTestModel.scoped_search_fields.include?(:text_field)
|
52
|
-
assert SearchTestModel.scoped_search_fields.include?(:date_field)
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_search_with_only_and_except
|
56
|
-
# :except should be ignored if :only is specified.
|
57
|
-
SearchTestModel.searchable_on({:only => [:text_field], :except => [:text_field]})
|
58
|
-
assert SearchTestModel.respond_to?(:search_for)
|
59
|
-
assert_equal SearchTestModel.scoped_search_fields.size, 1
|
60
|
-
assert SearchTestModel.scoped_search_fields.include?(:text_field), ':except should be ignored if :only is specified'
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_search
|
64
|
-
SearchTestModel.searchable_on :string_field, :text_field, :date_field
|
65
|
-
|
66
|
-
assert_equal SearchTestModel.count, SearchTestModel.search_for('').count
|
67
|
-
assert_equal 0, SearchTestModel.search_for('456').count
|
68
|
-
assert_equal 2, SearchTestModel.search_for('hays').count
|
69
|
-
assert_equal 1, SearchTestModel.search_for('hay ob').count
|
70
|
-
assert_equal 15, SearchTestModel.search_for('o').count
|
71
|
-
assert_equal 2, SearchTestModel.search_for('-o').count
|
72
|
-
assert_equal 15, SearchTestModel.search_for('-Jim').count
|
73
|
-
assert_equal 1, SearchTestModel.search_for('Jim -Bush').count
|
74
|
-
assert_equal 1, SearchTestModel.search_for('"Hello World" -"Goodnight Moon"').count
|
75
|
-
assert_equal 2, SearchTestModel.search_for('Wes OR Bob').count
|
76
|
-
assert_equal 3, SearchTestModel.search_for('"Happy cow" OR "Sad Frog"').count
|
77
|
-
assert_equal 3, SearchTestModel.search_for('"Man made" OR Dogs').count
|
78
|
-
assert_equal 2, SearchTestModel.search_for('Cows OR "Frog Toys"').count
|
79
|
-
|
80
|
-
# ** DATES **
|
81
|
-
#
|
82
|
-
# The next two dates are invalid therefore it will be ignored.
|
83
|
-
# Since it is just a date being searched for it will also
|
84
|
-
# be searched for in text fields regardless of whether or
|
85
|
-
# not it is a valid date.
|
86
|
-
assert_equal 0, SearchTestModel.search_for('2/30/1980').count
|
87
|
-
assert_equal 0, SearchTestModel.search_for('99/99/9999').count
|
88
|
-
|
89
|
-
assert_equal 1, SearchTestModel.search_for('9/27/1980').count
|
90
|
-
assert_equal 1, SearchTestModel.search_for('hays 9/27/1980').count
|
91
|
-
assert_equal 0, SearchTestModel.search_for('hays 2/30/1980').count
|
92
|
-
|
93
|
-
assert_equal 1, SearchTestModel.search_for('< 12/01/1980').count
|
94
|
-
assert_equal 6, SearchTestModel.search_for('> 2006/1/1').count
|
95
|
-
|
96
|
-
assert_equal 5, SearchTestModel.search_for('< 12/26/2002').count
|
97
|
-
assert_equal 6, SearchTestModel.search_for('<= 12/26/2002').count
|
98
|
-
|
99
|
-
assert_equal 6, SearchTestModel.search_for('> 2/5/2005').count
|
100
|
-
assert_equal 7, SearchTestModel.search_for('>= 2/5/2005').count
|
101
|
-
|
102
|
-
assert_equal 3, SearchTestModel.search_for('1/1/2005 TO 1/1/2007').count
|
103
|
-
|
104
|
-
assert_equal 2, SearchTestModel.search_for('Happy 1/1/2005 TO 1/1/2007').count
|
105
|
-
|
106
|
-
# This should return one with a date of 7/15/2006 found in the text.
|
107
|
-
assert_equal 2, SearchTestModel.search_for('7/15/2006').count
|
108
|
-
end
|
109
|
-
|
110
|
-
def test_search_belongs_to_association
|
111
|
-
User.searchable_on :first_name, :last_name, :group_name
|
112
|
-
|
113
|
-
assert_equal User.count, User.search_for('').count
|
114
|
-
assert_equal 1, User.search_for('Wes').count
|
115
|
-
assert_equal 2, User.search_for('System Administrator').count
|
116
|
-
assert_equal 2, User.search_for('Managers').count
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_search_has_many_association
|
120
|
-
User.searchable_on :first_name, :last_name, :notes_title, :notes_content
|
121
|
-
|
122
|
-
assert_equal User.count, User.search_for('').count
|
123
|
-
assert_equal 2, User.search_for('Router').count
|
124
|
-
assert_equal 1, User.search_for('milk').count
|
125
|
-
assert_equal 1, User.search_for('"Spec Tests"').count
|
126
|
-
assert_equal 0, User.search_for('Wes "Spec Tests"').count
|
127
|
-
end
|
128
|
-
|
129
|
-
def test_search_has_many_through_association
|
130
|
-
User.searchable_on :first_name, :last_name, :clients_first_name, :clients_last_name
|
131
|
-
|
132
|
-
assert_equal User.count, User.search_for('').count
|
133
|
-
assert_equal 2, User.search_for('Smith').count
|
134
|
-
assert_equal 1, User.search_for('Sam').count
|
135
|
-
assert_equal 1, User.search_for('Johnson').count
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_search_has_one_association
|
139
|
-
User.searchable_on :first_name, :last_name, :address_street, :address_city, :address_state, :address_postal_code
|
140
|
-
|
141
|
-
assert_equal User.count, User.search_for('').count
|
142
|
-
assert_equal 1, User.search_for('Fernley').count
|
143
|
-
assert_equal 4, User.search_for('NV').count
|
144
|
-
assert_equal 1, User.search_for('Haskell').count
|
145
|
-
assert_equal 2, User.search_for('89434').count
|
146
|
-
end
|
147
|
-
|
148
|
-
def test_search_has_and_belongs_to_many_association
|
149
|
-
User.searchable_on :first_name, :last_name, :locations_name
|
150
|
-
|
151
|
-
assert_equal User.count, User.search_for('').count
|
152
|
-
assert_equal 2, User.search_for('Office').count
|
153
|
-
assert_equal 1, User.search_for('Store').count
|
154
|
-
assert_equal 1, User.search_for('John Office').count
|
155
|
-
end
|
156
|
-
|
157
|
-
def test_search_with_very_long_query
|
158
|
-
User.searchable_on :first_name, :last_name, :address_street, :address_city, :address_state, :address_postal_code
|
159
|
-
really_long_string = ''
|
160
|
-
10000.times {really_long_string << 'really long string'}
|
161
|
-
assert_equal 0, User.search_for(really_long_string).count
|
162
|
-
end
|
163
|
-
|
164
|
-
end
|
165
|
-
|
166
|
-
|