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/just_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def Fbe.just_one(fb: Fbe.fb)
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 just_one') 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
21 changes: 21 additions & 0 deletions test/fbe/test_just_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ def test_raises_on_nil
end
end

def test_rejects_empty_matching_attributes
fb = Factbase.new
fb.insert.foo = 'existing'
error = assert_raises(Fbe::Error) do
Fbe.just_one(fb:) { |_f| }
end
assert_match(/non-system attribute/, error.message)
assert_equal(1, fb.size)
end

def test_rejects_only_system_matching_attributes
fb = Factbase.new
error = assert_raises(Fbe::Error) do
Fbe.just_one(fb:) do |f|
f._id = 42
end
end
assert_match(/non-system attribute/, error.message)
assert_equal(0, fb.size)
end

def test_raises_without_block
assert_raises(Fbe::Error, 'just_one accepted a call without a block') do
Fbe.just_one(fb: Factbase.new)
Expand Down
Loading