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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +95 -0
  3. data/README.md +101 -6
  4. data/lib/amount/active_record/attribute_definition.rb +12 -8
  5. data/lib/amount/active_record/calculations.rb +127 -0
  6. data/lib/amount/active_record/dirty.rb +43 -0
  7. data/lib/amount/active_record/model.rb +12 -5
  8. data/lib/amount/active_record/persistence.rb +34 -0
  9. data/lib/amount/active_record/serialization.rb +57 -0
  10. data/lib/amount/active_record.rb +6 -0
  11. data/lib/amount/compact.rb +5 -1
  12. data/lib/amount/concerns/calculable.rb +4 -1
  13. data/lib/amount/concerns/exportable.rb +31 -0
  14. data/lib/amount/concerns/serializable.rb +18 -1
  15. data/lib/amount/display.rb +7 -4
  16. data/lib/amount/manifest.rb +128 -0
  17. data/lib/amount/registry.rb +9 -0
  18. data/lib/amount/version.rb +1 -1
  19. data/lib/amount.rb +5 -0
  20. data/test/amount/active_record/amount_validator_test.rb +8 -0
  21. data/test/amount/active_record/attribute_definition_test.rb +25 -0
  22. data/test/amount/active_record/calculations_test.rb +165 -0
  23. data/test/amount/active_record/dirty_test.rb +70 -0
  24. data/test/amount/active_record/model_test.rb +32 -0
  25. data/test/amount/active_record/persistence_test.rb +53 -0
  26. data/test/amount/active_record/serialization_test.rb +75 -0
  27. data/test/amount/compact_test.rb +22 -7
  28. data/test/amount/concerns/calculable_test.rb +33 -0
  29. data/test/amount/concerns/exportable_test.rb +25 -0
  30. data/test/amount/concerns/serializable_test.rb +42 -4
  31. data/test/amount/display_test.rb +42 -0
  32. data/test/amount/manifest_test.rb +325 -0
  33. data/test/amount/registry_test.rb +14 -0
  34. data/test/integration/postgresql_test.rb +30 -0
  35. data/test/support/active_record_test_support.rb +20 -0
  36. metadata +14 -2
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../test_helper"
4
+ require_relative "../../support/active_record_test_support"
5
+
6
+ class AmountActiveRecordSerializationTest < Minitest::Test
7
+ include ActiveRecordTestSetup
8
+
9
+ def setup
10
+ super
11
+ @holding = Holding.create!(amount: "USDC|10.06", fee: "SOL|1.123456789")
12
+ end
13
+
14
+ def test_serializes_amounts_in_place_of_their_columns
15
+ assert_equal %w[id amount fee reserve bonus_atomic parent_id], @holding.as_json.keys
16
+ end
17
+
18
+ def test_serializes_multi_symbol_amount_as_exact_compact_string
19
+ assert_equal "USDC|10.060000", @holding.as_json["amount"]
20
+ end
21
+
22
+ def test_serializes_fixed_symbol_amount_as_exact_compact_string
23
+ assert_equal "SOL|1.123456789", @holding.as_json["fee"]
24
+ end
25
+
26
+ def test_serializes_nil_amount_as_nil
27
+ assert_nil Holding.create!.as_json["fee"]
28
+ end
29
+
30
+ def test_serializable_hash_holds_the_amount
31
+ assert_equal Amount.new("1.123456789", :SOL), @holding.serializable_hash["fee"]
32
+ end
33
+
34
+ def test_only_takes_an_amount_name
35
+ assert_equal({ "fee" => "SOL|1.123456789" }, @holding.as_json(only: :fee))
36
+ end
37
+
38
+ def test_only_takes_amount_names_with_columns
39
+ assert_equal({ "id" => @holding.id, "amount" => "USDC|10.060000" }, @holding.as_json(only: %i[id amount]))
40
+ end
41
+
42
+ def test_except_takes_an_amount_name
43
+ assert_equal %w[id fee bonus_atomic parent_id], @holding.as_json(except: %i[amount reserve]).keys
44
+ end
45
+
46
+ def test_to_json_writes_amounts
47
+ assert_equal '{"fee":"SOL|1.123456789"}', @holding.to_json(only: :fee)
48
+ end
49
+
50
+ def test_keeps_columns_of_partly_selected_amount
51
+ assert_equal %w[id amount_atomic], Holding.select(:id, :amount_atomic).first.as_json.keys
52
+ end
53
+
54
+ def test_serializes_amounts_of_included_association
55
+ child = ChildHolding.create!(parent: @holding)
56
+
57
+ assert_equal({ "fee" => "SOL|1.123456789" }, child.as_json(only: :id, include: { parent: { only: :fee } })["parent"])
58
+ end
59
+
60
+ def test_serializes_inherited_amount
61
+ assert_equal "SOL|1.123456789", SubHolding.find(@holding.id).as_json["fee"]
62
+ end
63
+
64
+ def test_serializes_amount_a_subclass_adds
65
+ assert_equal "SOL|2.000000000", BonusHolding.create!(bonus: "SOL|2").as_json["bonus"]
66
+ end
67
+
68
+ def test_included_only_into_models_with_amounts
69
+ refute_includes ::ActiveRecord::Base.ancestors, Amount::ActiveRecord::Serialization
70
+ end
71
+
72
+ def test_included_once_per_model
73
+ assert_equal 1, BonusHolding.ancestors.count(Amount::ActiveRecord::Serialization)
74
+ end
75
+ end
@@ -123,22 +123,27 @@ class AmountCompactTest < Minitest::Test
123
123
  assert_equal "1T TOKENS", tokens(999_999_999_999).ui(compact: true, direction: :ceil)
