Is this a bug?

Hi all. I think this is a Phoenix bug but I’m not positive. I’m also not positive that this is a bug with Phoenix and not Ecto. The following is my proposed bug report. Please let me know whether I’m on the right track here with this being a bug and if so whether it’s with Phoenix or Ecto. If not, please help me understand what I’m doing wrong. Thanks.

Environment

  • Elixir version (elixir -v): 1.5.2
  • Phoenix version (mix deps): 1.3.0
  • NodeJS version (node -v): 8.9.0
  • NPM version (npm -v): 5.5.1
  • Operating system: macOS High Sierra 10.13
  • PostgreSQL version: 9.6.3
  • Ecto version: 2.2.6

Steps to reproduce

  1. mix phx.new bug
  2. mix phx.gen.json Core Bar bars date:date
  3. Uncomment the /api scope in router.ex and paste the following in: resources "/bars", BarController, except: [:new, :edit]
  4. mix test

Expected behavior

All tests (auto-generated) pass with no failures.

Actual behavior

➜  bug mix test
....

  1) test create bar renders bar when data is valid (BugWeb.BarControllerTest)
     test/bug_web/controllers/bar_controller_test.exs:28
     Assertion with == failed
     code:  assert json_response(conn, 200)["data"] == %{"id" => id, "date" => ~D"2010-04-17"}
     left:  %{"id" => 13, "date" => "2010-04-17"}
     right: %{"id" => 13, "date" => ~D[2010-04-17]}
     stacktrace:
       test/bug_web/controllers/bar_controller_test.exs:33: (test)

.

  2) test update bar renders bar when data is valid (BugWeb.BarControllerTest)
     test/bug_web/controllers/bar_controller_test.exs:47
     Assertion with == failed
     code:  assert json_response(conn, 200)["data"] == %{"id" => id, "date" => ~D"2011-05-18"}
     left:  %{"id" => 14, "date" => "2011-05-18"}
     right: %{"id" => 14, "date" => ~D[2011-05-18]}
     stacktrace:
       test/bug_web/controllers/bar_controller_test.exs:52: (test)

...........

Finished in 0.2 seconds
18 tests, 2 failures

Randomized with seed 221519

It might be the test suite that has a bug :slight_smile:

Generating a json endpoint with date ends up testing string date (from json) with a Date sigil…

One quick fix to make it pass is to change the test like this

assert json_response(conn, 200)["data"] == %{
        "id" => id,
        "date" => "2010-04-17"}

The date format does not really exists in JSON, and will be returned as string, but the test compares with a Date sigil ~D[2010-04-17]

That might be a bug, but I guess it is more in the test generator, for a json api, with a date field…

Yes I agree with you, it seems to be a bug with the generated test. I did manage to easily fix it like you suggested. However, I think this should be fixed because auto-generated tests shouldn’t fail immediately after generation. These tests are supposed to model good and correct testing practices.

Here You can find source code for json controller test generator

assert json_response(conn, 200)["data"] == %{
  "id" => id<%= for {key, val} <- schema.params.create do %>,
  "<%= key %>" => <%= inspect val %><% end %>}

It does not work with date, because the type date is not equal to the json response type