forked from grosser/search_do
/
hyper_estraier_spec.rb
205 lines (165 loc) · 6.19 KB
/
hyper_estraier_spec.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
require File.expand_path("../spec_helper", File.dirname(__FILE__))
describe SearchDo::Backends::HyperEstraier do
before do
@backend = SearchDo::Backends::HyperEstraier.new(Story, ActiveRecord::Base.configurations["test"]["estraier"])
end
describe "#index // there is no index" do
before do
@backend.connection.should_receive(:search).and_return(nil)
end
it "should == []" do
@backend.index.should == []
end
end
describe "#add_to_index(['foo',nil]) // include nil to indexed text" do
it "should not raise error" do
lambda{
@backend.add_to_index(['foo', nil], {})
}.should_not raise_error
end
end
describe "#add_to_index([Time.local(2008,9,17)]) // include nil to indexed text" do
before do
@time = Time.local(2008,9,17)
@backend.add_to_index([@time], 'db_id' => "1", '@uri' => "/Story/1")
end
it "should searchable with '2008-09-17T00:00:00'" do
@backend.count(@time.iso8601).should > 0
end
end
describe "raw" do
before do
@backend.add_to_index([], 'db_id' => "1", '@uri' => "/Story/1")
end
it "finds a raw document" do
@backend.raw(1).should_not be_nil
@backend.raw(1).attr('db_id').should == '1'
end
end
describe :build_fulltext_condition do
it "does not use limit for counting" do
@backend.send(:build_fulltext_condition,'',:count=>true).max.should == -1
end
describe 'parsing attributes' do
def condition(options)
@backend.send(:build_fulltext_condition,'something',options)
end
it 'raises on unknown' do
lambda{condition(:attributes=>1)}.should raise_error(RuntimeError)
end
it "removes blanks" do
condition(:attributes=>['',' ','a']).attrs.should == ['a']
end
it 'ignores empty' do
condition(:attributes=>nil).attrs.should == []
condition(:attributes=>'').attrs.should == []
end
it "adds a always-true condition when search is blank" do
@backend.send(:build_fulltext_condition,' ').attrs.should_not be_blank
end
it "parses a string" do
condition(:attributes=>'x y z').attrs.should == ['x y z']
end
it "parses an array" do
condition(:attributes=>['a b c','d e f']).attrs.should == ['a b c','d e f']
end
describe 'parsing a hash' do
before :all do
Story.columns_hash['popularity'].should be_number
Story.columns_hash['title'].should be_text
end
it "parses a simple hash" do
condition(:attributes=>{:a=>'b'}).attrs.should == ['a iSTRINC b']
end
it "parses a blank hash" do
condition(:attributes=>{:a=>''}).attrs.should == []
end
it "parses a keyless hash" do
condition(:attributes=>{''=>'wtf'}).attrs.should == []
end
it "parses a number column to number search" do
condition(:attributes=>{'popularity'=>12}).attrs.should == ['popularity NUMEQ 12']
end
it "parses a string column to string search" do
condition(:attributes=>{'title'=>12}).attrs.should == ['title iSTRINC 12']
end
it "translates columns" do
condition(:attributes=>{'id'=>1}).attrs.should == ['db_id NUMEQ 1']
end
it "parses a unknown column to string search" do
condition(:attributes=>{'xxx'=>'x'}).attrs.should == ['xxx iSTRINC x']
end
it "parses a date or time" do
pending do
#raw output: "@mdate" "2008-09-21T09:51:27+02:00"
raise
end
end
end
end
describe "translate order" do
def translated_order(order)
@backend.send(:build_fulltext_condition,'',:order=>order).order
end
it "translates strings to STRA" do
translated_order('title ASC').should == 'title STRA'
translated_order('title asc').should == 'title STRA'
end
it "translates strings to STRD" do
translated_order('title').should == 'title STRD'
translated_order('title DESC').should == 'title STRD'
translated_order('title desc').should == 'title STRD'
end
it "translates dates to NUMD" do
translated_order(:written_on).should == "written_on NUMD"
end
it "translates datetimes to NUM" do
translated_order("read_at ASC").should == "read_at NUMA"
end
it "translates numbers to NUMD" do
translated_order(:popularity).should == "popularity NUMD"
end
it "translates numbers with asc to NUMA" do
translated_order("popularity ASC").should == "popularity NUMA"
end
it "translate non-columns to string" do
translated_order("paid_at ASC").should == "paid_at STRA"
end
describe "translating rails-terms" do
before :all do
class FakeColumn
def number?;false;end
def type;:date;end
end
Story.columns_hash["created_on"] = FakeColumn.new
Story.columns_hash["updated_on"] = FakeColumn.new
end
after :all do
Story.reset_column_information
end
#symbols and desc <-> DESC only need testing once, to see if order values get normalized
['updated_at','updated_on',:updated_at,'updated_at DESC','updated_at desc'].each do |order|
it "translates #{order}" do
translated_order(order).should == "@mdate NUMD"
end
end
['created_at','created_on','created_at DESC'].each do |order|
it "translates #{order}" do
translated_order(order).should == "@cdate NUMD"
end
end
['id','id DESC'].each do |order|
it "translates #{order}" do
translated_order(order).should == "db_id NUMD"
end
end
it "does not translate strange things" do
translated_order('id strange').should == 'id strange'
end
it "does not translate long orders" do
translated_order('id desc wtf').should == 'id desc wtf'
end
end
end
end
end