124
124
  end
125
125
 
126
- # floor goes toward negative infinity, as `ui` does for any negative amount
126
+ # Rounding is by magnitude, as `ui` rounds any negative amount: a negative
127
+ # compacts like its positive, with a minus sign.
127
128
 
128
129
  def test_negative_exact_value
129
130
  assert_equal "-1.5M TOKENS", tokens(-1_500_000).ui(compact: true)
130
131
  end
131
132
 
132
- def test_negative_floor_rounds_away_from_zero
133
- assert_equal "-2M TOKENS", tokens(-1_999_999).ui(compact: true)
133
+ def test_negative_floor_rounds_toward_zero
134
+ assert_equal "-1.99M TOKENS", tokens(-1_999_999).ui(compact: true)
134
135
  end
135
136
 
136
- def test_negative_ceil_rounds_toward_zero
137
- assert_equal "-1.99M TOKENS", tokens(-1_999_999).ui(compact: true, direction: :ceil)
137
+ def test_negative_ceil_rounds_away_from_zero
138
+ assert_equal "-2M TOKENS", tokens(-1_999_999).ui(compact: true, direction: :ceil)
138
139
  end
139
140
 
140
- def test_negative_floor_rounds_into_the_next_scale
141
- assert_equal "-1M TOKENS", tokens(-999_999).ui(compact: true)
141
+ def test_negative_floor_stays_in_its_scale
142
+ assert_equal "-999K TOKENS", tokens(-999_999).ui(compact: true)
143
+ end
144
+
145
+ def test_negative_ceil_rounds_into_the_next_scale
146
+ assert_equal "-1M TOKENS", tokens(-999_999).ui(compact: true, direction: :ceil)
142
147
  end
143
148
 
144
149
  def test_negative_below_one_thousand_is_formatted_normally
@@ -336,6 +341,16 @@ class AmountCompactTest < Minitest::Test
336
341
  assert_equal "1,5 Tsd. TOKENS", tokens(1_500).ui(compact: true, locale: :de)
337
342
  end
338
343
 
344
+ def test_locale_format_reads_the_locale_template
345
+ store_decimal_units(:de, format: "%n%u", units: {})
346
+
347
+ assert_equal "%n%u", Amount::Compact.locale_format(:de)
348
+ end
349
+
350
+ def test_locale_format_defaults_without_a_template
351
+ assert_equal "%n %u", Amount::Compact.locale_format(:de)
352
+ end
353
+
339
354
  def test_compact_works_without_i18n
340
355
  without_i18n { assert_equal "50M TOKENS", tokens(50_000_000).ui(compact: true) }
341
356
  end
@@ -121,6 +121,39 @@ class AmountCalculableTest < Minitest::Test
121
121
  assert_raises(ZeroDivisionError) { Amount.new(1, :USDC) / 0 }
122
122
  end
123
123
 
