Mix test - Missing remainders on Decimal

I was playing around mix test with phx and noticed that the result the I am get from the database was different from what I have inserted.

I created the schema with:

mix phx.gen.html Commerce Product products name:string price:decimal description:text

He’res the trimmed down test of what I was working on

  test "testing products" do
     {:ok, product} =
         name: "Some product",
         description: "Some product description",
         price: Decimal.new(25.20)
       } |> Repo.insert
       inserted_product = Commerce.get_product!(product.id)
       assert  inserted_product.price ==  product.price

And here’s the result of the tests. Here you can see that upon retrieval of the value from the DB.
the inserted_product.price has retrieved %Decimal{sign: 1, coef: 25, exp: 0} while the inserted value is %Decimal{sign: 1, coef: 252, exp: -1}

test testing products (PhxCommerce.Acceptance.ProductsTest)
Assertion with == failed
code: inserted_product.price() == product.price()
left: %Decimal{sign: 1, coef: 25, exp: 0}
right: %Decimal{sign: 1, coef: 252, exp: -1}
test/acceptance/products_test.exs:41: (test)

I am still working/researching on what could have caused the problem and would appreciate if you could point out what I have missed.

Can you please share the generated migration? As far as I can remember you need to specify a precission for decimal fields.

Thanks NobbZ !,

Just added precision and scale, now the tests are passing . I also thought it would allow it since some of the examples of migrations I saw did not bother to add precision.

anyway, here’s my migration now:

  use Ecto.Migration
  def change do
    create table(:shop_products) do
      add :price, :decimal , precision: 10 , scale: 2
      add :name, :string
      add :description, :text

Also make sure to compare for equality using Decimal.equal?/2.

1 Like