From 7cb007d32db777750bfa74ddada89e08929ec5e8 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Wed, 10 Jul 2024 13:13:55 -0400 Subject: [PATCH] test: add tests for integers --- test/type/integer_test.exs | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 test/type/integer_test.exs diff --git a/test/type/integer_test.exs b/test/type/integer_test.exs new file mode 100644 index 00000000..dc95c700 --- /dev/null +++ b/test/type/integer_test.exs @@ -0,0 +1,40 @@ +defmodule Ash.Test.Type.IntegerTest do + @moduledoc false + use ExUnit.Case, async: true + + require Ash.Query + + describe "atomic validation" do + test "valid value" do + {:atomic, expr} = Ash.Type.Integer.cast_atomic(1, min: 0, max: 2) + + assert Ash.Expr.eval!(expr) == 1 + end + + test "invalid values" do + assert_raise Ash.Error.Changes.InvalidChanges, ~r/must be greater than or equal to 2/, fn -> + {:atomic, expr} = Ash.Type.Integer.cast_atomic(1, min: 2, max: 4) + Ash.Expr.eval!(expr) + end + + assert_raise Ash.Error.Changes.InvalidChanges, ~r/must be less than or equal to 4/, fn -> + {:atomic, expr} = Ash.Type.Integer.cast_atomic(5, min: 2, max: 4) + Ash.Expr.eval!(expr) + end + end + end + + describe "validation" do + test "valid value" do + assert {:ok, 1} = Ash.Type.Integer.cast_input(1, min: 0, max: 2) + end + + test "invalid values" do + assert {:error, [[message: "must be more than or equal to %{min}", min: 2]]} = + Ash.Type.Integer.apply_constraints(1, min: 2, max: 4) + + assert {:error, [[message: "must be less than or equal to %{max}", max: 4]]} = + Ash.Type.Integer.apply_constraints(5, min: 2, max: 4) + end + end +end