124
+ def test_division_by_integer
125
+ assert_equal Amount.new("5", :USDC), Amount.new("10", :USDC) / 2
126
+ end
127
+
128
+ def test_division_by_integer_truncates_toward_zero
129
+ assert_equal Amount.new(1, :USDC, from: :atomic), Amount.new(3, :USDC, from: :atomic) / 2
130
+ end
131
+
132
+ def test_division_of_negative_amount_truncates_toward_zero
133
+ assert_equal Amount.new(-1, :USDC, from: :atomic), Amount.new(-3, :USDC, from: :atomic) / 2
134
+ end
135
+
136
+ def test_division_by_decimal
137
+ assert_equal Amount.new("4", :USDC), Amount.new("10", :USDC) / BigDecimal("2.5")
138
+ end
139
+
140
+ def test_division_of_large_amount_by_integer_is_exact
141
+ assert_equal Amount.new((10**38) - 1, :SOL, from: :atomic),
142
+ Amount.new((3 * (10**38)) - 1, :SOL, from: :atomic) / 3
143
+ end
144
+
145
+ # BigDecimal division rounded this quotient, which came out 1429 atomic
146
+ # units off.
147
+ def test_division_of_large_amount_by_small_decimal_is_exact
148
+ assert_equal Amount.new(1_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571, :SOL, from: :atomic),
149
+ Amount.new(10**40, :SOL, from: :atomic) / BigDecimal("0.000000000000000000007")
150
+ end
151
+
152
+ def test_division_of_large_negative_amount_by_small_decimal_is_exact
153
+ assert_equal Amount.new(-1_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571_428_571, :SOL, from: :atomic),
154
+ Amount.new(-(10**40), :SOL, from: :atomic) / BigDecimal("0.000000000000000000007")
155
+ end
156
+
124
157
  def test_cross_type_addition_via_explicit_conversion
125
158
  gold = Amount.new("0.0001", :GOLD)
126
159
  usdc = Amount.new("100", :USDC)
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../test_helper"
4
+ require_relative "../../support/locale_test_setup"
5
+
6
+ class AmountExportableTest < Minitest::Test
7
+ include LocaleTestSetup
8
+
9
+ def test_manifest_exports_the_global_registry
10
+ assert_equal Amount::Manifest.new(Amount.registry).to_h, Amount.manifest
11
+ end
12
+
13
+ def test_manifest_resolves_the_given_locales
14
+ assert_equal Amount::Manifest.new(Amount.registry, locales: [:de]).to_h, Amount.manifest(locales: [:de])
15
+ end
16
+
17
+ def test_manifest_follows_a_swapped_registry
18
+ exported = Amount.with_registry(Amount::Registry.new) do
19
+ Amount.register :TEST, decimals: 2
20
+ Amount.manifest
21
+ end
22
+
23
+ assert_equal %w[TEST], exported["types"].keys
24
+ end
25
+ end
@@ -53,18 +53,56 @@ class AmountSerializableTest < Minitest::Test
53
53
  assert_match(/missing key/, error_from(Amount::InvalidInput) { Amount.load({}) }.message)
54
54
  end
55
55
 
56
- def test_as_json_returns_compact_string
57
- assert_equal "USDC|1.50", Amount.new("1.5", :USDC).as_json
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
58
  end
59
59
 
60
60
  def test_to_json_returns_quoted_compact_string
61
- assert_equal '"USDC|1.50"', Amount.new("1.5", :USDC).to_json
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) }
62
100
  end
63
101
 
64
102
  def test_as_json_works_when_nested_in_hash
65
103
  hash = { amount: Amount.new("1.5", :USDC) }
66
104
 
67
- assert_equal({ "amount" => "USDC|1.50" }, JSON.parse(hash.to_json))
105
+ assert_equal({ "amount" => "USDC|1.500000" }, JSON.parse(hash.to_json))
68
106
  end
69
107
 
70
108
  def test_load_rejects_unknown_serialization_version
@@ -39,6 +39,48 @@ class AmountDisplayTest < Minitest::Test
39
39
  assert_equal "$1.57", Amount.new("1.561", :USDC).ui(direction: :ceil)
40
40
  end
41
41
 
