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
5 changes: 3 additions & 2 deletions lib/fbe/overwrite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
# an exception will be raised.
#
# @param [Factbase::Fact] fact The fact to modify (must have _id property)
# @param [String, Hash] property_or_hash The name of the property to set, or a hash of properties
# @param [String, Symbol, Hash] property_or_hash The name of the property to set, or a hash of properties
# @param [Any] values The value to set (can be any type, including array) - ignored if first param is Hash
# @param [Factbase] fb The factbase to use (defaults to Fbe.fb)
# @param [String] fid The property used as the fact identifier (defaults to '_id')
# @return [nil] Nothing
# @raise [Fbe::Error] If fact is nil, has no identifier, or property is neither a String nor a Hash
# @raise [Fbe::Error] If fact is nil, has no identifier, or property is neither a String, Symbol, nor Hash
# @note This operation preserves all other properties during recreation
# @note If property already has the same single value, no changes are made
# @example Update a user's status
Expand Down Expand Up @@ -88,6 +88,7 @@ def Fbe.overwrite(fact, property_or_hash, values = nil, fb: Fbe.fb, fid: '_id')
return
end
property = property_or_hash
property = property.to_s if property.is_a?(Symbol)
raise(Fbe::Error, "The property is not a String but #{property.class} (#{property})") unless property.is_a?(String)
raise(Fbe::Error, 'The values is nil') if values.nil?
values = [values] unless values.is_a?(Array)
Expand Down
37 changes: 37 additions & 0 deletions test/fbe/test_overwrite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,43 @@ def test_simple_overwrite
assert_equal(2, fb.query('(always)').each.first['many'].size)
end

def test_overwrites_missing_symbol_property
fb = Factbase.new
f = fb.insert
f._id = 1
f.foo = 42
Fbe.overwrite(f, :bar, 55, fb:)
result = fb.query('(eq _id 1)').each.to_a
assert_equal(1, result.size)
assert_equal([42], result.first['foo'])
assert_equal([55], result.first['bar'])
end

def test_overwrites_existing_symbol_property
fb = Factbase.new
f = fb.insert
f._id = 1
f.foo = 42
f.bar = 'keep'
Fbe.overwrite(f, :foo, 55, fb:)
result = fb.query('(eq _id 1)').each.to_a
assert_equal(1, result.size)
assert_equal([55], result.first['foo'])
assert_equal(['keep'], result.first['bar'])
end

def test_rejects_unsupported_property_name_types
fb = Factbase.new
f = fb.insert
{
42 => 'The property is not a String but Integer (42)',
nil => 'The property is not a String but NilClass ()'
}.each do |property, message|
error = assert_raises(Fbe::Error) { Fbe.overwrite(f, property, 55, fb:) }
assert_equal(message, error.message)
end
end

def test_avoids_duplicates
fb = Factbase.new
f = fb.insert
Expand Down
Loading