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

Validate VegaLite data points and automatically convert to map #13

Merged
merged 1 commit into from
Jun 16, 2021
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
10 changes: 8 additions & 2 deletions lib/kino/vega_lite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ defmodule Kino.VegaLite do
def push(widget, data_point, opts \\ []) do
dataset = opts[:dataset]
window = opts[:window]

data_point = Map.new(data_point)

GenServer.cast(widget.pid, {:push, dataset, [data_point], window})
end

Expand All @@ -80,10 +83,13 @@ defmodule Kino.VegaLite do
See `push/3` for more details.
"""
@spec push_many(t(), list(map()), keyword()) :: :ok
def push_many(widget, data, opts \\ []) do
def push_many(widget, data_points, opts \\ []) when is_list(data_points) do
dataset = opts[:dataset]
window = opts[:window]
GenServer.cast(widget.pid, {:push, dataset, data, window})

data_points = Enum.map(data_points, &Map.new/1)

GenServer.cast(widget.pid, {:push, dataset, data_points, window})
end

@doc """
Expand Down
29 changes: 29 additions & 0 deletions test/kino/vega_lite_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ defmodule Kino.VegaLiteTest do
assert_receive {:push, %{data: [%{x: 1, y: 1}], dataset: "points", window: nil}}
end

test "push/3 converts keyword list to map" do
widget = start_widget()

connect_self(widget)

Kino.VegaLite.push(widget, x: 1, y: 1)
assert_receive {:push, %{data: [%{x: 1, y: 1}], dataset: nil, window: nil}}
end

test "push/3 raises if an invalid data type is given" do
widget = start_widget()

connect_self(widget)

assert_raise Protocol.UndefinedError, ~r/"invalid"/, fn ->
Kino.VegaLite.push(widget, "invalid")
end
end

test "sends current data after initial connection" do
widget = start_widget()
Kino.VegaLite.push(widget, %{x: 1, y: 1})
Expand Down Expand Up @@ -50,6 +69,16 @@ defmodule Kino.VegaLiteTest do
assert_receive {:push, %{data: ^points, dataset: nil, window: nil}}
end

test "push_many/3 raises if an invalid data type is given" do
widget = start_widget()

connect_self(widget)

assert_raise Protocol.UndefinedError, ~r/"invalid"/, fn ->
Kino.VegaLite.push_many(widget, ["invalid"])
end
end

test "clear/2 pushes empty data" do
widget = start_widget()

Expand Down