amounts 0.0.8 → 0.2.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/.rubocop.yml +37 -0
- data/CHANGELOG.md +180 -0
- data/Gemfile +4 -0
- data/README.md +274 -2
- data/Rakefile +47 -10
- data/lib/amount/active_record/amount_validator.rb +9 -18
- data/lib/amount/active_record/attribute_definition.rb +14 -8
- data/lib/amount/active_record/calculations.rb +127 -0
- data/lib/amount/active_record/dirty.rb +43 -0
- data/lib/amount/active_record/locale/en.yml +7 -0
- data/lib/amount/active_record/model.rb +19 -3
- data/lib/amount/active_record/persistence.rb +34 -0
- data/lib/amount/active_record/serialization.rb +57 -0
- data/lib/amount/active_record.rb +18 -0
- data/lib/amount/compact.rb +137 -0
- data/lib/amount/{allocation.rb → concerns/allocatable.rb} +1 -1
- data/lib/amount/{arithmetic.rb → concerns/calculable.rb} +5 -2
- data/lib/amount/concerns/coercible.rb +65 -0
- data/lib/amount/concerns/constructible.rb +92 -0
- data/lib/amount/{conversion.rb → concerns/convertible.rb} +1 -1
- data/lib/amount/concerns/formattable.rb +112 -0
- data/lib/amount/concerns/localizable.rb +39 -0
- data/lib/amount/{comparison.rb → concerns/orderable.rb} +2 -2
- data/lib/amount/concerns/parsable.rb +58 -0
- data/lib/amount/concerns/registrable.rb +80 -0
- data/lib/amount/{serialization.rb → concerns/serializable.rb} +21 -4
- data/lib/amount/display.rb +81 -46
- data/lib/amount/locale.rb +102 -0
- data/lib/amount/registry.rb +69 -7
- data/lib/amount/ui_parser.rb +109 -0
- data/lib/amount/version.rb +1 -1
- data/lib/amount.rb +55 -199
- data/test/amount/active_record/amount_validator_test.rb +209 -0
- data/test/amount/active_record/attribute_definition_test.rb +259 -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/migration_methods_test.rb +60 -0
- data/test/amount/active_record/model_test.rb +72 -0
- data/test/amount/active_record/persistence_test.rb +53 -0
- data/test/amount/active_record/serialization_test.rb +75 -0
- data/test/amount/active_record/type_test.rb +80 -0
- data/test/amount/active_record_test.rb +12 -0
- data/test/amount/compact_test.rb +371 -0
- data/test/amount/concerns/allocatable_test.rb +166 -0
- data/test/amount/concerns/calculable_test.rb +185 -0
- data/test/amount/concerns/coercible_test.rb +49 -0
- data/test/amount/concerns/constructible_test.rb +113 -0
- data/test/amount/concerns/convertible_test.rb +54 -0
- data/test/amount/concerns/formattable_test.rb +336 -0
- data/test/amount/concerns/localizable_test.rb +59 -0
- data/test/amount/concerns/orderable_test.rb +83 -0
- data/test/amount/concerns/parsable_test.rb +98 -0
- data/test/amount/concerns/registrable_test.rb +67 -0
- data/test/amount/concerns/serializable_test.rb +136 -0
- data/test/amount/display_test.rb +181 -0
- data/test/amount/locale_test.rb +590 -0
- data/test/amount/parser_test.rb +29 -0
- data/test/amount/registry/generated_constructors_test.rb +94 -0
- data/test/amount/registry_test.rb +149 -0
- data/test/amount/ui_parser_test.rb +284 -0
- data/test/amount_test.rb +55 -0
- data/test/dummy/config/application.rb +3 -0
- data/test/generators/amount/active_record/registry_generator_test.rb +116 -0
- data/test/integration/postgresql_test.rb +124 -0
- data/test/support/active_record_test_support.rb +85 -0
- data/test/support/error_helpers.rb +22 -0
- data/test/support/locale_test_setup.rb +36 -0
- data/test/test_helper.rb +1 -0
- metadata +55 -11
- data/test/postgresql_integration_test.rb +0 -71
- data/test/test_active_record.rb +0 -375
- data/test/test_amount.rb +0 -675
- data/test/test_registry_generator.rb +0 -104
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "../../test_helper"
|
|
5
|
+
|
|
6
|
+
class AmountSerializableTest < Minitest::Test
|
|
7
|
+
class GoldAmount < Amount
|
|
8
|
+
def purity_estimate
|
|
9
|
+
"24k"
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def setup
|
|
14
|
+
AmountTestSupport.register_default_types!
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def teardown
|
|
18
|
+
Amount.registry.clear!
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_to_h_and_load_round_trip_for_large_negative_amount
|
|
22
|
+
amount = Amount.new(-(10**30), :USDC, from: :atomic)
|
|
23
|
+
|
|
24
|
+
assert_equal amount, Amount.load(amount.to_h)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_to_h_returns_versioned_string_safe_payload
|
|
28
|
+
assert_equal(
|
|
29
|
+
{ v: 1, atomic: "1500000", symbol: "USDC" },
|
|
30
|
+
Amount.new("1.5", :USDC).to_h
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def test_load_accepts_legacy_unversioned_payload
|
|
35
|
+
amount = Amount.load(atomic: 1_500_000, symbol: :USDC)
|
|
36
|
+
|
|
37
|
+
assert_equal Amount.new("1.5", :USDC), amount
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def test_load_without_atomic_raises_invalid_input
|
|
41
|
+
assert_raises(Amount::InvalidInput) { Amount.load(v: 1, symbol: "USDC") }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_load_without_atomic_names_the_missing_key
|
|
45
|
+
assert_match(/missing key: atomic/, error_from(Amount::InvalidInput) { Amount.load(v: 1, symbol: "USDC") }.message)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def test_load_empty_payload_raises_invalid_input
|
|
49
|
+
assert_raises(Amount::InvalidInput) { Amount.load({}) }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_load_empty_payload_reports_missing_key
|
|
53
|
+
assert_match(/missing key/, error_from(Amount::InvalidInput) { Amount.load({}) }.message)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def test_as_json_returns_compact_string_with_all_decimals
|
|
57
|
+
assert_equal "USDC|1.500000", Amount.new("1.5", :USDC).as_json
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def test_to_json_returns_quoted_compact_string
|
|
61
|
+
assert_equal '"USDC|1.500000"', Amount.new("1.5", :USDC).to_json
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def test_as_json_keeps_digits_past_ui_decimals
|
|
65
|
+
assert_equal "SOL|1.123456789", Amount.new("1.123456789", :SOL).as_json
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def test_as_json_pads_whole_amount_to_all_decimals
|
|
69
|
+
assert_equal "SOL|42.000000000", Amount.new("42", :SOL).as_json
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def test_as_json_writes_zero_without_sign
|
|
73
|
+
assert_equal "SOL|0.000000000", Amount.new("-0", :SOL).as_json
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def test_as_json_writes_negative_amount
|
|
77
|
+
assert_equal "SOL|-1.500000000", Amount.new("-1.5", :SOL).as_json
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_as_json_writes_negative_smallest_unit
|
|
81
|
+
assert_equal "SOL|-0.000000001", Amount.new(-1, :SOL, from: :atomic).as_json
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def test_as_json_writes_large_amount_exactly
|
|
85
|
+
assert_equal "SOL|123456789012345678901234567890.123456789",
|
|
86
|
+
Amount.new("123456789012345678901234567890.123456789", :SOL).as_json
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def test_as_json_of_whole_symbol_has_no_decimal_point
|
|
90
|
+
Amount.register :TOKENS, decimals: 0
|
|
91
|
+
|
|
92
|
+
assert_equal "TOKENS|42", Amount.new(42, :TOKENS).as_json
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def test_as_json_round_trips_through_parse
|
|
96
|
+
amounts = ["1.123456789", "-1.5", "0", "0.000000001", "98765432109876543210.987654321"]
|
|
97
|
+
.map { |value| Amount.new(value, :SOL) }
|
|
98
|
+
|
|
99
|
+
assert_equal amounts, amounts.map { |amount| Amount.parse(amount.as_json) }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def test_as_json_works_when_nested_in_hash
|
|
103
|
+
hash = { amount: Amount.new("1.5", :USDC) }
|
|
104
|
+
|
|
105
|
+
assert_equal({ "amount" => "USDC|1.500000" }, JSON.parse(hash.to_json))
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def test_load_rejects_unknown_serialization_version
|
|
109
|
+
assert_raises(Amount::InvalidInput) do
|
|
110
|
+
Amount.load(v: 2, atomic: "1500000", symbol: "USDC")
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def test_load_dispatches_to_registered_subclass
|
|
115
|
+
assert_instance_of GoldAmount, restored_gold
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def test_load_restores_subclass_behavior
|
|
119
|
+
assert_equal "24k", restored_gold.purity_estimate
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
private
|
|
123
|
+
|
|
124
|
+
def restored_gold
|
|
125
|
+
register_gold_subclass!
|
|
126
|
+
Amount.load(GoldAmount.new("1", :GOLD).to_h)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Replaces the default registry with GOLD bound to GoldAmount.
|
|
130
|
+
def register_gold_subclass!
|
|
131
|
+
Amount.registry.clear!
|
|
132
|
+
Amount.register :GOLD, decimals: 8, display_symbol: "oz t",
|
|
133
|
+
display_position: :suffix, ui_decimals: 4,
|
|
134
|
+
class: GoldAmount
|
|
135
|
+
end
|
|
136
|
+
end
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bigdecimal"
|
|
4
|
+
require_relative "../test_helper"
|
|
5
|
+
|
|
6
|
+
class AmountDisplayTest < Minitest::Test
|
|
7
|
+
def setup
|
|
8
|
+
AmountTestSupport.register_default_types!
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def teardown
|
|
12
|
+
Amount.registry.clear!
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# name => [expected, value, symbol]
|
|
16
|
+
{
|
|
17
|
+
"usdc" => ["1.500000", "1.5", :USDC],
|
|
18
|
+
"sol" => ["1.500000000", "1.5", :SOL],
|
|
19
|
+
"logs" => ["1", 1, :LOGS]
|
|
20
|
+
}.each do |name, (expected, value, symbol)|
|
|
21
|
+
define_method("test_formatted_respects_storage_decimals_for_#{name}") do
|
|
22
|
+
assert_equal expected, Amount.new(value, symbol).formatted
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# name => [expected, value, symbol]
|
|
27
|
+
{
|
|
28
|
+
"usdc" => ["$1.50", "1.5", :USDC],
|
|
29
|
+
"usdc_rounded_down" => ["$1.56", "1.567", :USDC],
|
|
30
|
+
"sol" => ["1.5000 SOL", "1.5", :SOL],
|
|
31
|
+
"logs" => ["1 logs", 1, :LOGS]
|
|
32
|
+
}.each do |name, (expected, value, symbol)|
|
|
33
|
+
define_method("test_ui_respects_ui_decimals_and_position_for_#{name}") do
|
|
34
|
+
assert_equal expected, Amount.new(value, symbol).ui
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_ui_ceil_direction
|
|
39
|
+
assert_equal "$1.57", Amount.new("1.561", :USDC).ui(direction: :ceil)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# name => [expected, value, symbol]
|
|
43
|
+
{
|
|
44
|
+
"usdc" => ["1.50", "1.5", :USDC],
|
|
45
|
+
"sol" => ["1.5000", "1.5", :SOL],
|
|
46
|
+
"logs" => ["1", 1, :LOGS]
|
|
47
|
+
}.each do |name, (expected, value, symbol)|
|
|
48
|
+
define_method("test_ui_decorated_false_omits_the_display_symbol_for_#{name}") do
|
|
49
|
+
assert_equal expected, Amount.new(value, symbol).ui(decorated: false)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_ui_decorated_false_with_ceil_direction
|
|
54
|
+
assert_equal "1.57", Amount.new("1.561", :USDC).ui(direction: :ceil, decorated: false)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# name => [expected, ui options]
|
|
58
|
+
{
|
|
59
|
+
"default_unit" => ["1.5000 oz t", {}],
|
|
60
|
+
"gram" => ["46.65 g", { unit: :gram }],
|
|
61
|
+
"kg" => ["0.04665 kg", { unit: :kg }]
|
|
62
|
+
}.each do |name, (expected, options)|
|
|
63
|
+
define_method("test_display_units_scale_output_in_#{name}") do
|
|
64
|
+
assert_equal expected, Amount.new("1.5", :GOLD).ui(**options)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# name => [expected, ui options]
|
|
69
|
+
{
|
|
70
|
+
"default_unit" => ["1.5000", { decorated: false }],
|
|
71
|
+
"gram" => ["46.65", { unit: :gram, decorated: false }],
|
|
72
|
+
"kg" => ["0.04665", { unit: :kg, decorated: false }]
|
|
73
|
+
}.each do |name, (expected, options)|
|
|
74
|
+
define_method("test_display_units_with_decorated_false_in_#{name}") do
|
|
75
|
+
assert_equal expected, Amount.new("1.5", :GOLD).ui(**options)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def test_in_unit_returns_raw_bigdecimal
|
|
80
|
+
gold = Amount.new("1.0", :GOLD)
|
|
81
|
+
|
|
82
|
+
assert_in_delta 31.1035, gold.in_unit(:gram).to_f, 0.0001
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def test_unknown_display_unit_raises
|
|
86
|
+
assert_raises(Amount::Registry::InvalidDisplayUnit) do
|
|
87
|
+
Amount.new("1", :GOLD).ui(unit: :pound)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# name => [expected, value, from]
|
|
92
|
+
{
|
|
93
|
+
"fraction" => ["2.5 TZ", "2.5", nil],
|
|
94
|
+
"whole" => ["1 TZ", "1.0", nil],
|
|
95
|
+
"zero" => ["0 TZ", "0.0", nil],
|
|
96
|
+
"no_trailing_zeros" => ["1.123 TZ", "1.123", nil],
|
|
97
|
+
"small_atomic" => ["0.00005 TZ", 50_000, :atomic]
|
|
98
|
+
}.each do |name, (expected, value, from)|
|
|
99
|
+
define_method("test_trim_zeros_strips_trailing_zeros_for_#{name}") do
|
|
100
|
+
register_trim_zeros_type!
|
|
101
|
+
amount = from ? Amount.new(value, :TZ, from:) : Amount.new(value, :TZ)
|
|
102
|
+
|
|
103
|
+
assert_equal expected, amount.ui
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# name => [expected, value]
|
|
108
|
+
{
|
|
109
|
+
"fraction" => ["$1.50", "1.5"],
|
|
110
|
+
"whole" => ["$1.00", "1.0"]
|
|
111
|
+
}.each do |name, (expected, value)|
|
|
112
|
+
define_method("test_trim_zeros_false_preserves_trailing_zeros_for_#{name}") do
|
|
113
|
+
assert_equal expected, Amount.new(value, :USDC).ui
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# name => [expected, value]
|
|
118
|
+
{
|
|
119
|
+
"fraction" => ["2.5", "2.5"],
|
|
120
|
+
"whole" => ["1", "1.0"]
|
|
121
|
+
}.each do |name, (expected, value)|
|
|
122
|
+
define_method("test_trim_zeros_with_decorated_false_for_#{name}") do
|
|
123
|
+
register_trim_zeros_type!
|
|
124
|
+
|
|
125
|
+
assert_equal expected, Amount.new(value, :TZ).ui(decorated: false)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def test_trim_zeros_with_display_units
|
|
130
|
+
Amount.register :TZG, decimals: 8, display_symbol: "oz t", display_position: :suffix,
|
|
131
|
+
trim_zeros: true,
|
|
132
|
+
display_units: { gram: { scale: "31.1035", symbol: "g", ui_decimals: 4 } }
|
|
133
|
+
|
|
134
|
+
assert_equal "31.1035 g", Amount.new("1.0", :TZG).ui(unit: :gram)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def test_trim_zeros_display_unit_override
|
|
138
|
+
Amount.register :TZO, decimals: 6, display_symbol: "T", display_position: :suffix,
|
|
139
|
+
trim_zeros: true,
|
|
140
|
+
display_units: { fixed: { scale: 1, symbol: "F", ui_decimals: 2, trim_zeros: false } }
|
|
141
|
+
|
|
142
|
+
assert_equal "1.50 F", Amount.new("1.5", :TZO).ui(unit: :fixed)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def test_trim_zeros_call_site_true_strips_zeros
|
|
146
|
+
assert_equal "1.5 SOL", Amount.new("1.5", :SOL).ui(trim_zeros: true)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def test_trim_zeros_call_site_false_keeps_zeros
|
|
150
|
+
assert_equal "1.0000 SOL", Amount.new("1.0", :SOL).ui(trim_zeros: false)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def test_trim_zeros_call_site_overrides_registry
|
|
154
|
+
Amount.register :TZR, decimals: 6, display_symbol: "T", display_position: :suffix, trim_zeros: true
|
|
155
|
+
|
|
156
|
+
assert_equal "1.500000 T", Amount.new("1.5", :TZR).ui(trim_zeros: false)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def test_in_unit_with_rational_scale
|
|
160
|
+
assert_equal BigDecimal("0.5"), rational_scale_metal.in_unit(:half)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def test_ui_with_rational_scale
|
|
164
|
+
assert_match(/\A0\.50/, rational_scale_metal.ui(unit: :half))
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
private
|
|
168
|
+
|
|
169
|
+
def register_trim_zeros_type!
|
|
170
|
+
Amount.register :TZ, decimals: 9, display_symbol: "TZ", display_position: :suffix, trim_zeros: true
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def rational_scale_metal
|
|
174
|
+
Amount.registry.clear!
|
|
175
|
+
Amount.register :METAL, decimals: 4, display_symbol: "x", display_position: :suffix,
|
|
176
|
+
ui_decimals: 2,
|
|
177
|
+
display_units: { half: { scale: Rational(1, 2), symbol: "h", ui_decimals: 2 } },
|
|
178
|
+
default_display: :half
|
|
179
|
+
Amount.new("1", :METAL)
|
|
180
|
+
end
|
|
181
|
+
end
|