-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidixir_test.exs
More file actions
100 lines (80 loc) · 2.83 KB
/
validixir_test.exs
File metadata and controls
100 lines (80 loc) · 2.83 KB
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
defmodule ValidixirTest do
use ExUnit.Case
doctest Validixir
alias Example
alias Validixir.Failure
alias Validixir.Error
test "validate/2 with empty validation always succeeds" do
result = Validixir.validate(fn -> :something end, [])
assert result == {:ok, :something}
end
describe "The example code" do
test "does not construct invalid addresses" do
street = %{}
number = -1
zip = nil
city = nil
failure = {:error, inner_failure} = Example.Address.make(street, number, zip, city)
assert {:error, %Failure{}} = failure
context = [Example.Address, Validixir.Validations]
assert inner_failure.errors == [
Error.make(street, :not_a_binary, context),
Error.make(number, :not_a_positive_integer, context),
Error.make(zip, :not_a_binary, context),
Error.make(city, :not_a_binary, context)
]
end
test "does construct valid addresses" do
street = "Jane Street"
number = 1
zip = "99999"
city = "New York"
success = Example.Address.make(street, number, city, zip)
assert {:ok, _} = success
{:ok, candidate} = success
assert candidate.street == street
assert candidate.number == number
assert candidate.zip == zip
assert candidate.city == city
end
test "does not construct invalid persons" do
name = 1
username = "this username is too long"
email = "not an email"
address = %{}
failure = Example.Person.make(name, username, email, address)
assert {:error, %Failure{}} = failure
{:error, inner_failure} = failure
assert inner_failure.errors == [
Error.make(name, :not_a_binary, [Example.Person, Validixir.Validations]),
Error.make(username, :longer_than_10, Example.Person),
Error.make(email, :not_an_email, Example.Person),
Error.make(address, :not_an_address, Example.Person)
]
end
test "does construct valid persons" do
# credentials for a valid person
name = "Simon"
username = "smoes"
email = "smoes@github.com"
# ... but construct valid address first
street = "Jane Street"
number = 1
zip = "99999"
city = "New York"
result =
Example.Address.make(street, number, city, zip)
# then ignore what the result would be and just continue
# as if it is a success
|> Validixir.and_then(fn address ->
Example.Person.make(name, username, email, address)
end)
assert {:ok, _} = result
{:ok, candidate} = result
assert candidate.name == name
assert candidate.username == username
assert candidate.email == email
# we dont have to check address - we know it's valid :)
end
end
end