-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mbramson/improve-address-validation-error-…
…structure Improve address validation failure error structure
- Loading branch information
Showing
3 changed files
with
67 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,8 @@ defmodule CatalogApi.AddressTest do | |
|
||
test "returns an error when given invalid address params" do | ||
params = Map.put(@valid_address_params, "city", "") | ||
assert {:error, {:invalid_address, _}} = Address.validate_params(params) | ||
assert {:error, {:invalid_address, errors}} = Address.validate_params(params) | ||
assert %{city: ["cannot be blank"]} = errors | ||
end | ||
|
||
test "returns an error when given an empty map" do | ||
|
@@ -62,22 +63,22 @@ defmodule CatalogApi.AddressTest do | |
test "returns an error tuple if first_name is blank" do | ||
address = Map.put(@valid_address, :first_name, "") | ||
error_message = "cannot be blank" | ||
assert {:error, {:invalid_address, [{:first_name, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{first_name: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if first_name is longer than 40 characters" do | ||
first_name = "< ten >< ten >< ten >< ten >1" | ||
address = Map.put(@valid_address, :first_name, first_name) | ||
error_message = "cannot be longer than 40 characters" | ||
assert {:error, {:invalid_address, [{:first_name, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{first_name: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if first_name is not a string" do | ||
address = Map.put(@valid_address, :first_name, 123) | ||
error_message = "must be a string" | ||
assert {:error, {:invalid_address, [{:first_name, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{first_name: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
|
@@ -86,22 +87,22 @@ defmodule CatalogApi.AddressTest do | |
test "returns an error tuple if last_name is blank" do | ||
address = Map.put(@valid_address, :last_name, "") | ||
error_message = "cannot be blank" | ||
assert {:error, {:invalid_address, [{:last_name, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{last_name: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if last_name is longer than 40 characters" do | ||
last_name = "< ten >< ten >< ten >< ten >1" | ||
address = Map.put(@valid_address, :last_name, last_name) | ||
error_message = "cannot be longer than 40 characters" | ||
assert {:error, {:invalid_address, [{:last_name, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{last_name: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if last_name is not a string" do | ||
address = Map.put(@valid_address, :last_name, 123) | ||
error_message = "must be a string" | ||
assert {:error, {:invalid_address, [{:last_name, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{last_name: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
|
@@ -110,22 +111,22 @@ defmodule CatalogApi.AddressTest do | |
test "returns an error tuple if address_1 is blank" do | ||
address = Map.put(@valid_address, :address_1, "") | ||
error_message = "cannot be blank" | ||
assert {:error, {:invalid_address, [{:address_1, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{address_1: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if address_1 is longer than 75 characters" do | ||
address_1 = "< ten >< ten >< ten >< ten >< ten >< ten >< ten >123456" | ||
address = Map.put(@valid_address, :address_1, address_1) | ||
error_message = "cannot be longer than 75 characters" | ||
assert {:error, {:invalid_address, [{:address_1, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{address_1: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if address_1 is not a string" do | ||
address = Map.put(@valid_address, :address_1, 123) | ||
error_message = "must be a string" | ||
assert {:error, {:invalid_address, [{:address_1, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{address_1: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
|
@@ -140,14 +141,14 @@ defmodule CatalogApi.AddressTest do | |
address_2 = "< ten >< ten >< ten >< ten >< ten >< ten >1" | ||
address = Map.put(@valid_address, :address_2, address_2) | ||
error_message = "cannot be longer than 60 characters" | ||
assert {:error, {:invalid_address, [{:address_2, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{address_2: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if address_2 is not a string" do | ||
address = Map.put(@valid_address, :address_2, 123) | ||
error_message = "must be a string" | ||
assert {:error, {:invalid_address, [{:address_2, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{address_2: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
|
@@ -162,14 +163,14 @@ defmodule CatalogApi.AddressTest do | |
address_3 = "< ten >< ten >< ten >< ten >< ten >< ten >1" | ||
address = Map.put(@valid_address, :address_3, address_3) | ||
error_message = "cannot be longer than 60 characters" | ||
assert {:error, {:invalid_address, [{:address_3, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{address_3: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if address_3 is not a string" do | ||
address = Map.put(@valid_address, :address_3, 123) | ||
error_message = "must be a string" | ||
assert {:error, {:invalid_address, [{:address_3, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{address_3: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
|
@@ -178,22 +179,22 @@ defmodule CatalogApi.AddressTest do | |
test "returns an error tuple if city is blank" do | ||
address = Map.put(@valid_address, :city, "") | ||
error_message = "cannot be blank" | ||
assert {:error, {:invalid_address, [{:city, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{city: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if city is longer than 40 characters" do | ||
city = "< ten >< ten >< ten >< ten >1" | ||
address = Map.put(@valid_address, :city, city) | ||
error_message = "cannot be longer than 40 characters" | ||
assert {:error, {:invalid_address, [{:city, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{city: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if city is not a string" do | ||
address = Map.put(@valid_address, :city, 123) | ||
error_message = "must be a string" | ||
assert {:error, {:invalid_address, [{:city, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{city: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
|
@@ -202,22 +203,22 @@ defmodule CatalogApi.AddressTest do | |
test "returns an error tuple if state_province is blank" do | ||
address = Map.put(@valid_address, :state_province, "") | ||
error_message = "cannot be blank" | ||
assert {:error, {:invalid_address, [{:state_province, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{state_province: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if state_province is longer than 50 characters" do | ||
state_province = "< ten >< ten >< ten >< ten >< ten >1" | ||
address = Map.put(@valid_address, :state_province, state_province) | ||
error_message = "cannot be longer than 50 characters" | ||
assert {:error, {:invalid_address, [{:state_province, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{state_province: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if state_province is not a string" do | ||
address = Map.put(@valid_address, :state_province, 123) | ||
error_message = "must be a string" | ||
assert {:error, {:invalid_address, [{:state_province, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{state_province: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
|
@@ -226,22 +227,22 @@ defmodule CatalogApi.AddressTest do | |
test "returns an error tuple if postal_code is blank" do | ||
address = Map.put(@valid_address, :postal_code, "") | ||
error_message = "cannot be blank" | ||
assert {:error, {:invalid_address, [{:postal_code, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{postal_code: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if postal_code is longer than 15 characters" do | ||
postal_code = "< ten >123456" | ||
address = Map.put(@valid_address, :postal_code, postal_code) | ||
error_message = "cannot be longer than 15 characters" | ||
assert {:error, {:invalid_address, [{:postal_code, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{postal_code: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if postal_code is not a string" do | ||
address = Map.put(@valid_address, :postal_code, 123) | ||
error_message = "must be a string" | ||
assert {:error, {:invalid_address, [{:postal_code, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{postal_code: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
|
@@ -250,21 +251,21 @@ defmodule CatalogApi.AddressTest do | |
test "returns an error tuple if country is blank" do | ||
address = Map.put(@valid_address, :country, "") | ||
error_message = "cannot be blank" | ||
assert {:error, {:invalid_address, [{:country, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{country: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if country is not an ISO-3166-1 Alpha 2 Country Code" do | ||
address = Map.put(@valid_address, :country, "AP") | ||
error_message = "country code must be valid ISO 3166-1 alpha 2 country code" | ||
assert {:error, {:invalid_address, [{:country, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{country: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if country is not a string" do | ||
address = Map.put(@valid_address, :country, 123) | ||
error_message = "must be a string" | ||
assert {:error, {:invalid_address, [{:country, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{country: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
|
@@ -279,22 +280,22 @@ defmodule CatalogApi.AddressTest do | |
email = String.pad_trailing("[email protected]", 255, "< ten >") | ||
address = Map.put(@valid_address, :email, email) | ||
error_message = "cannot be longer than 254 characters" | ||
assert {:error, {:invalid_address, [{:email, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{email: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if email is invalid" do | ||
email = "no_at_sign" | ||
address = Map.put(@valid_address, :email, email) | ||
error_message = "must be a valid email" | ||
assert {:error, {:invalid_address, [{:email, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{email: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if email is not a string" do | ||
address = Map.put(@valid_address, :email, 123) | ||
error_message = "must be a string" | ||
assert {:error, {:invalid_address, [{:email, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{email: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
|
@@ -309,14 +310,14 @@ defmodule CatalogApi.AddressTest do | |
phone_number = String.pad_trailing("", 21, "< ten >") | ||
address = Map.put(@valid_address, :phone_number, phone_number) | ||
error_message = "cannot be longer than 20 characters" | ||
assert {:error, {:invalid_address, [{:phone_number, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{phone_number: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
|
||
test "returns an error tuple if phone_number is not a string" do | ||
address = Map.put(@valid_address, :phone_number, 123) | ||
error_message = "must be a string" | ||
assert {:error, {:invalid_address, [{:phone_number, [^error_message]}]}} = | ||
assert {:error, {:invalid_address, %{phone_number: [^error_message]}}} = | ||
Address.validate(address) | ||
end | ||
end | ||
|
Oops, something went wrong.