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

nested type cast fail #118

Merged
merged 1 commit into from
Aug 22, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- fix array casts with `Ch` subtype https://github.com/plausible/ch/pull/118

## 0.2.0 (2023-07-28)

- move loading and dumping from `Ch` type to the adapter https://github.com/plausible/ch/pull/112
Expand Down
2 changes: 1 addition & 1 deletion lib/ch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ defmodule Ch do
end
end

def cast(value, {:array, _type} = array), do: Ecto.Type.cast(array, value)
def cast(value, {:array, type}), do: Ecto.Type.cast({:array, type(type)}, value)
def cast(value, {:nullable, type}), do: cast(value, type)
def cast(value, {:low_cardinality, type}), do: cast(value, type)
def cast(value, {:simple_aggregate_function, _name, type}), do: cast(value, type)
Expand Down
27 changes: 27 additions & 0 deletions test/ch/ecto_type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,33 @@ defmodule Ch.EctoTypeTest do
assert {:ok, "something"} = Ecto.Type.load(type, "something")
end

test "SimpleAggregateFunction(groupArrayArray, Array(DateTime('UTC')))" do
assert {:parameterized, Ch,
{:simple_aggregate_function, "groupArrayArray", {:array, {:datetime, "UTC"}}}} =
type =
Ecto.ParameterizedType.init(Ch,
type: "SimpleAggregateFunction(groupArrayArray, Array(DateTime('UTC')))"
)

assert Ecto.Type.type(type) == type

assert Ecto.Type.format(type) ==
"#Ch<SimpleAggregateFunction(groupArrayArray, Array(DateTime('UTC')))>"

assert {:ok, [~U[2022-11-24 11:57:23Z]]} = Ecto.Type.cast(type, [~U[2022-11-24 11:57:23Z]])
assert {:ok, [~U[2022-11-24 11:57:23Z]]} = Ecto.Type.cast(type, ["2022-11-24 11:57:23Z"])
assert {:ok, []} = Ecto.Type.cast(type, [])
assert {:ok, nil} = Ecto.Type.cast(type, nil)

assert :error = Ecto.Type.cast(type, [~c"something"])
assert :error = Ecto.Type.cast(type, [:something])
assert :error = Ecto.Type.cast(type, [123])
assert :error = Ecto.Type.cast(type, 123)

assert {:ok, "no-op"} = Ecto.Type.dump(type, "no-op")
assert {:ok, "no-op"} = Ecto.Type.load(type, "no-op")
end

# TODO check size?
test "FixedString(3)" do
assert {:parameterized, Ch, {:fixed_string, 3}} =
Expand Down