1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-23 15:19:38 +02:00

Add the ability to "rollup" values in a time series (#554)

* Clean up time series models

* Add value group rollup class for summarizing hierarchical data

* Integrate new classes

* Update UI to use new patterns

* Update D3 charts to expect new data format

* Clean up account model

* More cleanup

* Money improvements

* Use new money fields

* Remove invalid fixture data to avoid orphaned accountables

* Update time series to work better with collections

* Fix tests and UI bugs
This commit is contained in:
Zach Gollwitzer 2024-03-19 09:10:40 -04:00 committed by GitHub
parent 0a8518506c
commit f904d9d062
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 687 additions and 391 deletions

View file

@ -0,0 +1,75 @@
require "test_helper"
class TimeSeriesTest < ActiveSupport::TestCase
test "it can accept array of money values" do
series = TimeSeries.new([ { date: 1.day.ago.to_date, value: Money.new(100) }, { date: Date.current, value: Money.new(200) } ])
assert_equal Money.new(100), series.first.value
assert_equal Money.new(200), series.last.value
assert_equal :normal, series.type
assert_equal "up", series.trend.direction
assert_equal Money.new(100), series.trend.value
assert_equal 100.0, series.trend.percent
end
test "it can accept array of numeric values" do
series = TimeSeries.new([ { date: 1.day.ago.to_date, value: 100 }, { date: Date.current, value: 200 } ])
assert_equal 100, series.first.value
assert_equal 200, series.last.value
assert_equal 100, series.on(1.day.ago.to_date).value
assert_equal :normal, series.type
assert_equal "up", series.trend.direction
assert_equal 100, series.trend.value
assert_equal 100.0, series.trend.percent
end
test "when nil or empty array passed, it returns empty series" do
series = TimeSeries.new(nil)
assert_equal [], series.values
series = TimeSeries.new([])
assert_nil series.first
assert_nil series.last
assert_equal({ values: [], trend: { type: "normal", direction: "flat", value: 0, percent: 0.0 }, type: "normal" }.to_json, series.to_json)
end
test "money series can be serialized to json" do
expected_values = {
values: [
{
date: "2024-03-17",
value: { amount: "100.0", currency: "USD" },
trend: { type: "normal", direction: "flat", value: { amount: "0.0", currency: "USD" }, percent: 0.0 }
},
{
date: "2024-03-18",
value: { amount: "200.0", currency: "USD" },
trend: { type: "normal", direction: "up", value: { amount: "100.0", currency: "USD" }, percent: 100.0 }
}
],
trend: { type: "normal", direction: "up", value: { amount: "100.0", currency: "USD" }, percent: 100.0 },
type: "normal"
}.to_json
series = TimeSeries.new([ { date: 1.day.ago.to_date, value: Money.new(100) }, { date: Date.current, value: Money.new(200) } ])
assert_equal expected_values, series.to_json
end
test "numeric series can be serialized to json" do
expected_values = {
values: [
{ date: 1.day.ago.to_date, value: 100, trend: { type: "normal", direction: "flat", value: 0, percent: 0.0 } },
{ date: Date.current, value: 200, trend: { type: "normal", direction: "up", value: 100, percent: 100.0 } }
],
trend: { type: "normal", direction: "up", value: 100, percent: 100.0 },
type: "normal"
}.to_json
series = TimeSeries.new([ { date: 1.day.ago.to_date, value: 100 }, { date: Date.current, value: 200 } ])
assert_equal expected_values, series.to_json
end
end