Skip to content
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

feat(ecto_adapter): cast maybe dates to date #78

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/snowflex/ecto/ecto_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ defmodule Snowflex.EctoAdapter do
def loaders(:id, type), do: [&int_decode/1, type]
def loaders(:time, type), do: [&time_decode/1, type]
def loaders(:time_usec, type), do: [&time_decode/1, type]

# NB: Handles instances where date may be wrapped in an additional call, such as `Ecto.Query.API.max/1`
def loaders({:maybe, :date}, type), do: [&date_decode/1, type]
def loaders(_, type), do: [type]

def dumpers(:binary, type), do: [type, &binary_encode/1]
Expand Down
9 changes: 9 additions & 0 deletions test/snowflex_ecto_adapter_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule SnowflexEctoAdapterTest do
use ExUnit.Case

test "handles casting maybe date type to date" do
assert [date_decode, {:maybe, :date}] = Snowflex.EctoAdapter.loaders({:maybe, :date}, {:maybe, :date})
assert is_function(date_decode, 1)
assert {:ok, ~D[2023-10-23]} = date_decode.("2023-10-23")
end
end
2 changes: 1 addition & 1 deletion test/snowflex_sqlite_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule SnowflexSqliteTest do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
end

test "can crete schema" do
test "can create schema" do
Repo.insert!(%TestSchema{x: 1, y: 2, z: 3})

assert [%TestSchema{x: 1, y: 2, z: 3}] = Repo.all(TestSchema)
Expand Down