-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_target.py
222 lines (186 loc) · 6.55 KB
/
test_target.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from pathlib import Path
import holoviews as hv
import numpy as np
import pytest
from bokeh.document import Document
from panel.io.server import set_curdoc
from panel.param import Param
from lumen.sources import DerivedSource, FileSource
from lumen.state import state
from lumen.target import Target
from lumen.transforms import Astype
def test_view_controls(set_root):
set_root(str(Path(__file__).parent))
source = FileSource(tables={'test': 'sources/test.csv'})
views = {
'test': {
'type': 'hvplot', 'table': 'test', 'controls': ['x', 'y'],
'x': 'A', 'y': 'B', 'kind': 'scatter'
}
}
target = Target.from_spec({'source': source, 'views': views})
filter_panel = target.get_filter_panel()
param_pane = filter_panel[0][0]
assert isinstance(param_pane, Param)
assert param_pane.parameters == ['x', 'y']
assert len(target._cards) == 1
card = target._cards[0]
hv_pane = card._card[0][0]
isinstance(hv_pane.object, hv.Scatter)
assert hv_pane.object.kdims == ['A']
assert hv_pane.object.vdims == ['B']
param_pane._widgets['x'].value = 'C'
param_pane._widgets['y'].value = 'D'
isinstance(hv_pane.object, hv.Scatter)
assert hv_pane.object.kdims == ['C']
assert hv_pane.object.vdims == ['D']
def test_transform_controls(set_root):
set_root(str(Path(__file__).parent))
source = FileSource(tables={'test': 'sources/test.csv'})
views = {
'test': {
'type': 'hvplot',
'table': 'test',
'x': 'A',
'y': 'B',
'kind': 'scatter',
'transforms': [{
'type': 'sort',
'controls': ['by']
}]
}
}
doc = Document()
with set_curdoc(doc):
target = Target.from_spec({'views': views, 'source': source})
filter_panel = target.get_filter_panel()
param_pane = filter_panel[0][0]
assert isinstance(param_pane, Param)
assert param_pane.parameters == ['by']
assert len(target._cards) == 1
card = target._cards[0]
hv_pane = card._card[0][0]
isinstance(hv_pane.object, hv.Scatter)
assert hv_pane.object.kdims == ['A']
assert hv_pane.object.vdims == ['B']
assert np.array_equal(hv_pane.object['A'], np.array([0, 1, 2, 3, 4]))
assert np.array_equal(hv_pane.object['B'], np.array([0, 1, 0, 1, 0]))
param_pane.object.by = ['B']
assert np.array_equal(hv_pane.object['A'], np.array([0., 2, 4, 1, 3]))
assert np.array_equal(hv_pane.object['B'], np.array([0, 0, 0, 1, 1]))
def test_view_controls_facetted(set_root):
set_root(str(Path(__file__).parent))
source = FileSource(tables={'test': 'sources/test.csv'})
state.sources['test'] = source
views = {
'test': {
'type': 'hvplot', 'table': 'test', 'controls': ['x', 'y'],
'x': 'A', 'y': 'B', 'kind': 'scatter'
},
'test2': {
'type': 'hvplot', 'table': 'test',
'x': 'A', 'y': 'B', 'kind': 'scatter'
}
}
spec = {
'source': 'test',
'facet': {'by': 'C'},
'views': views
}
target = Target.from_spec(spec, sources={'test': source})
filter_panel = target.get_filter_panel()
param_pane = filter_panel[3][0]
assert isinstance(param_pane, Param)
assert param_pane.parameters == ['x', 'y']
assert len(target._cards) == 5
for card in target._cards:
hv_pane = card._card[0][0]
isinstance(hv_pane.object, hv.Scatter)
assert hv_pane.object.kdims == ['A']
assert hv_pane.object.vdims == ['B']
param_pane._widgets['x'].value = 'C'
param_pane._widgets['y'].value = 'D'
for card in target._cards:
hv_pane = card._card[0][0]
isinstance(hv_pane.object, hv.Scatter)
assert hv_pane.object.kdims == ['C']
assert hv_pane.object.vdims == ['D']
def test_transform_controls_facetted(set_root):
set_root(str(Path(__file__).parent))
source = FileSource(tables={'test': 'sources/test.csv'})
derived = DerivedSource(source=source, transforms=[Astype(dtypes={'B': 'int'})])
views = {
'test': {
'type': 'hvplot',
'table': 'test',
'x': 'D',
'y': 'A',
'kind': 'scatter',
'transforms': [{
'type': 'sort',
'by': 'C',
'controls': ['ascending']
}]
},
}
spec = {
'source': 'test',
'facet': {'by': 'B'},
'views': views
}
doc = Document()
with set_curdoc(doc):
state.sources['test'] = derived
target = Target.from_spec(spec, sources={'test': derived})
filter_panel = target.get_filter_panel()
param_pane = filter_panel[3][0]
assert isinstance(param_pane, Param)
assert param_pane.parameters == ['ascending']
assert len(target._cards) == 2
card1, card2 = target._cards
hv_pane1 = card1._card[0][0]
isinstance(hv_pane1.object, hv.Scatter)
assert hv_pane1.object.kdims == ['D']
assert hv_pane1.object.vdims == ['A']
hv_pane2 = card2._card[0][0]
isinstance(hv_pane2.object, hv.Scatter)
assert hv_pane2.object.kdims == ['D']
assert hv_pane2.object.vdims == ['A']
assert np.array_equal(hv_pane1.object['A'], np.array([0, 2, 4]))
assert np.array_equal(hv_pane2.object['A'], np.array([1, 3]))
param_pane.object.ascending = False
assert np.array_equal(hv_pane1.object['A'], np.array([4, 2, 0]))
assert np.array_equal(hv_pane2.object['A'], np.array([3, 1]))
@pytest.mark.parametrize(
"layout,error",
[
([[0]], None),
([[0], [1]], ValueError),
([[0], [1, 2]], ValueError),
([{"test": 0}], None),
([{"test1": 0}], KeyError),
]
)
def test_layout_view(set_root, layout, error):
set_root(str(Path(__file__).parent))
source = FileSource(tables={'test': 'sources/test.csv'})
derived = DerivedSource(source=source, transforms=[Astype(dtypes={'B': 'int'})])
views = {
'test': {
'type': 'table',
'table': 'test',
},
}
spec = {
'source': 'test',
'views': views,
'layout': layout
}
doc = Document()
with set_curdoc(doc):
state.sources['test'] = derived
if error is not None:
with pytest.raises(error):
Target.from_spec(spec, sources={'test': derived})
else:
Target.from_spec(spec, sources={'test': derived})