Skip to content

RCBC-490: Raise Error::InvalidArgument for invalid search queries #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/couchbase/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def get_multi(ids, options = Options::GetMulti::DEFAULT)
# @return [GetResult]
def get_and_lock(id, lock_time, options = Options::GetAndLock::DEFAULT)
resp = @backend.document_get_and_lock(bucket_name, @scope_name, @name, id,
lock_time.respond_to?(:in_seconds) ? lock_time.public_send(:in_seconds) : lock_time,
lock_time.respond_to?(:in_seconds) ? lock_time.in_seconds : lock_time,
options.to_backend)
GetResult.new do |res|
res.transcoder = options.transcoder
Expand Down
2 changes: 1 addition & 1 deletion lib/couchbase/protostellar/request_generator/kv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def get_and_lock_request(id, lock_time, options)
proto_req = Generated::KV::V1::GetAndLockRequest.new(
**location,
key: id,
lock_time: lock_time.respond_to?(:in_seconds) ? lock_time.public_send(:in_seconds) : lock_time
lock_time: lock_time.respond_to?(:in_seconds) ? lock_time.in_seconds : lock_time
)

create_kv_request(proto_req, :get_and_lock, options)
Expand Down
23 changes: 13 additions & 10 deletions lib/couchbase/search_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def initialize

# @return [Hash<Symbol, #to_json>]
def to_h
raise ArgumentError, "either start_time or end_time must be set for DateRangeQuery" if @start_time.nil? && @end_time.nil?
raise Error::InvalidArgument, "either start_time or end_time must be set for DateRangeQuery" if @start_time.nil? && @end_time.nil?

data = {}
data[:boost] = boost if boost
Expand Down Expand Up @@ -483,7 +483,7 @@ def initialize

# @return [Hash<Symbol, #to_json>]
def to_h
raise ArgumentError, "either min or max must be set for NumericRangeQuery" if @min.nil? && @max.nil?
raise Error::InvalidArgument, "either min or max must be set for NumericRangeQuery" if @min.nil? && @max.nil?

data = {}
data[:boost] = boost if boost
Expand Down Expand Up @@ -551,7 +551,7 @@ def initialize

# @return [Hash<Symbol, #to_json>]
def to_h
raise ArgumentError, "either min or max must be set for TermRangeQuery" if @min.nil? && @max.nil?
raise Error::InvalidArgument, "either min or max must be set for TermRangeQuery" if @min.nil? && @max.nil?

data = {}
data[:boost] = boost if boost
Expand Down Expand Up @@ -754,7 +754,7 @@ def empty?

# @return [Hash<Symbol, #to_json>]
def to_h
raise ArgumentError, "compound conjunction query must have sub-queries" if @queries.nil? || @queries.empty?
raise Error::InvalidArgument, "compound conjunction query must have sub-queries" if @queries.nil? || @queries.empty?

data = {:conjuncts => @queries.uniq.map(&:to_h)}
data[:boost] = boost if boost
Expand Down Expand Up @@ -799,11 +799,11 @@ def empty?

# @return [Hash<Symbol, #to_json>]
def to_h
raise ArgumentError, "compound disjunction query must have sub-queries" if @queries.nil? || @queries.empty?
raise Error::InvalidArgument, "compound disjunction query must have sub-queries" if @queries.nil? || @queries.empty?

data = {:disjuncts => @queries.uniq.map(&:to_h)}
if min
raise ArgumentError, "disjunction query has fewer sub-queries than configured minimum" if @queries.size < min
raise Error::InvalidArgument, "disjunction query has fewer sub-queries than configured minimum" if @queries.size < min

data[:min] = min
end
Expand All @@ -828,7 +828,7 @@ class BooleanQuery < SearchQuery

# @yieldparam [BooleanQuery] self
def initialize
super()
super
@must = ConjunctionQuery.new
@must_not = DisjunctionQuery.new
@should = DisjunctionQuery.new
Expand Down Expand Up @@ -861,7 +861,10 @@ def should(*queries)

# @return [Hash<Symbol, #to_json>]
def to_h
raise ArgumentError, "BooleanQuery must have at least one non-empty sub-query" if @must.empty? && @must_not.empty? && @should.empty?
if @must.empty? && @must_not.empty? && @should.empty?
raise Error::InvalidArgument,
"BooleanQuery must have at least one non-empty sub-query"
end

data = {}
data[:must] = @must.to_h unless @must.empty?
Expand Down Expand Up @@ -1007,7 +1010,7 @@ def self.match_all(&block)
class MatchAllQuery < SearchQuery
# @yieldparam [MatchAllQuery] self
def initialize
super()
super
yield self if block_given?
end

Expand All @@ -1030,7 +1033,7 @@ def self.match_none(&block)
class MatchNoneQuery < SearchQuery
# @yieldparam [MatchNoneQuery] self
def initialize
super()
super
yield self if block_given?
end

Expand Down
2 changes: 1 addition & 1 deletion lib/couchbase/utils/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def extract_duration(number_or_duration)
return number_or_duration if number_or_duration.class == Integer # rubocop:disable Style/ClassEqualityComparison avoid overrides of #is_a?, #kind_of?

if number_or_duration.respond_to?(:in_milliseconds)
number_or_duration.public_send(:in_milliseconds)
number_or_duration.in_milliseconds
else
number_or_duration
end.to_i
Expand Down
5 changes: 2 additions & 3 deletions test/ds_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def test_new_list_empty
def test_new_list_yields_no_elements
doc_id = uniq_id(:foo)
list = CouchbaseList.new(doc_id, @collection)
actual = []
list.each do |element|
actual << element
actual = list.map do |element|
element
end

assert_empty actual
Expand Down
10 changes: 4 additions & 6 deletions test/ds_map_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def test_new_map_empty
def test_new_map_yields_no_elements
doc_id = uniq_id(:foo)
map = CouchbaseMap.new(doc_id, @collection)
actual = []
map.each do |key, value|
actual << [key, value]
actual = map.map do |key, value|
[key, value]
end

assert_empty actual
Expand Down Expand Up @@ -128,9 +127,8 @@ def test_changes_the_value_by_key
refute_empty map
map["baz"] = "bar"

pairs = []
map.each do |*pair|
pairs << pair
pairs = map.map do |*pair|
pair
end

assert_equal [%w[baz bar], ["foo", 100]], pairs.sort
Expand Down
5 changes: 2 additions & 3 deletions test/ds_queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def test_new_queue_empty
def test_new_queue_yields_no_elements
doc_id = uniq_id(:foo)
queue = CouchbaseQueue.new(doc_id, @collection)
actual = []
queue.each do |element|
actual << element
actual = queue.map do |element|
element
end

assert_empty actual
Expand Down
10 changes: 4 additions & 6 deletions test/ds_set_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def test_new_set_empty
def test_new_set_yields_no_elements
doc_id = uniq_id(:foo)
set = CouchbaseSet.new(doc_id, @collection)
actual = []
set.each do |element|
actual << element
actual = set.map do |element|
element
end

assert_empty actual
Expand All @@ -57,9 +56,8 @@ def test_add_does_not_create_duplicates
set.add("foo")
set.add("foo")

actual = []
set.each do |element|
actual << element
actual = set.map do |element|
element
end

assert_equal %w[foo], actual
Expand Down
Loading