diff --git a/lib/fbe/tombstone.rb b/lib/fbe/tombstone.rb index d4e4e87a..f1b3a0e5 100644 --- a/lib/fbe/tombstone.rb +++ b/lib/fbe/tombstone.rb @@ -29,11 +29,7 @@ def initialize(fb: Fbe.fb, fid: '_id') def issues(where, repo) raise(Fbe::Error, 'The type of "where" is not String') unless where.is_a?(String) raise(Fbe::Error, 'The type of "repo" is not Integer') unless repo.is_a?(Integer) - f = @fb.query( - "(and (eq where '#{where}') (eq what 'tombstone') (eq repository #{repo}) (exists issues))" - ).each.first - return [] if f.nil? - f['issues'].flat_map do |ii| + buried(where, repo).flat_map do |ii| a, b = ii.split('-').map { |i| Integer(i, 10) } b = a if b.nil? (a..b).to_a @@ -92,13 +88,11 @@ def has?(where, repo, issue) raise(Fbe::Error, 'The type of "issue" is neither Integer nor Array') end issue = numbers(issue) - f = @fb.query( - "(and (eq where '#{where}') (eq what 'tombstone') (eq repository #{repo}) (exists issues))" - ).each.first - return false if f.nil? + ranges = buried(where, repo) + return false if ranges.empty? return false if issue.empty? issue.all? do |i| - f['issues'].any? do |ii| + ranges.any? do |ii| a, b = ii.split('-').map { |i| Integer(i, 10) } b.nil? ? a == i : (a..b).cover?(i) end @@ -107,6 +101,12 @@ def has?(where, repo, issue) private + def buried(where, repo) + @fb.query( + "(and (eq where '#{where}') (eq what 'tombstone') (eq repository #{repo}) (exists issues))" + ).each.flat_map { |f| f['issues'] } + end + def numbers(issue) list = issue.is_a?(Array) ? issue : [issue] list.each do |i| diff --git a/test/fbe/test_tombstone.rb b/test/fbe/test_tombstone.rb index 90f7c6e5..862ae061 100644 --- a/test/fbe/test_tombstone.rb +++ b/test/fbe/test_tombstone.rb @@ -201,4 +201,20 @@ def test_dont_touch_factbase_when_member_is_invalid end assert_equal(0, fb.size, "factbase is touched by member #{member.inspect}, seed is #{seed}") end + + def test_reads_all_tombstone_facts_of_repository + fb = Factbase.new + %w[5 9].each do |i| + f = fb.insert + f.what = 'tombstone' + f.where = 'github' + f.repository = 7 + f.issues = i + end + ts = Fbe::Tombstone.new(fb:) + assert(ts.has?('github', 7, 5), 'The first tombstone fact must be read') + assert(ts.has?('github', 7, 9), 'The second tombstone fact must be read') + assert(ts.has?('github', 7, [5, 9]), 'Both tombstone facts must be read together') + assert_equal([5, 9], ts.issues('github', 7).sort) + end end