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.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +37 -0
  3. data/CHANGELOG.md +180 -0
  4. data/Gemfile +4 -0
  5. data/README.md +274 -2
  6. data/Rakefile +47 -10
  7. data/lib/amount/active_record/amount_validator.rb +9 -18
  8. data/lib/amount/active_record/attribute_definition.rb +14 -8
  9. data/lib/amount/active_record/calculations.rb +127 -0
  10. data/lib/amount/active_record/dirty.rb +43 -0
  11. data/lib/amount/active_record/locale/en.yml +7 -0
  12. data/lib/amount/active_record/model.rb +19 -3
  13. data/lib/amount/active_record/persistence.rb +34 -0
  14. data/lib/amount/active_record/serialization.rb +57 -0
  15. data/lib/amount/active_record.rb +18 -0
  16. data/lib/amount/compact.rb +137 -0
  17. data/lib/amount/{allocation.rb → concerns/allocatable.rb} +1 -1
  18. data/lib/amount/{arithmetic.rb → concerns/calculable.rb} +5 -2
  19. data/lib/amount/concerns/coercible.rb +65 -0
  20. data/lib/amount/concerns/constructible.rb +92 -0
  21. data/lib/amount/{conversion.rb → concerns/convertible.rb} +1 -1
  22. data/lib/amount/concerns/formattable.rb +112 -0
  23. data/lib/amount/concerns/localizable.rb +39 -0
  24. data/lib/amount/{comparison.rb → concerns/orderable.rb} +2 -2
  25. data/lib/amount/concerns/parsable.rb +58 -0
  26. data/lib/amount/concerns/registrable.rb +80 -0
  27. data/lib/amount/{serialization.rb → concerns/serializable.rb} +21 -4
  28. data/lib/amount/display.rb +81 -46
  29. data/lib/amount/locale.rb +102 -0
  30. data/lib/amount/registry.rb +69 -7
  31. data/lib/amount/ui_parser.rb +109 -0
  32. data/lib/amount/version.rb +1 -1
  33. data/lib/amount.rb +55 -199
  34. data/test/amount/active_record/amount_validator_test.rb +209 -0
  35. data/test/amount/active_record/attribute_definition_test.rb +259 -0
  36. data/test/amount/active_record/calculations_test.rb +165 -0
  37. data/test/amount/active_record/dirty_test.rb +70 -0
  38. data/test/amount/active_record/migration_methods_test.rb +60 -0
  39. data/test/amount/active_record/model_test.rb +72 -0
  40. data/test/amount/active_record/persistence_test.rb +53 -0
  41. data/test/amount/active_record/serialization_test.rb +75 -0
  42. data/test/amount/active_record/type_test.rb +80 -0
  43. data/test/amount/active_record_test.rb +12 -0
  44. data/test/amount/compact_test.rb +371 -0
  45. data/test/amount/concerns/allocatable_test.rb +166 -0
  46. data/test/amount/concerns/calculable_test.rb +185 -0
  47. data/test/amount/concerns/coercible_test.rb +49 -0
  48. data/test/amount/concerns/constructible_test.rb +113 -0
  49. data/test/amount/concerns/convertible_test.rb +54 -0
  50. data/test/amount/concerns/formattable_test.rb +336 -0
  51. data/test/amount/concerns/localizable_test.rb +59 -0
  52. data/test/amount/concerns/orderable_test.rb +83 -0
  53. data/test/amount/concerns/parsable_test.rb +98 -0
  54. data/test/amount/concerns/registrable_test.rb +67 -0
  55. data/test/amount/concerns/serializable_test.rb +136 -0
  56. data/test/amount/display_test.rb +181 -0
  57. data/test/amount/locale_test.rb +590 -0
  58. data/test/amount/parser_test.rb +29 -0
  59. data/test/amount/registry/generated_constructors_test.rb +94 -0
  60. data/test/amount/registry_test.rb +149 -0
  61. data/test/amount/ui_parser_test.rb +284 -0
  62. data/test/amount_test.rb +55 -0
  63. data/test/dummy/config/application.rb +3 -0
  64. data/test/generators/amount/active_record/registry_generator_test.rb +116 -0
  65. data/test/integration/postgresql_test.rb +124 -0
  66. data/test/support/active_record_test_support.rb +85 -0
  67. data/test/support/error_helpers.rb +22 -0
  68. data/test/support/locale_test_setup.rb +36 -0
  69. data/test/test_helper.rb +1 -0
  70. metadata +55 -11
  71. data/test/postgresql_integration_test.rb +0 -71
  72. data/test/test_active_record.rb +0 -375
  73. data/test/test_amount.rb +0 -675
  74. data/test/test_registry_generator.rb +0 -104
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../test_helper"
4
+ require_relative "../../support/active_record_test_support"
5
+
6
+ class AmountActiveRecordAmountValidatorTest < Minitest::Test
7
+ include ActiveRecordTestSetup
8
+
9
+ def test_accepts_valid_multi_symbol_amount
10
+ assert validated(amount: "USDC|100.00").valid?
11
+ end
12
+
13
+ def test_rejects_wrong_symbol
14
+ refute validated(amount: "USD|100.00").valid?
15
+ end
16
+
17
+ def test_reports_wrong_symbol
18
+ assert_includes errors_on(validated(amount: "USD|100.00"), :amount), "must have symbol USDC"
19
+ end
20
+
21
+ def test_rejects_amount_below_lower_bound
22
+ refute validated(amount: "USDC|0.00").valid?
23
+ end
24
+
25
+ def test_reports_amount_below_lower_bound
26
+ assert_includes errors_on(validated(amount: "USDC|0.00"), :amount), "must be greater than USDC|0.00"
27
+ end
28
+
29
+ def test_rejects_amount_above_upper_bound
30
+ refute validated(amount: "USDC|1000.01").valid?
31
+ end
32
+
33
+ def test_reports_amount_above_upper_bound
34
+ assert_includes errors_on(validated(amount: "USDC|1000.01"), :amount), "must be less than or equal to USDC|1000.00"
35
+ end
36
+
37
+ def test_allows_nil_when_allow_nil_is_set
38
+ assert validated(amount: "USDC|100.00", fee: nil).valid?
39
+ end
40
+
41
+ def test_supports_fixed_symbol_numeric_thresholds
42
+ assert validated(amount: "USDC|100.00", fee: 0.5).valid?
43
+ end
44
+
45
+ def test_rejects_fixed_symbol_amount_below_lower_bound
46
+ refute validated(amount: "USDC|100.00", fee: 0).valid?
47
+ end
48
+
49
+ def test_reports_fixed_symbol_amount_below_lower_bound
50
+ assert_includes errors_on(validated(amount: "USDC|100.00", fee: 0), :fee), "must be greater than SOL|0.0000"
51
+ end
52
+
53
+ def test_rejects_fixed_symbol_amount_at_exclusive_upper_bound
54
+ refute validated(amount: "USDC|100.00", fee: 10).valid?
55
+ end
56
+
57
+ def test_reports_fixed_symbol_amount_at_exclusive_upper_bound
58
+ assert_includes errors_on(validated(amount: "USDC|100.00", fee: 10), :fee), "must be less than SOL|10.0000"
59
+ end
60
+
61
+ def test_rejects_cross_type_threshold_without_rate
62
+ refute cross_type_threshold_model.new(amount: "USDC|2.00").valid?
63
+ end
64
+
65
+ def test_reports_cross_type_threshold_without_rate
66
+ holding = cross_type_threshold_model.new(amount: "USDC|2.00")
67
+
68
+ assert_includes errors_on(holding, :amount), "cannot compare USDC to USD for greater_than"
69
+ end
70
+
71
+ def test_adds_symbol_error_by_key_with_interpolations
72
+ holding = validated(amount: "USD|100.00")
73
+ holding.valid?
74
+
75
+ assert_includes holding.errors.details[:amount], { error: :amount_symbol, expected: :USDC }
76
+ end
77
+
78
+ def test_adds_comparison_error_by_key_with_interpolations
79
+ holding = validated(amount: "USDC|0.00")
80
+ holding.valid?
81
+
82
+ assert_includes holding.errors.details[:amount], { error: :greater_than, count: "USDC|0.00" }
83
+ end
84
+
85
+ def test_incomparable_error_details
86
+ holding = cross_type_threshold_model.new(amount: "USDC|2.00")
87
+ holding.valid?
88
+
89
+ assert_includes holding.errors.details[:amount],
90
+ { error: :amount_incomparable, symbol: :USDC, other_symbol: :USD, option: :greater_than }
91
+ end
92
+
93
+ def test_symbol_message_can_be_translated_by_the_app
94
+ messages = with_german_messages { errors_on(validated(amount: "USD|100.00"), :amount) }
95
+
96
+ assert_includes messages, "muss das Symbol USDC haben"
97
+ end
98
+
99
+ def test_comparison_message_can_be_translated_by_the_app
100
+ messages = with_german_messages { errors_on(validated(amount: "USDC|0.00"), :amount) }
101
+
102
+ assert_includes messages, "muss groesser als USDC|0.00 sein"
103
+ end
104
+
105
+ def test_uses_registered_rate_for_cross_type_threshold_when_available
106
+ Amount.register_default_rate :USD, :USDC, "1"
107
+
108
+ assert cross_type_threshold_model.new(amount: "USDC|2.00").valid?
109
+ end
110
+
111
+ def test_numeric_thresholds_on_multi_symbol_attribute_accept_value_inside_range
112
+ assert bounded_numeric_threshold_model.new(amount: "USDC|50.00").valid?
113
+ end
114
+
115
+ def test_numeric_thresholds_on_multi_symbol_attribute_reject_value_at_exclusive_lower_bound
116
+ refute bounded_numeric_threshold_model.new(amount: "USDC|0.00").valid?
117
+ end
118
+
119
+ def test_numeric_thresholds_on_multi_symbol_attribute_report_lower_bound
120
+ holding = bounded_numeric_threshold_model.new(amount: "USDC|0.00")
121
+
122
+ assert_includes errors_on(holding, :amount).join, "greater than"
123
+ end
124
+
125
+ def test_numeric_thresholds_on_multi_symbol_attribute_reject_value_above_upper_bound
126
+ refute bounded_numeric_threshold_model.new(amount: "USDC|150.00").valid?
127
+ end
128
+
129
+ def test_numeric_thresholds_on_multi_symbol_attribute_report_upper_bound
130
+ holding = bounded_numeric_threshold_model.new(amount: "USDC|150.00")
131
+
132
+ assert_includes errors_on(holding, :amount).join, "less than"
133
+ end
134
+
135
+ # The same `greater_than: 1` is interpreted as 1 USDC for a USDC value and
136
+ # as 1 SOL for a SOL value, because the threshold inherits the value's
137
+ # symbol on multi-symbol attributes.
138
+ def test_numeric_threshold_accepts_usdc_value_above_one_usdc
139
+ assert greater_than_one_model.new(amount: "USDC|2.00").valid?
140
+ end
141
+
142
+ def test_numeric_threshold_rejects_usdc_value_below_one_usdc
143
+ refute greater_than_one_model.new(amount: "USDC|0.50").valid?
144
+ end
145
+
146
+ def test_numeric_threshold_accepts_sol_value_above_one_sol
147
+ assert greater_than_one_model.new(amount: "SOL|2").valid?
148
+ end
149
+
150
+ def test_numeric_threshold_rejects_sol_value_below_one_sol
151
+ refute greater_than_one_model.new(amount: "SOL|0.5").valid?
152
+ end
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
+
162
+ private
163
+
164
+ def validated(**attributes)
165
+ ValidatedHolding.new(reserve: "USDC|1.25", **attributes)
166
+ end
167
+
168
+ def errors_on(record, attribute)
169
+ record.valid?
170
+ record.errors[attribute]
171
+ end
172
+
173
+ def holdings_model_validating_amount(**options)
174
+ Class.new(::ActiveRecord::Base) do
175
+ self.table_name = "holdings"
176
+
177
+ has_amount :amount
178
+ validates :amount, amount: options
179
+ end
180
+ end
181
+
182
+ def cross_type_threshold_model
183
+ holdings_model_validating_amount(greater_than: "USD|1.00")
184
+ end
185
+
186
+ def bounded_numeric_threshold_model
187
+ holdings_model_validating_amount(greater_than: 0, less_than_or_equal_to: 100)
188
+ end
189
+
190
+ def greater_than_one_model
191
+ holdings_model_validating_amount(greater_than: 1)
192
+ end
193
+
194
+ # Runs the block under :de with app-provided translations of the
195
+ # validator's error keys.
196
+ def with_german_messages(&)
197
+ original_locales = I18n.available_locales
198
+ I18n.available_locales = original_locales | [:de]
199
+ # rubocop:disable Style/FormatStringToken
200
+ I18n.backend.store_translations(:de, errors: { messages: {
201
+ amount_symbol: "muss das Symbol %{expected} haben",
202
+ greater_than: "muss groesser als %{count} sein"
203
+ } })
204
+ # rubocop:enable Style/FormatStringToken
205
+ I18n.with_locale(:de, &)
206
+ ensure
207
+ I18n.available_locales = original_locales
208
+ end
209
+ end
@@ -0,0 +1,259 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../test_helper"
4
+ require_relative "../../support/active_record_test_support"
5
+
6
+ class AmountActiveRecordAttributeDefinitionTest < Minitest::Test
7
+ include ActiveRecordTestSetup
8
+
9
+ def test_reader_returns_amount
10
+ holding = Holding.create!(amount_atomic: 1_500_000, amount_symbol: "USDC")
11
+
12
+ assert_equal Amount.new("1.5", :USDC), holding.amount
13
+ end
14
+
15
+ def test_multi_symbol_reader_returns_nil_when_atomic_is_nil
16
+ assert_nil Holding.create!.amount
17
+ end
18
+
19
+ def test_fixed_symbol_reader_returns_nil_when_atomic_is_nil
20
+ assert_nil Holding.create!.fee
21
+ end
22
+
23
+ def test_multi_symbol_writer_stores_amount_atomic
24
+ assert_equal 1_500_000, amount_written_holding.amount_atomic
25
+ end
26
+
27
+ def test_multi_symbol_writer_stores_amount_symbol
28
+ assert_equal "USDC", amount_written_holding.amount_symbol
29
+ end
30
+
31
+ # Rails 8.1 cuts a String cast to an integer column to 16 bytes, so the
32
+ # writer must hand Rails the Integer, never its string form.
33
+ def test_writer_keeps_atomic_values_longer_than_sixteen_digits
34
+ holding = Holding.new(amount: Amount.new(10**30, :SOL, from: :atomic))
35
+
36
+ assert_equal 10**30, holding.amount_atomic
37
+ end
38
+
39
+ def test_fixed_symbol_writer_keeps_atomic_values_longer_than_sixteen_digits
40
+ holding = Holding.new(fee: Amount.new(10**30, :SOL, from: :atomic))
41
+
42
+ assert_equal 10**30, holding.fee_atomic
43
+ end
44
+
45
+ def test_fixed_symbol_writer_stores_amount_atomic
46
+ holding = Holding.new
47
+ holding.fee = Amount.new("2.5", :SOL)
48
+
49
+ assert_equal 2_500_000_000, holding.fee_atomic
50
+ end
51
+
52
+ def test_nil_assignment_clears_atomic_column
53
+ assert_nil nil_assigned_holding.amount_atomic
54
+ end
55
+
56
+ def test_nil_assignment_clears_symbol_column
57
+ assert_nil nil_assigned_holding.amount_symbol
58
+ end
59
+
60
+ def test_nil_assignment_reads_back_nil
61
+ assert_nil nil_assigned_holding.amount
62
+ end
63
+
64
+ def test_where_scope_matches_atomic_and_symbol
65
+ usdc = create_same_value_in_two_symbols
66
+
67
+ assert_equal [usdc.id], Holding.where_amount("USDC|1.5").pluck(:id)
68
+ end
69
+
70
+ def test_in_scope_matches_symbol
71
+ usdc = create_same_value_in_two_symbols
72
+
73
+ assert_equal [usdc.id], Holding.amount_in(:USDC).pluck(:id)
74
+ end
75
+
76
+ def test_gt_scope_filters_multi_symbol_amounts_by_symbol_and_atomic_value
77
+ _low, mid, high = create_usdc_ladder
78
+
79
+ assert_equal [mid.id, high.id], Holding.where_amount_gt("USDC|1.50").pluck(:id)
80
+ end
81
+
82
+ def test_gte_scope_filters_multi_symbol_amounts_by_symbol_and_atomic_value
83
+ _low, mid, high = create_usdc_ladder
84
+
85
+ assert_equal [mid.id, high.id], Holding.where_amount_gte("USDC|2.00").pluck(:id)
86
+ end
87
+
88
+ def test_lt_scope_filters_multi_symbol_amounts_by_symbol_and_atomic_value
89
+ low, mid, _high = create_usdc_ladder
90
+
91
+ assert_equal [low.id, mid.id], Holding.where_amount_lt("USDC|2.50").pluck(:id)
92
+ end
93
+
94
+ def test_lte_scope_filters_multi_symbol_amounts_by_symbol_and_atomic_value
95
+ low, mid, _high = create_usdc_ladder
96
+
97
+ assert_equal [low.id, mid.id], Holding.where_amount_lte("USDC|2.00").pluck(:id)
98
+ end
99
+
100
+ def test_between_scope_includes_both_bounds_for_multi_symbol_amounts
101
+ low, mid, high = create_usdc_ladder(other: "USD|2.00")
102
+
103
+ assert_equal [low.id, mid.id, high.id], Holding.where_amount_between("USDC|1.00", "USDC|3.00").pluck(:id)
104
+ end
105
+
106
+ def test_between_scope_filters_multi_symbol_amounts_inside_bounds
107
+ _low, mid, _high = create_usdc_ladder(other: "USD|2.00")
108
+
109
+ assert_equal [mid.id], Holding.where_amount_between("USDC|1.50", "USDC|2.50").pluck(:id)
110
+ end
111
+
112
+ def test_between_scope_requires_matching_symbols_for_multi_symbol_amounts
113
+ assert_raises(Amount::TypeMismatch) do
114
+ Holding.where_amount_between("USDC|1.00", "USD|2.00").load
115
+ end
116
+ end
117
+
118
+ def test_gt_scope_supports_fixed_symbol_numeric_values
119
+ _low, mid, high = create_fee_ladder
120
+
121
+ assert_equal [mid.id, high.id], Holding.where_fee_gt(0.25).pluck(:id)
122
+ end
123
+
124
+ def test_gte_scope_supports_fixed_symbol_numeric_values
125
+ _low, mid, high = create_fee_ladder
126
+
127
+ assert_equal [mid.id, high.id], Holding.where_fee_gte(0.50).pluck(:id)
128
+ end
129
+
130
+ def test_lt_scope_supports_fixed_symbol_numeric_values
131
+ low, mid, _high = create_fee_ladder
132
+
133
+ assert_equal [low.id, mid.id], Holding.where_fee_lt(0.75).pluck(:id)
134
+ end
135
+
136
+ def test_lte_scope_supports_fixed_symbol_numeric_values
137
+ low, mid, _high = create_fee_ladder
138
+
139
+ assert_equal [low.id, mid.id], Holding.where_fee_lte(0.50).pluck(:id)
140
+ end
141
+
142
+ def test_between_scope_supports_fixed_symbol_numeric_values
143
+ _low, mid, _high = create_fee_ladder
144
+
145
+ assert_equal [mid.id], Holding.where_fee_between(0.30, 0.60).pluck(:id)
146
+ end
147
+
148
+ def test_changed_predicate_uses_virtual_amount_attribute
149
+ assert changed_holding.amount_changed?
150
+ end
151
+
152
+ def test_change_uses_virtual_amount_attribute
153
+ assert_equal [Amount.new("1.0", :USDC), Amount.new("2.5", :USDC)], changed_holding.amount_change
154
+ end
155
+
156
+ def test_saved_change_predicate_uses_virtual_amount_attribute
157
+ assert saved_holding.saved_change_to_amount?
158
+ end
159
+
160
+ def test_before_last_save_uses_virtual_amount_attribute
161
+ assert_equal Amount.new("1.0", :USDC), saved_holding.amount_before_last_save
162
+ end
163
+
164
+ def test_saved_change_uses_virtual_amount_attribute
165
+ assert_equal [Amount.new("1.0", :USDC), Amount.new("2.5", :USDC)], saved_holding.saved_change_to_amount
166
+ end
167
+
168
+ def test_validation_rejects_atomic_without_symbol
169
+ refute Holding.new(amount_atomic: 100).valid?
170
+ end
171
+
172
+ def test_validation_requires_both_amount_columns_or_neither
173
+ holding = Holding.new(amount_atomic: 100)
174
+ holding.valid?
175
+
176
+ assert_includes holding.errors[:amount], "must set both atomic and symbol or neither"
177
+ end
178
+
179
+ def test_group_sum_totals_first_symbol_atomic_column
180
+ assert_equal 3_500_000, grouped_sums["USDC"]
181
+ end
182
+
183
+ def test_group_sum_totals_second_symbol_atomic_column
184
+ assert_equal 300, grouped_sums["USD"]
185
+ end
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
+
208
+ private
209
+
210
+ def definition(name)
211
+ Holding.amount_attribute_definitions.fetch(name)
212
+ end
213
+
214
+ def amount_written_holding
215
+ Holding.new.tap { |holding| holding.amount = Amount.new("1.5", :USDC) }
216
+ end
217
+
218
+ def nil_assigned_holding
219
+ holding = Holding.create!(amount: Amount.new("1", :USDC))
220
+ holding.amount = nil
221
+ holding
222
+ end
223
+
224
+ # The USDC record; a USD record with the same value is created alongside.
225
+ def create_same_value_in_two_symbols
226
+ usdc = Holding.create!(amount: Amount.new("1.5", :USDC))
227
+ Holding.create!(amount: Amount.new("1.5", :USD))
228
+ usdc
229
+ end
230
+
231
+ # USDC 1.00, 2.00 and 3.00 (returned), plus one record in another symbol.
232
+ def create_usdc_ladder(other: "USD|2.50")
233
+ ladder = ["USDC|1.00", "USDC|2.00", "USDC|3.00"].map { |amount| Holding.create!(amount:) }
234
+ Holding.create!(amount: other)
235
+ ladder
236
+ end
237
+
238
+ def create_fee_ladder
239
+ [0.25, 0.50, 0.75].map { |fee| Holding.create!(fee:) }
240
+ end
241
+
242
+ def changed_holding
243
+ holding = Holding.create!(amount: Amount.new("1.0", :USDC))
244
+ holding.amount = "USDC|2.5"
245
+ holding
246
+ end
247
+
248
+ def saved_holding
249
+ changed_holding.tap(&:save!)
250
+ end
251
+
252
+ def grouped_sums
253
+ Holding.create!(amount: Amount.new("1.5", :USDC))
254
+ Holding.create!(amount: Amount.new("2.0", :USDC))
255
+ Holding.create!(amount: Amount.new("3.0", :USD))
256
+
257
+ Holding.group(:amount_symbol).sum(:amount_atomic)
258
+ end
259
+ 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