Skip to content

Commit

Permalink
tests for types and fields
Browse files Browse the repository at this point in the history
  • Loading branch information
phcurado committed Jan 6, 2023
1 parent f5596a3 commit 1cb6f27
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/parameter/field.ex
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ defmodule Parameter.Field do
:ok
end

defp function_valid?(_validator, _arity, message) do
{:error, message}
defp function_valid?(_validator, arity, message) do
{:error, "#{message} with #{arity} arity"}
end
end
3 changes: 2 additions & 1 deletion lib/parameter/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ defmodule Parameter.Types do

@spec assoc_type?(any) :: boolean
def assoc_type?({type, _}), do: type in @assoc_types
def assoc_type?(_), do: false

@types_mod %{
any: Parameter.Types.Any,
Expand Down Expand Up @@ -128,7 +129,7 @@ defmodule Parameter.Types do
end

def validate({:map, _inner_type}, _values) do
{:error, "invalid array type"}
{:error, "invalid map type"}
end

def validate({:has_one, inner_type}, values) when is_map(values) do
Expand Down
6 changes: 6 additions & 0 deletions lib/parameter/types/boolean.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ defmodule Parameter.Types.Boolean do
loads boolean type
## Examples
iex> Parameter.Types.Boolean.load(true)
{:ok, true}
iex> Parameter.Types.Boolean.load("true")
{:ok, true}
iex> Parameter.Types.Boolean.load("True")
{:ok, true}
iex> Parameter.Types.Boolean.load("false")
{:ok, false}
Expand Down
19 changes: 19 additions & 0 deletions lib/parameter/types/float.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ defmodule Parameter.Types.Float do

use Parameter.Parametrizable

@doc """
loads float type
## Examples
iex> Parameter.Types.Float.load(1.5)
{:ok, 1.5}
iex> Parameter.Types.Float.load("2.5")
{:ok, 2.5}
iex> Parameter.Types.Float.load(1)
{:ok, 1.0}
iex> Parameter.Types.Float.load("not float")
{:error, "invalid float type"}
iex> Parameter.Types.Float.load(:atom)
{:error, "invalid float type"}
"""
@impl true
def load(value) when is_float(value) do
{:ok, value}
Expand Down
19 changes: 19 additions & 0 deletions lib/parameter/types/naive_datetime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ defmodule Parameter.Types.NaiveDateTime do

use Parameter.Parametrizable

@doc """
loads NaiveDateTime type
## Examples
iex> Parameter.Types.NaiveDateTime.load(~N[2000-01-01 23:00:07])
{:ok, ~N[2000-01-01 23:00:07]}
iex> Parameter.Types.NaiveDateTime.load("2000-01-01 22:00:07")
{:ok, ~N[2000-01-01 22:00:07]}
iex> Parameter.Types.NaiveDateTime.load({{2021, 05, 11}, {22, 30, 10}})
{:ok, ~N[2021-05-11 22:30:10]}
iex> Parameter.Types.NaiveDateTime.load({{2021, 25, 11}, {22, 30, 10}})
{:error, "invalid naive_datetime type"}
iex> Parameter.Types.NaiveDateTime.load("2015-25-23")
{:error, "invalid naive_datetime type"}
"""
@impl true
def load(%NaiveDateTime{} = value) do
{:ok, value}
Expand Down
24 changes: 24 additions & 0 deletions test/parameter/field_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ defmodule Parameter.FieldTest do
assert {:error, "invalid float type"} == Field.new(opts)
end

test "fails if name is not an atom" do
opts = [
name: "main_address",
type: :float,
key: "mainAddress",
required: true,
default: "Hello"
]

assert {:error, "invalid atom type"} == Field.new(opts)
end

test "fails on invalid function" do
opts = [
name: :address,
type: :float,
on_load: fn val -> val end,
key: "mainAddress",
required: true
]

assert {:error, "on_load must be a function with 2 arity"} == Field.new(opts)
end

test "fails if a default value used at the sametime with load_default and dump_default" do
opts = [
name: :main_address,
Expand Down
24 changes: 24 additions & 0 deletions test/parameter/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ defmodule Parameter.SchemaTest do
end
end

describe "fields/1" do
import Parameter.Schema

param ModuleSchema do
field :first_name, :string
end

test "module schema schema fields" do
assert [%Parameter.Field{}] = Schema.fields(__MODULE__.ModuleSchema)
end

test "runtime schema fields" do
schema =
%{
first_name: [key: "firstName", type: :string, required: true],
address: [required: true, type: {:has_one, %{street: [type: :string, required: true]}}],
phones: [type: {:has_many, %{country: [type: :string, required: true]}}]
}
|> Schema.compile!()

assert [%Parameter.Field{}, %Parameter.Field{}, %Parameter.Field{}] = Schema.fields(schema)
end
end

defp from_name(parameters, name) do
Enum.find(parameters, &(&1.name == name))
end
Expand Down
4 changes: 4 additions & 0 deletions test/parameter/types/float_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Parameter.Types.FloatTest do
use ExUnit.Case
doctest Parameter.Types.Float
end
4 changes: 4 additions & 0 deletions test/parameter/types/naive_datetime_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Parameter.Types.NaiveDateTimeTest do
use ExUnit.Case
doctest Parameter.Types.NaiveDateTime
end
50 changes: 50 additions & 0 deletions test/parameter/types_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ defmodule Parameter.TypesTest do
enum values: [:user_online, :user_offline]
end

test "base_type?/1" do
assert Types.base_type?(:any)
refute Types.base_type?(:not_type)
end

test "composite_inner_type?/1" do
assert Types.composite_inner_type?({:array, :any})
assert Types.composite_inner_type?({:map, :not_type})
refute Types.composite_inner_type?(:not_type)
end

test "assoc_type?/1" do
assert Types.assoc_type?({:has_one, :any})
assert Types.assoc_type?({:has_many, :any})
refute Types.assoc_type?({:map, :not_type})
refute Types.assoc_type?(:not_type)
end

describe "load/2" do
test "load any type" do
assert Types.load(:any, "Test") == {:ok, "Test"}
Expand Down Expand Up @@ -354,5 +372,37 @@ defmodule Parameter.TypesTest do

assert Types.validate({:has_one, :float}, "21") == {:error, "invalid inner data type"}
end

test "validate map with inner type" do
assert Types.validate({:map, :string}, %{}) == :ok
assert Types.validate({:map, :string}, %{key: "value", other_key: "other value"}) == :ok

assert Types.validate({:map, :string}, %{key: "value", other_key: 22}) ==
{:error, "invalid string type"}

assert Types.validate({:map, :float}, "21") == {:error, "invalid map type"}
assert Types.validate({:map, {:array, :float}}, "21") == {:error, "invalid map type"}
assert Types.validate({:map, {:array, :float}}, %{k: [1.5]}) == :ok
end

test "validate array with inner type" do
assert Types.validate({:array, :string}, []) == :ok
assert Types.validate({:array, :string}, ["value", "other value"]) == :ok

assert Types.validate({:array, :float}, ["value", "other value"]) ==
{:error, "invalid float type"}

assert Types.validate({:array, :float}, "21") == {:error, "invalid array type"}
assert Types.validate({:array, {:array, :float}}, "21") == {:error, "invalid array type"}
assert Types.validate({:array, {:array, :float}}, [[1.5, 5.5], [1.2]]) == :ok
end
end

test "validate!/2" do
assert Types.validate!(:any, %{}) == :ok

assert_raise ArgumentError, "invalid inner data type", fn ->
Types.validate!({:has_one, :float}, "21") == {:error, "invalid inner data type"}
end
end
end

0 comments on commit 1cb6f27

Please sign in to comment.