From 3ace151f0de7dcdeef22fe0585fa0bbd4dad7ac3 Mon Sep 17 00:00:00 2001 From: Matt Beanland Date: Fri, 21 Aug 2026 21:57:48 +0930 Subject: [PATCH] chore: add tests for the round rendering fixed by ash_sql --- test/round_expr_test.exs | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 test/round_expr_test.exs diff --git a/test/round_expr_test.exs b/test/round_expr_test.exs new file mode 100644 index 00000000..bf7f3a79 --- /dev/null +++ b/test/round_expr_test.exs @@ -0,0 +1,64 @@ +# SPDX-FileCopyrightText: 2019 ash_postgres contributors +# +# SPDX-License-Identifier: MIT + +defmodule AshPostgres.RoundExprTest do + @moduledoc false + use AshPostgres.RepoCase, async: false + + alias AshPostgres.Test.Post + + require Ash.Query + import Ash.Expr + + defp calculate(post, name, type, expression) do + Post + |> Ash.Query.filter(id == ^post.id) + |> Ash.Query.calculate(name, type, expression) + |> Ash.read_one!() + |> Map.get(:calculations) + |> Map.get(name) + end + + test "round with no precision rounds to no decimal places" do + post = + Post + |> Ash.Changeset.for_create(:create, %{title: "a", decimal: Decimal.new("10.50")}) + |> Ash.create!() + + assert Decimal.equal?( + calculate(post, :rounded, :decimal, expr(round(decimal))), + Decimal.new("11") + ) + end + + test "round with no precision rounds a negative away from zero" do + post = + Post + |> Ash.Changeset.for_create(:create, %{title: "a", decimal: Decimal.new("-2.50")}) + |> Ash.create!() + + assert Decimal.equal?( + calculate(post, :rounded, :decimal, expr(round(decimal))), + Decimal.new("-3") + ) + end + + test "round to a given precision" do + post = + Post + |> Ash.Changeset.for_create(:create, %{title: "a", decimal: Decimal.new("3.14159")}) + |> Ash.create!() + + assert Decimal.equal?( + calculate(post, :rounded, :decimal, expr(round(decimal, 2))), + Decimal.new("3.14") + ) + end + + test "round over a float" do + post = Post |> Ash.Changeset.for_create(:create, %{title: "a", score: 3}) |> Ash.create!() + + assert calculate(post, :rounded, :float, expr(round(type(score, :float), 2))) == 3.0 + end +end