-
Notifications
You must be signed in to change notification settings - Fork 18
/
money_ecto_test.exs
121 lines (98 loc) · 5.19 KB
/
money_ecto_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
defmodule Money.Ecto.Test do
use ExUnit.Case
describe "Money.Ecto.Composite.Type specific tests" do
test "load a tuple with an unknown currency code produces an error" do
assert Money.Ecto.Composite.Type.load({"ABC", 100}) ==
{:error, {Cldr.UnknownCurrencyError, "The currency \"ABC\" is invalid"}}
end
test "load a tuple produces a Money struct" do
assert Money.Ecto.Composite.Type.load({"USD", 100}) == {:ok, Money.new(:USD, 100)}
end
test "dump a money struct" do
assert Money.Ecto.Composite.Type.dump(Money.new(:USD, 100)) ==
{:ok, {"USD", Decimal.new(100)}}
end
end
describe "Money.Ecto.Map.Type specific tests" do
test "load a json map with a string amount produces a Money struct" do
assert Money.Ecto.Map.Type.load(%{"currency" => "USD", "amount" => "100"}) ==
{:ok, Money.new(:USD, 100)}
end
test "load a json map with a number amount produces a Money struct" do
assert Money.Ecto.Map.Type.load(%{"currency" => "USD", "amount" => 100}) ==
{:ok, Money.new(:USD, 100)}
end
test "dump a money struct" do
assert Money.Ecto.Map.Type.dump(Money.new(:USD, 100)) ==
{:ok, %{"amount" => "100", "currency" => "USD"}}
end
end
for ecto_type_module <- [Money.Ecto.Composite.Type, Money.Ecto.Map.Type] do
test "#{inspect(ecto_type_module)}: dump anything other than a Money struct or a 2-tuple is an error" do
assert unquote(ecto_type_module).dump(100) == :error
end
test "#{inspect(ecto_type_module)}: cast a map with the current structure but an empty amount" do
assert unquote(ecto_type_module).cast(%{"currency" => "USD", "amount" => ""}) == {:ok, nil}
end
test "#{inspect(ecto_type_module)}: cast a money struct" do
assert unquote(ecto_type_module).cast(Money.new(:USD, 100)) == {:ok, Money.new(:USD, 100)}
end
test "#{inspect(ecto_type_module)}: cast a map with string keys and values" do
assert unquote(ecto_type_module).cast(%{"currency" => "USD", "amount" => "100"}) ==
{:ok, Money.new(:USD, 100)}
end
test "#{inspect(ecto_type_module)}: cast a map with string keys and numeric amount" do
assert unquote(ecto_type_module).cast(%{"currency" => "USD", "amount" => 100}) ==
{:ok, Money.new(:USD, 100)}
end
test "#{inspect(ecto_type_module)}: cast a map with string keys, atom currency, and string amount" do
assert unquote(ecto_type_module).cast(%{"currency" => :USD, "amount" => "100"}) ==
{:ok, Money.new(100, :USD)}
end
test "#{inspect(ecto_type_module)}: cast a map with string keys, atom currency, and numeric amount" do
assert unquote(ecto_type_module).cast(%{"currency" => :USD, "amount" => 100}) ==
{:ok, Money.new(100, :USD)}
end
test "#{inspect(ecto_type_module)}: cast a map with string keys and invalid currency" do
assert unquote(ecto_type_module).cast(%{"currency" => "AAA", "amount" => 100}) ==
{:error, {Cldr.UnknownCurrencyError, "The currency \"AAA\" is invalid"}}
end
test "#{inspect(ecto_type_module)}: cast a map with atom keys and values" do
assert unquote(ecto_type_module).cast(%{currency: "USD", amount: "100"}) ==
{:ok, Money.new(100, :USD)}
end
test "#{inspect(ecto_type_module)}: cast a map with atom keys and numeric amount" do
assert unquote(ecto_type_module).cast(%{currency: "USD", amount: 100}) ==
{:ok, Money.new(100, :USD)}
end
test "#{inspect(ecto_type_module)}: cast a map with atom keys, atom currency, and numeric amount" do
assert unquote(ecto_type_module).cast(%{currency: :USD, amount: 100}) ==
{:ok, Money.new(100, :USD)}
end
test "#{inspect(ecto_type_module)}: cast a map with atom keys, atom currency, and string amount" do
assert unquote(ecto_type_module).cast(%{currency: :USD, amount: "100"}) ==
{:ok, Money.new(100, :USD)}
end
test "#{inspect(ecto_type_module)}: cast a map with atom keys and invalid currency" do
assert unquote(ecto_type_module).cast(%{currency: "AAA", amount: 100}) ==
{:error, {Cldr.UnknownCurrencyError, "The currency \"AAA\" is invalid"}}
end
test "#{inspect(ecto_type_module)}: cast a string that includes currency code and amount" do
assert unquote(ecto_type_module).cast("100 USD") == {:ok, Money.new(100, :USD)}
assert unquote(ecto_type_module).cast("USD 100") == {:ok, Money.new(100, :USD)}
end
test "#{inspect(ecto_type_module)}: cast a string that includes currency code and localised amount" do
locale = Test.Cldr.get_locale
Test.Cldr.put_locale "de"
assert unquote(ecto_type_module).cast("100,00 USD") == {:ok, Money.new("100,00", :USD)}
Test.Cldr.put_locale locale
end
test "#{inspect(ecto_type_module)}: cast an invalid string is an error" do
assert unquote(ecto_type_module).cast("100 USD and other stuff") == :error
assert unquote(ecto_type_module).cast("100") == :error
end
test "#{inspect(ecto_type_module)}: cast anything else is an error" do
assert unquote(ecto_type_module).cast(:atom) == :error
end
end
end