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,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../test_helper"
|
|
4
|
+
require_relative "../../support/active_record_test_support"
|
|
5
|
+
|
|
6
|
+
class AmountActiveRecordMigrationMethodsTest < Minitest::Test
|
|
7
|
+
include ActiveRecordTestSetup
|
|
8
|
+
|
|
9
|
+
def test_creates_atomic_column_for_multi_symbol_amount
|
|
10
|
+
assert columns.key?("amount_atomic")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_creates_symbol_column_for_multi_symbol_amount
|
|
14
|
+
assert columns.key?("amount_symbol")
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_creates_atomic_column_for_fixed_symbol_amount
|
|
18
|
+
assert columns.key?("fee_atomic")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_creates_no_symbol_column_for_fixed_symbol_amount
|
|
22
|
+
refute columns.key?("fee_symbol")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def test_atomic_column_precision_defaults_to_seventy_eight
|
|
26
|
+
assert_equal 78, columns.fetch("amount_atomic").precision
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_symbol_column_limit_is_ten_characters
|
|
30
|
+
assert_equal 10, columns.fetch("amount_symbol").limit
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_atomic_column_uses_given_precision
|
|
34
|
+
assert_equal 40, columns.fetch("reserve_atomic").precision
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_default_sets_symbol_column_default
|
|
38
|
+
assert_equal "USDC", columns.fetch("reserve_symbol").default
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_schema_declares_default_atomic_column_as_scale_zero_decimal
|
|
42
|
+
assert_includes schema_sql, "\"amount_atomic\" decimal(78,0)"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_schema_declares_atomic_column_with_given_precision_as_scale_zero_decimal
|
|
46
|
+
assert_includes schema_sql, "\"reserve_atomic\" decimal(40,0)"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def columns
|
|
52
|
+
Holding.columns_hash
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def schema_sql
|
|
56
|
+
::ActiveRecord::Base.connection.select_value(<<~SQL)
|
|
57
|
+
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'holdings'
|
|
58
|
+
SQL
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../test_helper"
|
|
4
|
+
require_relative "../../support/active_record_test_support"
|
|
5
|
+
|
|
6
|
+
class AmountActiveRecordModelTest < Minitest::Test
|
|
7
|
+
include ActiveRecordTestSetup
|
|
8
|
+
|
|
9
|
+
def test_fixed_symbol_writer_with_wrong_symbol_makes_record_invalid
|
|
10
|
+
refute wrong_symbol_fee_holding.valid?
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_fixed_symbol_writer_with_wrong_symbol_reports_error_on_validation
|
|
14
|
+
holding = wrong_symbol_fee_holding
|
|
15
|
+
holding.valid?
|
|
16
|
+
|
|
17
|
+
assert_includes holding.errors[:fee], "expected SOL, got USDC"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_multi_symbol_writer_with_raw_numeric_makes_record_invalid
|
|
21
|
+
refute raw_numeric_amount_holding.valid?
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_multi_symbol_writer_with_raw_numeric_reports_error_on_validation
|
|
25
|
+
holding = raw_numeric_amount_holding
|
|
26
|
+
holding.valid?
|
|
27
|
+
|
|
28
|
+
assert_includes holding.errors[:amount], "raw numeric assignment requires a fixed symbol"
|
|
29
|
+
end
|
|
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
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def wrong_symbol_fee_holding
|
|
66
|
+
Holding.new.tap { |holding| holding.fee = Amount.new("1", :USDC) }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def raw_numeric_amount_holding
|
|
70
|
+
Holding.new.tap { |holding| holding.amount = 5 }
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -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
|
|
@@ -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
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../test_helper"
|
|
4
|
+
require_relative "../../support/active_record_test_support"
|
|
5
|
+
|
|
6
|
+
class AmountActiveRecordTypeTest < Minitest::Test
|
|
7
|
+
include ActiveRecordTestSetup
|
|
8
|
+
|
|
9
|
+
def test_multi_symbol_writer_parses_string_into_atomic_column
|
|
10
|
+
assert_equal 250, holding_with(amount: "USD|2.50").amount_atomic
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_multi_symbol_writer_parses_string_into_symbol_column
|
|
14
|
+
assert_equal "USD", holding_with(amount: "USD|2.50").amount_symbol
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_multi_symbol_writer_loads_atomic_hash
|
|
18
|
+
holding = holding_with(amount: { "atomic" => 42, "symbol" => "USDC" })
|
|
19
|
+
|
|
20
|
+
assert_equal Amount.new(42, :USDC, from: :atomic), holding.amount
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_fixed_symbol_writer_converts_raw_float_into_atomic_column
|
|
24
|
+
assert_equal 1_250_000_000, holding_with(fee: 1.25).fee_atomic
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_fixed_symbol_writer_reads_back_raw_float_as_amount
|
|
28
|
+
assert_equal Amount.new("1.25", :SOL), holding_with(fee: 1.25).fee
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_large_atomic_value_round_trips_through_sqlite
|
|
32
|
+
skip "SQLite stores DECIMAL(78,0) values above 64-bit range imprecisely under ActiveRecord"
|
|
33
|
+
|
|
34
|
+
huge_atomic = 10**30
|
|
35
|
+
holding = Holding.create!(amount: Amount.new(huge_atomic, :SOL, from: :atomic))
|
|
36
|
+
|
|
37
|
+
assert_equal huge_atomic, holding.reload.amount.atomic
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def test_custom_class_symbol_reads_back_as_registered_class
|
|
41
|
+
assert_instance_of metal_class, metal_record.weight
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_custom_class_symbol_reads_back_atomic_value
|
|
45
|
+
assert_equal 25_000, metal_record.weight.atomic
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def holding_with(**attributes)
|
|
51
|
+
holding = Holding.new
|
|
52
|
+
attributes.each { |name, value| holding.public_send("#{name}=", value) }
|
|
53
|
+
holding
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def metal_class
|
|
57
|
+
@metal_class ||= Class.new(Amount) do
|
|
58
|
+
def self.name
|
|
59
|
+
"MetalAmount"
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def metal_record
|
|
65
|
+
Amount.register :METAL, decimals: 4, class: metal_class
|
|
66
|
+
|
|
67
|
+
::ActiveRecord::Schema.define do
|
|
68
|
+
create_table :metal_holdings, force: true do |t|
|
|
69
|
+
t.amount :weight, symbol: :METAL, null: false
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
klass = Class.new(::ActiveRecord::Base) do
|
|
74
|
+
self.table_name = "metal_holdings"
|
|
75
|
+
has_amount :weight, symbol: :METAL
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
klass.create!(weight: metal_class.new("2.5", :METAL)).reload
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../test_helper"
|
|
4
|
+
require_relative "../../lib/amount/active_record"
|
|
5
|
+
|
|
6
|
+
class AmountActiveRecordTest < Minitest::Test
|
|
7
|
+
def test_locale_file_is_loaded_once
|
|
8
|
+
Amount::ActiveRecord.install_locale!
|
|
9
|
+
|
|
10
|
+
assert_equal 1, I18n.load_path.count(Amount::ActiveRecord::LOCALE_PATH)
|
|
11
|
+
end
|
|
12
|
+
end
|