chewy_query 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +3 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +37 -0
  7. data/Rakefile +2 -0
  8. data/chewy_query.gemspec +27 -0
  9. data/lib/chewy_query.rb +12 -0
  10. data/lib/chewy_query/builder.rb +865 -0
  11. data/lib/chewy_query/builder/compose.rb +64 -0
  12. data/lib/chewy_query/builder/criteria.rb +182 -0
  13. data/lib/chewy_query/builder/filters.rb +227 -0
  14. data/lib/chewy_query/builder/nodes/and.rb +26 -0
  15. data/lib/chewy_query/builder/nodes/base.rb +17 -0
  16. data/lib/chewy_query/builder/nodes/bool.rb +33 -0
  17. data/lib/chewy_query/builder/nodes/equal.rb +34 -0
  18. data/lib/chewy_query/builder/nodes/exists.rb +20 -0
  19. data/lib/chewy_query/builder/nodes/expr.rb +28 -0
  20. data/lib/chewy_query/builder/nodes/field.rb +106 -0
  21. data/lib/chewy_query/builder/nodes/has_child.rb +14 -0
  22. data/lib/chewy_query/builder/nodes/has_parent.rb +14 -0
  23. data/lib/chewy_query/builder/nodes/has_relation.rb +61 -0
  24. data/lib/chewy_query/builder/nodes/match_all.rb +11 -0
  25. data/lib/chewy_query/builder/nodes/missing.rb +20 -0
  26. data/lib/chewy_query/builder/nodes/not.rb +26 -0
  27. data/lib/chewy_query/builder/nodes/or.rb +26 -0
  28. data/lib/chewy_query/builder/nodes/prefix.rb +18 -0
  29. data/lib/chewy_query/builder/nodes/query.rb +20 -0
  30. data/lib/chewy_query/builder/nodes/range.rb +63 -0
  31. data/lib/chewy_query/builder/nodes/raw.rb +15 -0
  32. data/lib/chewy_query/builder/nodes/regexp.rb +33 -0
  33. data/lib/chewy_query/builder/nodes/script.rb +20 -0
  34. data/lib/chewy_query/version.rb +3 -0
  35. data/spec/chewy_query/builder/context_spec.rb +529 -0
  36. data/spec/chewy_query/builder/filters_spec.rb +181 -0
  37. data/spec/chewy_query/builder/nodes/and_spec.rb +16 -0
  38. data/spec/chewy_query/builder/nodes/bool_spec.rb +22 -0
  39. data/spec/chewy_query/builder/nodes/equal_spec.rb +58 -0
  40. data/spec/chewy_query/builder/nodes/exists_spec.rb +16 -0
  41. data/spec/chewy_query/builder/nodes/has_child_spec.rb +79 -0
  42. data/spec/chewy_query/builder/nodes/has_parent_spec.rb +84 -0
  43. data/spec/chewy_query/builder/nodes/match_all_spec.rb +11 -0
  44. data/spec/chewy_query/builder/nodes/missing_spec.rb +14 -0
  45. data/spec/chewy_query/builder/nodes/not_spec.rb +14 -0
  46. data/spec/chewy_query/builder/nodes/or_spec.rb +16 -0
  47. data/spec/chewy_query/builder/nodes/prefix_spec.rb +15 -0
  48. data/spec/chewy_query/builder/nodes/query_spec.rb +17 -0
  49. data/spec/chewy_query/builder/nodes/range_spec.rb +36 -0
  50. data/spec/chewy_query/builder/nodes/raw_spec.rb +11 -0
  51. data/spec/chewy_query/builder/nodes/regexp_spec.rb +45 -0
  52. data/spec/chewy_query/builder/nodes/script_spec.rb +16 -0
  53. data/spec/chewy_query/builder_spec.rb +196 -0
  54. data/spec/chewy_query_spec.rb +0 -0
  55. data/spec/spec_helper.rb +8 -0
  56. metadata +191 -0
