Files
sure/test/models/period_test.rb
T
7f5cf4c082 Add 'all_time' period option to Period model (#279)
* Add 'all_time' period option to Period model

Introduces an 'all_time' period to the Period model, which spans from the family's oldest entry date to the current date. Includes tests to verify correct creation and date range calculation for the new period.

* Update test/models/period_test.rb

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Mark Hendriksen <hendriksen-mark@hotmail.com>

* Improve 'all_time' period fallback logic

Updates the 'all_time' period to use a 5-year fallback range when no family or entries exist, or when the oldest entry date is today. Adds tests to verify correct behavior for these edge cases.

* Update period.rb

---------

Signed-off-by: Mark Hendriksen <hendriksen-mark@hotmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2025-11-10 23:07:07 +01:00

99 lines
3.5 KiB
Ruby

require "test_helper"
class PeriodTest < ActiveSupport::TestCase
test "raises validation error when start_date or end_date is missing" do
error = assert_raises(ActiveModel::ValidationError) do
Period.new(start_date: nil, end_date: nil)
end
assert_includes error.message, "Start date can't be blank"
assert_includes error.message, "End date can't be blank"
end
test "raises validation error when start_date is not before end_date" do
error = assert_raises(ActiveModel::ValidationError) do
Period.new(start_date: Date.current, end_date: Date.current - 1.day)
end
assert_includes error.message, "Start date must be before end date"
end
test "can create custom period" do
period = Period.new(start_date: Date.current - 15.days, end_date: Date.current)
assert_equal "Custom Period", period.label
end
test "from_key returns period for valid key" do
period = Period.from_key("last_30_days")
assert_equal 30.days.ago.to_date, period.start_date
assert_equal Date.current, period.end_date
end
test "from_key with invalid key and no fallback raises error" do
error = assert_raises(Period::InvalidKeyError) do
Period.from_key("invalid_key")
end
end
test "label returns correct label for known period" do
period = Period.from_key("last_30_days")
assert_equal "Last 30 Days", period.label
end
test "label returns Custom Period for unknown period" do
period = Period.new(start_date: Date.current - 15.days, end_date: Date.current)
assert_equal "Custom Period", period.label
end
test "comparison_label returns correct label for known period" do
period = Period.from_key("last_30_days")
assert_equal "vs. last month", period.comparison_label
end
test "comparison_label returns date range for unknown period" do
start_date = Date.current - 15.days
end_date = Date.current
period = Period.new(start_date: start_date, end_date: end_date)
expected = "#{start_date.strftime("%b %d, %Y")} to #{end_date.strftime("%b %d, %Y")}"
assert_equal expected, period.comparison_label
end
test "all_time period can be created" do
period = Period.from_key("all_time")
assert_equal "all_time", period.key
assert_equal "All Time", period.label
assert_equal "All", period.label_short
assert_equal "vs. beginning", period.comparison_label
end
test "all_time period uses family's oldest entry date" do
# Mock Current.family to return a family with oldest_entry_date
mock_family = mock("family")
mock_family.expects(:oldest_entry_date).returns(2.years.ago.to_date)
Current.expects(:family).at_least_once.returns(mock_family)
period = Period.from_key("all_time")
assert_equal 2.years.ago.to_date, period.start_date
assert_equal Date.current, period.end_date
end
test "all_time period uses fallback when no family or entries exist" do
Current.expects(:family).returns(nil)
period = Period.from_key("all_time")
assert_equal 5.years.ago.to_date, period.start_date
assert_equal Date.current, period.end_date
end
test "all_time period uses fallback when oldest_entry_date equals current date" do
# Mock a family that has no historical entries (oldest_entry_date returns today)
mock_family = mock("family")
mock_family.expects(:oldest_entry_date).returns(Date.current)
Current.expects(:family).at_least_once.returns(mock_family)
period = Period.from_key("all_time")
assert_equal 5.years.ago.to_date, period.start_date
assert_equal Date.current, period.end_date
end
end