enso/test/Visualization_Tests/src/Scatter_Plot_Spec.enso

127 lines
5.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from Standard.Base import all
from Standard.Table import Table, Column
import Standard.Visualization.Scatter_Plot
from Standard.Test import Test, Test_Suite
import Standard.Test.Extensions
import project
spec =
expect_text text axis_expected_text data_expected_text =
json = Json.parse text
json.field_names.should_equal ['data', 'axis']
expect_text = '{"axis": ' + axis_expected_text + ', "data": ' + data_expected_text + '}'
expected_result = Json.parse expect_text
json.should_equal expected_result
expect value axis_expected_text data_expected_text =
text = Scatter_Plot.process_to_json_text value
expect_text text axis_expected_text data_expected_text
index = Scatter_Plot.index_name
axis label = JS_Object.from_pairs [['label',label]]
labels x y = JS_Object.from_pairs [['x', axis x], ['y', axis y]] . to_text
no_labels = 'null'
Test.group "Scatter Plot Visualization" <|
Test.specify "plots first column if none recognized" <|
header = ['α', 'ω']
row_1 = [11 , 10 ]
row_2 = [21 , 20 ]
table = Table.from_rows header [row_1, row_2]
expect table (labels index 'α') '[{"x":0,"y":11},{"x":1,"y":21}]'
Test.specify "plots 'y' against indices when no 'x' recognized" <|
header = ['α', 'y']
row_1 = [11 , 10 ]
row_2 = [21 , 20 ]
table = Table.from_rows header [row_1, row_2]
expect table (labels index 'y') '[{"x":0,"y":10},{"x":1,"y":20}]'
Test.specify "recognizes all relevant columns" <|
header = ['x' , 'y' , 'size' , 'shape' , 'label' , 'color' ]
row_1 = [11 , 10 , 50 , 'square' , 'label' , 'ff0000']
table = Table.from_rows header [row_1]
expect table (labels 'x' 'y') '[{"color":"ff0000","label":"label","shape":"square","size":50,"x":11,"y":10}]'
Test.specify "is case-insensitive" <|
header = ['X' , 'Y' , 'Size' , 'Shape' , 'Label' , 'Color' ]
row_1 = [11 , 10 , 50 , 'square' , 'label' , 'ff0000']
table = Table.from_rows header [row_1]
expect table (labels 'X' 'Y') '[{"color":"ff0000","label":"label","shape":"square","size":50,"x":11,"y":10}]'
Test.specify "uses first unrecognized numeric column as `y` fallback" <|
header = ['x' , 'size' , 'name' , 'z' , 'ω']
row_1 = [11 , 50 , 'circul' , 20 , 30]
table = Table.from_rows header [row_1]
expect table (labels 'x' 'z') '[{"size":50,"x":11,"y":20}]'
Test.specify "provided only recognized columns" <|
header = ['x', 'y' , 'bar' , 'size']
row_1 = [11 , 10 , 'aa' , 40 ]
row_2 = [21 , 20 , 'bb' , 50 ]
table = Table.from_rows header [row_1, row_2]
expect table (labels 'x' 'y') '[{"size":40,"x":11,"y":10},{"size":50,"x":21,"y":20}]'
Test.specify "provided only recognized columns within bounds" <|
header = ['x', 'y' , 'bar' , 'size']
row_1 = [1 , 1 , '11' , 30 ]
row_2 = [11 , 10 , 'aa' , 40 ]
row_3 = [21 , 20 , 'bb' , 50 ]
row_4 = [31 , 30 , 'cc' , 60 ]
table = Table.from_rows header [row_1, row_2, row_3, row_4]
bounds = [0,5,25,25]
text = Scatter_Plot.process_to_json_text table bounds
expect_text text (labels 'x' 'y') '[{"size":40,"x":11,"y":10},{"size":50,"x":21,"y":20}]'
Test.specify "used default index for `x` if none set" <|
header = [ 'y' , 'bar' , 'size']
row_1 = [ 10 , 'aa' , 40 ]
row_2 = [ 20 , 'bb' , 50 ]
table = Table.from_rows header [row_1, row_2]
expect table (labels index 'y') '[{"size":40,"x":0,"y":10},{"size":50,"x":1,"y":20}]'
Test.specify "using indices for x if given a vector" <|
vector = [0,10,20]
expect vector no_labels '[{"x":0,"y":0},{"x":1,"y":10},{"x":2,"y":20}]'
Test.specify "limit the number of elements" <|
vector = [0,10,20,30]
text = Scatter_Plot.process_to_json_text vector limit=2
json = Json.parse text
json.field_names.should_equal ['data','axis']
data = json.get 'data'
data.should_be_a Vector
data.length . should_equal 2
Test.specify "limit the number of squared elements" <|
vector = (-15).up_to 15 . map (x -> x * x)
text = Scatter_Plot.process_to_json_text vector limit=10
json = Json.parse text
json.field_names.should_equal ['data','axis']
data = json.get 'data'
data.should_be_a Vector
data.length . should_equal 10
(data.take (First 3)).to_text . should_equal '[{"x":0,"y":225}, {"x":29,"y":196}, {"x":15,"y":0}]'
Test.specify "filter the elements" <|
vector = [0,10,20,30]
bounds = [0,5,10,25]
text = Scatter_Plot.process_to_json_text vector bounds
expect_text text no_labels '[{"x":1,"y":10},{"x":2,"y":20}]'
Test.specify "using indices for x if given a column" <|
column = Column.from_vector 'some_col' [10,2,3]
expect column (labels 'index' 'some_col') '[{"x":0,"y":10},{"x":1,"y":2},{"x":2,"y":3}]'
Test.specify "using indices for x if given a range" <|
value = 2.up_to 5
expect value no_labels '[{"x":0,"y":2},{"x":1,"y":3},{"x":2,"y":4}]'
main = Test_Suite.run_main spec