Isdriai

Isdriai

Errors during tests when I add a Ecto.Enum to a schema

Hi !

I’m new in Elixir/Phoenix world,

I started a project and now, I would like add a new attribute to a schema, but this attribute must be an Enum. I saw Ecto.Enum for that, So I added an Ecto.Enum, I did a migration and when I run my server, It’s ok, I have my datas with the new attribute. But when I run tests, there are errors :cry:

I put my github’s project link because I don’t see to put my code here:

https://github.com/PicotRodolphe/cym

I think error or errors are into these files:
users_test.exs
users_controller_test.exs
user.ex
users.ex

Thanks for your help :slight_smile:

Ps: my errors during tests:

mix test
Compiling 1 file (.ex)
warning: function embed_as/1 required by behaviour Ecto.Type is not implemented (in module Cym.Users.User.Type)
lib/chooseYourMis/users/user.ex:7: Cym.Users.User.Type (module)

warning: function equal?/2 required by behaviour Ecto.Type is not implemented (in module Cym.Users.User.Type)
lib/chooseYourMis/users/user.ex:7: Cym.Users.User.Type (module)

..

  1. test users get_user!/1 returns the user with given id (Cym.UsersTest)
    test/chooseYourMis/users_test.exs:27
    ** (MatchError) no match of right hand side value: {:error, #Ecto.Changeset<action: :insert, changes: %{firstName: “some firstName”, lastName: “some lastName”, mail: “some mail”}, errors:
    [type: {“is invalid”, [type: Cym.Users.User.Type, validation: :cast]}], data: #Cym.Users.User<>, valid?: false>}
    code: user = user_fixture()
    stacktrace:
    test/chooseYourMis/users_test.exs:14: Cym.UsersTest.user_fixture/1
    test/chooseYourMis/users_test.exs:28: (test)

  2. test users create_user/1 with valid data creates a user (Cym.UsersTest)
    test/chooseYourMis/users_test.exs:32
    match (=) failed
    code: assert {:ok, %User{} = user} = Users.create_user(@valid_attrs)
    right: {:error,
    #Ecto.Changeset<
    action: :insert,
    changes: %{firstName: “some firstName”, lastName: “some lastName”, mail: “some mail”},
    errors: [type: {“is invalid”, [type: Cym.Users.User.Type, validation: :cast]}],
    data: #Cym.Users.User<>,
    valid?: false
    >}
    stacktrace:
    test/chooseYourMis/users_test.exs:33: (test)

  3. test users update_user/2 with valid data updates the user (Cym.UsersTest)
    test/chooseYourMis/users_test.exs:44
    ** (MatchError) no match of right hand side value: {:error, #Ecto.Changeset<action: :insert, changes: %{firstName: “some firstName”, lastName: “some lastName”, mail: “some mail”}, errors: [type: {“is invalid”, [type: Cym.Users.User.Type, validation: :cast]}], data: #Cym.Users.User<>, valid?: false>}
    code: user = user_fixture()
    stacktrace:
    test/chooseYourMis/users_test.exs:14: Cym.UsersTest.user_fixture/1
    test/chooseYourMis/users_test.exs:45: (test)

  4. test users list_users/0 returns all users (Cym.UsersTest)
    test/chooseYourMis/users_test.exs:22
    ** (MatchError) no match of right hand side value: {:error, #Ecto.Changeset<action: :insert, changes: %{firstName: “some firstName”, lastName: “some lastName”, mail: “some mail”}, errors: [type: {“is invalid”, [type: Cym.Users.User.Type, validation: :cast]}], data: #Cym.Users.User<>, valid?: false>}
    code: user = user_fixture()
    stacktrace:
    test/chooseYourMis/users_test.exs:14: Cym.UsersTest.user_fixture/1
    test/chooseYourMis/users_test.exs:23: (test)

.

  1. test users delete_user/1 deletes the user (Cym.UsersTest)
    test/chooseYourMis/users_test.exs:59
    ** (MatchError) no match of right hand side value: {:error, #Ecto.Changeset<action: :insert, changes: %{firstName: “some firstName”, lastName: “some lastName”, mail: “some mail”}, errors: [type: {“is invalid”, [type: Cym.Users.User.Type, validation: :cast]}], data: #Cym.Users.User<>, valid?: false>}
    code: user = user_fixture()
    stacktrace:
    test/chooseYourMis/users_test.exs:14: Cym.UsersTest.user_fixture/1
    test/chooseYourMis/users_test.exs:60: (test)

  2. test users change_user/1 returns a user changeset (Cym.UsersTest)
    test/chooseYourMis/users_test.exs:65
    ** (MatchError) no match of right hand side value: {:error, #Ecto.Changeset<action: :insert, changes: %{firstName: “some firstName”, lastName: “some lastName”, mail: “some mail”}, errors: [type: {“is invalid”, [type: Cym.Users.User.Type, validation: :cast]}], data: #Cym.Users.User<>, valid?: false>}
    code: user = user_fixture()
    stacktrace:
    test/chooseYourMis/users_test.exs:14: Cym.UsersTest.user_fixture/1
    test/chooseYourMis/users_test.exs:66: (test)

  3. test users update_user/2 with invalid data returns error changeset (Cym.UsersTest)
    test/chooseYourMis/users_test.exs:53
    ** (MatchError) no match of right hand side value: {:error, #Ecto.Changeset<action: :insert, changes: %{firstName: “some firstName”, lastName: “some lastName”, mail: “some mail”}, errors: [type: {“is invalid”, [type: Cym.Users.User.Type, validation: :cast]}], data: #Cym.Users.User<>, valid?: false>}
    code: user = user_fixture()
    stacktrace:
    test/chooseYourMis/users_test.exs:14: Cym.UsersTest.user_fixture/1
    test/chooseYourMis/users_test.exs:54: (test)

..

  1. test create user renders user when data is valid (CymWeb.UserControllerTest)
    test/chooseYourMis_web/controllers/user_controller_test.exs:38
    ** (RuntimeError) expected response with status 201, got: 422, with body:
    {“errors”:{“type”:[“is invalid”]}}
    code: assert %{“id” => id} = json_response(conn, 201)[“data”]
    stacktrace:
    (phoenix) lib/phoenix/test/conn_test.ex:373: Phoenix.ConnTest.response/2
    (phoenix) lib/phoenix/test/conn_test.ex:419: Phoenix.ConnTest.json_response/2
    test/chooseYourMis_web/controllers/user_controller_test.exs:40: (test)

  2. test update user renders errors when data is invalid (CymWeb.UserControllerTest)
    test/chooseYourMis_web/controllers/user_controller_test.exs:77
    ** (MatchError) no match of right hand side value: {:error, #Ecto.Changeset<action: :insert, changes: %{firstName: “some firstName”, lastName: “some lastName”, mail: “some mail”}, errors: [type: {“is invalid”, [type: Cym.Users.User.Type, validation: :cast]}], data: #Cym.Users.User<>, valid?: false>}
    stacktrace:
    test/chooseYourMis_web/controllers/user_controller_test.exs:22: CymWeb.UserControllerTest.fixture/1
    test/chooseYourMis_web/controllers/user_controller_test.exs:97: CymWeb.UserControllerTest.create_user/1
    test/chooseYourMis_web/controllers/user_controller_test.exs:1: CymWeb.UserControllerTest.ex_unit/2

  3. test delete user deletes chosen user (CymWeb.UserControllerTest)
    test/chooseYourMis_web/controllers/user_controller_test.exs:86
    ** (MatchError) no match of right hand side value: {:error, #Ecto.Changeset<action: :insert, changes: %{firstName: “some firstName”, lastName: “some lastName”, mail: “some mail”}, errors: [type: {“is invalid”, [type: Cym.Users.User.Type, validation: :cast]}], data: #Cym.Users.User<>, valid?: false>}
    stacktrace:
    test/chooseYourMis_web/controllers/user_controller_test.exs:22: CymWeb.UserControllerTest.fixture/1
    test/chooseYourMis_web/controllers/user_controller_test.exs:97: CymWeb.UserControllerTest.create_user/1
    test/chooseYourMis_web/controllers/user_controller_test.exs:1: CymWeb.UserControllerTest.ex_unit/2

  4. test update user renders user when data is valid (CymWeb.UserControllerTest)
    test/chooseYourMis_web/controllers/user_controller_test.exs:62
    ** (MatchError) no match of right hand side value: {:error, #Ecto.Changeset<action: :insert, changes: %{firstName: “some firstName”, lastName: “some lastName”, mail: “some mail”}, errors: [type: {“is invalid”, [type: Cym.Users.User.Type, validation: :cast]}], data: #Cym.Users.User<>, valid?: false>}
    stacktrace:
    test/chooseYourMis_web/controllers/user_controller_test.exs:22: CymWeb.UserControllerTest.fixture/1
    test/chooseYourMis_web/controllers/user_controller_test.exs:97: CymWeb.UserControllerTest.create_user/1
    test/chooseYourMis_web/controllers/user_controller_test.exs:1: CymWeb.UserControllerTest.ex_unit/2

Finished in 0.2 seconds
30 tests, 11 failures

Randomized with seed 429207

First 4 of 4 Posts Switch mode

Eiji

Eiji

Please take a look at changes in my PR:

https://github.com/PicotRodolphe/cym/pull/1

Isdriai

Isdriai OP

Hey ! I test your changes, it works now !!! thanks :slight_smile:

Just, I saw VisualCode doesn’t propose me :candidate when I start typing :can for example. If you use VisualCode, which plugins do use you for Elixir ? ( For the moment, I have ElixirLS only )

Eiji

Eiji

Since it’s not a type known by Elixir compiler probably no editors would ever allow to prompt for atom enum values.

It would be possible if this would be merged:
https://github.com/gjaldon/ecto_enum/pull/90

With this instead of atoms you could use macros. Every editor seeing functions and macros would be able to give a proper prompt.

Of course nothing stops you from implementing your own plugin, but I do not see it a good way unless you have a good idea how to solve it nicely, because there may be multiple enums with same one or more values.

Eiji

Eiji

ping @Isdriai
My PR is still not closed/merged. Also it would be nice if you could mark this topic solved as well.

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement