/
test_shape_controller.js
223 lines (188 loc) · 8.54 KB
/
test_shape_controller.js
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
223
requirejs([
'jquery',
'underscore',
'model',
'view',
'viewcontroller',
'three',
'shapecontroller',
'shape-editor',
'shapes'
], function($, _, model, DecompositionView, viewcontroller, THREE,
ShapeController, ShapeEditor, shapes) {
$(document).ready(function() {
var EmperorAttributeABC = viewcontroller.EmperorAttributeABC;
var DecompositionModel = model.DecompositionModel;
module('Shape Controller', {
setup: function() {
// setup function
this.shapesAvailable = ['Sphere', 'Diamond', 'Cone', 'Cylinder',
'Ring', 'Square', 'Icosahedron'];
this.sharedDecompositionViewDict = {};
// setup function
var data = {name: 'pcoa', sample_ids: ['PC.636', 'PC.635'],
coordinates: [[-0.276542, -0.144964, 0.066647, -0.067711,
0.176070, 0.072969, -0.229889, -0.046599],
[-0.237661, 0.046053, -0.138136, 0.159061,
-0.247485, -0.115211, -0.112864, 0.064794]],
percents_explained: [26.6887048633, 16.2563704022,
13.7754129161, 11.217215823,
10.024774995, 8.22835130237,
7.55971173665, 6.24945796136]};
var md_headers = ['SampleID', 'LinkerPrimerSequence', 'Treatment',
'DOB'];
var metadata = [
['PC.636', 'YATGCTGCCTCCCGTAGGAGT', 'Control', '20070314'],
['PC.635', 'YATGCTGCCTCCCGTAGGAGT', 'Fast', '20071112']
];
decomp = new DecompositionModel(data, md_headers, metadata);
var dv = new DecompositionView(decomp);
this.sharedDecompositionViewDict.scatter = dv;
data = {name: 'biplot', sample_ids: ['tax_1', 'tax_2'],
coordinates: [[-1, -0.144964, 0.066647, -0.067711, 0.176070,
0.072969, -0.229889, -0.046599],
[-0.237661, 0.046053, -0.138136, 0.159061,
-0.247485, -0.115211, -0.112864, 0.064794]],
percents_explained: [26.6887048633, 16.2563704022,
13.7754129161, 11.217215823, 10.024774995,
8.22835130237, 7.55971173665,
6.24945796136]};
md_headers = ['SampleID', 'Gram'];
metadata = [['tax_1', '1'], ['tax_2', '0']];
this.decomp = new DecompositionModel(data, md_headers, metadata);
this.dv = new DecompositionView(this.decomp);
this.sharedDecompositionViewDict.biplot = dv;
},
teardown: function() {
// teardown function
}
});
test('Shapes dropdown', function() {
var values = [];
shapes.$shapesDropdown.find('option').each(function() {
values.push($(this).attr('value'));
});
deepEqual(values, this.shapesAvailable);
});
test('Constructor tests', function(assert) {
var container = $('<div id="does-not-exist" style="height:11px; ' +
'width:12px"></div>');
assert.ok(ShapeController.prototype instanceof EmperorAttributeABC);
var controller = new ShapeController(container,
this.sharedDecompositionViewDict);
equal(controller.title, 'Shape');
var testColumn = controller.bodyGrid.getColumns()[0];
equal(testColumn.field, 'value');
});
test('Test getGeometry', function() {
var geom, range;
range = {'min': [-2, -1, -3], 'max': [3, 8, 9]};
geom = shapes.getGeometry('Sphere', range);
equal(geom.parameters.radius, 0.06);
geom = shapes.getGeometry('Square', range);
equal(geom.parameters.width, 0.12);
equal(geom.parameters.height, 0.12);
geom = shapes.getGeometry('Cone', range);
equal(geom.parameters.radiusTop, 0.06);
equal(geom.parameters.radiusBottom, 0);
equal(geom.parameters.height, 0.12);
geom = shapes.getGeometry('Icosahedron', range);
equal(geom.parameters.radius, 0.06);
geom = shapes.getGeometry('Diamond', range);
equal(geom.parameters.radius, 0.06);
geom = shapes.getGeometry('Ring', range);
equal(geom.parameters.innerRadius, 0.06 / 1.618033);
equal(geom.parameters.outerRadius, 0.06);
geom = shapes.getGeometry('Cylinder', range);
equal(geom.parameters.radiusTop, 0.06);
equal(geom.parameters.radiusBottom, 0.06);
equal(geom.parameters.height, 0.12);
});
test('Check getGeometry raises an exception with unknown shape',
function() {
var range = {'min': [-2, -1, -3], 'max': [3, 8, 9]};
throws(function() {
shapes.getGeometry('Geometry McGeometryface', range);
}, Error, 'Throw error if unknown shape given');
});
test('Testing setPlottableAttributes helper function', function(assert) {
// testing with one plottable
var idx = 0;
plottables = [{idx: idx}];
equal(this.dv.markers[idx].geometry.type, 'SphereGeometry');
equal(this.dv.markers[idx + 1].geometry.type, 'SphereGeometry');
ShapeController.prototype.setPlottableAttributes(this.dv, 'Square',
plottables);
equal(this.dv.markers[idx].geometry.type, 'PlaneGeometry');
equal(this.dv.markers[idx + 1].geometry.type, 'SphereGeometry');
equal(this.dv.needsUpdate, true);
// testing with multiple plottable
plottables = [{idx: idx}, {idx: idx + 1}];
ShapeController.prototype.setPlottableAttributes(this.dv, 'Cylinder',
plottables);
equal(this.dv.markers[idx].geometry.type, 'CylinderGeometry');
equal(this.dv.markers[idx + 1].geometry.type, 'CylinderGeometry');
equal(this.dv.needsUpdate, true);
});
test('Testing setPlottableAttributes unknown shape', function(assert) {
// testing with one plottable
plottables = [{0: 0}];
throws(function() {
ShapeController.prototype.setPlottableAttributes(this.dv, 'WEIRD',
plottables);
}, Error, 'Throw error if unknown shape given');
});
test('Testing toJSON', function() {
var container = $('<div id="does-not-exist" style="height:11px; ' +
'width:12px"></div>');
var controller = new ShapeController(container,
this.sharedDecompositionViewDict);
controller.setMetadataField('DOB');
var obs = controller.toJSON();
var exp = {category: 'DOB',
data: {'20070314': 'Sphere', '20071112': 'Sphere'}
};
deepEqual(obs, exp);
});
test('Testing fromJSON', function() {
var json = {'category': 'SampleID',
'data': {'PC.636': 'Square', 'PC.635': 'Sphere'}
};
var container = $('<div id="does-not-exist" style="height:11px; ' +
'width:12px"></div>');
var controller = new ShapeController(container,
this.sharedDecompositionViewDict);
controller.fromJSON(json);
var idx = 0;
equal(controller.decompViewDict.scatter.markers[idx].geometry.type,
'PlaneGeometry');
equal(controller.decompViewDict.scatter.markers[idx + 1].geometry.type,
'SphereGeometry');
});
test('Testing toJSON (null)', function() {
var container = $('<div id="does-not-exist" style="height:11px; ' +
'width:12px"></div>');
var controller = new ShapeController(container,
this.sharedDecompositionViewDict);
controller.setMetadataField(null);
var obs = controller.toJSON();
var exp = {category: null,
data: {}
};
deepEqual(obs, exp);
});
test('Testing fromJSON (null)', function() {
var json = {'category': null, 'data': {}};
var container = $('<div id="does-not-exist" style="height:11px; ' +
'width:12px"></div>');
var controller = new ShapeController(container,
this.sharedDecompositionViewDict);
controller.fromJSON(json);
var idx = 0;
equal(controller.decompViewDict.scatter.markers[idx].geometry.type,
'SphereGeometry');
equal(controller.decompViewDict.scatter.markers[idx + 1].geometry.type,
'SphereGeometry');
});
});
});