From 35cd950949b3a8cb7422ccc40bdd6fe6b50823b3 Mon Sep 17 00:00:00 2001 From: Cristine Guadelupe Date: Mon, 11 Sep 2023 21:25:46 +0700 Subject: [PATCH] Tests for export/download (#107) --- test/kino/explorer_test.exs | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/test/kino/explorer_test.exs b/test/kino/explorer_test.exs index 4c137a8..527ca8a 100644 --- a/test/kino/explorer_test.exs +++ b/test/kino/explorer_test.exs @@ -334,4 +334,72 @@ defmodule Kino.ExplorerTest do name: "Lazy - DataFrame" } = data end + + test "supports export" do + df = Explorer.DataFrame.new(%{n: Enum.to_list(1..25)}) + + widget = Kino.Explorer.new(df) + data = connect(widget) + + assert %{ + features: [:export, :pagination, :sorting], + content: %{ + page: 1, + max_page: 3, + data: [["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]] + } + } = data + + for format <- ["CSV", "NDJSON", "Parquet"] do + extension = ".#{String.downcase(format)}" + push_event(widget, "download", %{"format" => format}) + assert_receive({:event, "download_content", {:binary, exported, data}, _}) + assert %{format: ^extension} = exported + assert is_binary(data) + end + end + + test "supports export for lazy data frames" do + df = Explorer.DataFrame.new(%{n: Enum.to_list(1..25)}, lazy: true) + + widget = Kino.Explorer.new(df) + data = connect(widget) + + assert %{ + features: [:export, :pagination, :sorting], + content: %{ + page: 1, + max_page: nil, + data: [["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]] + } + } = data + + for format <- ["CSV", "NDJSON", "Parquet"] do + extension = ".#{String.downcase(format)}" + push_event(widget, "download", %{"format" => format}) + assert_receive({:event, "download_content", {:binary, exported, data}, _}) + assert %{format: ^extension} = exported + assert is_binary(data) + end + end + + test "export to" do + df = Explorer.DataFrame.new(%{n: Enum.to_list(1..25)}) + + for format <- ["CSV", "NDJSON", "Parquet"] do + exported = Kino.Explorer.export_data(%{df: df}, format) + extension = ".#{String.downcase(format)}" + assert %{extension: ^extension} = exported + end + end + + test "export to for lazy data frames" do + df = Explorer.DataFrame.new(%{n: Enum.to_list(1..25)}, lazy: true) + + for format <- ["CSV", "NDJSON", "Parquet"] do + exported = Kino.Explorer.export_data(%{df: df}, format) + extension = ".#{String.downcase(format)}" + assert %{extension: ^extension} = exported + end + end end