diff --git a/.dialyzer_ignore.exs b/.dialyzer_ignore.exs new file mode 100644 index 0000000..b131230 --- /dev/null +++ b/.dialyzer_ignore.exs @@ -0,0 +1,3 @@ +[ + {"lib/ex_double_entry/money_proxy.ex"} +] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 896748c..2a1aed2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,24 +6,46 @@ jobs: strategy: matrix: include: - - elixir: 1.12.3 - otp: 24.0 - env: test - - elixir: 1.12.3 - otp: 24.0 - env: test_mysql - - elixir: 1.12.3 + + - elixir: 1.13.3 + otp: 24.2 + env: test_money + - elixir: 1.13.3 + otp: 24.2 + env: test_mysql_money + - elixir: 1.13.3 + otp: 24.2 + env: test_ex_money + - elixir: 1.13.3 + otp: 24.2 + env: test_mysql_ex_money + + - elixir: 1.13.3 otp: 23.3 - env: test - - elixir: 1.12.3 + env: test_money + - elixir: 1.13.3 otp: 23.3 - env: test_mysql - - elixir: 1.12.3 + env: test_mysql_money + - elixir: 1.13.3 + otp: 23.3 + env: test_ex_money + - elixir: 1.13.3 + otp: 23.3 + env: test_mysql_ex_money + + - elixir: 1.13.3 + otp: 22.3 + env: test_money + - elixir: 1.13.3 + otp: 22.3 + env: test_mysql_money + - elixir: 1.13.3 otp: 22.3 - env: test - - elixir: 1.12.3 + env: test_ex_money + - elixir: 1.13.3 otp: 22.3 - env: test_mysql + env: test_mysql_ex_money + services: postgres: image: postgres @@ -51,7 +73,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Set up Elixir - uses: erlef/setup-elixir@885971a72ed1f9240973bd92ab57af8c1aa68f24 + uses: erlef/setup-beam@v1 with: elixir-version: ${{ matrix.elixir }} otp-version: ${{ matrix.otp }} diff --git a/README.md b/README.md index df3ae98..57e5973 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,9 @@ def deps do # pick one DB package {:postgrex, ">= 0.0.0"}, {:myxql, ">= 0.0.0"}, + # pick one money package + {:money, "~> 1.9"}, + {:ex_money, "~> 5.9"}, ] end ``` diff --git a/config/test_ex_money.exs b/config/test_ex_money.exs new file mode 100644 index 0000000..8603a3f --- /dev/null +++ b/config/test_ex_money.exs @@ -0,0 +1,18 @@ +import Config + +config :ex_double_entry, + db: :postgres, + money: :ex_money + +config :ex_double_entry, ExDoubleEntry.Repo, + username: System.get_env("POSTGRES_DB_USERNAME", "postgres"), + password: System.get_env("POSTGRES_DB_PASSWORD", "postgres"), + database: System.get_env("POSTGRES_DB_NAME", "ex_double_entry_test"), + hostname: System.get_env("POSTGRES_DB_HOST", "localhost"), + pool: Ecto.Adapters.SQL.Sandbox, + show_sensitive_data_on_connection_error: true, + timeout: :infinity, + queue_target: 200, + queue_interval: 10 + +config :logger, level: :info diff --git a/config/test.exs b/config/test_money.exs similarity index 94% rename from config/test.exs rename to config/test_money.exs index 00c1501..4500bb8 100644 --- a/config/test.exs +++ b/config/test_money.exs @@ -1,7 +1,8 @@ import Config config :ex_double_entry, - db: :postgres + db: :postgres, + money: :money config :ex_double_entry, ExDoubleEntry.Repo, username: System.get_env("POSTGRES_DB_USERNAME", "postgres"), diff --git a/config/test_mysql_ex_money.exs b/config/test_mysql_ex_money.exs new file mode 100644 index 0000000..73588b1 --- /dev/null +++ b/config/test_mysql_ex_money.exs @@ -0,0 +1,18 @@ +import Config + +config :ex_double_entry, + db: :mysql, + money: :ex_money + +config :ex_double_entry, ExDoubleEntry.Repo, + username: System.get_env("MYSQL_DB_USERNAME", "root"), + password: System.get_env("MYSQL_DB_PASSWORD", ""), + database: System.get_env("MYSQL_DB_NAME", "ex_double_entry_test"), + hostname: System.get_env("MYSQL_DB_HOST", "localhost"), + pool: Ecto.Adapters.SQL.Sandbox, + show_sensitive_data_on_connection_error: true, + timeout: :infinity, + queue_target: 200, + queue_interval: 10 + +config :logger, level: :info diff --git a/config/test_mysql.exs b/config/test_mysql_money.exs similarity index 94% rename from config/test_mysql.exs rename to config/test_mysql_money.exs index 4dc5e85..3737071 100644 --- a/config/test_mysql.exs +++ b/config/test_mysql_money.exs @@ -1,7 +1,8 @@ import Config config :ex_double_entry, - db: :mysql + db: :mysql, + money: :money config :ex_double_entry, ExDoubleEntry.Repo, username: System.get_env("MYSQL_DB_USERNAME", "root"), diff --git a/lib/ex_double_entry.ex b/lib/ex_double_entry.ex index 7c38048..537eb9a 100644 --- a/lib/ex_double_entry.ex +++ b/lib/ex_double_entry.ex @@ -34,9 +34,9 @@ defmodule ExDoubleEntry do ## Examples iex> %ExDoubleEntry.Transfer{ - iex> money: Money.new(42, :USD), - iex> from: %ExDoubleEntry.Account{identifier: :checking, currency: :USD, balance: Money.new(42, :USD), positive_only?: false}, - iex> to: %ExDoubleEntry.Account{identifier: :savings, currency: :USD, balance: Money.new(0, :USD)}, + iex> money: MoneyProxy.new(42, :USD), + iex> from: %ExDoubleEntry.Account{identifier: :checking, currency: :USD, balance: MoneyProxy.new(42, :USD), positive_only?: false}, + iex> to: %ExDoubleEntry.Account{identifier: :savings, currency: :USD, balance: MoneyProxy.new(0, :USD)}, iex> code: :deposit iex> } iex> |> ExDoubleEntry.transfer!() diff --git a/lib/ex_double_entry/ecto_types/amount.ex b/lib/ex_double_entry/ecto_types/amount.ex new file mode 100644 index 0000000..17763fb --- /dev/null +++ b/lib/ex_double_entry/ecto_types/amount.ex @@ -0,0 +1,21 @@ +if Code.ensure_loaded?(Ecto.Type) do + defmodule ExDoubleEntry.EctoType.Amount do + if macro_exported?(Ecto.Type, :__using__, 1) do + use Ecto.Type + else + @behaviour Ecto.Type + end + + def type, do: :integer + + def cast(val) when is_integer(val), do: {:ok, val} + def cast(%Decimal{} = val), do: {:ok, Decimal.to_integer(val)} + def cast(_), do: :error + + def load(val) when is_integer(val), do: {:ok, val} + + def dump(val) when is_integer(val), do: {:ok, val} + def dump(%Decimal{} = val), do: {:ok, Decimal.to_integer(val)} + def dump(_), do: :error + end +end diff --git a/lib/ex_double_entry/ecto_types/currency.ex b/lib/ex_double_entry/ecto_types/currency.ex index 7e5136c..80107c5 100644 --- a/lib/ex_double_entry/ecto_types/currency.ex +++ b/lib/ex_double_entry/ecto_types/currency.ex @@ -10,7 +10,7 @@ if Code.ensure_loaded?(Ecto.Type) do def cast(val) - def cast(%Money{currency: currency}), do: {:ok, currency} + def cast(%{currency: currency}), do: {:ok, currency} def cast(atom) when is_atom(atom), do: {:ok, atom} diff --git a/lib/ex_double_entry/money_proxy.ex b/lib/ex_double_entry/money_proxy.ex new file mode 100644 index 0000000..15db34d --- /dev/null +++ b/lib/ex_double_entry/money_proxy.ex @@ -0,0 +1,49 @@ +defmodule ExDoubleEntry.MoneyProxy do + @moduledoc """ + The `Money` and `ExMoney` packages both claim the `Money` module namespace, + therefore this proxy module normalises the function uses so that ExDoubleEntry + can be used by either. + """ + + defdelegate new(amount, currency), to: Money + + def add(a, b) do + if function_exported?(Money, :add!, 2) do + apply(Money, :add!, [a, b]) + else + apply(Money, :add, [a, b]) + end + end + + def subtract(a, b) do + if function_exported?(Money, :sub!, 2) do + apply(Money, :sub!, [a, b]) + else + apply(Money, :subtract, [a, b]) + end + end + + def cmp(a, b) do + if function_exported?(Money, :compare!, 2) do + apply(Money, :compare!, [a, b]) + else + apply(Money, :cmp, [a, b]) + end + end + + def neg(money) do + if function_exported?(Money, :neg, 1) do + apply(Money, :neg, [money]) + else + apply(Money, :mult!, [money, -1]) + end + end + + def positive?(money) do + if function_exported?(Money, :positive?, 1) do + apply(Money, :positive?, [money]) + else + apply(Money, :cmp!, [money, Money.new(0, money.currency)]) >= 0 + end + end +end diff --git a/lib/ex_double_entry/schemas/account_balance.ex b/lib/ex_double_entry/schemas/account_balance.ex index a728277..e112e20 100644 --- a/lib/ex_double_entry/schemas/account_balance.ex +++ b/lib/ex_double_entry/schemas/account_balance.ex @@ -2,13 +2,13 @@ defmodule ExDoubleEntry.AccountBalance do use Ecto.Schema import Ecto.{Changeset, Query} - alias ExDoubleEntry.{Account, AccountBalance} + alias ExDoubleEntry.{Account, AccountBalance, EctoType} schema "#{ExDoubleEntry.db_table_prefix()}account_balances" do - field(:identifier, ExDoubleEntry.EctoType.Identifier) - field(:currency, ExDoubleEntry.EctoType.Currency) - field(:scope, ExDoubleEntry.EctoType.Scope) - field(:balance_amount, :integer) + field(:identifier, EctoType.Identifier) + field(:currency, EctoType.Currency) + field(:scope, EctoType.Scope) + field(:balance_amount, EctoType.Amount) timestamps(type: :utc_datetime_usec) end diff --git a/lib/ex_double_entry/schemas/line.ex b/lib/ex_double_entry/schemas/line.ex index 383e82a..6af9aaa 100644 --- a/lib/ex_double_entry/schemas/line.ex +++ b/lib/ex_double_entry/schemas/line.ex @@ -2,17 +2,17 @@ defmodule ExDoubleEntry.Line do use Ecto.Schema import Ecto.Changeset - alias ExDoubleEntry.{AccountBalance, Line} + alias ExDoubleEntry.{AccountBalance, EctoType, Line, MoneyProxy} schema "#{ExDoubleEntry.db_table_prefix()}lines" do - field(:account_identifier, ExDoubleEntry.EctoType.Identifier) - field(:account_scope, ExDoubleEntry.EctoType.Scope) - field(:currency, ExDoubleEntry.EctoType.Currency) - field(:amount, :integer) - field(:balance_amount, :integer) - field(:code, ExDoubleEntry.EctoType.Identifier) - field(:partner_identifier, ExDoubleEntry.EctoType.Identifier) - field(:partner_scope, ExDoubleEntry.EctoType.Scope) + field(:account_identifier, EctoType.Identifier) + field(:account_scope, EctoType.Scope) + field(:currency, EctoType.Currency) + field(:amount, EctoType.Amount) + field(:balance_amount, EctoType.Amount) + field(:code, EctoType.Identifier) + field(:partner_identifier, EctoType.Identifier) + field(:partner_scope, EctoType.Scope) field(:metadata, :map) belongs_to(:partner_line, Line) @@ -55,7 +55,7 @@ defmodule ExDoubleEntry.Line do currency: money.currency, code: code, amount: money.amount, - balance_amount: Money.add(account.balance, money).amount, + balance_amount: MoneyProxy.add(account.balance, money).amount, partner_identifier: partner.identifier, partner_scope: partner.scope, metadata: metadata, diff --git a/lib/ex_double_entry/services/account.ex b/lib/ex_double_entry/services/account.ex index 8c39357..50606da 100644 --- a/lib/ex_double_entry/services/account.ex +++ b/lib/ex_double_entry/services/account.ex @@ -1,8 +1,10 @@ defmodule ExDoubleEntry.Account do + @type t() :: %__MODULE__{} + @enforce_keys [:identifier, :currency] defstruct [:id, :identifier, :scope, :currency, :balance, :positive_only?] - alias ExDoubleEntry.{Account, AccountBalance} + alias ExDoubleEntry.{Account, AccountBalance, MoneyProxy} def present(nil), do: nil @@ -13,7 +15,7 @@ defmodule ExDoubleEntry.Account do currency: params.currency, scope: params.scope, positive_only?: positive_only?(params.identifier), - balance: Money.new(params.balance_amount, params.currency) + balance: MoneyProxy.new(params.balance_amount, params.currency) } end diff --git a/lib/ex_double_entry/services/guard.ex b/lib/ex_double_entry/services/guard.ex index 97446fd..6362f03 100644 --- a/lib/ex_double_entry/services/guard.ex +++ b/lib/ex_double_entry/services/guard.ex @@ -1,19 +1,19 @@ defmodule ExDoubleEntry.Guard do - alias ExDoubleEntry.Transfer + alias ExDoubleEntry.{MoneyProxy, Transfer} @doc """ ## Examples - iex> %Transfer{money: Money.new(42, :USD), from: nil, to: nil, code: nil} + iex> %Transfer{money: MoneyProxy.new(42, :USD), from: nil, to: nil, code: nil} iex> |> Guard.positive_amount?() - {:ok, %Transfer{money: Money.new(42, :USD), from: nil, to: nil, code: nil}} + {:ok, %Transfer{money: MoneyProxy.new(42, :USD), from: nil, to: nil, code: nil}} - iex> %Transfer{money: Money.new(-42, :USD), from: nil, to: nil, code: nil} + iex> %Transfer{money: MoneyProxy.new(-42, :USD), from: nil, to: nil, code: nil} iex> |> Guard.positive_amount?() {:error, :positive_amount_only, ""} """ def positive_amount?(%Transfer{money: money} = transfer) do - case Money.positive?(money) do + case MoneyProxy.positive?(money) do true -> {:ok, transfer} false -> {:error, :positive_amount_only, ""} end @@ -78,7 +78,7 @@ defmodule ExDoubleEntry.Guard do ## Examples iex> %Transfer{ - iex> money: Money.new(42, :USD), + iex> money: MoneyProxy.new(42, :USD), iex> from: %Account{identifier: :checking, currency: :USD}, iex> to: %Account{identifier: :savings, currency: :USD}, iex> code: :deposit @@ -87,7 +87,7 @@ defmodule ExDoubleEntry.Guard do { :ok, %Transfer{ - money: Money.new(42, :USD), + money: MoneyProxy.new(42, :USD), code: :deposit, from: %Account{identifier: :checking, currency: :USD}, to: %Account{identifier: :savings, currency: :USD}, @@ -95,7 +95,7 @@ defmodule ExDoubleEntry.Guard do } iex> %Transfer{ - iex> money: Money.new(42, :AUD), + iex> money: MoneyProxy.new(42, :AUD), iex> from: %Account{identifier: :checking, currency: :USD}, iex> to: %Account{identifier: :savings, currency: :USD}, iex> code: :deposit @@ -104,7 +104,7 @@ defmodule ExDoubleEntry.Guard do {:error, :mismatched_currencies, "Attempted to transfer :AUD from :checking in :USD to :savings in :USD."} iex> %Transfer{ - iex> money: Money.new(42, :USD), + iex> money: MoneyProxy.new(42, :USD), iex> from: %Account{identifier: :checking, currency: :USD}, iex> to: %Account{identifier: :savings, currency: :AUD}, iex> code: :deposit @@ -125,8 +125,8 @@ defmodule ExDoubleEntry.Guard do ## Examples iex> %Transfer{ - iex> money: Money.new(42, :USD), - iex> from: %Account{identifier: :checking, currency: :USD, balance: Money.new(42, :USD), positive_only?: true}, + iex> money: MoneyProxy.new(42, :USD), + iex> from: %Account{identifier: :checking, currency: :USD, balance: MoneyProxy.new(42, :USD), positive_only?: true}, iex> to: %Account{identifier: :savings, currency: :USD}, iex> code: :deposit iex> } @@ -134,16 +134,16 @@ defmodule ExDoubleEntry.Guard do { :ok, %Transfer{ - money: Money.new(42, :USD), + money: MoneyProxy.new(42, :USD), code: :deposit, - from: %Account{identifier: :checking, currency: :USD, balance: Money.new(42, :USD), positive_only?: true}, + from: %Account{identifier: :checking, currency: :USD, balance: MoneyProxy.new(42, :USD), positive_only?: true}, to: %Account{identifier: :savings, currency: :USD}, } } iex> %Transfer{ - iex> money: Money.new(42, :USD), - iex> from: %Account{identifier: :checking, currency: :USD, balance: Money.new(10, :USD), positive_only?: false}, + iex> money: MoneyProxy.new(42, :USD), + iex> from: %Account{identifier: :checking, currency: :USD, balance: MoneyProxy.new(10, :USD), positive_only?: false}, iex> to: %Account{identifier: :savings, currency: :USD}, iex> code: :deposit iex> } @@ -151,16 +151,16 @@ defmodule ExDoubleEntry.Guard do { :ok, %Transfer{ - money: Money.new(42, :USD), + money: MoneyProxy.new(42, :USD), code: :deposit, - from: %Account{identifier: :checking, currency: :USD, balance: Money.new(10, :USD), positive_only?: false}, + from: %Account{identifier: :checking, currency: :USD, balance: MoneyProxy.new(10, :USD), positive_only?: false}, to: %Account{identifier: :savings, currency: :USD}, } } iex> %Transfer{ - iex> money: Money.new(42, :USD), - iex> from: %Account{identifier: :checking, currency: :USD, balance: Money.new(10, :USD)}, + iex> money: MoneyProxy.new(42, :USD), + iex> from: %Account{identifier: :checking, currency: :USD, balance: MoneyProxy.new(10, :USD)}, iex> to: %Account{identifier: :savings, currency: :USD}, iex> code: :deposit iex> } @@ -168,16 +168,16 @@ defmodule ExDoubleEntry.Guard do { :ok, %Transfer{ - money: Money.new(42, :USD), + money: MoneyProxy.new(42, :USD), code: :deposit, - from: %Account{identifier: :checking, currency: :USD, balance: Money.new(10, :USD), positive_only?: nil}, + from: %Account{identifier: :checking, currency: :USD, balance: MoneyProxy.new(10, :USD), positive_only?: nil}, to: %Account{identifier: :savings, currency: :USD}, } } iex> %Transfer{ - iex> money: Money.new(42, :USD), - iex> from: %Account{identifier: :checking, currency: :USD, balance: Money.new(10, :USD), positive_only?: true}, + iex> money: MoneyProxy.new(42, :USD), + iex> from: %Account{identifier: :checking, currency: :USD, balance: MoneyProxy.new(10, :USD), positive_only?: true}, iex> to: %Account{identifier: :savings, currency: :USD}, iex> code: :deposit iex> } @@ -185,7 +185,7 @@ defmodule ExDoubleEntry.Guard do {:error, :insufficient_balance, "Transfer amount: 42, :checking balance amount: 10"} """ def positive_balance_if_enforced?(%Transfer{money: money, from: from} = transfer) do - if !!from.positive_only? and Money.cmp(from.balance, money) == :lt do + if !!from.positive_only? and MoneyProxy.cmp(from.balance, money) == :lt do {:error, :insufficient_balance, "Transfer amount: #{money.amount}, :#{from.identifier} balance amount: #{from.balance.amount}"} else diff --git a/lib/ex_double_entry/services/transfer.ex b/lib/ex_double_entry/services/transfer.ex index 95cf21b..4edf1ae 100644 --- a/lib/ex_double_entry/services/transfer.ex +++ b/lib/ex_double_entry/services/transfer.ex @@ -1,8 +1,10 @@ defmodule ExDoubleEntry.Transfer do + @type t() :: %__MODULE__{} + @enforce_keys [:money, :from, :to, :code] defstruct [:money, :from, :to, :code, :metadata] - alias ExDoubleEntry.{Account, AccountBalance, Guard, Line, Transfer} + alias ExDoubleEntry.{Account, AccountBalance, Guard, Line, MoneyProxy, Transfer} def perform!(%Transfer{} = transfer) do perform!(transfer, ensure_accounts: true) @@ -43,7 +45,7 @@ defmodule ExDoubleEntry.Transfer do AccountBalance.lock_multi!([from, to], fn -> line1 = - Line.insert!(Money.neg(money), + Line.insert!(MoneyProxy.neg(money), account: from, partner: to, code: code, @@ -61,8 +63,8 @@ defmodule ExDoubleEntry.Transfer do Line.update_partner_line_id!(line1, line2.id) Line.update_partner_line_id!(line2, line1.id) - from_amount = Money.subtract(from.balance, money).amount - to_amount = Money.add(to.balance, money).amount + from_amount = MoneyProxy.subtract(from.balance, money).amount + to_amount = MoneyProxy.add(to.balance, money).amount AccountBalance.update_balance!(from, from_amount) AccountBalance.update_balance!(to, to_amount) diff --git a/mix.exs b/mix.exs index 8e08671..653b66f 100644 --- a/mix.exs +++ b/mix.exs @@ -24,19 +24,24 @@ defmodule ExDoubleEntry.MixProject do ] end - defp elixirc_paths(:test), do: ["lib", "test/support"] - defp elixirc_paths(:test_mysql), do: ["lib", "test/support"] + defp elixirc_paths(:test_money), do: ["lib", "test/support"] + defp elixirc_paths(:test_ex_money), do: ["lib", "test/support"] + defp elixirc_paths(:test_mysql_money), do: ["lib", "test/support"] + defp elixirc_paths(:test_mysql_ex_money), do: ["lib", "test/support"] defp elixirc_paths(_), do: ["lib"] defp deps do [ {:jason, "~> 1.2"}, - {:money, "~> 1.9"}, + {:money, "~> 1.9", only: [:test_money, :test_mysql_money]}, + {:ex_money, "~> 5.9", only: [:test_ex_money, :test_mysql_ex_money]}, {:ecto_sql, "~> 3.7"}, {:postgrex, ">= 0.0.0", optional: true}, {:myxql, ">= 0.0.0", optional: true}, - {:ex_machina, "~> 2.7", only: [:test, :test_mysql]}, - {:ex_doc, ">= 0.0.0", only: :dev, runtime: false} + {:ex_machina, "~> 2.7", + only: [:test_money, :test_mysql_money, :test_ex_money, :test_mysql_ex_money]}, + {:ex_doc, ">= 0.0.0", only: :dev, runtime: false}, + {:dialyxir, ">= 0.0.0", only: [:dev], runtime: false} ] end diff --git a/test/ex_double_entry/account_test.exs b/test/ex_double_entry/account_test.exs index 6a9e168..51a4bd3 100644 --- a/test/ex_double_entry/account_test.exs +++ b/test/ex_double_entry/account_test.exs @@ -1,10 +1,10 @@ defmodule ExDoubleEntry.AccountTest do use ExDoubleEntry.DataCase - alias ExDoubleEntry.Account + alias ExDoubleEntry.{Account, MoneyProxy} doctest Account test "present/1" do - balance = Money.new(42, :USD) + balance = MoneyProxy.new(42, :USD) account = :account_balance @@ -24,7 +24,7 @@ defmodule ExDoubleEntry.AccountTest do test "found" do insert(:account_balance, identifier: :savings, currency: :USD, balance_amount: 42) - balance = Money.new(42, :USD) + balance = MoneyProxy.new(42, :USD) assert %Account{ identifier: :savings, @@ -36,7 +36,7 @@ defmodule ExDoubleEntry.AccountTest do test "with default currency" do insert(:account_balance, identifier: :savings, currency: :USD, balance_amount: 42) - balance = Money.new(42, :USD) + balance = MoneyProxy.new(42, :USD) assert %Account{ identifier: :savings, diff --git a/test/ex_double_entry/guard_test.exs b/test/ex_double_entry/guard_test.exs index 798a28d..a81e7ca 100644 --- a/test/ex_double_entry/guard_test.exs +++ b/test/ex_double_entry/guard_test.exs @@ -1,5 +1,5 @@ defmodule ExDoubleEntry.GuardTest do - use ExDoubleEntry.DataCase, async: true - alias ExDoubleEntry.{Account, Guard, Transfer} + use ExDoubleEntry.DataCase + alias ExDoubleEntry.{Account, Guard, MoneyProxy, Transfer} doctest Guard end diff --git a/test/ex_double_entry/schemas/account_balance_test.exs b/test/ex_double_entry/schemas/account_balance_test.exs index bc9a7a0..4e957dc 100644 --- a/test/ex_double_entry/schemas/account_balance_test.exs +++ b/test/ex_double_entry/schemas/account_balance_test.exs @@ -1,6 +1,6 @@ defmodule ExDoubleEntry.AccountBalanceTest do use ExDoubleEntry.DataCase - alias ExDoubleEntry.{Account, AccountBalance, Transfer} + alias ExDoubleEntry.{Account, AccountBalance, MoneyProxy, Transfer} doctest AccountBalance test "create!/1 - balance will always be 0" do @@ -8,7 +8,7 @@ defmodule ExDoubleEntry.AccountBalanceTest do identifier: :savings, scope: "user/1", currency: :USD, - balance: Money.new(42, :USD) + balance: MoneyProxy.new(42, :USD) } assert %AccountBalance{balance_amount: 0} = AccountBalance.create!(account) @@ -137,7 +137,7 @@ defmodule ExDoubleEntry.AccountBalanceTest do assert_raise(DBConnection.ConnectionError, fn -> AccountBalance.lock_multi!([acc_a, acc_b], fn -> Transfer.perform(%Transfer{ - money: Money.new(42, :USD), + money: MoneyProxy.new(42, :USD), from: acc_a, to: acc_b, code: :deposit, diff --git a/test/ex_double_entry/schemas/line_test.exs b/test/ex_double_entry/schemas/line_test.exs index d41a799..9a28180 100644 --- a/test/ex_double_entry/schemas/line_test.exs +++ b/test/ex_double_entry/schemas/line_test.exs @@ -1,6 +1,6 @@ defmodule ExDoubleEntry.LineTest do use ExDoubleEntry.DataCase - alias ExDoubleEntry.{Account, Line} + alias ExDoubleEntry.{Account, Line, MoneyProxy} doctest Line test "insert/2" do @@ -11,7 +11,7 @@ defmodule ExDoubleEntry.LineTest do line = Line.insert!( - Money.new(100, :USD), + MoneyProxy.new(100, :USD), account: acc_a, partner: acc_b, code: :deposit, diff --git a/test/ex_double_entry/transfer_test.exs b/test/ex_double_entry/transfer_test.exs index fc5bd42..f6be2b8 100644 --- a/test/ex_double_entry/transfer_test.exs +++ b/test/ex_double_entry/transfer_test.exs @@ -1,6 +1,6 @@ defmodule ExDoubleEntry.TransferTest do use ExDoubleEntry.DataCase - alias ExDoubleEntry.{Account, AccountBalance, Line, Transfer} + alias ExDoubleEntry.{Account, AccountBalance, Line, MoneyProxy, Transfer} doctest Transfer setup do @@ -21,7 +21,7 @@ defmodule ExDoubleEntry.TransferTest do test "successful", %{acc_a: acc_a, acc_b: acc_b} do transfer = Transfer.perform!(%Transfer{ - money: Money.new(123_45, :USD), + money: MoneyProxy.new(123_45, :USD), from: acc_a, to: acc_b, code: :deposit @@ -34,7 +34,7 @@ defmodule ExDoubleEntry.TransferTest do test "failure", %{acc_a: acc_a, acc_b: acc_b} do transfer = Transfer.perform!(%Transfer{ - money: Money.new(123_45, :USD), + money: MoneyProxy.new(123_45, :USD), from: acc_a, to: acc_b, code: :give_away @@ -49,7 +49,7 @@ defmodule ExDoubleEntry.TransferTest do test "perform/1", %{acc_a: acc_a, acc_b: acc_b} do Transfer.perform(%Transfer{ - money: Money.new(123_45, :USD), + money: MoneyProxy.new(123_45, :USD), from: acc_a, to: acc_b, code: :deposit, diff --git a/test/ex_double_entry_stress_test.exs b/test/ex_double_entry_stress_test.exs index 36c0e87..9e941da 100644 --- a/test/ex_double_entry_stress_test.exs +++ b/test/ex_double_entry_stress_test.exs @@ -2,7 +2,7 @@ defmodule ExDoubleEntryStressTest do use ExDoubleEntry.DataCase import ExDoubleEntry import Ecto.Query - alias ExDoubleEntry.{Line} + alias ExDoubleEntry.{Line, MoneyProxy} @processes 5 @account_pairs_per_process 5 @@ -69,7 +69,7 @@ defmodule ExDoubleEntryStressTest do lock_accounts([acc_a, acc_b], fn -> {:ok, _} = transfer!( - money: Money.new(amount, :USD), + money: MoneyProxy.new(amount, :USD), from: acc_a, to: acc_b, code: :stress_test diff --git a/test/ex_double_entry_test.exs b/test/ex_double_entry_test.exs index dd634d3..2ae0793 100644 --- a/test/ex_double_entry_test.exs +++ b/test/ex_double_entry_test.exs @@ -1,6 +1,6 @@ defmodule ExDoubleEntryTest do use ExDoubleEntry.DataCase - alias ExDoubleEntry.{Account, Line} + alias ExDoubleEntry.{Account, Line, MoneyProxy} doctest ExDoubleEntry describe "lock accounts and transfer" do @@ -15,7 +15,7 @@ defmodule ExDoubleEntryTest do result = ExDoubleEntry.lock_accounts([acc_a, acc_b], fn -> ExDoubleEntry.transfer!( - money: Money.new(100, :USD), + money: MoneyProxy.new(100, :USD), from: acc_a, to: acc_b, code: :deposit @@ -32,7 +32,7 @@ defmodule ExDoubleEntryTest do assert_raise(RuntimeError, fn -> ExDoubleEntry.lock_accounts([acc_a, acc_b], fn -> ExDoubleEntry.transfer!( - money: Money.new(100, :USD), + money: MoneyProxy.new(100, :USD), from: acc_a, to: acc_b, code: :deposit @@ -58,7 +58,7 @@ defmodule ExDoubleEntryTest do result = ExDoubleEntry.lock_accounts([acc_a, acc_b], fn -> ExDoubleEntry.transfer!( - money: Money.new(100, :USD), + money: MoneyProxy.new(100, :USD), from: acc_a, to: acc_b, code: :deposit @@ -75,7 +75,7 @@ defmodule ExDoubleEntryTest do assert_raise(Account.NotFoundError, fn -> ExDoubleEntry.lock_accounts([acc_a, acc_b], fn -> ExDoubleEntry.transfer( - money: Money.new(100, :USD), + money: MoneyProxy.new(100, :USD), from: acc_a, to: acc_b, code: :deposit diff --git a/test/test_helper.exs b/test/test_helper.exs index a5f6683..5076b05 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -4,4 +4,5 @@ Ecto.Adapters.SQL.Sandbox.mode(ExDoubleEntry.repo(), :manual) require Logger db = Application.fetch_env!(:ex_double_entry, :db) -Logger.info("Running tests with #{db}...") +money = Application.fetch_env!(:ex_double_entry, :money) +Logger.info("Running tests with #{db} and #{money}...")