42
+ # Negatives round by magnitude: like their positive, with a minus sign.
43
+
44
+ def test_ui_negative_floor_rounds_toward_zero
45
+ assert_equal "$-1.56", Amount.new("-1.567", :USDC).ui
46
+ end
47
+
48
+ def test_ui_negative_ceil_rounds_away_from_zero
49
+ assert_equal "$-1.57", Amount.new("-1.561", :USDC).ui(direction: :ceil)
50
+ end
51
+
52
+ def test_ui_negative_rounding_to_zero_has_no_sign
53
+ assert_equal "$0.00", Amount.new("-0.001", :USDC).ui
54
+ end
55
+
56
+ def test_ui_negative_ceil_shows_the_smallest_unit
57
+ assert_equal "$-0.01", Amount.new("-0.001", :USDC).ui(direction: :ceil)
58
+ end
59
+
60
+ def test_ui_negative_display_unit_rounds_toward_zero
61
+ assert_equal "-31.10 g", Amount.new("-1", :GOLD).ui(unit: :gram)
62
+ end
63
+
64
+ def test_to_s_negative_rounds_toward_zero
65
+ assert_equal "USDC|-1.56", Amount.new("-1.567", :USDC).to_s
66
+ end
67
+
68
+ def test_to_s_negative_rounding_to_zero_has_no_sign
69
+ assert_equal "USDC|0.00", Amount.new("-0.001", :USDC).to_s
70
+ end
71
+
72
+ def test_round_negative_floor_toward_zero
73
+ assert_equal BigDecimal("-1.56"), Amount::Display.round(BigDecimal("-1.567"), 2, :floor)
74
+ end
75
+
76
+ def test_round_negative_ceil_away_from_zero
77
+ assert_equal BigDecimal("-1.57"), Amount::Display.round(BigDecimal("-1.561"), 2, :ceil)
78
+ end
79
+
80
+ def test_round_negative_to_tens_toward_zero
81
+ assert_equal BigDecimal("-120"), Amount::Display.round(BigDecimal("-129"), -1, :floor)
82
+ end
83
+
42
84
  # name => [expected, value, symbol]