@@ -0,0 +1,181 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Filters do
4
+ def Bool(options)
5
+ ChewyQuery::Builder::Nodes::Bool.new.tap do |bool|
6
+ bool.must(*options[:must]) if options[:must].present?
7
+ bool.must_not(*options[:must_not]) if options[:must_not].present?
8
+ bool.should(*options[:should]) if options[:should].present?
9
+ end
10
+ end
11
+
12
+ %w(field group and or not raw exists missing prefix regexp range equal query script).each do |method|
13
+ define_method method.camelize do |*args|
14
+ "ChewyQuery::Builder::Nodes::#{method.camelize}".constantize.new *args
15
+ end
16
+ end
17
+
18
+ def query(&block)
19
+ ChewyQuery::Builder::Filters.new(&block).__result__
20
+ end
21
+
22
+ context 'outer scope' do
23
+ let(:email){ 'email' }
24
+ specify{ expect(query{ email }).to be_eql(Field(:email)) }
25
+ specify{ expect(query{ o{ email } }).to eq('email') }
26
+ end
27
+
28
+ context 'field' do
29
+ let(:email) { 'email' }
30
+ specify{ expect(query{ f(:email) }).to be_eql(Field(:email)) }
31
+ specify{ expect(query{ f{ :email } }).to be_eql(Field(:email)) }
32
+ specify{ expect(query{ f{ email } }).to be_eql(Field(:email)) }
33
+ specify{ expect(query{ email }).to be_eql(Field(:email)) }
34
+ specify{ expect(query{ emails.first }).to be_eql(Field('emails.first')) }
35
+ specify{ expect(query{ emails.first.second }).to be_eql(Field('emails.first.second')) }
36
+ end
37
+
38
+ context 'term' do
39
+ specify{ expect(query{ email == 'email' }).to be_eql(Equal(:email, 'email')) }
40
+ specify{ expect(query{ name != 'name' }).to be_eql(Not(Equal(:name, 'name'))) }
41
+ specify{ expect(query{ email == ['email1', 'email2'] }).to be_eql(Equal(:email, ['email1', 'email2'])) }
42
+ specify{ expect(query{ email != ['email1', 'email2'] }).to be_eql(Not(Equal(:email, ['email1', 'email2']))) }
43
+ specify{ expect(
44
+ query{ email(execution: :bool) == ['email1', 'email2'] }
45
+ ).to be_eql(Equal(:email, ['email1', 'email2'], execution: :bool)) }
46
+
47
+ specify{ expect(
48
+ query{ email(:bool) == ['email1', 'email2'] }
49
+ ).to be_eql(Equal(:email, ['email1', 'email2'], execution: :bool)) }
50
+
51
+ specify{ expect(
52
+ query{ email(:b) == ['email1', 'email2'] }
53
+ ).to be_eql(Equal(:email, ['email1', 'email2'], execution: :bool)) }
54
+ end
55
+
56
+ context 'bool' do
57
+ specify{ expect(query{ must(email == 'email') }).to be_eql(Bool(must: [Equal(:email, 'email')])) }
58
+ specify{ expect(query{ must_not(email == 'email') }).to be_eql(Bool(must_not: [Equal(:email, 'email')])) }
59
+ specify{ expect(query{ should(email == 'email') }).to be_eql(Bool(should: [Equal(:email, 'email')])) }
60
+ specify{ expect(query{
61
+ must(email == 'email').should(address != 'address', age == 42).must_not(sex == 'm').must(name == 'name')
62
+ }).to be_eql(Bool(
63
+ must: [Equal(:email, 'email'), Equal(:name, 'name')],
64
+ must_not: [Equal(:sex, 'm')],
65
+ should: [Not(Equal(:address, 'address')), Equal(:age, 42)]
66
+ )) }
67
+ end
68
+
69
+ context 'exists' do
70
+ specify{ expect(query{ email? }).to be_eql(Exists(:email)) }
71
+ specify{ expect(query{ !!email? }).to be_eql(Exists(:email)) }
72
+ specify{ expect(query{ emails.first? }).to be_eql(Exists('emails.first')) }
73
+ specify{ expect(query{ !!emails.first? }).to be_eql(Exists('emails.first')) }
74
+ specify{ expect(query{ emails != nil }).to be_eql(Exists('emails')) }
75
+ specify{ expect(query{ !(emails == nil) }).to be_eql(Exists('emails')) }
76
+ end
77
+
78
+ context 'missing' do
79
+ specify{ expect(query{ !email }).to be_eql(Missing(:email)) }
80
+ specify{ expect(query{ !email? }).to be_eql(Missing(:email, null_value: true)) }
81
+ specify{ expect(query{ !emails.first }).to be_eql(Missing('emails.first')) }
82
+ specify{ expect(query{ !emails.first? }).to be_eql(Missing('emails.first', null_value: true)) }
83
+ specify{ expect(query{ emails == nil }).to be_eql(Missing('emails', existence: false, null_value: true)) }
84
+ specify{ expect(query{ emails.first == nil }).to be_eql(
85
+ Missing('emails.first', existence: false, null_value: true)
86
+ ) }
87
+ end
88
+
89
+ context 'range' do
90
+ specify{ expect(query{ age > 42 }).to be_eql(Range(:age, gt: 42)) }
91
+ specify{ expect(query{ age >= 42 }).to be_eql(Range(:age, gt: 42, left_closed: true)) }
92
+ specify{ expect(query{ age < 42 }).to be_eql(Range(:age, lt: 42)) }
93
+ specify{ expect(query{ age <= 42 }).to be_eql(Range(:age, lt: 42, right_closed: true)) }
94
+ specify{ expect(query{ age == (30..42) }).to be_eql(Range(:age, gt: 30, lt: 42)) }
95
+ specify{ expect(query{ age == [30..42] }).to be_eql(
96
+ Range(:age, gt: 30, lt: 42, left_closed: true, right_closed: true)
97
+ ) }
98
+
99
+ specify{ expect(query{ (age > 30) & (age < 42) }).to be_eql(Range(:age, gt: 30, lt: 42)) }
100
+ specify{ expect(query{ (age > 30) & (age <= 42) }).to be_eql(Range(:age, gt: 30, lt: 42, right_closed: true)) }
101
+ specify{ expect(query{ (age >= 30) & (age < 42) }).to be_eql(Range(:age, gt: 30, lt: 42, left_closed: true)) }
102
+ specify{ expect(query{ (age >= 30) & (age <= 42) }).to be_eql(
103
+ Range(:age, gt: 30, lt: 42, right_closed: true, left_closed: true)
104
+ ) }
105
+
106
+ specify{ expect(query{ (age > 30) | (age < 42) }).to be_eql(Or(Range(:age, gt: 30), Range(:age, lt: 42))) }
107
+ end
108
+
109
+ context 'prefix' do
110
+ specify{ expect(query{ name =~ 'nam' }).to be_eql(Prefix(:name, 'nam')) }
111
+ specify{ expect(query{ name !~ 'nam' }).to be_eql(Not(Prefix(:name, 'nam'))) }
112
+ end
113
+
114
+ context 'regexp' do
115
+ specify{ expect(query{ name =~ /name/ }).to be_eql(Regexp(:name, 'name')) }
116
+ specify{ expect(query{ name == /name/ }).to be_eql(Regexp(:name, 'name')) }
117
+ specify{ expect(query{ name !~ /name/ }).to be_eql(Not(Regexp(:name, 'name'))) }
118
+ specify{ expect(query{ name != /name/ }).to be_eql(Not(Regexp(:name, 'name'))) }
119
+ specify{ expect(query{ name(:anystring, :intersection) =~ /name/ }).to be_eql(
120
+ Regexp(:name, 'name', flags: %w(anystring intersection))
121
+ ) }
122
+ end
123
+
124
+ context 'query' do
125
+ let(:some_query){ 'some query' }
126
+ specify{ expect(query{ q('some query') }).to be_eql(Query('some query')) }
127
+ specify{ expect(query{ q{'some query'} }).to be_eql(Query('some query')) }
128
+ specify{ expect(query{ q{ some_query } }).to be_eql(Query('some query')) }
129
+ end
130
+
131
+ context 'raw' do
132
+ let(:raw_query){ { term: { name: 'name' } } }
133
+ specify{ expect(query{ r(term: { name: 'name' }) }).to be_eql(Raw(term: { name: 'name' })) }
134
+ specify{ expect(query{ r{ { term: { name: 'name' } } } }).to be_eql(Raw(term: { name: 'name' })) }
135
+ specify{ expect(query{ r{ raw_query } }).to be_eql(Raw(term: { name: 'name' })) }
136
+ end
137
+
138
+ context 'script' do
139
+ let(:some_script){ 'some script' }
140
+ specify{ expect(query{ s('some script') }).to be_eql(Script('some script')) }
141
+ specify{ expect(query{ s('some script', param1: 42) }).to be_eql(Script('some script', param1: 42)) }
142
+ specify{ expect(query{ s{'some script'} }).to be_eql(Script('some script')) }
143
+ specify{ expect(query{ s(param1: 42){ some_script } }).to be_eql(Script('some script', param1: 42)) }
144
+ end
145
+
146
+ context 'and or not' do
147
+ specify{ expect(query{ (email == 'email') & (name == 'name') }).to be_eql(
148
+ And(Equal(:email, 'email'), Equal(:name, 'name'))
149
+ ) }
150
+
151
+ specify{ expect(query{ (email == 'email') | (name == 'name') }).to be_eql(
152
+ Or(Equal(:email, 'email'), Equal(:name, 'name'))
153
+ ) }
154
+
155
+ specify{ expect(query{ !(email == 'email') }).to be_eql Not(Equal(:email, 'email')) }
156
+
157
+ specify{ expect(query{ (email == 'email') & (name == 'name') | (address != 'address') }).to be_eql(
158
+ Or(And(Equal(:email, 'email'), Equal(:name, 'name')), Not(Equal(:address, 'address')))
159
+ ) }
160
+
161
+ specify{ expect(query{ (email == 'email') & ((name == 'name') | (address != 'address')) }).to be_eql(
162
+ And(Equal(:email, 'email'), Or(Equal(:name, 'name'), Not(Equal(:address, 'address'))))
163
+ ) }
164
+
165
+ specify{ expect(query{ (email == 'email') & ((name == 'name') & (address != 'address')) }).to be_eql(
166
+ And(Equal(:email, 'email'), Equal(:name, 'name'), Not(Equal(:address, 'address')))
167
+ ) }
168
+
169
+ specify{ expect(query{ ((email == 'email') | (name == 'name')) | (address != 'address') }).to be_eql(
170
+ Or(Equal(:email, 'email'), Equal(:name, 'name'), Not(Equal(:address, 'address')))
171
+ ) }
172
+
173
+ specify{ expect(query{ !((email == 'email') | (name == 'name')) }).to be_eql(
174
+ Not(Or(Equal(:email, 'email'), Equal(:name, 'name')))
175
+ ) }
176
+
177
+ specify{ expect(query{ !!((email == 'email') | (name == 'name')) }).to be_eql(
178
+ Or(Equal(:email, 'email'), Equal(:name, 'name'))
179
+ ) }
180
+ end
181
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::And do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::Filters.new(&block).__render__
7
+ end
8
+
9
+ specify{ expect(render { name? & (email == 'email') }).to eq(
10
+ and: [{ exists: { field: 'name' } }, { term: { 'email' => 'email' } }]
11
+ ) }
12
+ specify{ expect(render { ~(name? & (email == 'email')) }).to eq(
13
+ and: { filters: [{ exists: { field: 'name' } }, { term: { 'email' => 'email' } }], _cache: true}
14
+ ) }
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Bool do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::Filters.new(&block).__render__
7
+ end
8
+
9
+ specify{ expect(render{ must(name == 'name', email == 'email') }).to eq(
10
+ bool: { must: [{ term: {'name' => 'name' } }, { term: { 'email' => 'email' } }]
11
+ } ) }
12
+ specify{ expect(render{ must(name == 'name').must_not(email == 'email') }).to eq(
13
+ bool: { must: [{ term: {'name' => 'name' } }], must_not: [{ term: { 'email' => 'email' } }]
14
+ } ) }
15
+ specify{ expect(render{ must(name == 'name').should(email == 'email') }).to eq(
16
+ bool: { must: [{ term: {'name' => 'name' } }], should: [{ term: { 'email' => 'email' } }]
17
+ } ) }
18
+ specify{ expect(render{ ~must(name == 'name').should(email == 'email') }).to eq(
19
+ bool: { must: [{ term: {'name' => 'name' } }], should: [{ term: { 'email' => 'email' } }], _cache: true
20
+ } ) }
21
+ end
22
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Equal do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::Filters.new(&block).__render__
7
+ end
8
+
9
+ specify{ expect(render{ name == 'name' }).to eq(term: { 'name' => 'name' }) }
10
+ specify{ expect(render{ name != 'name' }).to eq(not: {term: { 'name' => 'name' }}) }
11
+ specify{ expect(render{ name == ['name1', 'name2'] }).to eq(terms: { 'name' => ['name1', 'name2'] }) }
12
+ specify{ expect(render{ name != ['name1', 'name2'] }).to eq(not: { terms: { 'name' => ['name1', 'name2'] } }) }
13
+ specify{ expect(render{ name(:bool) == 'name' }).to eq(term: { 'name' => 'name' }) }
14
+ specify{ expect(render{ name(:borogoves) == ['name1', 'name2'] }).to eq(terms: { 'name' => ['name1', 'name2'] }) }
15
+ specify{ expect(render{ name(:|) == ['name1', 'name2'] }).to eq(
16
+ terms: { 'name' => ['name1', 'name2'], execution: :or }
17
+ ) }
18
+
19
+ specify{ expect(render{ name(:or) == ['name1', 'name2'] }).to eq(
20
+ terms: { 'name' => ['name1', 'name2'], execution: :or }
21
+ ) }
22
+
23
+ specify{ expect(render{ name(:&) == ['name1', 'name2'] }).to eq(
24
+ terms: { 'name' => ['name1', 'name2'], execution: :and }
25
+ ) }
26
+
27
+ specify{ expect(render{ name(:and) == ['name1', 'name2'] }).to eq(
28
+ terms: { 'name' => ['name1', 'name2'], execution: :and}
29
+ ) }
30
+
31
+ specify{ expect(render{ name(:b) == ['name1', 'name2'] }).to eq(
32
+ terms: { 'name' => ['name1', 'name2'], execution: :bool}
33
+ ) }
34
+
35
+ specify{ expect(render{ name(:bool) == ['name1', 'name2'] }).to eq(
36
+ terms: { 'name' => ['name1', 'name2'], execution: :bool}
37
+ ) }
38
+
39
+ specify{ expect(render{ name(:f) == ['name1', 'name2'] }).to eq(
40
+ terms: { 'name' => ['name1', 'name2'], execution: :fielddata }
41
+ ) }
42
+
43
+ specify{ expect(render{ name(:fielddata) == ['name1', 'name2'] }).to eq(
44
+ terms: { 'name' => ['name1', 'name2'], execution: :fielddata }
45
+ ) }
46
+
47
+ specify{ expect(render{ ~name == 'name' }).to eq(term: { 'name' => 'name', _cache: true }) }
48
+ specify{ expect(render{ ~(name == 'name') }).to eq(term: { 'name' => 'name', _cache: true }) }
49
+ specify{ expect(render{ ~name != 'name' }).to eq(not: { term: { 'name' => 'name', _cache: true } }) }
50
+ specify{ expect(render{ ~name(:|) == ['name1', 'name2'] }).to eq(
51
+ terms: { 'name' => ['name1', 'name2'], execution: :or, _cache: true }
52
+ ) }
53
+
54
+ specify{ expect(render{ ~name != ['name1', 'name2'] }).to eq(
55
+ not: { terms: { 'name' => ['name1', 'name2'], _cache: true } }
56
+ ) }
57
+ end
58
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Exists do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::Filters.new(&block).__render__
7
+ end
8
+
9
+ specify{ expect(render{ name? }).to eq(exists: { field: 'name' }) }
10
+ specify{ expect(render{ !!name? }).to eq(exists: { field: 'name' }) }
11
+ specify{ expect(render{ !!name }).to eq(exists: { field: 'name' }) }
12
+ specify{ expect(render{ name != nil }).to eq(exists: { field: 'name' }) }
13
+ specify{ expect(render{ !(name == nil) }).to eq(exists: { field: 'name' }) }
14
+ specify{ expect(render{ ~name? }).to eq(exists: { field: 'name' }) }
15
+ end
16
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::HasChild do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::Filters.new(&block).__render__
7
+ end
8
+
9
+ specify{ expect(render{ has_child('child') }).to eq(has_child: { type: 'child' }) }
10
+ specify{ expect(render{ has_child('child').filter(term: { name: 'name' }) }).to eq(
11
+ has_child: { type: 'child', filter: { term: { name: 'name' } } }
12
+ ) }
13
+
14
+ specify{ expect(render{ has_child('child').filter{ name == 'name' } }).to eq(
15
+ has_child: { type: 'child', filter: { term: { 'name' => 'name' } } }
16
+ ) }
17
+
18
+ specify{ expect(render{ has_child('child').filter(term: { name: 'name' }).filter{ age < 42 } }).to eq(
19
+ has_child: { type: 'child', filter: { and: [{ term: { name: 'name' } }, range: { 'age' => { lt: 42 } }] } }
20
+ ) }
21
+
22
+ specify{ expect(render{
23
+ has_child('child').filter(term: { name: 'name' }).filter{ age < 42 }.filter_mode(:or)
24
+ }).to eq(
25
+ has_child: { type: 'child', filter: { or: [{ term: { name: 'name' } }, range: { 'age' => { lt: 42 } }] } }
26
+ ) }
27
+
28
+ specify{ expect(render{ has_child('child').query(match: { name: 'name' }) }).to eq(
29
+ has_child: { type: 'child', query: { match: { name: 'name' } } }
30
+ ) }
31
+
32
+ specify{ expect(render{
33
+ has_child('child').query(match: { name: 'name' }).query(match: { surname: 'surname' })
34
+ }).to eq(
35
+ has_child: {
36
+ type: 'child',
37
+ query: { bool: { must: [{ match: { name: 'name' } }, { match: { surname: 'surname' } }] }}
38
+ }
39
+ ) }
40
+
41
+ specify{ expect(render{
42
+ has_child('child').query(match: { name: 'name' }).query(match: { surname: 'surname' }).query_mode(:should)
43
+ }).to eq(
44
+ has_child: {
45
+ type: 'child',
46
+ query: { bool: { should: [{ match: { name: 'name' } }, { match: { surname: 'surname' } }] } }
47
+ }
48
+ ) }
49
+
50
+ specify{ expect(render{ has_child('child').filter{ name == 'name' }.query(match: {name: 'name'}) }).to eq(
51
+ has_child: {
52
+ type: 'child',
53
+ query: { filtered: { query: { match: { name: 'name' } }, filter: { term: { 'name' => 'name' } } } }
54
+ }
55
+ ) }
56
+
57
+ specify{ expect(render{
58
+ has_child('child').filter{ name == 'name' }.query(match: {name: 'name'}).filter{ age < 42 }
59
+ }).to eq(
60
+ has_child: {
61
+ type: 'child',
62
+ query: {
63
+ filtered: {
64
+ query: { match: { name: 'name' } },
65
+ filter: { and: [{ term: {'name' => 'name' } }, range: { 'age' => { lt: 42 } }] }
66
+ }
67
+ }
68
+ }
69
+ ) }
70
+
71
+ context do
72
+ let(:name){ 'Name' }
73
+
74
+ specify{ expect(render{ has_child('child').filter{ name == o{name} } }).to eq(
75
+ has_child: { type: 'child', filter: { term: { 'name' => 'Name' } } }
76
+ ) }
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::HasParent do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::Filters.new(&block).__render__
7
+ end
8
+
9
+ specify{ expect(render{ has_parent('child') }).to eq(has_parent: { type: 'child' }) }
10
+ specify{ expect(render{ has_parent('child').filter(term: { name: 'name' }) }).to eq(
11
+ has_parent: { type: 'child', filter: { term: { name: 'name' } } }
12
+ ) }
13
+
14
+ specify{ expect(render{ has_parent('child').filter{ name == 'name' } }).to eq(
15
+ has_parent: { type: 'child', filter: { term: { 'name' => 'name' } } }
16
+ ) }
17
+
18
+ specify{ expect(render{ has_parent('child').filter(term: { name: 'name' }).filter{ age < 42 } }).to eq(
19
+ has_parent: { type: 'child', filter: { and: [{ term: { name: 'name' } }, range: { 'age' => { lt: 42 } }] } }
20
+ ) }
21
+
22
+ specify{ expect(render{
23
+ has_parent('child').filter(term: { name: 'name' }).filter{ age < 42 }.filter_mode(:or)
24
+ }).to eq(
25
+ has_parent: {
26
+ type: 'child',
27
+ filter: { or: [{ term: { name: 'name' } }, range: { 'age' => { lt: 42 } }] }
28
+ }
29
+ ) }
30
+
31
+ specify{ expect(render{ has_parent('child').query(match: { name: 'name' }) }).to eq(
32
+ has_parent: { type: 'child', query: { match: { name: 'name' } } }
33
+ ) }
34
+
35
+ specify{ expect(render{
36
+ has_parent('child').query(match: { name: 'name' }).query(match: { surname: 'surname' })
37
+ }).to eq(
38
+ has_parent: {
39
+ type: 'child',
40
+ query: { bool: { must: [{ match: {name: 'name' } }, { match: { surname: 'surname' } }] } }
41
+ }
42
+ ) }
43
+
44
+ specify{ expect(render{
45
+ has_parent('child').query(match: { name: 'name' }).query(match: { surname: 'surname' }).query_mode(:should)
46
+ }).to eq(
47
+ has_parent: {
48
+ type: 'child',
49
+ query: { bool: { should: [{ match: { name: 'name' } }, { match: { surname: 'surname' } }] } }
50
+ }
51
+ ) }
52
+
53
+ specify{ expect(render{
54
+ has_parent('child').filter{ name == 'name' }.query(match: { name: 'name' })
55
+ }).to eq(
56
+ has_parent: {
57
+ type: 'child',
58
+ query: { filtered: { query: { match: { name: 'name' } }, filter: { term: { 'name' => 'name' } } } }
59
+ }
60
+ ) }
61
+
62
+ specify{ expect(render{
63
+ has_parent('child').filter{ name == 'name' }.query(match: { name: 'name' }).filter{ age < 42 }
64
+ }).to eq(
65
+ has_parent: {
66
+ type: 'child',
67
+ query: {
68
+ filtered: {
69
+ query: { match: { name: 'name' } },
70
+ filter: { and: [{ term: { 'name' => 'name' } }, range: { 'age' => { lt: 42 } }] }
71
+ }
72
+ }
73
+ }
74
+ ) }
75
+
76
+ context do
77
+ let(:name){ 'Name' }
78
+
79
+ specify{ expect(render{ has_parent('child').filter{ name == o{ name } } }).to eq(
80
+ has_parent: { type: 'child', filter: { term: { 'name' => 'Name' } } }
81
+ ) }
82
+ end
83
+ end
84
+ end