enso/test/Visualization_Tests/src/Table_Spec.enso
Radosław Waśko b513839418
Refactor create_database_table into Connection.create_table and select_into_database_table, implement Set. (#6925)
First part for #6498 - refactoring of the upload infrastructure, in preparation for `update_database_table`.

Implemented a `Set` data structure which was long needed.

The APIs are added and an initial implementation is created, but it is not complete - but it has grown significantly already so the remaining implementation will be done as a separate PR.

Adds some basic ability for a function to ensure that it is only executed from within a transaction.
2023-06-06 10:36:05 +00:00

115 lines
5.4 KiB
Plaintext

from Standard.Base import all
from Standard.Table import Table, Aggregate_Column, Value_Type
from Standard.Database import all
import Standard.Database.Data.Table.Table as Database_Table
import Standard.Visualization.Table.Visualization
import Standard.Visualization.Helpers
import Standard.Visualization.Id.Id
from Standard.Test import Test, Test_Suite
import Standard.Test.Extensions
import Standard.Base.Errors.Common.Type_Error
polyglot java import java.util.UUID
type Foo
Value x
to_js_object : JS_Object
to_js_object self = JS_Object.from_pairs [["x", self.x]]
visualization_spec connection =
in_mem = Table.new [["A", ['a', 'a', 'a']], ["B", [2, 2, 3]], ["C", [3, 5, 6]]]
t = in_mem.select_into_database_table connection "T" primary_key=Nothing temporary=True
make_json header data all_rows ixes_header ixes =
p_header = ["header", header]
p_data = ["data", data]
p_all_rows = ["all_rows_count", all_rows]
p_ixes = ["indices", ixes]
p_ixes_header = ["indices_header", ixes_header]
pairs = [p_header, p_data, p_all_rows, p_ixes, p_ixes_header, ["type", "Table"]]
JS_Object.from_pairs pairs . to_text
Test.group "Table Visualization" <|
Test.specify "should forward internal errors" <|
bad_table_test =
bad_table = Database_Table.Value Nothing Nothing Nothing Nothing
result = Panic.catch Any (Visualization.prepare_visualization bad_table 2) caught_panic->
caught_panic.payload
message = "Method `set_limit` of type Nothing could not be found."
result.to_display_text . should_equal message
Panic.catch Type_Error bad_table_test (x -> x.payload) . should_be_a Type_Error
Test.specify "should visualize database tables" <|
vis = Visualization.prepare_visualization t 1
json = make_json header=["A", "B", "C"] data=[['a'], [2], [3]] all_rows=3 ixes_header=[] ixes=[]
vis . should_equal json
Test.specify "should visualize database columns" <|
vis = Visualization.prepare_visualization (t.at "A") 2
json = make_json header=["A"] data=[['a', 'a']] all_rows=3 ixes_header=[] ixes=[]
vis . should_equal json
g = t.aggregate [Aggregate_Column.Group_By "A", Aggregate_Column.Group_By "B", Aggregate_Column.Average "C"] . at "Average C"
vis2 = Visualization.prepare_visualization g 1
json2 = make_json header=["Average C"] data=[[4.0]] all_rows=2 ixes_header=[] ixes=[]
vis2 . should_equal json2
t2 = Table.new [["A", [1, 2, 3]], ["B", [4, 5, 6]], ["C", [7, 8, 9]]]
Test.specify "should visualize dataframe tables" <|
vis = Visualization.prepare_visualization t2 1
json = make_json header=["A", "B", "C"] data=[[1], [4], [7]] all_rows=3 ixes_header=["#"] ixes=[[0]]
vis . should_equal json
Test.specify "should visualize dataframe columns" <|
vis = Visualization.prepare_visualization (t2.at "A") 2
json = make_json header=["A"] data=[[1, 2]] all_rows=3 ixes_header=["#"] ixes=[[0, 1]]
vis . should_equal json
Test.specify "should handle Vectors" <|
vis = Visualization.prepare_visualization [1, 2, 3] 2
json = JS_Object.from_pairs [["type", "Vector"], ["all_rows_count", 3], ["json", [1, 2]]]
vis . should_equal json.to_text
vis2 = Visualization.prepare_visualization [[1, 2], [3, 4]] 2
json2 = JS_Object.from_pairs [["type", "Matrix"], ["all_rows_count", 2], ["json", [[1, 2], [3, 4]]], ["column_count", 2]]
vis2 . should_equal json2.to_text
Test.specify "should handle Arrays" <|
vis = Visualization.prepare_visualization ([1, 2, 3] . to_array) 2
json = JS_Object.from_pairs [["type", "Vector"], ["all_rows_count", 3], ["json", [1, 2]]]
vis . should_equal json.to_text
Test.specify "should handle other datatypes" <|
vis = Visualization.prepare_visualization (Foo.Value 42) 2
json = JS_Object.from_pairs [["json", JS_Object.from_pairs [["_display_text_", (Foo.Value 42).to_display_text],["x", 42]]]]
vis . should_equal json.to_text
Test.specify "should visualize value type info" <|
make_json vt =
js_object = vt.to_js_object
pairs = [["_display_text_", vt.to_display_text]] + vt.to_js_object.field_names.map f-> [f, js_object.get f]
JS_Object.from_pairs [["json", JS_Object.from_pairs pairs]] . to_text
Visualization.prepare_visualization Value_Type.Boolean . should_equal (make_json Value_Type.Boolean)
Visualization.prepare_visualization Value_Type.Float . should_equal (make_json Value_Type.Float)
Visualization.prepare_visualization Value_Type.Decimal . should_equal (make_json Value_Type.Decimal)
Visualization.prepare_visualization Value_Type.Char . should_equal (make_json Value_Type.Char)
Visualization.prepare_visualization Value_Type.Unsupported_Data_Type . should_equal (make_json Value_Type.Unsupported_Data_Type)
spec =
enso_project.data.create_directory
file = enso_project.data / "sqlite_test.db"
file.delete_if_exists
connection = Database.connect (SQLite file)
visualization_spec connection
connection.close
file.delete
main = Test_Suite.run_main spec