43
85
  {
44
86
  "usdc" => ["1.50", "1.5", :USDC],
@@ -0,0 +1,325 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require_relative "../test_helper"
5
+ require_relative "../support/locale_test_setup"
6
+
7
+ class AmountManifestTest < Minitest::Test
8
+ include LocaleTestSetup
9
+
10
+ # Types
11
+
12
+ def test_version_is_one
13
+ assert_equal 1, manifest["v"]
14
+ end
15
+
16
+ def test_types_are_keyed_by_symbol
17
+ assert_equal %w[USDC USD SOL GOLD LOGS], manifest["types"].keys
18
+ end
19
+
20
+ def test_type_decimals
21
+ assert_equal 6, type("USDC")["decimals"]
22
+ end
23
+
24
+ def test_type_display_symbol
25
+ assert_equal "$", type("USDC")["display_symbol"]
26
+ end
27
+
28
+ def test_type_plural_display_symbol_has_string_keys
29
+ Amount.register :SEARCHES, decimals: 0, display_symbol: { one: "search", other: "searches" }
30
+
31
+ assert_equal({ "one" => "search", "other" => "searches" }, type("SEARCHES")["display_symbol"])
32
+ end
33
+
34
+ def test_type_display_format_is_the_resolved_template
35
+ assert_equal "%u%n", type("USDC")["display_format"]
36
+ end
37
+
38
+ def test_type_display_format_written_as_given
39
+ Amount.register :EUR, decimals: 2, display_symbol: "€", display_format: "%n %u"
40
+
41
+ assert_equal "%n %u", type("EUR")["display_format"]
42
+ end
43
+
44
+ def test_type_ui_decimals
45
+ assert_equal 2, type("USDC")["ui_decimals"]
46
+ end
47
+
48
+ def test_type_trim_zeros_defaults_to_false
49
+ assert_equal false, type("USDC")["trim_zeros"]
50
+ end
51
+
52
+ def test_type_trim_zeros_when_set
53
+ Amount.register :TOKENS, decimals: 0, trim_zeros: true
54
+
55
+ assert_equal true, type("TOKENS")["trim_zeros"]
56
+ end
57
+
58
+ def test_type_without_display_units
59
+ assert_nil type("USDC")["display_units"]
60
+ end
61
+
62
+ def test_type_default_display
63
+ assert_equal "oz_t", type("GOLD")["default_display"]
64
+ end
65
+
66
+ def test_type_without_default_display
67
+ assert_nil type("USDC")["default_display"]
68
+ end
69
+
70
+ # Display units
71
+
72
+ def test_display_units_are_keyed_by_unit
73
+ assert_equal %w[oz_t gram kg], type("GOLD")["display_units"].keys
74
+ end
75
+
76
+ def test_display_unit_scale_is_an_exact_string
77
+ assert_equal "31.1035", unit("GOLD", "gram")["scale"]
78
+ end
79
+
80
+ def test_display_unit_integer_scale
81
+ assert_equal "1.0", unit("GOLD", "oz_t")["scale"]
82
+ end
83
+
84
+ def test_display_unit_rational_scale_is_written_as_the_gem_uses_it
85
+ register_unit_type(third: { scale: Rational(1, 3) })
86
+
87
+ assert_equal "0.3333333333333333333", unit("UNITS", "third")["scale"]
88
+ end
89
+
90
+ def test_display_unit_symbol
91
+ assert_equal "g", unit("GOLD", "gram")["symbol"]
92
+ end
93
+
94
+ def test_display_unit_without_symbol
95
+ register_unit_type(plain: { scale: 1 })
96
+
97
+ assert_nil unit("UNITS", "plain")["symbol"]
98
+ end
99
+
100
+ def test_display_unit_ui_decimals
101
+ assert_equal 2, unit("GOLD", "gram")["ui_decimals"]
102
+ end
103
+
104
+ def test_display_unit_without_ui_decimals
105
+ register_unit_type(plain: { scale: 1 })
106
+
107
+ assert_nil unit("UNITS", "plain")["ui_decimals"]
108
+ end
109
+
110
+ def test_display_unit_format
111
+ register_unit_type(dozen: { scale: "0.083", format: "%n doz" })
112
+
113
+ assert_equal "%n doz", unit("UNITS", "dozen")["format"]
114
+ end
115
+
116
+ def test_display_unit_position_is_written_as_its_template
117
+ register_unit_type(dozen: { scale: "0.083", position: :prefix })
118
+
119
+ assert_equal "%u%n", unit("UNITS", "dozen")["format"]
120
+ end
121
+
122
+ def test_display_unit_without_format_or_position
123
+ assert_nil unit("GOLD", "gram")["format"]
124
+ end
125
+
126
+ def test_display_unit_trim_zeros_when_set
127
+ register_unit_type(plain: { scale: 1, trim_zeros: true })
128
+
129
+ assert_equal true, unit("UNITS", "plain")["trim_zeros"]
130
+ end
131
+
132
+ def test_display_unit_trim_zeros_set_to_false
133
+ register_unit_type(plain: { scale: 1, trim_zeros: false })
134
+
135
+ assert_equal false, unit("UNITS", "plain")["trim_zeros"]
136
+ end
137
+
138
+ def test_display_unit_without_trim_zeros
139
+ assert_nil unit("GOLD", "gram")["trim_zeros"]
140
+ end
141
+
142
+ # Rates
143
+
144
+ def test_without_rates
145
+ assert_empty manifest["rates"]
146
+ end
147
+
148
+ def test_rate_row
149
+ Amount.register_default_rate :USD, :USDC, "1"
150
+
151
+ assert_equal [{ "from" => "USD", "to" => "USDC", "rate" => "1.0" }], manifest["rates"]
152
+ end
153
+
154
+ def test_rational_rate_is_written_as_the_gem_uses_it
155
+ Amount.register_default_rate :USDC, :USD, Rational(1, 3)
156
+
157
+ assert_equal "0.3333333333333333333", manifest["rates"].first["rate"]
158
+ end
159
+
160
+ # Locales
161
+
162
+ def test_without_locales
163
+ assert_empty manifest["locales"]
164
+ end
165
+
166
+ def test_locales_are_keyed_by_locale
167
+ assert_equal %w[en de], manifest(locales: %i[en de])["locales"].keys
168
+ end
169
+
170
+ def test_locales_need_i18n
171
+ without_i18n do
172
+ assert_raises(ArgumentError) { manifest(locales: [:de]) }
173
+ end
174
+ end
175
+
176
+ def test_number_format
177
+ assert_equal({ "delimiter" => ".", "separator" => "," }, locale_table(:de)["number"])
178
+ end
179
+
180
+ def test_number_format_defaults
181
+ assert_equal({ "delimiter" => ",", "separator" => "." }, locale_table(:xx)["number"])
182
+ end
183
+
184
+ def test_compact_format_defaults
185
+ assert_equal "%n %u", locale_table(:de)["compact"]["format"]
186
+ end
187
+
188
+ def test_compact_format_translated
189
+ store(:de, number: { human: { decimal_units: { format: "%n%u" } } })
190
+
191
+ assert_equal "%n%u", locale_table(:de)["compact"]["format"]
192
+ end
193
+
194
+ def test_compact_format_without_number_falls_back
195
+ store(:de, number: { human: { decimal_units: { format: "%u" } } })
196
+
197
+ assert_equal "%n %u", locale_table(:de)["compact"]["format"]
198
+ end
199
+
200
+ def test_compact_units_list_every_scale
201
+ assert_equal %w[thousand million billion trillion quadrillion quintillion sextillion septillion
202
+ octillion nonillion decillion], locale_table(:de)["compact"]["units"].keys
203
+ end
204
+
205
+ def test_compact_unit_translated
206
+ store(:de, number: { human: { decimal_units: { units: { thousand: "Tsd." } } } })
207
+
208
+ assert_equal "Tsd.", locale_table(:de)["compact"]["units"]["thousand"]
209
+ end
210
+
211
+ def test_compact_unit_plural_forms
212
+ store(:de, number: { human: { decimal_units: { units: { million: { one: "Million", other: "Millionen" } } } } })
213
+
214
+ assert_equal({ "one" => "Million", "other" => "Millionen" }, locale_table(:de)["compact"]["units"]["million"])
215
+ end
216
+
217
+ def test_compact_unit_without_translation
218
+ assert_nil locale_table(:de)["compact"]["units"]["thousand"]
219
+ end
220
+
221
+ def test_type_label_without_translation
222
+ assert_nil locale_type(:de, "GOLD")["label"]
223
+ end
224
+
225
+ def test_type_label_translated
226
+ store(:de, amounts: { units: { gold: "Feinunze" } })
227
+
228
+ assert_equal "Feinunze", locale_type(:de, "GOLD")["label"]
229
+ end
230
+
231
+ def test_type_label_from_label_key
232
+ store(:de, coins: { searches: "Suchen" })
233
+ Amount.register :SEARCHES, decimals: 0, label_key: "coins.searches"
234
+
235
+ assert_equal "Suchen", locale_type(:de, "SEARCHES")["label"]
236
+ end
237
+
238
+ def test_type_label_plural_forms
239
+ store(:de, amounts: { units: { logs: { one: "Stamm", other: "Stämme" } } })
240
+
241
+ assert_equal({ "one" => "Stamm", "other" => "Stämme" }, locale_type(:de, "LOGS")["label"])
242
+ end
243
+
244
+ def test_type_format_translated
245
+ store(:de, amounts: { formats: { usd: "%n %u" } })
246
+
247
+ assert_equal "%n %u", locale_type(:de, "USD")["format"]
248
+ end
249
+
250
+ def test_type_format_without_number_is_ignored
251
+ store(:de, amounts: { formats: { usd: "%u" } })
252
+
253
+ assert_nil locale_type(:de, "USD")["format"]
254
+ end
255
+
256
+ def test_type_without_display_units_has_no_unit_tables
257
+ assert_empty locale_type(:de, "USD")["units"]
258
+ end
259
+
260
+ def test_unit_label_translated
261
+ store(:de, amounts: { display_units: { gold: { gram: "Gramm" } } })
262
+
263
+ assert_equal "Gramm", locale_type(:de, "GOLD")["units"]["gram"]["label"]
264
+ end
265
+
266
+ def test_unit_label_from_label_key
267
+ store(:de, metric: { gram: "Gramm" })
268
+ register_unit_type(gram: { scale: 1, label_key: "metric.gram" })
269
+
270
+ assert_equal "Gramm", locale_type(:de, "UNITS")["units"]["gram"]["label"]
271
+ end
272
+
273
+ def test_unit_label_without_translation
274
+ assert_nil locale_type(:de, "GOLD")["units"]["gram"]["label"]
275
+ end
276
+
277
+ def test_unit_format_translated
278
+ store(:de, amounts: { display_unit_formats: { gold: { gram: "%n %u" } } })
279
+
280
+ assert_equal "%n %u", locale_type(:de, "GOLD")["units"]["gram"]["format"]
281
+ end
282
+
283
+ def test_unit_format_without_translation
284
+ assert_nil locale_type(:de, "GOLD")["units"]["gram"]["format"]
285
+ end
286
+
287
+ # JSON
288
+
289
+ def test_survives_a_json_round_trip
290
+ Amount.register_default_rate :USD, :USDC, "1"
291
+ written = manifest(locales: [:de])
292
+
293
+ assert_equal written, JSON.parse(written.to_json)
294
+ end
295
+
296
+ private
297
+
298
+ def manifest(locales: [])
299
+ Amount::Manifest.new(Amount.registry, locales:).to_h
300
+ end
301
+
302
+ def type(symbol)
303
+ manifest["types"][symbol]
304
+ end
305
+
306
+ def unit(symbol, unit)
307
+ type(symbol)["display_units"][unit]
308
+ end
309
+
310
+ def locale_table(locale)
311
+ manifest(locales: [locale])["locales"][locale.to_s]
312
+ end
313
+
314
+ def locale_type(locale, symbol)
315
+ locale_table(locale)["types"][symbol]
316
+ end
317
+
318
+ def store(locale, translations)
319
+ I18n.backend.store_translations(locale, translations)
320
+ end
321
+
322
+ def register_unit_type(units)
323
+ Amount.register :UNITS, decimals: 2, display_units: units
324
+ end
325
+ end
@@ -79,6 +79,20 @@ class AmountRegistryTest < Minitest::Test
79
79
  assert_equal BigDecimal("0.5"), Amount.new("1", :USDC).to(:USD).decimal
80
80
  end
81
81
 
82
+ def test_default_rates_lists_each_rate_by_direction
83
+ Amount.register_default_rate :USD, :USDC, "1"
84
+
85
+ assert_equal({ %i[USD USDC] => BigDecimal("1") }, Amount.registry.default_rates)
86
+ end
87
+
88
+ def test_default_rates_is_empty_without_rates
89
+ assert_empty Amount.registry.default_rates
90
+ end
91
+
92
+ def test_default_rates_is_frozen
93
+ assert_predicate Amount.registry.default_rates, :frozen?
94
+ end
95
+
82
96
  def test_prefix_display_position_is_stored_as_its_template
83
97
  assert_equal "%u%n", Amount.registry.lookup(:USD).display_format
84
98
  end
@@ -69,8 +69,38 @@ class PostgreSQLIntegrationTest < Minitest::Test
69
69
  assert_equal({ "USDC" => 1_500_000, "USD" => 300 }, Holding.group(:amount_symbol).sum(:amount_atomic))
70
70
  end
71
71
 
72
+ def test_postgresql_sum_amount_is_exact_past_64_bits
73
+ holdings_with_atomic_amounts(10**30, (10**30) + 1)
74
+
75
+ assert_equal (2 * (10**30)) + 1, Holding.sum_amount(:amount).atomic
76
+ end
77
+
78
+ def test_postgresql_average_amount_is_exact_past_64_bits
79
+ holdings_with_atomic_amounts(10**30, (10**30) + 3)
80
+
81
+ assert_equal (10**30) + 1, Holding.average_amount(:amount).atomic
82
+ end
83
+
84
+ def test_postgresql_sum_amount_of_fixed_symbol
85
+ Holding.create!(fee: "SOL|0.1")
86
+ Holding.create!(fee: "SOL|0.2")
87
+
88
+ assert_equal Amount.new("0.3", :SOL), Holding.sum_amount(:fee)
89
+ end
90
+
91
+ def test_postgresql_sum_amount_grouped_by_symbol_column
92
+ create_usdc_and_usd_holdings
93
+
94
+ assert_equal({ "USDC" => Amount.new("1.5", :USDC), "USD" => Amount.new("3", :USD) },
95
+ Holding.group(:amount_symbol).sum_amount(:amount))
96
+ end
97
+
72
98
  private
73
99
 
100
+ def holdings_with_atomic_amounts(*atomics)
101
+ atomics.each { |atomic| Holding.create!(amount: Amount.new(atomic, :SOL, from: :atomic)) }
102
+ end
103
+
74
104
  # The USDC holding; a USD holding is created alongside.
75
105
  def create_usdc_and_usd_holdings
76
106
  usdc = Holding.create!(amount: "USDC|1.50")