amounts 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +95 -0
- data/README.md +101 -6
- data/lib/amount/active_record/attribute_definition.rb +12 -8
- data/lib/amount/active_record/calculations.rb +127 -0
- data/lib/amount/active_record/dirty.rb +43 -0
- data/lib/amount/active_record/model.rb +12 -5
- data/lib/amount/active_record/persistence.rb +34 -0
- data/lib/amount/active_record/serialization.rb +57 -0
- data/lib/amount/active_record.rb +6 -0
- data/lib/amount/compact.rb +5 -1
- data/lib/amount/concerns/calculable.rb +4 -1
- data/lib/amount/concerns/exportable.rb +31 -0
- data/lib/amount/concerns/serializable.rb +18 -1
- data/lib/amount/display.rb +7 -4
- data/lib/amount/manifest.rb +128 -0
- data/lib/amount/registry.rb +9 -0
- data/lib/amount/version.rb +1 -1
- data/lib/amount.rb +5 -0
- data/test/amount/active_record/amount_validator_test.rb +8 -0
- data/test/amount/active_record/attribute_definition_test.rb +25 -0
- data/test/amount/active_record/calculations_test.rb +165 -0
- data/test/amount/active_record/dirty_test.rb +70 -0
- data/test/amount/active_record/model_test.rb +32 -0
- data/test/amount/active_record/persistence_test.rb +53 -0
- data/test/amount/active_record/serialization_test.rb +75 -0
- data/test/amount/compact_test.rb +22 -7
- data/test/amount/concerns/calculable_test.rb +33 -0
- data/test/amount/concerns/exportable_test.rb +25 -0
- data/test/amount/concerns/serializable_test.rb +42 -4
- data/test/amount/display_test.rb +42 -0
- data/test/amount/manifest_test.rb +325 -0
- data/test/amount/registry_test.rb +14 -0
- data/test/integration/postgresql_test.rb +30 -0
- data/test/support/active_record_test_support.rb +20 -0
- metadata +14 -2
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Amount
|
|
4
|
+
# Concern: exporting the registry for a client in another language to
|
|
5
|
+
# mirror. See {Manifest} for what the export holds.
|
|
6
|
+
module Exportable
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend(ClassMethods)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Class-level methods extended onto any class that does `include Exportable`.
|
|
12
|
+
module ClassMethods
|
|
13
|
+
# The registry as a JSON-ready manifest: its types, its default rates,
|
|
14
|
+
# and for each locale in `locales` every translation the core looks up.
|
|
15
|
+
# The `@zarpay/amounts` JavaScript client is built from it.
|
|
16
|
+
#
|
|
17
|
+
# @param locales [Array<Symbol, String>] locales to resolve (needs I18n)
|
|
18
|
+
# @return [Hash{String=>Object}]
|
|
19
|
+
# @raise [ArgumentError] when locales are given without I18n loaded
|
|
20
|
+
# @example
|
|
21
|
+
# Amount.manifest["types"]["USDC"]["decimals"]
|
|
22
|
+
# # => 6
|
|
23
|
+
#
|
|
24
|
+
# @example Serving it from a Rails controller
|
|
25
|
+
# render json: Amount.manifest(locales: I18n.available_locales)
|
|
26
|
+
def manifest(locales: [])
|
|
27
|
+
Manifest.new(registry, locales:).to_h
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -51,9 +51,26 @@ class Amount
|
|
|
51
51
|
}
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
+
# The exact compact string: whole units with exactly `decimals` places.
|
|
55
|
+
# Unlike `to_s`, it ignores the display settings and never rounds, so
|
|
56
|
+
# `Amount.parse` reads it back exactly.
|
|
57
|
+
#
|
|
54
58
|
# @return [String]
|
|
59
|
+
# @example
|
|
60
|
+
# Amount.of_usdc("1.5").as_json # => "USDC|1.500000"
|
|
55
61
|
def as_json(*)
|
|
56
|
-
|
|
62
|
+
whole, fraction = atomic.abs.divmod(10**decimals)
|
|
63
|
+
number = decimals.zero? ? whole.to_s : "#{whole}.#{fraction.to_s.rjust(decimals, '0')}"
|
|
64
|
+
|
|
65
|
+
"#{symbol}|#{'-' if atomic.negative?}#{number}"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Without ActiveSupport, the `json` library writes an object through
|
|
69
|
+
# `to_s`, so `to_json` goes through {#as_json} explicitly.
|
|
70
|
+
#
|
|
71
|
+
# @return [String]
|
|
72
|
+
def to_json(*args)
|
|
73
|
+
as_json.to_json(*args)
|
|
57
74
|
end
|
|
58
75
|
end
|
|
59
76
|
end
|
data/lib/amount/display.rb
CHANGED
|
@@ -6,16 +6,19 @@ class Amount
|
|
|
6
6
|
# Formats amounts for UI output without changing their type.
|
|
7
7
|
class Display
|
|
8
8
|
# Rounds to `decimals` places (negative rounds to tens, hundreds, ...),
|
|
9
|
-
#
|
|
9
|
+
# like `ui`'s `direction:`. The direction is by magnitude: `:floor` rounds
|
|
10
|
+
# toward zero and `:ceil` away from it, so a negative amount rounds like
|
|
11
|
+
# its positive and never displays more than it is. The rounded value is
|
|
12
|
+
# never negative zero.
|
|
10
13
|
# @param value [BigDecimal]
|
|
11
14
|
# @param decimals [Integer]
|
|
12
15
|
# @param direction [Symbol]
|
|
13
16
|
# @return [BigDecimal]
|
|
14
17
|
def self.round(value, decimals, direction)
|
|
15
18
|
factor = BigDecimal(10)**decimals
|
|
16
|
-
scaled = value * factor
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
scaled = value.abs * factor
|
|
20
|
+
magnitude = direction == :ceil ? scaled.ceil : scaled.floor
|
|
21
|
+
(value.negative? ? -magnitude : magnitude) / factor
|
|
19
22
|
end
|
|
20
23
|
|
|
21
24
|
# @param amount [Amount]
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Amount
|
|
4
|
+
# Writes a registry as a manifest: a JSON-ready Hash with everything the
|
|
5
|
+
# core reads at runtime, for a client in another language to mirror (the
|
|
6
|
+
# `@zarpay/amounts` JavaScript client reads it).
|
|
7
|
+
#
|
|
8
|
+
# Types are written with the `Amount.register` option names. Every I18n
|
|
9
|
+
# lookup the core makes is resolved for each requested locale, so a client
|
|
10
|
+
# needs neither I18n nor its key conventions. The fallbacks (a missing label
|
|
11
|
+
# to `display_symbol`, a missing format to the registered template) are left
|
|
12
|
+
# to the client, which applies them as the gem does.
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
15
|
+
# Amount::Manifest.new(Amount.registry, locales: [:de]).to_h["types"]["USD"]["decimals"]
|
|
16
|
+
# # => 2
|
|
17
|
+
class Manifest
|
|
18
|
+
VERSION = 1
|
|
19
|
+
|
|
20
|
+
# @param registry [Amount::Registry]
|
|
21
|
+
# @param locales [Array<Symbol, String>] locales to resolve (needs I18n)
|
|
22
|
+
# @raise [ArgumentError] when locales are given without I18n loaded
|
|
23
|
+
def initialize(registry, locales: [])
|
|
24
|
+
Locale.require_i18n! unless locales.empty?
|
|
25
|
+
|
|
26
|
+
@registry = registry
|
|
27
|
+
@locales = locales
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @return [Hash{String=>Object}]
|
|
31
|
+
def to_h
|
|
32
|
+
{
|
|
33
|
+
"v" => VERSION,
|
|
34
|
+
"types" => @registry.symbols.to_h { |symbol| [symbol.to_s, type(@registry.lookup(symbol))] },
|
|
35
|
+
"rates" => @registry.default_rates.map { |(from, to), rate| rate_row(from, to, rate) },
|
|
36
|
+
"locales" => @locales.to_h { |locale| [locale.to_s, locale_table(locale)] }
|
|
37
|
+
}
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def type(entry)
|
|
43
|
+
{
|
|
44
|
+
"decimals" => entry.decimals,
|
|
45
|
+
"display_symbol" => label(entry.display_symbol),
|
|
46
|
+
"display_format" => entry.display_format,
|
|
47
|
+
"ui_decimals" => entry.ui_decimals,
|
|
48
|
+
"trim_zeros" => entry.trim_zeros ? true : false,
|
|
49
|
+
"display_units" => entry.display_units&.to_h { |unit, spec| [unit.to_s, display_unit(spec)] },
|
|
50
|
+
"default_display" => entry.default_display&.to_s
|
|
51
|
+
}
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def display_unit(spec)
|
|
55
|
+
{
|
|
56
|
+
"scale" => Amount.coerce_decimal(spec[:scale]).to_s("F"),
|
|
57
|
+
"symbol" => label(spec[:symbol]),
|
|
58
|
+
"ui_decimals" => spec[:ui_decimals],
|
|
59
|
+
"format" => spec[:format] || (spec[:position] && Formattable.template_for(spec[:position])),
|
|
60
|
+
"trim_zeros" => unit_trim_zeros(spec)
|
|
61
|
+
}
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# `nil` when the unit doesn't set it, so the client falls back to the type.
|
|
65
|
+
def unit_trim_zeros(spec)
|
|
66
|
+
return unless spec.key?(:trim_zeros)
|
|
67
|
+
|
|
68
|
+
spec[:trim_zeros] ? true : false
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def rate_row(from, to, rate)
|
|
72
|
+
{ "from" => from.to_s, "to" => to.to_s, "rate" => rate.to_s("F") }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def locale_table(locale)
|
|
76
|
+
{
|
|
77
|
+
"number" => Locale.number_format(locale).transform_keys(&:to_s),
|
|
78
|
+
"compact" => compact_table(locale),
|
|
79
|
+
"types" => @registry.symbols.to_h { |symbol| [symbol.to_s, type_table(@registry.lookup(symbol), locale)] }
|
|
80
|
+
}
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def compact_table(locale)
|
|
84
|
+
units = Compact::SCALES.to_h do |_, key, _|
|
|
85
|
+
[key.to_s, label(lookup("#{Compact::LOCALE_UNITS_KEY}.#{key}", locale))]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
{ "format" => Compact.locale_format(locale), "units" => units }
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def type_table(entry, locale)
|
|
92
|
+
downcased = entry.symbol.to_s.downcase
|
|
93
|
+
units = (entry.display_units || {}).to_h do |unit, spec|
|
|
94
|
+
[unit.to_s, unit_table(downcased, unit, spec, locale)]
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
{
|
|
98
|
+
"label" => label(lookup(entry.label_key || "amounts.units.#{downcased}", locale)),
|
|
99
|
+
"format" => template(lookup("amounts.formats.#{downcased}", locale)),
|
|
100
|
+
"units" => units
|
|
101
|
+
}
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def unit_table(downcased, unit, spec, locale)
|
|
105
|
+
{
|
|
106
|
+
"label" => label(lookup(spec[:label_key] || "amounts.display_units.#{downcased}.#{unit}", locale)),
|
|
107
|
+
"format" => template(lookup("amounts.display_unit_formats.#{downcased}.#{unit}", locale))
|
|
108
|
+
}
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def lookup(key, locale)
|
|
112
|
+
::I18n.t(key.to_s, locale:, default: nil)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# A String, or a plural Hash with String keys. Anything else is not a
|
|
116
|
+
# label the core could display, so it is written as `nil`.
|
|
117
|
+
def label(value)
|
|
118
|
+
case value
|
|
119
|
+
when String then value
|
|
120
|
+
when Hash then value.select { |_, form| form.is_a?(String) }.transform_keys(&:to_s)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def template(value)
|
|
125
|
+
value if Formattable.valid_template?(value)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
data/lib/amount/registry.rb
CHANGED
|
@@ -223,6 +223,15 @@ class Amount
|
|
|
223
223
|
@lock.synchronize { @default_rates.key?([from.to_sym, to.to_sym]) }
|
|
224
224
|
end
|
|
225
225
|
|
|
226
|
+
# @return [Hash{Array(Symbol, Symbol)=>BigDecimal}] every registered
|
|
227
|
+
# default rate, keyed by `[from, to]`. A frozen copy.
|
|
228
|
+
# @example
|
|
229
|
+
# Amount.registry.default_rates
|
|
230
|
+
# # => { [:USD, :USDC] => 0.1e1 }
|
|
231
|
+
def default_rates
|
|
232
|
+
@lock.synchronize { @default_rates.dup.freeze }
|
|
233
|
+
end
|
|
234
|
+
|
|
226
235
|
# @return [void]
|
|
227
236
|
# @example Locking the global registry after initialization
|
|
228
237
|
# Amount.registry.lock!
|
data/lib/amount/version.rb
CHANGED
data/lib/amount.rb
CHANGED
|
@@ -14,6 +14,7 @@ require_relative "amount/compact"
|
|
|
14
14
|
require_relative "amount/display"
|
|
15
15
|
require_relative "amount/parser"
|
|
16
16
|
require_relative "amount/ui_parser"
|
|
17
|
+
require_relative "amount/manifest"
|
|
17
18
|
|
|
18
19
|
# Concerns: modules mixed into Amount. Files live in amount/concerns/, but the
|
|
19
20
|
# constants are Amount::<Name> (the folder is not a namespace).
|
|
@@ -28,6 +29,7 @@ require_relative "amount/concerns/orderable"
|
|
|
28
29
|
require_relative "amount/concerns/convertible"
|
|
29
30
|
require_relative "amount/concerns/serializable"
|
|
30
31
|
require_relative "amount/concerns/formattable"
|
|
32
|
+
require_relative "amount/concerns/exportable"
|
|
31
33
|
|
|
32
34
|
# Represents a precise quantity of a registered fungible type.
|
|
33
35
|
#
|
|
@@ -53,6 +55,8 @@ require_relative "amount/concerns/formattable"
|
|
|
53
55
|
# - {Serializable} — `to_h`, `as_json`, `Amount.load`
|
|
54
56
|
# - {Formattable} — `display_format(unit:, locale:)`, where the number and
|
|
55
57
|
# unit go
|
|
58
|
+
# - {Exportable} — `Amount.manifest(locales:)`, the registry for a client
|
|
59
|
+
# in another language
|
|
56
60
|
#
|
|
57
61
|
# New behavior belongs in a concern, not in this class. See
|
|
58
62
|
# `.claude/rules/concerns.md` for the shape a concern takes.
|
|
@@ -78,6 +82,7 @@ class Amount
|
|
|
78
82
|
include Convertible
|
|
79
83
|
include Serializable
|
|
80
84
|
include Formattable
|
|
85
|
+
include Exportable
|
|
81
86
|
|
|
82
87
|
extend Forwardable
|
|
83
88
|
|
|
@@ -151,6 +151,14 @@ class AmountActiveRecordAmountValidatorTest < Minitest::Test
|
|
|
151
151
|
refute greater_than_one_model.new(amount: "SOL|0.5").valid?
|
|
152
152
|
end
|
|
153
153
|
|
|
154
|
+
def test_subclass_accepts_valid_inherited_amount
|
|
155
|
+
assert SubValidatedHolding.new(reserve: "USDC|1.25", amount: "USDC|100.00").valid?
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def test_subclass_rejects_inherited_amount_with_wrong_symbol
|
|
159
|
+
refute SubValidatedHolding.new(reserve: "USDC|1.25", amount: "USD|100.00").valid?
|
|
160
|
+
end
|
|
161
|
+
|
|
154
162
|
private
|
|
155
163
|
|
|
156
164
|
def validated(**attributes)
|
|
@@ -184,8 +184,33 @@ class AmountActiveRecordAttributeDefinitionTest < Minitest::Test
|
|
|
184
184
|
assert_equal 300, grouped_sums["USD"]
|
|
185
185
|
end
|
|
186
186
|
|
|
187
|
+
def test_column_values_of_multi_symbol_amount
|
|
188
|
+
assert_equal({ "amount_atomic" => 1_500_000, "amount_symbol" => "USDC" },
|
|
189
|
+
definition(:amount).column_values(Amount.new("1.5", :USDC)))
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def test_column_values_of_fixed_symbol_amount_omit_symbol_column
|
|
193
|
+
assert_equal({ "fee_atomic" => 250_000_000 }, definition(:fee).column_values(Amount.new("0.25", :SOL)))
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def test_column_values_of_nil_multi_symbol_amount_keep_every_column
|
|
197
|
+
assert_equal({ "amount_atomic" => nil, "amount_symbol" => nil }, definition(:amount).column_values(nil))
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def test_column_values_of_nil_fixed_symbol_amount
|
|
201
|
+
assert_equal({ "fee_atomic" => nil }, definition(:fee).column_values(nil))
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def test_column_values_hold_atomic_as_integer
|
|
205
|
+
assert_kind_of Integer, definition(:amount).column_values(Amount.new(10**30, :SOL, from: :atomic))["amount_atomic"]
|
|
206
|
+
end
|
|
207
|
+
|
|
187
208
|
private
|
|
188
209
|
|
|
210
|
+
def definition(name)
|
|
211
|
+
Holding.amount_attribute_definitions.fetch(name)
|
|
212
|
+
end
|
|
213
|
+
|
|
189
214
|
def amount_written_holding
|
|
190
215
|
Holding.new.tap { |holding| holding.amount = Amount.new("1.5", :USDC) }
|
|
191
216
|
end
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../test_helper"
|
|
4
|
+
require_relative "../../support/active_record_test_support"
|
|
5
|
+
|
|
6
|
+
class AmountActiveRecordCalculationsTest < Minitest::Test
|
|
7
|
+
include ActiveRecordTestSetup
|
|
8
|
+
|
|
9
|
+
# Two USDC holdings with SOL fees, one USD holding without a fee, and one
|
|
10
|
+
# holding with neither. Every holding's reserve is USDC|1.25.
|
|
11
|
+
def setup
|
|
12
|
+
super
|
|
13
|
+
Holding.create!(amount: "USDC|1.5", fee: "SOL|0.1")
|
|
14
|
+
Holding.create!(amount: "USDC|2", fee: "SOL|0.2")
|
|
15
|
+
Holding.create!(amount: "USD|3")
|
|
16
|
+
Holding.create!
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_sum_amount_of_fixed_symbol
|
|
20
|
+
assert_equal Amount.new("0.3", :SOL), Holding.sum_amount(:fee)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_sum_amount_of_fixed_symbol_over_no_rows_is_zero
|
|
24
|
+
assert_equal Amount.new(0, :SOL), Holding.where(id: nil).sum_amount(:fee)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_sum_amount_of_fixed_symbol_on_none_is_zero
|
|
28
|
+
assert_equal Amount.new(0, :SOL), Holding.none.sum_amount(:fee)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_sum_amount_of_multi_symbol_with_one_symbol_in_scope
|
|
32
|
+
assert_equal Amount.new("3.5", :USDC), Holding.amount_in(:USDC).sum_amount(:amount)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_sum_amount_of_multi_symbol_over_no_rows_is_nil
|
|
36
|
+
assert_nil Holding.where(id: nil).sum_amount(:amount)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_sum_amount_with_mixed_symbols_raises_type_mismatch
|
|
40
|
+
assert_raises(Amount::TypeMismatch) { Holding.sum_amount(:amount) }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_sum_amount_with_mixed_symbols_names_the_symbols
|
|
44
|
+
assert_match(/amount has more than one symbol in scope: USD, USDC/,
|
|
45
|
+
error_from(Amount::TypeMismatch) { Holding.sum_amount(:amount) }.message)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def test_sum_amount_grouped_by_symbol_column
|
|
49
|
+
assert_equal({ "USD" => Amount.new("3", :USD), "USDC" => Amount.new("3.5", :USDC) },
|
|
50
|
+
Holding.group(:amount_symbol).sum_amount(:amount))
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_sum_amount_of_fixed_symbol_grouped
|
|
54
|
+
assert_equal({ nil => Amount.new(0, :SOL), "USD" => Amount.new(0, :SOL), "USDC" => Amount.new("0.3", :SOL) },
|
|
55
|
+
Holding.group(:amount_symbol).sum_amount(:fee))
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_sum_amount_grouped_by_two_columns_keys_by_array
|
|
59
|
+
assert_equal({ %w[USDC USD] => Amount.new("3", :USD), %w[USDC USDC] => Amount.new("3.5", :USDC) },
|
|
60
|
+
Holding.group(:reserve_symbol, :amount_symbol).sum_amount(:amount))
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_sum_amount_grouped_by_sql_expression
|
|
64
|
+
assert_equal({ "USD" => Amount.new("3", :USD), "USDC" => Amount.new("3.5", :USDC) },
|
|
65
|
+
Holding.group("UPPER(amount_symbol)").sum_amount(:amount))
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# `pluck` refuses a raw SQL string like this one unless it's wrapped in
|
|
69
|
+
# `Arel.sql`; Rails' `group(...).sum` accepts it.
|
|
70
|
+
def test_sum_amount_grouped_by_raw_sql_expression
|
|
71
|
+
assert_equal({ "none" => Amount.new(0, :SOL), "USD" => Amount.new(0, :SOL), "USDC" => Amount.new("0.3", :SOL) },
|
|
72
|
+
Holding.group("COALESCE(amount_symbol, 'none')").sum_amount(:fee))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def test_sum_amount_grouped_by_arel_node
|
|
76
|
+
assert_equal({ "USD" => Amount.new("3", :USD), "USDC" => Amount.new("3.5", :USDC) },
|
|
77
|
+
Holding.group(Holding.arel_table[:amount_symbol]).sum_amount(:amount))
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_sum_amount_grouped_on_none_is_empty
|
|
81
|
+
assert_empty Holding.none.group(:amount_symbol).sum_amount(:fee)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def test_sum_amount_with_mixed_symbols_in_one_group_raises_type_mismatch
|
|
85
|
+
assert_raises(Amount::TypeMismatch) { Holding.group(:reserve_symbol).sum_amount(:amount) }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def test_sum_amount_grouped_by_association_raises_argument_error
|
|
89
|
+
assert_raises(ArgumentError) { ChildHolding.group(:parent).sum_amount(:fee) }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def test_sum_amount_grouped_by_association_names_its_foreign_key
|
|
93
|
+
assert_match(/group by parent_id, not the parent association/,
|
|
94
|
+
error_from(ArgumentError) { ChildHolding.group(:parent).sum_amount(:fee) }.message)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def test_sum_amount_of_inherited_amount
|
|
98
|
+
assert_equal Amount.new("0.3", :SOL), SubHolding.sum_amount(:fee)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def test_minimum_amount
|
|
102
|
+
assert_equal Amount.new("0.1", :SOL), Holding.minimum_amount(:fee)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def test_minimum_amount_over_no_rows_is_nil
|
|
106
|
+
assert_nil Holding.where(id: nil).minimum_amount(:fee)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def test_minimum_amount_with_mixed_symbols_raises_type_mismatch
|
|
110
|
+
assert_raises(Amount::TypeMismatch) { Holding.minimum_amount(:amount) }
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def test_maximum_amount
|
|
114
|
+
assert_equal Amount.new("0.2", :SOL), Holding.maximum_amount(:fee)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def test_maximum_amount_grouped_by_symbol_column
|
|
118
|
+
assert_equal({ "USD" => Amount.new("3", :USD), "USDC" => Amount.new("2", :USDC) },
|
|
119
|
+
Holding.group(:amount_symbol).maximum_amount(:amount))
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def test_average_amount_counts_only_rows_with_an_amount
|
|
123
|
+
assert_equal Amount.new("0.15", :SOL), Holding.average_amount(:fee)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def test_average_amount_truncates_toward_zero
|
|
127
|
+
holdings_with_atomic_fees(1, 2)
|
|
128
|
+
|
|
129
|
+
assert_equal Amount.new(1, :SOL, from: :atomic), Holding.average_amount(:fee)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def test_average_amount_of_negative_amounts_truncates_toward_zero
|
|
133
|
+
holdings_with_atomic_fees(-1, -2)
|
|
134
|
+
|
|
135
|
+
assert_equal Amount.new(-1, :SOL, from: :atomic), Holding.average_amount(:fee)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def test_average_amount_over_no_rows_is_nil
|
|
139
|
+
assert_nil Holding.where(id: nil).average_amount(:fee)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def test_average_amount_grouped_by_symbol_column
|
|
143
|
+
assert_equal({ "USD" => Amount.new("3", :USD), "USDC" => Amount.new("1.75", :USDC) },
|
|
144
|
+
Holding.group(:amount_symbol).average_amount(:amount))
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def test_average_amount_of_group_without_amounts_is_nil
|
|
148
|
+
assert_nil Holding.group(:amount_symbol).average_amount(:fee).fetch("USD")
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def test_average_amount_with_mixed_symbols_raises_type_mismatch
|
|
152
|
+
assert_raises(Amount::TypeMismatch) { Holding.average_amount(:amount) }
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def test_rails_average_still_works
|
|
156
|
+
assert_equal BigDecimal("150000000"), Holding.average(:fee_atomic)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
private
|
|
160
|
+
|
|
161
|
+
def holdings_with_atomic_fees(*atomics)
|
|
162
|
+
Holding.delete_all
|
|
163
|
+
atomics.each { |atomic| Holding.create!(fee: Amount.new(atomic, :SOL, from: :atomic)) }
|
|
164
|
+
end
|
|
165
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../test_helper"
|
|
4
|
+
require_relative "../../support/active_record_test_support"
|
|
5
|
+
|
|
6
|
+
class AmountActiveRecordDirtyTest < Minitest::Test
|
|
7
|
+
include ActiveRecordTestSetup
|
|
8
|
+
|
|
9
|
+
def setup
|
|
10
|
+
super
|
|
11
|
+
@holding = Holding.create!(amount: "USDC|10.06", fee: "SOL|0.5")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_amount_changes_keys_a_changed_amount_by_name
|
|
15
|
+
@holding.amount = "USD|3"
|
|
16
|
+
|
|
17
|
+
assert_equal({ "amount" => [Amount.new("10.06", :USDC), Amount.new("3", :USD)] }, @holding.amount_changes)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_amount_changes_reports_a_cleared_amount
|
|
21
|
+
@holding.fee = nil
|
|
22
|
+
|
|
23
|
+
assert_equal({ "fee" => [Amount.new("0.5", :SOL), nil] }, @holding.amount_changes)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_amount_changes_keeps_other_attribute_changes
|
|
27
|
+
@holding.parent_id = 7
|
|
28
|
+
|
|
29
|
+
assert_equal({ "parent_id" => [nil, 7] }, @holding.amount_changes)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_amount_changes_of_unchanged_record_is_empty
|
|
33
|
+
assert_empty @holding.amount_changes
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_amount_changes_reads_with_symbol_keys
|
|
37
|
+
@holding.amount = "USD|3"
|
|
38
|
+
|
|
39
|
+
assert_equal [Amount.new("10.06", :USDC), Amount.new("3", :USD)], @holding.amount_changes[:amount]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_saved_amount_changes_keys_a_saved_amount_by_name
|
|
43
|
+
@holding.update!(fee: "SOL|0.75")
|
|
44
|
+
|
|
45
|
+
assert_equal({ "fee" => [Amount.new("0.5", :SOL), Amount.new("0.75", :SOL)] }, @holding.saved_amount_changes)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def test_saved_amount_changes_of_created_record
|
|
49
|
+
assert_equal [nil, Amount.new("10.06", :USDC)], @holding.saved_amount_changes["amount"]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_changes_stay_keyed_by_column
|
|
53
|
+
@holding.amount = "USD|3"
|
|
54
|
+
|
|
55
|
+
assert_equal %w[amount_atomic amount_symbol], @holding.changes.keys
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_saved_changes_stay_keyed_by_column
|
|
59
|
+
@holding.update!(fee: "SOL|0.75")
|
|
60
|
+
|
|
61
|
+
assert_equal %w[fee_atomic], @holding.saved_changes.keys
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def test_saved_amount_changes_of_inherited_amount
|
|
65
|
+
sub = SubHolding.find(@holding.id)
|
|
66
|
+
sub.update!(fee: "SOL|0.75")
|
|
67
|
+
|
|
68
|
+
assert_equal [Amount.new("0.5", :SOL), Amount.new("0.75", :SOL)], sub.saved_amount_changes["fee"]
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -28,6 +28,38 @@ class AmountActiveRecordModelTest < Minitest::Test
|
|
|
28
28
|
assert_includes holding.errors[:amount], "raw numeric assignment requires a fixed symbol"
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
+
def test_subclass_inherits_amount_attribute_definitions
|
|
32
|
+
assert_equal %i[amount fee reserve], SubHolding.amount_attribute_definitions.keys
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_subclass_resolves_inherited_atomic_column
|
|
36
|
+
assert_equal "fee_atomic", SubHolding.amount_atomic_column(:fee)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_subclass_resolves_inherited_component_columns
|
|
40
|
+
assert_equal %w[amount_atomic amount_symbol], SubHolding.amount_component_columns(:amount)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_subclass_adds_its_own_amount
|
|
44
|
+
assert_equal %i[amount fee reserve bonus], BonusHolding.amount_attribute_definitions.keys
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_subclass_amount_does_not_reach_parent
|
|
48
|
+
refute_includes Holding.amount_attribute_definitions.keys, :bonus
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_subclass_amount_does_not_reach_sibling
|
|
52
|
+
refute_includes SubHolding.amount_attribute_definitions.keys, :bonus
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_model_without_amounts_has_no_definitions
|
|
56
|
+
assert_empty ::ActiveRecord::Base.amount_attribute_definitions
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def test_definitions_are_not_readable_from_a_record
|
|
60
|
+
refute_respond_to Holding.new, :amount_attribute_definitions
|
|
61
|
+
end
|
|
62
|
+
|
|
31
63
|
private
|
|
32
64
|
|
|
33
65
|
def wrong_symbol_fee_holding
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../test_helper"
|
|
4
|
+
require_relative "../../support/active_record_test_support"
|
|
5
|
+
|
|
6
|
+
class AmountActiveRecordPersistenceTest < Minitest::Test
|
|
7
|
+
include ActiveRecordTestSetup
|
|
8
|
+
|
|
9
|
+
def test_amount_column_values_replaces_multi_symbol_amount_with_its_columns
|
|
10
|
+
assert_equal({ "amount_atomic" => 1_500_000, "amount_symbol" => "USDC" },
|
|
11
|
+
Holding.amount_column_values("amount" => "USDC|1.5"))
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_amount_column_values_replaces_fixed_symbol_amount_with_its_column
|
|
15
|
+
assert_equal({ "fee_atomic" => 250_000_000 }, Holding.amount_column_values("fee" => "SOL|0.25"))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_amount_column_values_accepts_symbol_keys
|
|
19
|
+
assert_equal({ "fee_atomic" => 250_000_000 }, Holding.amount_column_values(fee: "SOL|0.25"))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_amount_column_values_casts_numeric_for_fixed_symbol
|
|
23
|
+
assert_equal({ "fee_atomic" => 250_000_000 }, Holding.amount_column_values("fee" => 0.25))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_amount_column_values_accepts_an_amount
|
|
27
|
+
assert_equal({ "fee_atomic" => 250_000_000 }, Holding.amount_column_values("fee" => Amount.new("0.25", :SOL)))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_amount_column_values_passes_other_keys_through
|
|
31
|
+
assert_equal({ "fee_atomic" => 250_000_000, :parent_id => 7 },
|
|
32
|
+
Holding.amount_column_values("fee" => 0.25, :parent_id => 7))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_amount_column_values_keeps_every_column_of_nil_amount
|
|
36
|
+
assert_equal({ "amount_atomic" => nil, "amount_symbol" => nil }, Holding.amount_column_values("amount" => nil))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_amount_column_values_raises_for_wrong_fixed_symbol
|
|
40
|
+
assert_raises(Amount::TypeMismatch) { Holding.amount_column_values("fee" => "USDC|1") }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_amount_column_values_of_inherited_amount
|
|
44
|
+
assert_equal({ "fee_atomic" => 250_000_000 }, SubHolding.amount_column_values("fee" => 0.25))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_amount_column_values_rows_insert_with_insert_all
|
|
48
|
+
Holding.insert_all([Holding.amount_column_values("amount" => "USDC|1.5"),
|
|
49
|
+
Holding.amount_column_values("amount" => nil)])
|
|
50
|
+
|
|
51
|
+
assert_equal [Amount.new("1.5", :USDC), nil], Holding.order(:id).map(&:amount)
|
|
52
|
+
end
|
|
53
|
+
end
|