2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-17 13:03:08 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe TimelineLookup do
|
|
|
|
it "returns an empty array for empty input" do
|
|
|
|
expect(TimelineLookup.build([])).to eq([])
|
|
|
|
end
|
|
|
|
|
2018-06-26 23:11:22 -04:00
|
|
|
it "returns an empty array for if input is an array if post ids" do
|
|
|
|
expect(TimelineLookup.build([1, 2, 3])).to eq([])
|
|
|
|
end
|
|
|
|
|
2016-05-17 13:03:08 -04:00
|
|
|
it "returns the lookup for a series of posts" do
|
2018-06-26 23:11:22 -04:00
|
|
|
result = TimelineLookup.build([[111, 10], [222, 9], [333, 8]])
|
2016-05-17 13:03:08 -04:00
|
|
|
expect(result).to eq([[1, 10], [2, 9], [3, 8]])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "omits duplicate dates" do
|
2018-06-26 23:11:22 -04:00
|
|
|
result = TimelineLookup.build([[111, 10], [222, 10], [333, 8]])
|
2016-05-26 12:49:54 -04:00
|
|
|
expect(result).to eq([[1, 10], [3, 8]])
|
|
|
|
end
|
|
|
|
|
2016-05-17 13:03:08 -04:00
|
|
|
it "respects a `max_values` setting" do
|
2018-06-26 23:11:22 -04:00
|
|
|
input = (1..100).map { |i| [1000 + i, 100 - i] }
|
2016-05-17 13:03:08 -04:00
|
|
|
|
|
|
|
result = TimelineLookup.build(input, 5)
|
2021-01-25 06:04:27 -05:00
|
|
|
# even if max_value is 5 we might get 6 (5 + 1)
|
|
|
|
# to ensure the last tuple is captured
|
|
|
|
expect(result).to eq([[1, 99], [21, 79], [41, 59], [61, 39], [81, 19], [input.size, input.last[1]]])
|
2016-05-17 13:03:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "respects an uneven `max_values` setting" do
|
2018-06-26 23:11:22 -04:00
|
|
|
input = (1..100).map { |i| [1000 + i, 100 - i] }
|
2016-05-17 13:03:08 -04:00
|
|
|
|
|
|
|
result = TimelineLookup.build(input, 3)
|
2021-01-25 06:04:27 -05:00
|
|
|
# even if max_value is 3 we might get 4 (3 + 1)
|
|
|
|
# to ensure the last tuple is captured
|
|
|
|
expect(result.size).to eq(4)
|
|
|
|
expect(result).to eq([[1, 99], [35, 65], [69, 31], [input.size, input.last[1]]])
|
2016-05-17 13:03:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|