de 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,251 @@
1
+ require 'test/unit'
2
+ require 'de'
3
+ require 'de/sunspot_solr'
4
+
5
+ class DeSunspotSolrSearchTest < Test::Unit::TestCase
6
+ include De::SunspotSolr
7
+ # fixtures :clients
8
+ # fixtures :products
9
+
10
+ def setup
11
+ @search = Search.new('search_product', 'Product', {
12
+ :properties => {
13
+ :client_id => {:type => :integer, :dynamic => false},
14
+ :name => {:type => :string, :dynamic => false},
15
+ :price => {:type => :integer, :dynamic => true}
16
+ },
17
+ :dynamics => {:integer => :int_params, :string => :string_params, :time => :time_params, :text => :string_params}
18
+ })
19
+ end
20
+
21
+ def test_constructor
22
+ assert_nothing_raised(De::Error::AbstractClassObjectCreationError) {
23
+ search = Search.new('search_product', 'Product', {
24
+ :properties => {
25
+ :client_id => {:type => :integer, :dynamic => false},
26
+ :name => {:type => :string, :dynamic => false},
27
+ :price => {:type => :integer, :dynamic => true}
28
+ },
29
+ :dynamics => {:integer => :int_params, :string => :string_params, :time => :time_params, :text => :string_params}
30
+ })
31
+ assert_equal(1, search.size, "No child noDes should be adDed")
32
+ assert_equal('search_product', search.name)
33
+ assert_equal('Product', search.klass)
34
+ }
35
+
36
+ assert_nothing_raised(De::Error::AbstractClassObjectCreationError) {
37
+ operand = EqualTo.new(:client_id, 44)
38
+ search = Search.new('search_product', 'Product', {
39
+ :properties => {
40
+ :client_id => {:type => :integer, :dynamic => false},
41
+ :name => {:type => :string, :dynamic => false},
42
+ :price => {:type => :integer, :dynamic => true}
43
+ },
44
+ :dynamics => {:integer => :int_params, :string => :string_params, :time => :time_params, :text => :string_params}
45
+ }, [operand])
46
+ assert_equal(2, search.size, "operand should be adDed as child noDe")
47
+ assert_equal(operand, search.first_child)
48
+ }
49
+ end
50
+
51
+ def test_equal
52
+ options = {
53
+ :properties => {
54
+ :client_id => {:type => :integer, :dynamic => false},
55
+ :name => {:type => :string, :dynamic => false},
56
+ :price => {:type => :integer, :dynamic => true}
57
+ },
58
+ :dynamics => {:integer => :int_params, :string => :string_params, :time => :time_params, :text => :string_params}
59
+ }
60
+ sa = Search.new('a', 'Product', options)
61
+ sb = Search.new('a', 'Product', options)
62
+ assert_equal(sa, sb)
63
+
64
+ sa << EqualTo.new(:client_id, 42)
65
+ sb << EqualTo.new(:client_id, 42)
66
+ assert_equal(sa, sb)
67
+
68
+ sb << GreaterThan.new(:price, 200)
69
+ assert(sa != sb)
70
+
71
+ sc = Search.new('c', 'Product', options, [EqualTo.new(:client_id, 42), GreaterThan.new(:price, 200)])
72
+ sd = Search.new('d', 'Product', options, [EqualTo.new(:client_id, 42), GreaterThan.new(:price, 200)])
73
+ se = Search.new('e', 'Product', options, [GreaterThan.new(:price, 200), EqualTo.new(:client_id, 42)])
74
+ sf = Search.new('f', 'Product', options, [GreaterThan.new(:price, 200), EqualTo.new(:client_id, 42), Without.new(:products_group_id, 188)])
75
+ assert_equal(sc, sd)
76
+ assert_equal(sc, se)
77
+ assert(sc != sf)
78
+
79
+ sg = Search.new('g', 'Product', options, [Or.new([EqualTo.new(:client_id, 42), GreaterThan.new(:price, 200)])])
80
+ sh = Search.new('h', 'Product', options, [Or.new([GreaterThan.new(:price, 200), EqualTo.new(:client_id, 42)])])
81
+ si = Search.new('h', 'Product', options, [And.new([GreaterThan.new(:price, 200), EqualTo.new(:client_id, 42)])])
82
+ assert_equal(sg, sh)
83
+ assert(sg != si)
84
+ end
85
+
86
+ def test_add
87
+ operand = EqualTo.new(:client_id, 44)
88
+ operator = And.new
89
+
90
+ assert_nothing_raised(De::Error::TypeError) {
91
+ result = @search << operand
92
+ assert_equal(2, @search.size)
93
+ assert(operand === result)
94
+ assert_equal(operand, @search.first_child)
95
+
96
+ @search << operator
97
+ assert_equal(3, @search.size)
98
+ assert_equal(operator, @search.children[1])
99
+
100
+ operand2 = EqualTo.new(:client_id, 44)
101
+ result = @search << operand2
102
+ assert_equal(3, @search.size)
103
+ assert_equal(operand2, result)
104
+ assert(operand === result)
105
+ }
106
+
107
+ assert_raise(De::Error::TypeError) {
108
+ @search << "some string"
109
+ }
110
+
111
+ options = {
112
+ :properties => {
113
+ :client_id => {:type => :integer, :dynamic => false},
114
+ :name => {:type => :string, :dynamic => false},
115
+ :price => {:type => :integer, :dynamic => true}
116
+ },
117
+ :dynamics => {:integer => :int_params, :string => :string_params, :time => :time_params, :text => :string_params}
118
+ }
119
+ sa = Search.new('a', 'Product', options)
120
+ or1 = Or.new([EqualTo.new(:client_id, 42), GreaterThan.new(:price, 200)])
121
+ or2 = Or.new([GreaterThan.new(:price, 200), EqualTo.new(:client_id, 42)])
122
+
123
+ sa << or1
124
+ assert_equal(4, sa.size)
125
+ assert_equal(1, sa.children.size)
126
+ assert_equal(or1, sa.first_child)
127
+
128
+ result = sa << or2
129
+ assert(or1 === result)
130
+ assert_equal(4, sa.size)
131
+ assert_equal(1, sa.children.size)
132
+ end
133
+
134
+ def test_valid
135
+ assert(@search.valid?)
136
+
137
+ operand = EqualTo.new(:client_id, 44)
138
+ operator = And.new
139
+
140
+ @search << operand
141
+ assert(@search.valid?)
142
+
143
+ @search << operator
144
+ assert(!@search.valid?)
145
+ end
146
+
147
+ # def test_evaluate
148
+ # client = clients(:one)
149
+ # product = products(:one)
150
+ #
151
+ # Product.reindex
152
+ # operand = EqualTo.new(:client_id, client.id)
153
+ # @search << operand
154
+ #
155
+ # sunspot_search = @search.evaluate
156
+ # product_ids = sunspot_search.results.map(&:id)
157
+ #
158
+ # assert(product_ids.incluDe?(product.id))
159
+ # end
160
+
161
+ def test_union
162
+ search2 = Search.new('search_product2', 'AnotherClass', {
163
+ :properties => {
164
+ :client_id => {:type => :integer, :dynamic => false},
165
+ },
166
+ :dynamics => {:integer => :int_params, :string => :string_params, :time => :time_params, :text => :string_params}
167
+ })
168
+ search3 = @search | search2
169
+
170
+ assert_instance_of(Search, search3)
171
+ assert_equal('search_product+search_product2', search3.name)
172
+ assert_equal('Product', search3.klass)
173
+ assert_equal(@search.options, search3.options)
174
+ assert(!search3.has_children?)
175
+
176
+ operand = EqualTo.new(:client_id, 44)
177
+ operand2 = EqualTo.new(:client_id, 42)
178
+ search2 << operand
179
+ search2 << operand2
180
+ search3 = @search | search2
181
+ assert(search3.has_children?)
182
+ assert_equal(5, search3.size)
183
+ assert_equal(1, search3.children.length)
184
+ or_operator = search3.first_child
185
+ assert_instance_of(Or, or_operator)
186
+ assert_equal(1, or_operator.children.length)
187
+ and_operator = or_operator.first_child
188
+ assert_instance_of(And, and_operator)
189
+ assert_equal(2, and_operator.children.length)
190
+ assert_equal(operand, and_operator.children[0])
191
+ assert_equal(operand2, and_operator.children[1])
192
+
193
+ operand3 = EqualTo.new(:client_id, 43)
194
+ operand4 = EqualTo.new(:client_id, 56)
195
+ @search << operand3
196
+ @search << operand4
197
+ search3 = @search | search2
198
+ assert(search3.has_children?)
199
+ assert_equal(8, search3.size)
200
+ assert_equal(1, search3.children.length)
201
+ or_operator = search3.first_child
202
+ assert_instance_of(Or, or_operator)
203
+ assert_equal(2, or_operator.children.length)
204
+ and_operator1 = or_operator.first_child
205
+ assert_instance_of(And, and_operator1)
206
+ assert_equal(2, and_operator1.children.length)
207
+ assert(!and_operator1[operand.name].nil? && !and_operator1[operand2.name].nil? || !and_operator1[operand3.name].nil? && !and_operator1[operand4.name].nil?)
208
+ and_operator2 = or_operator.children[1]
209
+ assert_instance_of(And, and_operator2)
210
+ assert_equal(2, and_operator2.children.length)
211
+ assert(!and_operator2[operand.name].nil? && !and_operator2[operand2.name].nil? || !and_operator2[operand3.name].nil? && !and_operator2[operand4.name].nil?)
212
+ end
213
+
214
+ def test_intersection
215
+ search2 = Search.new('search_product2', 'AnotherClass', {
216
+ :properties => {
217
+ :client_id => {:type => :integer, :dynamic => false},
218
+ },
219
+ :dynamics => {:integer => :int_params, :string => :string_params, :time => :time_params, :text => :string_params}
220
+ })
221
+ search3 = @search & search2
222
+
223
+ assert_instance_of(Search, search3)
224
+ assert_equal('search_product+search_product2', search3.name)
225
+ assert_equal('Product', search3.klass)
226
+ assert_equal(@search.options, search3.options)
227
+ assert(!search3.has_children?)
228
+
229
+ operand = EqualTo.new(:client_id, 44)
230
+ operand2 = EqualTo.new(:client_id, 42)
231
+ search2 << operand
232
+ search2 << operand2
233
+ search3 = @search & search2
234
+ assert(search3.has_children?)
235
+ assert_equal(3, search3.size)
236
+ assert_equal(2, search3.children.length)
237
+ assert(!search3[operand.name].nil? && !search3[operand2.name].nil?)
238
+
239
+ operand3 = EqualTo.new(:client_id, 43)
240
+ operand4 = EqualTo.new(:client_id, 56)
241
+ or_operator = Or.new([operand3, operand4])
242
+ @search << or_operator
243
+ search3 = @search & search2
244
+ assert(search3.has_children?)
245
+ assert_equal(6, search3.size)
246
+ assert_equal(3, search3.children.length)
247
+ assert(!search3[operand.name].nil? && !search3[operand2.name].nil? && !search3[or_operator.name].nil?)
248
+ assert_equal(2, search3[or_operator.name].children.length)
249
+ end
250
+
251
+ end