elasticquery 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+ require 'elasticquery'
3
+
4
+ class TestChainableCalls < MiniTest::Test
5
+
6
+ def setup
7
+ @query = Elasticquery::Base.new
8
+ end
9
+
10
+ def test_sign_call_return_itself
11
+ assert_kind_of Elasticquery::Base, @query.term(a: 1)
12
+ end
13
+
14
+ def test_apply_query
15
+ query = @query.term(a: 1).term(a: 3)
16
+ assert_equal query.build, query: {filtered: {filter: {and: [{term: {a: 3}}]}}}
17
+ end
18
+
19
+ def test_with_not_filter
20
+ query = @query.search('hi').term.not(a: 1)
21
+ assert_equal query.build, {query: {filtered: {query: {multi_match: {fields: "_all", operator: "and", type: "best_fields", query: "hi"}}, filter: {and: [{not: {filter: {term: {a: 1}}}}]}}}}
22
+ end
23
+
24
+ def test_apply_kinds_of_query
25
+ query = @query.term(a: 1).search('hello', operator: 'or').build
26
+ terms = query[:query][:filtered][:filter][:and]
27
+ search = query[:query][:filtered][:query][:multi_match]
28
+ assert_equal terms, [{term: {a: 1}}]
29
+ assert_equal search, {fields: "_all", operator: "or", type: "best_fields", query: "hello"}
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+ require 'elasticquery'
3
+
4
+ class TestNotCase < MiniTest::Test
5
+ class PostQuery < Elasticquery::Base
6
+ filtered do |param|
7
+ term.not id: params[:id]
8
+ end
9
+ end
10
+
11
+ class InvalidQuery < Elasticquery::Base
12
+ filtered do |param|
13
+ term.not
14
+ end
15
+ end
16
+
17
+ def test_invalid_previous_filter
18
+ query = InvalidQuery.new({}).build
19
+ assert_equal query, {}
20
+ end
21
+
22
+ def test_term_case
23
+ params = {id: 1}
24
+ actual = PostQuery.new(params).build
25
+ expected = [{not: {filter: {term: {id: 1}}}}]
26
+ assert_equal expected, actual[:query][:filtered][:filter][:and]
27
+ end
28
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+ require 'elasticquery'
3
+
4
+ class TestQueriesInheritence < MiniTest::Test
5
+ class MultipleFilteredQuery < Elasticquery::Base
6
+ filtered do |params|
7
+ term :'category.id' => params[:category_id]
8
+ end
9
+
10
+ filtered do |params|
11
+ term :'author.name' => params[:author_name]
12
+ end
13
+ end
14
+
15
+ class ParentPostQuery < Elasticquery::Base
16
+ filtered do |params|
17
+ term :'category.id' => params[:category_id]
18
+ end
19
+ end
20
+
21
+ class ChildPostQuery < ParentPostQuery
22
+ filtered do |params|
23
+ term :'author.name' => params[:author_name]
24
+ end
25
+ end
26
+
27
+ class ChildOverrideQuery < ParentPostQuery
28
+ filtered do |params|
29
+ term :'category.id' => params[:category_id_new]
30
+ end
31
+ end
32
+
33
+ def setup
34
+ @params = {category_id: 1, author_name: 'Sergey'}
35
+ end
36
+
37
+ def test_parent_subqueries_builder
38
+ query = ParentPostQuery.new @params
39
+ actual = query.build
40
+ expected = [{term: {:"category.id" => 1}}]
41
+ assert_equal expected, actual[:query][:filtered][:filter][:and]
42
+ end
43
+
44
+ def test_child_query_builder
45
+ query = ChildPostQuery.new @params
46
+ actual = query.build
47
+ expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" => 'Sergey'}}]
48
+ assert_equal expected, actual[:query][:filtered][:filter][:and]
49
+ end
50
+
51
+ def test_concatenate_all_filters
52
+ actual = MultipleFilteredQuery.new(@params).build
53
+ expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" => 'Sergey'}}]
54
+ assert_equal expected, actual[:query][:filtered][:filter][:and]
55
+ end
56
+
57
+ def test_override_same_keys
58
+ params = { category_id: 1, author_name: 'Sergey', category_id_new: 14 }
59
+ query = ChildOverrideQuery.new params
60
+ actual = query.build
61
+ expected = [{term: {:"category.id" => 14}}]
62
+ assert_equal expected, actual[:query][:filtered][:filter][:and]
63
+ end
64
+ end
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+ require 'elasticquery'
3
+
4
+ class TestRangeCase < MiniTest::Test
5
+ class HumanQuery < Elasticquery::Base
6
+ filtered do |params|
7
+ range :year, lte: params[:max_year], gte: params[:min_year]
8
+ range :revenue, lte: params[:max_revenue], gte: params[:min_revenue]
9
+ end
10
+ end
11
+
12
+ def test_simple_range
13
+ params = { min_year: 1960, max_year: 2015 }
14
+ actual = HumanQuery.new(params).build
15
+ expected = [{range: {year:{ lte: 2015, gte: 1960}}}]
16
+ assert_equal expected, actual[:query][:filtered][:filter][:and]
17
+ end
18
+
19
+ def test_multiple_ranges
20
+ params = { min_year: 1960, max_year: 2015, min_revenue: 100 }
21
+ actual = HumanQuery.new(params).build
22
+ expected = [{range: {year:{ lte: 2015, gte: 1960}}}, {range: {revenue: {gte: 100}}}]
23
+ assert_equal expected, actual[:query][:filtered][:filter][:and]
24
+ end
25
+ end
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+ require 'elasticquery'
3
+
4
+ class TestSearchQueryCase < MiniTest::Test
5
+ class PostQuery < Elasticquery::Base
6
+ filtered do |params|
7
+ term :'category' => params[:category]
8
+ search params[:search]
9
+ end
10
+ end
11
+
12
+ class PostOptionsQuery < Elasticquery::Base
13
+ filtered do |params|
14
+ search params[:search],
15
+ fields: params[:fields],
16
+ operator: params[:operator],
17
+ type: "phrase"
18
+ end
19
+ end
20
+
21
+ class MultipleSearch < PostQuery
22
+ filtered do |params|
23
+ search "look for"
24
+ end
25
+ end
26
+
27
+ def test_search_with_other_query
28
+ params = {category: 'zoo', search: 'hello'}
29
+ actual = PostQuery.new(params).build
30
+ expected_term = [{term: {category: 'zoo'}}]
31
+ assert_equal expected_term, actual[:query][:filtered][:filter][:and]
32
+ expected_search = { query: 'hello', fields: "_all", operator: 'and', type: 'best_fields'}
33
+ assert_equal expected_search, actual[:query][:filtered][:query][:multi_match]
34
+ end
35
+
36
+ def test_all_options_to_configure
37
+ params = {search: 'hello', fields: %w(title body), operator: 'or'}
38
+ actual = PostOptionsQuery.new(params).build
39
+ expected = {fields: %w(title body), operator: "or", type: "phrase", query: 'hello'}
40
+ assert_equal expected, actual[:query][:filtered][:query][:multi_match]
41
+ end
42
+
43
+ def test_multiple_search_use_last_search_declaration
44
+ params = {search: 'hello'}
45
+ actual = MultipleSearch.new(params).build
46
+ assert_equal "look for", actual[:query][:filtered][:query][:multi_match][:query]
47
+ end
48
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+ require 'elasticquery'
3
+
4
+ class TestTermCase < MiniTest::Test
5
+ class PostQuery < Elasticquery::Base
6
+ filtered do |params|
7
+ term :'category.id' => params[:category_id], :"author.name" => params[:author_name]
8
+ end
9
+ end
10
+
11
+ def test_multiple_valid_terms
12
+ params = {category_id: 1, author_name: 'Sergey'}
13
+ actual = PostQuery.new(params).build
14
+ expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" => 'Sergey'}}]
15
+ assert_equal expected, actual[:query][:filtered][:filter][:and]
16
+ end
17
+
18
+ def test_extra_params
19
+ params = { category_id: 1, author_name: 'Sergey', a: 1 }
20
+ actual = PostQuery.new(params).build
21
+ expected = [{term: {:"category.id" => 1}}, {term: {:"author.name" => 'Sergey'}}]
22
+ assert_equal expected, actual[:query][:filtered][:filter][:and]
23
+ end
24
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+ require 'elasticquery/queries/all'
3
+
4
+ class TestAllBaseQueries < MiniTest::Test
5
+ include Elasticquery::Queries::All
6
+
7
+ attr_accessor :query
8
+
9
+ def setup
10
+ clear_query
11
+ end
12
+
13
+ def teardown
14
+ clear_query
15
+ end
16
+
17
+ def test_have_term
18
+ assert_respond_to self, :term
19
+ end
20
+
21
+ def test_have_search
22
+ assert_respond_to self, :search
23
+ end
24
+
25
+ def test_defined_instances_return_sel
26
+ assert_equal self, term
27
+ end
28
+
29
+ def test_push_hash_term_to_query
30
+ term a: 1
31
+ search b: 2
32
+ term c: 3
33
+ assert_equal 3, query.size
34
+ end
35
+
36
+ private
37
+
38
+ def clear_query
39
+ @query = []
40
+ end
41
+ end
@@ -0,0 +1,70 @@
1
+ require 'test_helper'
2
+ require 'elasticquery/query'
3
+
4
+ class TestQuery < MiniTest::Test
5
+
6
+ def setup
7
+ @query = Elasticquery::Query.new
8
+ @rule = MiniTest::Mock.new
9
+ end
10
+
11
+ def teardown
12
+ assert @rule.verify
13
+ end
14
+
15
+ def test_default_query
16
+ assert_equal({query: {filtered: {query: {match_all:{}}}}}, @query.to_hash)
17
+ end
18
+
19
+ def test_default_query_should_be_matched_all
20
+ assert @query.match_all?
21
+ @rule.expect :to_hash, {a: 1}
22
+ @query << @rule
23
+ refute @query.match_all?
24
+ end
25
+
26
+ def test_pushing_of_hash_to_new_query
27
+ @rule.expect :to_hash, {a: 2, b: 1}
28
+ @query << @rule
29
+ assert_equal({a:2, b: 1}, @query.to_hash)
30
+ end
31
+
32
+ def test_pushing_of_subquery_to_existing_one
33
+ @rule.expect :to_hash, {a: 2, b: 1}
34
+ @query << @rule
35
+ @rule.expect :to_hash, {a: 1}
36
+ @query << @rule
37
+ assert_equal({a:1, b: 1}, @query.to_hash)
38
+ end
39
+
40
+ def test_push_filters_to_variable
41
+ @rule.expect :to_hash, {a: 2, b: 1}
42
+ @query << @rule
43
+ @rule.expect :to_hash, {a: 1}
44
+ @query << @rule
45
+ assert_equal 2, @query.filters.size
46
+ end
47
+
48
+ def test_should_group_filters_in_array
49
+ @rule.expect :to_hash, query: {filtered: {filter: {and: {term: {a: 1, b: 2}}}}}
50
+ @query << @rule
51
+ @rule.expect :to_hash, query: {filtered: {filter: {and: {term: {c: 3}}}}}
52
+ @query << @rule
53
+ @rule.expect :to_hash, query: {filtered: {filter: {and: {range: {x: 3}}}}}
54
+ @query << @rule
55
+ expected = {query: {filtered: {filter: {and: [{term: {a: 1}}, {term: {b: 2}}, {term: {c: 3}}, {range: {x: 3}}]}}}}
56
+ assert_equal(expected, @query.to_hash)
57
+ end
58
+
59
+ class Elasticquery::Query
60
+ # query context
61
+ def forty_two
62
+ 42
63
+ end
64
+ end
65
+ def test_merge_when_filters_is_lambda_uses_query_context
66
+ @rule.expect :to_hash, -> { {forty_two: forty_two} }
67
+ @query << @rule
68
+ assert_equal @query.to_hash, {forty_two: 42}
69
+ end
70
+ end
@@ -0,0 +1,8 @@
1
+ require 'minitest/test'
2
+ require 'minitest/autorun'
3
+
4
+ require "minitest/reporters"
5
+ Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(:color => true)]
6
+
7
+ require "codeclimate-test-reporter"
8
+ CodeClimate::TestReporter.start
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elasticquery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Kuchmistov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-reporters
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Powerful and flexibal query factory for you elasticsearch&ruby application
112
+ email:
113
+ - sergey.kuchmistov@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - elasticquery.gemspec
125
+ - lib/elasticquery.rb
126
+ - lib/elasticquery/base.rb
127
+ - lib/elasticquery/filters/base.rb
128
+ - lib/elasticquery/filters/not.rb
129
+ - lib/elasticquery/filters/range.rb
130
+ - lib/elasticquery/filters/search.rb
131
+ - lib/elasticquery/filters/term.rb
132
+ - lib/elasticquery/queries/all.rb
133
+ - lib/elasticquery/query.rb
134
+ - lib/elasticquery/version.rb
135
+ - test/base_test.rb
136
+ - test/filters/not_test.rb
137
+ - test/filters/range_test.rb
138
+ - test/filters/search_test.rb
139
+ - test/filters/term_test.rb
140
+ - test/integration/chainable_call_test.rb
141
+ - test/integration/not_case_test.rb
142
+ - test/integration/queries_inheritence_test.rb
143
+ - test/integration/range_case_test.rb
144
+ - test/integration/search_case_test.rb
145
+ - test/integration/term_case_test.rb
146
+ - test/queries/all_test.rb
147
+ - test/query_test.rb
148
+ - test/test_helper.rb
149
+ homepage: https://github.com/caulfield/elasticquery
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.4.5
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Elasticsearch query builder.
173
+ test_files:
174
+ - test/base_test.rb
175
+ - test/filters/not_test.rb
176
+ - test/filters/range_test.rb
177
+ - test/filters/search_test.rb
178
+ - test/filters/term_test.rb
179
+ - test/integration/chainable_call_test.rb
180
+ - test/integration/not_case_test.rb
181
+ - test/integration/queries_inheritence_test.rb
182
+ - test/integration/range_case_test.rb
183
+ - test/integration/search_case_test.rb
184
+ - test/integration/term_case_test.rb
185
+ - test/queries/all_test.rb
186
+ - test/query_test.rb
187
+ - test/test_helper.rb
188
+ has_rdoc: