Skip to content
Open
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
4 changes: 3 additions & 1 deletion lib/fbe/if_absent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def Fbe.if_absent(fb: Fbe.fb, always: false)
end
end
yield(f)
q = attrs.except(:_id, :_time, :_version).map do |k, v|
matching = attrs.except(:_id, :_time, :_version)
raise(Fbe::Error, 'At least one non-system attribute is required by if_absent') if matching.empty?
q = matching.map do |k, v|
raise(Fbe::Error, "Can't match #{k} by an array, only by one value") if v.is_a?(Array)
vv = v.to_s
if v.is_a?(String)
Expand Down
19 changes: 19 additions & 0 deletions test/fbe/test_if_absent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ def test_ignores
assert_nil(n)
end

def test_raises_when_block_sets_no_attributes
fb = Factbase.new
fb.insert.foo = 'unrelated'
error = assert_raises(Fbe::Error) do
Fbe.if_absent(fb:) { |_fact| }
end
assert_equal('At least one non-system attribute is required by if_absent', error.message)
assert_equal(1, fb.size)
end

def test_raises_when_only_system_attributes_are_set
fb = Factbase.new
error = assert_raises(Fbe::Error) do
Fbe.if_absent(fb:) { |fact| fact._id = 42 }
end
assert_equal('At least one non-system attribute is required by if_absent', error.message)
assert_equal(0, fb.size)
end

def test_raises_on_empty_value
assert_raises(StandardError) do
Fbe.if_absent(fb: Factbase.new) do |f|
Expand Down
Loading