New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Datetime serialization differs between params_for/2
and string_params_for/2
#362
Comments
Same constatation for us at work... Something like this could lead us to give up on using ex_machina. It would be great if |
@christianjgreen thanks for opening this issue. I see why that is a bit surprising.
I originally thought that changing this behavior to match that of If you consider that I just took a look at how Phoenix's form helper datetime_select sends params to a controller, and I see them coming through as a map like this, "born_at" => %{
"day" => "3",
"hour" => "8",
"minute" => "3",
"month" => "2",
"second" => "2",
"year" => "2018"
}, So I think being able to test the controller like this is a valid use case, test "sets user's birth datetime", %{conn: conn} do
user = string_params_for(:user, born_at: ~D[2018-11-15 11:00:00Z])
conn |> post(path_to_user(conn, :create, %{"user" => user})
# some assertion I tend to use |
@germsvel Yes, the use case is valid, but when it comes to build an api doc basing on tests the devs who use this api start sending "as it is" or complaining about bad api. |
Just revisiting this. I think if we wanted to change how |
@germsvel take a look at my PR - non breaking change with config option. |
A simple hack is encode and decode to json: def json_decoded_for(factory_name, attrs \\ %{}) do
factory_name
|> params_for(attrs)
|> json_decoded()
end
def json_decoded_with_assocs(factory_name, attrs \\ %{}) do
factory_name
|> params_with_assocs(attrs)
|> json_decoded()
end
defp json_decoded(map) do
map
|> Jason.encode!
|> Jason.decode!
end |
…t a breaking change yet (#420) Resolves #362 #362 This PR introduces a configuration option to preserve dates when using the string_params_for function, making it a non breaking change for now. Add the following to your config/test.exs file to enable this: `config :ex_machina, preserve_dates: true`
Given a factory that produces a struct
Foo
with a UTC datetime fieldpublished_at
, the follow is produced.params_for(:foo)
->
%Foo{published_at: ~U[2019-08-04 17:25:42.638664Z]}
string_params_for(:foo)
->
%Foo{"published_at" => %{ "calendar" => Calendar.ISO, "day" => 4, "hour" => 17, "microsecond" => {657863, 6}, "minute" => 25, "month" => 8, "second" => 42, "std_offset" => 0, "time_zone" => "Etc/UTC", "utc_offset" => 0, "year" => 2019, "zone_abbr" => "UTC" }}
I was assuming this function would return the same formatted datetime; is this correct?
The text was updated successfully, but these errors were encountered: