lsaville
Hi everyone!
I’m trying to follow the tutorial from thoughtbot here to create my first phoenix json api endpoint. The code for the test is following:
# Create this test in test/controllers/todo_controller_test.exs
defmodule Todos.TodoControllerTest do
use Todos.ConnCase
test "#index renders a list of todos" do
conn = build_conn()
todo = insert(:todo)
conn = get conn, todo_path(conn, :index)
assert json_response(conn, 200) == %{
"todos" => [%{
"title" => todo.title,
"description" => todo.description,
"inserted_at" => Ecto.DateTime.to_iso8601(todo.inserted_at),
"updated_at" => Ecto.DateTime.to_iso8601(todo.updated_at)
}]
}
end
end
When I run the test I get the following error:
** (FunctionClauseError) no function clause matching in Ecto.DateTime.to_iso8601/1
stacktrace:
(ecto) lib/ecto/date_time.ex:584: Ecto.DateTime.to_iso8601(~N[2017-03-23 19:55:47.300335])
test/controllers/todo_controller_test.exs:14: (test)
I’ve completed the tutorial up to the point where this should be passing. If I change the test so that I’m not attempting to format the date I get the following:
Assertion with == failed
code: json_response(conn, 200) == %{"todos" => [%{"title" => todo.title(), "description" => todo.description(), "inserted_at" => "Frank", "updated_at" => "Frank"}]}
lhs: %{"todos" => [%{"description" => "List of steps I need to complete", "title" => "Something I need to do", "inserted_at" => "2017-03-23T19:56:39.484442", "updated_at" => "2017-03-23T19:56:39.497129"}]}
rhs: %{"todos" => [%{"description" => "List of steps I need to complete", "title" => "Something I need to do", "inserted_at" => "Frank", "updated_at" => "Frank"}]}
stacktrace:
test/controllers/todo_controller_test.exs:10: (test)
Which seems to make sense. The problem is with Ecto.DateTime.to_iso and it’s no matching clause. The ecto code is this:
def to_iso8601(%Ecto.DateTime{year: year, month: month, day: day,
hour: hour, min: min, sec: sec, usec: usec}) do
str = zero_pad(year, 4) <> "-" <> zero_pad(month, 2) <> "-" <> zero_pad(day, 2) <> "T" <>
zero_pad(hour, 2) <> ":" <> zero_pad(min, 2) <> ":" <> zero_pad(sec, 2)
if is_nil(usec) or usec == 0 do
str
else
str <> "." <> zero_pad(usec, 6)
end
end
So possibly the ~N[2017-03-23 19:57:48.500354] is not matching with the
%Ecto.DateTime{year: year, month: month, day: day,
hour: hour, min: min, sec: sec, usec: usec}
in the ecto code? It is probably something small that I’m unaware of but I’ve very new to elixir and phoenix and would appreciate any advice on how to resolve this. Thanks so much for your time!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
gon782
Having looked at it only a little bit it seems you’re creating a NaiveDateTime and what this function is looking for is actually a DateTime.
Ecto.DateTimehas a function calledcast/1that you can use to convert it to an actualDateTime. You can then pass that to the function you’re trying to use. Note, though, that what you have in your sigil is what you’re going to get back.Also, I have to ask: Who is Frank and how did he end up in your
{inserted,updated}_atfields?lsaville
Gonz Thanks! I’ll check out that
cast/1I was just trying to figure out what the~N[2017-03-23 20:58:23.586989]was all about. Is NaiveDateTime what is automatically used in Phoenix or is it configurable?Frank is no one I know, just something to trip the test up so I can see what it gives me.
Obviously I have a lot of ground to cover in my knowledge in Elixir and Phoenix. Thanks for the tips!
OvermindDL1
If it is a NaiveDateTime, then why not use
NaiveDateTime.to_iso8601/1?lsaville
That works perfectly. Awesome tip, removes an extra step converting and discarding the
:okin the tuple that comes back fromcast/1.gon782
In my rush I looked for this in the
Ectonamespace instead of checking theElixirnamespace.gon782
I believe it’s the default timestamp type used by Ecto. You should be able to set it in your schema by using the following in your schema:
@timestamps_opts [type: :utc_datetime]You can find more info here.
The difference being that
DateTime(which corresponds to:utc_datetime) provides timezone info, which makes it more usable for conversions, etc.There have been some great talks and work on this by Lau Taarnskov ((“lau” on GitHub). I recommend them, as it’s a surprisingly interesting area with a lot of pitfalls.
lsaville
Great info thanks so much. I can see now how I got myself into the confusion by using
timestampswhich defaults toNaiveDateTime