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
2 changes: 1 addition & 1 deletion lib/fbe/same.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
# @param [Hash] attrs The attributes the caller asked for
# @return [Boolean] TRUE if every Time attribute is in the fact
def Fbe.same?(fact, attrs)
attrs.all? { |k, v| !v.is_a?(Time) || [fact.public_send(k)].flatten.include?(v) }
attrs.all? { |k, v| !v.is_a?(Time) || fact[k.to_s]&.include?(v) }
end
15 changes: 15 additions & 0 deletions test/fbe/test_if_absent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,19 @@ def test_raises_without_block
assert_equal('A block is required by if_absent', error.message)
assert_equal(0, fb.size, 'if_absent inserted a fact without a block')
end

def test_dont_duplicate_fact_whose_time_is_not_the_first_value
seed = Random.new_seed
hour = Random.new(seed).rand(0..20)
fb = Factbase.new
g = fb.insert
g.what = 'ëvent'
g.when = Time.utc(2025, 1, 1, hour)
g.when = Time.utc(2025, 1, 1, hour + 1)
Fbe.if_absent(fb:) do |f|
f.what = 'ëvent'
f.when = Time.utc(2025, 1, 1, hour + 1)
end
assert_equal(1, fb.size, "if_absent created a duplicate, seed #{seed}")
end
end
15 changes: 15 additions & 0 deletions test/fbe/test_just_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,19 @@ def test_dont_insert_fact_without_block
end
assert_equal(0, fb.size, 'just_one inserted a fact without a block')
end

def test_dont_duplicate_fact_whose_time_is_not_the_first_value
seed = Random.new_seed
hour = Random.new(seed).rand(0..20)
fb = Factbase.new
g = fb.insert
g.what = 'ëvent'
g.when = Time.utc(2025, 1, 1, hour)
g.when = Time.utc(2025, 1, 1, hour + 1)
Fbe.just_one(fb:) do |f|
f.what = 'ëvent'
f.when = Time.utc(2025, 1, 1, hour + 1)
end
assert_equal(1, fb.size, "just_one created a duplicate, seed #{seed}")
end
end
32 changes: 32 additions & 0 deletions test/fbe/test_same.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

# SPDX-FileCopyrightText: Copyright (c) 2024-2026 Zerocracy
# SPDX-License-Identifier: MIT

require 'factbase'
require_relative '../../lib/fbe/same'
require_relative '../test__helper'

# Test.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2024-2026 Zerocracy
# License:: MIT
class TestSame < Fbe::Test
def test_finds_time_that_is_not_the_first_value
seed = Random.new_seed
hour = Random.new(seed).rand(0..20)
f = Factbase.new.insert
f.when = Time.utc(2025, 1, 1, hour)
f.when = Time.utc(2025, 1, 1, hour + 1)
assert(Fbe.same?(f, { when: Time.utc(2025, 1, 1, hour + 1) }), "second time of the fact is not seen, seed #{seed}")
end

def test_refuses_time_that_is_in_no_value
seed = Random.new_seed
hour = Random.new(seed).rand(0..20)
f = Factbase.new.insert
f.when = Time.utc(2025, 1, 1, hour)
f.when = Time.utc(2025, 1, 1, hour + 1)
refute(Fbe.same?(f, { when: Time.utc(2025, 1, 1, hour + 2) }), "absent time is taken as present, seed #{seed}")
end
end
Loading