Repository: phoenixframework/phoenix

Issue #2272: Tests for JSON resource failing [%NaiveDateTime{...}]
Created: 2017-05-01T10:04:51Z
Note: The issue appears to be value of microsecond inside of the %NaiveDateTime{...} struct.
Environment

Elixir version (elixir -v): 1.4.2
Phoenix version (mix deps): 1.3.0-rc

Expected behavior

mix phx.new blog --database mysql --no-brunch --no-html
cd blog/
mix ecto.create
mix phx.gen.json Content Post posts title:string body:text
Add resources "/posts", PostController, except: [:new, :edit] to router inside of api scope.
mix ecto.migrate
mix test
Everything passes.

Actual behavior
Steps 1-7 work, 8 fails with:
1) test update_post/2 with invalid data returns error changeset (Blog.ContentTest)
     test/content_test.exs:44
     Assertion with == failed
     code:  post == Content.get_post!(post.id())
     left:  %Blog.Content.Post{__meta__: #Ecto.Schema.Metadata<:loaded, "content_posts">, body: "some body", id: 12, title: "some title", inserted_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {598611, 6}, second: 20}, updated_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {602694, 6}, second: 20}}
     right: %Blog.Content.Post{__meta__: #Ecto.Schema.Metadata<:loaded, "content_posts">, body: "some body", id: 12, title: "some title", inserted_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {0, 6}, second: 21}, updated_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {0, 6}, second: 21}}
     stacktrace:
       test/content_test.exs:47: (test)

.

  2) test list_posts/1 returns all posts (Blog.ContentTest)
     test/content_test.exs:16
     Assertion with == failed
     code:  Content.list_posts() == [post]
     left:  [%Blog.Content.Post{__meta__: #Ecto.Schema.Metadata<:loaded, "content_posts">, body: "some body", id: 14, title: "some title", inserted_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {0, 6}, second: 21}, updated_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {0, 6}, second: 21}}]
     right: [%Blog.Content.Post{__meta__: #Ecto.Schema.Metadata<:loaded, "content_posts">, body: "some body", id: 14, title: "some title", inserted_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {620130, 6}, second: 20}, updated_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {620135, 6}, second: 20}}]
     stacktrace:
       test/content_test.exs:18: (test)

..

  3) test get_post! returns the post with given id (Blog.ContentTest)
     test/content_test.exs:21
     Assertion with == failed
     code:  Content.get_post!(post.id()) == post
     left:  %Blog.Content.Post{__meta__: #Ecto.Schema.Metadata<:loaded, "content_posts">, body: "some body", id: 16, title: "some title", inserted_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {0, 6}, second: 21}, updated_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {0, 6}, second: 21}}
     right: %Blog.Content.Post{__meta__: #Ecto.Schema.Metadata<:loaded, "content_posts">, body: "some body", id: 16, title: "some title", inserted_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {624034, 6}, second: 20}, updated_at: %NaiveDateTime{calendar: Calendar.ISO, day: 1, hour: 9, minute: 51, month: 5, year: 2017, microsecond: {624040, 6}, second: 20}}
     stacktrace:
       test/content_test.exs:23: (test)

........

Duplicate issue #2700: Tests error with json generator and naive_datetime
Created: 2018-01-03T12:08:14Z
Environment

Elixir version (elixir -v): 1.4.1
Phoenix version (mix deps): 1.3.0
NodeJS version (node -v): 7.4.0
NPM version (npm -v): 5.6.0
Operating system: macOS

I run the following commands:
mix phx.new date_bug && cd date_bug && mix ecto.create
mix phx.gen.json X Y z date:naive_datetime
# edit the `router.ex` file appropriately, following:
# Add the resource to your :api scope in lib/date_bug_web/router.ex:
#
#    resources "/z", YController, except: [:new, :edit]
mix ecto.migrate
Expected behaviour
mix test # to not error
Actual behavior
mix test # errors
See the error on travis here: https://travis-ci.org/shouston3/date_bug/jobs/324551671#L609
Sorry if I've made any mistakes with raising this issue.
Thanks for all your amazing work @chrismccord @josevalim

Closing comment (2018-01-03T16:08:27Z): Are you using mysql by chance? This sounds like a dup of #2272 and #2152 . Let me know and I will take a look. Thanks!