daedal-sl 0.0.0 → 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,15 @@
1
+ def append_load_path(*paths)
2
+ full_path = File.join([File.dirname(__FILE__), ".."] << paths)
3
+ $: << File.expand_path(full_path)
4
+ end
5
+
6
+ append_load_path('')
7
+ append_load_path('lib')
8
+
9
+ require 'daedal-sl'
10
+
11
+ RSpec.configure do |config|
12
+ config.expect_with :rspec do |c|
13
+ c.syntax = :expect
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaedalSL::AndFilter do
4
+
5
+ subject do
6
+ DaedalSL::AndFilter.new(:parent, {})
7
+ end
8
+
9
+ let(:filter) do
10
+ Daedal::Filters::TermFilter.new(field: :foo, term: :bar)
11
+ end
12
+
13
+ context '#filter' do
14
+ before { subject.filter { filter } }
15
+ it "adds the filter given by executing the block to the and filter's filter list" do
16
+ expect(subject.base.filters.first).to eq filter
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaedalSL::BlockQuery do
4
+
5
+ let(:parent_class) do
6
+ class ParentClass
7
+ def data
8
+ :data
9
+ end
10
+ end
11
+ ParentClass
12
+ end
13
+
14
+ subject do
15
+ class DummyClass < DaedalSL::BlockQuery
16
+ def initialize(data, options)
17
+ @query_type = Daedal::Queries::MatchQuery
18
+ super
19
+ end
20
+ end
21
+ DummyClass.new(parent_class.new, {field: :foo, query: :bar})
22
+ end
23
+
24
+ context '#initialize' do
25
+ it 'sets @data, makes it readable' do
26
+ expect(subject.data).to eq :data
27
+ end
28
+ it 'sets @base, makes it readable' do
29
+ expect(subject.base.to_hash).to eq(Daedal::Queries::MatchQuery.new(field: :foo, query: :bar).to_hash)
30
+ end
31
+ end
32
+
33
+ context 'class method #build' do
34
+ it 'creates a new instance of the class, instance_evals a block, returns' do
35
+ expect(DummyClass).to receive(:new).with(parent_class, {field: :foo, query: :bar}).and_return(subject)
36
+ expect(subject).to receive(:foo)
37
+ DummyClass.build(parent_class, {field: :foo, query: :bar}) do
38
+ foo
39
+ end
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaedalSL::BoolFilter do
4
+
5
+ subject do
6
+ DaedalSL::BoolFilter.new(:parent, {})
7
+ end
8
+
9
+ let(:filter) do
10
+ Daedal::Filters::TermFilter.new(field: :foo, term: :bar)
11
+ end
12
+
13
+ context '#must' do
14
+ before { subject.must { filter } }
15
+ it "adds the filter given by executing the block to the bool filter's must list" do
16
+ expect(subject.base.must.first).to eq filter
17
+ end
18
+ end
19
+
20
+ context '#should' do
21
+ before { subject.should { filter } }
22
+ it "adds the filter given by executing the block to the bool filter's should list" do
23
+ expect(subject.base.should.first).to eq filter
24
+ end
25
+ end
26
+
27
+ context '#must_not' do
28
+ before { subject.must_not { filter } }
29
+ it "adds the filter given by executing the block to the bool filter's must_not list" do
30
+ expect(subject.base.must_not.first).to eq filter
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaedalSL::BoolQuery do
4
+
5
+ subject do
6
+ DaedalSL::BoolQuery.new(:parent, {})
7
+ end
8
+
9
+ let(:query) do
10
+ Daedal::Queries::MatchQuery.new(field: :foo, query: :bar)
11
+ end
12
+
13
+ context '#must' do
14
+ before { subject.must { query } }
15
+ it "adds the query given by executing the block to the bool query's must list" do
16
+ expect(subject.base.must.first).to eq query
17
+ end
18
+ end
19
+
20
+ context '#should' do
21
+ before { subject.should { query } }
22
+ it "adds the query given by executing the block to the bool query's should list" do
23
+ expect(subject.base.should.first).to eq query
24
+ end
25
+ end
26
+
27
+ context '#must_not' do
28
+ before { subject.must_not { query } }
29
+ it "adds the query given by executing the block to the bool query's must_not list" do
30
+ expect(subject.base.must_not.first).to eq query
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaedalSL::Builder do
4
+
5
+ subject { DaedalSL::Builder.new }
6
+
7
+ context '#query' do
8
+ context 'with a block' do
9
+ before do
10
+ subject.query { :foo }
11
+ end
12
+ it 'will yield the block and set that to @query' do
13
+ expect(subject.instance_variable_get(:@query)).to eq :foo
14
+ end
15
+ end
16
+ end
17
+
18
+ context '#filter' do
19
+ context 'with a block' do
20
+ before do
21
+ subject.filter { :foo }
22
+ end
23
+ it 'will yield the block and set that to @filter' do
24
+ expect(subject.instance_variable_get(:@filter)).to eq :foo
25
+ end
26
+ end
27
+ end
28
+
29
+ context '#from' do
30
+ before do
31
+ subject.from(5)
32
+ end
33
+ it 'will set the from parameter in the base hash correctly' do
34
+ expect(subject.instance_variable_get(:@options)[:from]).to eq 5
35
+ end
36
+ end
37
+
38
+ context '#size' do
39
+ before do
40
+ subject.size(5)
41
+ end
42
+ it 'will set the size parameter in the base hash correctly' do
43
+ expect(subject.instance_variable_get(:@options)[:size]).to eq 5
44
+ end
45
+ end
46
+
47
+ context '#paginate' do
48
+ it 'will convert the page and per_page into from and size, and call those methods' do
49
+ expect(subject).to receive(:from).with(14)
50
+ expect(subject).to receive(:size).with(7)
51
+ subject.paginate page: 3, per_page: 7
52
+ end
53
+ end
54
+
55
+ context '#fields' do
56
+ before { subject.fields(:foo, :bar) }
57
+ it 'will set the fields for the query' do
58
+ expect(subject.instance_variable_get(:@options)[:fields]).to eq [:foo, :bar]
59
+ end
60
+ end
61
+
62
+ context '#to_hash' do
63
+ context 'when a query is given' do
64
+ before do
65
+ subject.instance_variable_set(:@query, Daedal::Queries::MatchQuery.new(field: :foo, query: :bar))
66
+ end
67
+ it 'will convert @query to a hash' do
68
+ expect(subject.to_hash).to eq({query: {match: {foo: {query: :bar}}}})
69
+ end
70
+ end
71
+ context 'when a query is not given' do
72
+ it 'will use the default match_all query' do
73
+ expect(subject.to_hash).to eq({query: {match_all: {}}})
74
+ end
75
+ end
76
+ context 'when a filter is given' do
77
+ before do
78
+ subject.instance_variable_set(:@filter, Daedal::Filters::TermFilter.new(field: :foo, term: :bar))
79
+ end
80
+ it 'will convert @filter to a hash' do
81
+ expect(subject.to_hash).to eq({query: {match_all: {}}, filter: {term: {foo: :bar}}})
82
+ end
83
+ end
84
+ context 'when from and size were specified' do
85
+ before do
86
+ subject.from 3
87
+ subject.size 10
88
+ end
89
+ it 'will set them up appropriately in the query' do
90
+ expect(subject.to_hash).to eq({query: {match_all: {}}, from: 3, size: 10})
91
+ end
92
+ end
93
+ end
94
+
95
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaedalSL::DisMaxQuery do
4
+
5
+ subject do
6
+ DaedalSL::DisMaxQuery.new(:parent, {})
7
+ end
8
+
9
+ let(:query) do
10
+ Daedal::Queries::MatchQuery.new(field: :foo, query: :bar)
11
+ end
12
+
13
+ context '#query' do
14
+ before { subject.query { query } }
15
+ it "adds the query given by executing the block to the bool query's query list" do
16
+ expect(subject.base.queries.first).to eq query
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaedalSL::NestedBoolFilter do
4
+
5
+ subject do
6
+ DaedalSL::NestedBoolFilter.new(:parent, path: :foo)
7
+ end
8
+
9
+ let(:filter) do
10
+ Daedal::Filters::TermFilter.new(field: :foo, term: :bar)
11
+ end
12
+
13
+ context '#must' do
14
+ before { subject.must { filter } }
15
+ it "adds the filter given by executing the block to the bool filter's must list" do
16
+ expect(subject.base.filter.must.first).to eq filter
17
+ end
18
+ end
19
+
20
+ context '#should' do
21
+ before { subject.should { filter } }
22
+ it "adds the filter given by executing the block to the bool filter's should list" do
23
+ expect(subject.base.filter.should.first).to eq filter
24
+ end
25
+ end
26
+
27
+ context '#must_not' do
28
+ before { subject.must_not { filter } }
29
+ it "adds the filter given by executing the block to the bool filter's must_not list" do
30
+ expect(subject.base.filter.must_not.first).to eq filter
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaedalSL::NestedBoolQuery do
4
+
5
+ subject do
6
+ DaedalSL::NestedBoolQuery.new(:parent, path: :foo)
7
+ end
8
+
9
+ let(:query) do
10
+ Daedal::Queries::MatchQuery.new(field: :foo, query: :bar)
11
+ end
12
+
13
+ context '#must' do
14
+ before { subject.must { query } }
15
+ it "adds the query given by executing the block to the bool query's must list" do
16
+ expect(subject.base.query.must.first).to eq query
17
+ end
18
+ end
19
+
20
+ context '#should' do
21
+ before { subject.should { query } }
22
+ it "adds the query given by executing the block to the bool query's should list" do
23
+ expect(subject.base.query.should.first).to eq query
24
+ end
25
+ end
26
+
27
+ context '#must_not' do
28
+ before { subject.must_not { query } }
29
+ it "adds the query given by executing the block to the bool query's must_not list" do
30
+ expect(subject.base.query.must_not.first).to eq query
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaedalSL::NestedDisMaxQuery do
4
+
5
+ subject do
6
+ DaedalSL::NestedDisMaxQuery.new(:parent, path: :foo)
7
+ end
8
+
9
+ let(:query) do
10
+ Daedal::Queries::MatchQuery.new(field: :foo, query: :bar)
11
+ end
12
+
13
+ context '#query' do
14
+ before { subject.query { query } }
15
+ it "adds the query given by executing the block to the bool query's query list" do
16
+ expect(subject.base.query.queries.first).to eq query
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaedalSL::OrFilter do
4
+
5
+ subject do
6
+ DaedalSL::OrFilter.new(:parent, {})
7
+ end
8
+
9
+ let(:filter) do
10
+ Daedal::Filters::TermFilter.new(field: :foo, term: :bar)
11
+ end
12
+
13
+ context '#filter' do
14
+ before { subject.filter { filter } }
15
+ it "adds the filter given by executing the block to the and filter's filter list" do
16
+ expect(subject.base.filters.first).to eq filter
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,219 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaedalSL::QueryHelpers do
4
+
5
+ subject do
6
+ class QueryHelpersDummyClass
7
+ include DaedalSL::QueryHelpers
8
+ def data
9
+ :data
10
+ end
11
+ end
12
+ QueryHelpersDummyClass.new
13
+ end
14
+
15
+ context '#match_all' do
16
+ it 'will return a match all query' do
17
+ expect(Daedal::Queries::MatchAllQuery).to receive(:new).and_return(:foo)
18
+ expect(subject.match_all).to eq :foo
19
+ end
20
+ end
21
+
22
+ context '#match' do
23
+ it 'will return a match query' do
24
+ expect(Daedal::Queries::MatchQuery).to receive(:new).with(:foo).and_return(:bar)
25
+ expect(subject.match(:foo)).to eq :bar
26
+ end
27
+ end
28
+
29
+ context '#fuzzy' do
30
+ it 'will return a fuzzy query' do
31
+ expect(Daedal::Queries::FuzzyQuery).to receive(:new).with(:foo).and_return(:bar)
32
+ expect(subject.fuzzy(:foo)).to eq :bar
33
+ end
34
+ end
35
+
36
+ context '#geo_distance_filter' do
37
+ it 'will return a geo distance filter' do
38
+ expect(Daedal::Filters::GeoDistanceFilter).to receive(:new).with(:foo).and_return(:bar)
39
+ expect(subject.geo_distance_filter(:foo)).to eq :bar
40
+ end
41
+ end
42
+
43
+ context '#multi_match' do
44
+ it 'will return a multi match query' do
45
+ expect(Daedal::Queries::MultiMatchQuery).to receive(:new).with(:foo).and_return(:bar)
46
+ expect(subject.multi_match(:foo)).to eq :bar
47
+ end
48
+ end
49
+
50
+ context '#prefix' do
51
+ it 'will return a prefix query' do
52
+ expect(Daedal::Queries::PrefixQuery).to receive(:new).with(:foo).and_return(:bar)
53
+ expect(subject.prefix(:foo)).to eq :bar
54
+ end
55
+ end
56
+
57
+ context '#query_string' do
58
+ it 'will return a query string query' do
59
+ expect(Daedal::Queries::QueryStringQuery).to receive(:new).with(:foo).and_return(:bar)
60
+ expect(subject.query_string(:foo)).to eq :bar
61
+ end
62
+ end
63
+
64
+ context '#term_filter' do
65
+ it 'will return a term filter' do
66
+ expect(Daedal::Filters::TermFilter).to receive(:new).with(:foo).and_return(:bar)
67
+ expect(subject.term_filter(:foo)).to eq :bar
68
+ end
69
+ end
70
+
71
+ context '#range_filter' do
72
+ it 'will return a range filter' do
73
+ expect(Daedal::Filters::RangeFilter).to receive(:new).with(:foo).and_return(:bar)
74
+ expect(subject.range_filter(:foo)).to eq :bar
75
+ end
76
+ end
77
+
78
+ context '#terms_filter' do
79
+ it 'will return a terms filter' do
80
+ expect(Daedal::Filters::TermsFilter).to receive(:new).with(:foo).and_return(:bar)
81
+ expect(subject.terms_filter(:foo)).to eq :bar
82
+ end
83
+ end
84
+
85
+ context '#exists_filter' do
86
+ it 'will return an exists filter' do
87
+ expect(Daedal::Filters::ExistsFilter).to receive(:new).with(:foo).and_return(:bar)
88
+ expect(subject.exists_filter(:foo)).to eq :bar
89
+ end
90
+ end
91
+
92
+ context '#filtered_query' do
93
+ it 'will return a filtered query' do
94
+ expect(Daedal::Queries::FilteredQuery).to receive(:new).with(:foo).and_return(:bar)
95
+ expect(subject.filtered_query(:foo)).to eq :bar
96
+ end
97
+ end
98
+
99
+ context '#bool_query' do
100
+ it 'will return a bool query' do
101
+ expect(DaedalSL::BoolQuery).to receive(:build).with(subject, {}).and_return(:bar)
102
+ expect(subject.bool_query({}, &:foo)).to eq :bar
103
+ end
104
+ it 'sets up the parent correctly to allow @data and the other query helpers to be used' do
105
+ expected_result = {:bool=>{:should=>[{:match=>{:foo=>{:query=>:bar}}}], :must=>[{:match=>{:data=>{:query=>:data}}}], :must_not=>[{:match=>{:foo=>{:query=>:bar}}}], :boost=>10.0}}
106
+ result = subject.bool_query(boost: 10) do
107
+ must { match field: data, query: data }
108
+ should { match field: :foo, query: :bar }
109
+ must_not { match field: :foo, query: :bar }
110
+ end
111
+ expect(result.to_hash).to eq expected_result
112
+ end
113
+ end
114
+
115
+ context '#bool_filter' do
116
+ it 'will return a bool filter' do
117
+ expect(DaedalSL::BoolFilter).to receive(:build).with(subject, {}).and_return(:bar)
118
+ expect(subject.bool_filter({}, &:foo)).to eq :bar
119
+ end
120
+ it 'sets up the parent correctly to allow @data and the other query helpers to be used' do
121
+ expected_result = {:bool=>{:should=>[{:term=>{:foo=>:bar}}], :must=>[{:term=>{:data=>:data}}], :must_not=>[{:term=>{:foo=>:bar}}]}}
122
+ result = subject.bool_filter do
123
+ must { term_filter field: data, term: data }
124
+ should { term_filter field: :foo, term: :bar }
125
+ must_not { term_filter field: :foo, term: :bar }
126
+ end
127
+ expect(result.to_hash).to eq expected_result
128
+ end
129
+ end
130
+
131
+ context '#dis_max' do
132
+ it 'will return a dis max query' do
133
+ expect(DaedalSL::DisMaxQuery).to receive(:build).with(subject, {}).and_return(:bar)
134
+ expect(subject.dis_max({}, &:foo)).to eq :bar
135
+ end
136
+ it 'sets up the parent correctly to allow @data and the other query helpers to be used' do
137
+ expected_result = {:dis_max=>{:queries=>[{:match=>{:data=>{:query=>:data}}}], :boost=>10.0}}
138
+ result = subject.dis_max(boost: 10) do
139
+ query { match field: data, query: data }
140
+ end
141
+ expect(result.to_hash).to eq expected_result
142
+ end
143
+ end
144
+
145
+ context '#and_filter' do
146
+ it 'will return a and filter' do
147
+ expect(DaedalSL::AndFilter).to receive(:build).with(subject, {}).and_return(:bar)
148
+ expect(subject.and_filter({}, &:foo)).to eq :bar
149
+ end
150
+ it 'sets up the parent correctly to allow @data and the other query helpers to be used' do
151
+ expected_result = {:and=>[{:term=>{:data=>:data}}]}
152
+ result = subject.and_filter(boost: 10) do
153
+ filter { term_filter field: data, term: data }
154
+ end
155
+ expect(result.to_hash).to eq expected_result
156
+ end
157
+ end
158
+
159
+ context '#or_filter' do
160
+ it 'will return a and filter' do
161
+ expect(DaedalSL::OrFilter).to receive(:build).with(subject, {}).and_return(:bar)
162
+ expect(subject.or_filter({}, &:foo)).to eq :bar
163
+ end
164
+ it 'sets up the parent correctly to allow @data and the other query helpers to be used' do
165
+ expected_result = {:or=>[{:term=>{:data=>:data}}]}
166
+ result = subject.or_filter(boost: 10) do
167
+ filter { term_filter field: data, term: data }
168
+ end
169
+ expect(result.to_hash).to eq expected_result
170
+ end
171
+ end
172
+
173
+ context '#nested_bool_query' do
174
+ it 'will return a nested bool query' do
175
+ expect(DaedalSL::NestedBoolQuery).to receive(:build).with(subject, {path: :foo}).and_return(:bar)
176
+ expect(subject.nested_bool_query({path: :foo}, &:foo)).to eq :bar
177
+ end
178
+ it 'sets up the parent correctly to allow @data and the other query helpers to be used' do
179
+ expected_result = {:nested=>{:path=>:foo, :query=>{:bool=>{:should=>[{:match=>{:foo=>{:query=>:bar}}}], :must=>[{:match=>{:data=>{:query=>:data}}}], :must_not=>[{:match=>{:foo=>{:query=>:bar}}}]}}}}
180
+ result = subject.nested_bool_query(path: :foo) do
181
+ must { match field: data, query: data }
182
+ should { match field: :foo, query: :bar }
183
+ must_not { match field: :foo, query: :bar }
184
+ end
185
+ expect(result.to_hash).to eq expected_result
186
+ end
187
+ end
188
+
189
+ context '#nested_bool_filter' do
190
+ it 'will return a nested bool query' do
191
+ expect(DaedalSL::NestedBoolFilter).to receive(:build).with(subject, {path: :foo}).and_return(:bar)
192
+ expect(subject.nested_bool_filter({path: :foo}, &:foo)).to eq :bar
193
+ end
194
+ it 'sets up the parent correctly to allow @data and the other query helpers to be used' do
195
+ expected_result = {:nested=>{:path=>:foo, :filter=>{:bool=>{:should=>[{:term=>{:foo=>:bar}}], :must=>[{:term=>{:data=>:data}}], :must_not=>[{:term=>{:foo=>:bar}}]}}}}
196
+ result = subject.nested_bool_filter(path: :foo) do
197
+ must { term_filter field: data, term: data }
198
+ should { term_filter field: :foo, term: :bar }
199
+ must_not { term_filter field: :foo, term: :bar }
200
+ end
201
+ expect(result.to_hash).to eq expected_result
202
+ end
203
+ end
204
+
205
+ context '#nested_dis_max_query' do
206
+ it 'will return a nested bool query' do
207
+ expect(DaedalSL::NestedDisMaxQuery).to receive(:build).with(subject, {path: :foo}).and_return(:bar)
208
+ expect(subject.nested_dis_max_query({path: :foo}, &:foo)).to eq :bar
209
+ end
210
+ it 'sets up the parent correctly to allow @data and the other query helpers to be used' do
211
+ expected_result = {:nested=>{:path=>:foo, :query=>{:dis_max=>{:queries=>[{:match=>{:data=>{:query=>:data}}}]}}}}
212
+ result = subject.nested_dis_max_query(path: :foo) do
213
+ query { match field: data, query: data }
214
+ end
215
+ expect(result.to_hash).to eq expected_result
216
+ end
217
+ end
218
+
219
+ end