-
Notifications
You must be signed in to change notification settings - Fork 125
/
api_feature.test.js
396 lines (378 loc) · 12.9 KB
/
api_feature.test.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
var gdal = require('../lib/gdal.js');
var assert = require('chai').assert;
describe('gdal.Feature', function() {
afterEach(gc);
var ds, lyr, defn, fields, feature;
before(function() {
ds = gdal.open('', 'w', 'Memory');
lyr = ds.layers.create('', null, gdal.Point);
fields = [
new gdal.FieldDefn('id', gdal.OFTInteger),
new gdal.FieldDefn('name', gdal.OFTString),
new gdal.FieldDefn('value', gdal.OFTReal)
];
defn = new gdal.FeatureDefn();
defn.fields.add(fields);
lyr.fields.add(fields);
});
describe('constructor', function() {
describe('w/Layer', function() {
it('should create instance', function() {
new gdal.Feature(lyr);
});
it('instance should use fields from LayerDefn', function() {
var feature = new gdal.Feature(lyr);
assert.deepEqual(feature.fields.getNames(), lyr.fields.getNames());
});
it('should throw error if layer is destroyed', function() {
var ds = gdal.open('', 'w', 'Memory');
var lyr = ds.layers.create('', null, gdal.Point);
lyr.fields.add(fields);
ds.close();
assert.throws(function() {
new gdal.Feature(lyr);
});
});
it('should not throw error if layer is destroyed after feature is created', function() {
var ds = gdal.open('', 'w', 'Memory');
var lyr = ds.layers.create('', null, gdal.Point);
lyr.fields.add(fields);
var feature = new gdal.Feature(lyr);
ds.close();
assert.doesNotThrow(function() {
feature.defn.fields.getNames();
});
});
});
describe('w/FeatureDefn', function() {
it('should create instance', function() {
new gdal.Feature(defn);
});
it('instance should use fields from FeatureDefn', function() {
var feature = new gdal.Feature(defn);
assert.deepEqual(feature.fields.getNames(), defn.fields.getNames());
});
});
});
describe('instance', function() {
describe('"fid" property', function() {
describe('getter', function() {
it('should return integer', function() {
var feature = new gdal.Feature(defn);
assert.equal(feature.fid, -1);
});
});
describe('setter', function() {
it('should set fid', function() {
var feature = new gdal.Feature(defn);
feature.fid = 5;
assert.equal(feature.fid, 5);
});
});
});
describe('"defn" property', function() {
describe('getter', function() {
it('should return FeatureDefn', function() {
var feature = new gdal.Feature(defn);
assert.instanceOf(feature.defn, gdal.FeatureDefn);
});
});
describe('setter', function() {
it('should throw error', function() {
var feature = new gdal.Feature(defn);
assert.throws(function() {
feature.defn = null;
});
});
});
});
describe('"fields" property', function() {
describe('getter', function() {
it('should return FeatureFields', function() {
var feature = new gdal.Feature(defn);
assert.instanceOf(feature.fields, gdal.FeatureFields);
});
});
describe('setter', function() {
it('should throw error', function() {
var feature = new gdal.Feature(defn);
assert.throws(function() {
feature.fields = null;
}, /fields is a read-only property/);
});
});
describe('count()', function() {
it('should return an integer', function() {
var feature = new gdal.Feature(defn);
assert.equal(feature.fields.count(), 3);
});
});
describe('set()', function() {
describe('w/id argument', function() {
it('should properly set values', function() {
var feature = new gdal.Feature(defn);
feature.fields.set(0, 5);
feature.fields.set(1, 'test');
feature.fields.set(2, 3.14);
assert.equal(feature.fields.get(0), 5);
assert.equal(feature.fields.get(1), 'test');
assert.closeTo(feature.fields.get(2), 3.14, 0.0001);
});
it('should unset field if value is null', function() {
var feature = new gdal.Feature(defn);
feature.fields.set(1, 'test');
feature.fields.set(1, null);
assert.isNull(feature.fields.get(1));
});
it('should throw an error if id is out of range', function() {
var feature = new gdal.Feature(defn);
assert.throws(function() {
feature.fields.set(100, 'test');
});
});
});
describe('w/name argument', function() {
it('should properly set values', function() {
var feature = new gdal.Feature(defn);
feature.fields.set('id', 5);
feature.fields.set('name', 'test');
feature.fields.set('value', 3.14);
assert.equal(feature.fields.get('id'), 5);
assert.equal(feature.fields.get('name'), 'test');
assert.closeTo(feature.fields.get('value'), 3.14, 0.0001);
});
it('should unset field if value is null', function() {
var feature = new gdal.Feature(defn);
feature.fields.set('name', 'test');
feature.fields.set('name', null);
assert.isNull(feature.fields.get('name'));
});
it('should throw an error if field name does not exist', function() {
var feature = new gdal.Feature(defn);
assert.throws(function() {
feature.fields.set('bogus', 'test');
});
});
});
describe('w/array argument', function() {
it('should properly set all fields', function() {
var feature = new gdal.Feature(defn);
feature.fields.set([5, 'test', 3.14]);
assert.equal(feature.fields.get(0), 5);
assert.equal(feature.fields.get(1), 'test');
assert.closeTo(feature.fields.get(2), 3.14, 0.0001);
});
});
describe('w/object argument', function() {
it('should properly set all fields', function() {
var feature = new gdal.Feature(defn);
feature.fields.set({id:5, name:'test', value:3.14});
assert.equal(feature.fields.get(0), 5);
assert.equal(feature.fields.get(1), 'test');
assert.closeTo(feature.fields.get(2), 3.14, 0.0001);
});
});
});
describe('get()', function() {
describe('w/id argument', function() {
it('should properly get values', function() {
var feature = new gdal.Feature(defn);
feature.fields.set(0, 5);
feature.fields.set(1, 'test');
feature.fields.set(2, 3.14);
assert.equal(feature.fields.get(0), 5);
assert.equal(feature.fields.get(1), 'test');
assert.closeTo(feature.fields.get(2), 3.14, 0.0001);
});
it('should return unset fields as null', function() {
var feature = new gdal.Feature(defn);
assert.isNull(feature.fields.get(0));
assert.isNull(feature.fields.get(1));
assert.isNull(feature.fields.get(2));
});
it('should throw an error if id out of range', function() {
var feature = new gdal.Feature(defn);
assert.throws(function() {
feature.fields.get(100);
});
});
});
describe('w/name argument', function() {
it('should properly get/set values', function() {
var feature = new gdal.Feature(defn);
feature.fields.set('id', 5);
feature.fields.set('name', 'test');
feature.fields.set('value', 3.14);
assert.equal(feature.fields.get('id'), 5);
assert.equal(feature.fields.get('name'), 'test');
assert.closeTo(feature.fields.get('value'), 3.14, 0.0001);
});
it('should return unset fields as null', function() {
var feature = new gdal.Feature(defn);
assert.isNull(feature.fields.get('id'));
assert.isNull(feature.fields.get('name'));
assert.isNull(feature.fields.get('value'));
});
it('should throw an error if field name doesnt exist', function() {
var feature = new gdal.Feature(defn);
assert.throws(function() {
feature.fields.get('bogus');
});
});
});
});
describe('toObject()', function() {
it('should return the fields as a JSON object', function() {
var feature = new gdal.Feature(defn);
feature.fields.set([5, 'test', 3.14]);
var obj = feature.fields.toObject();
assert.equal(obj.id, 5);
assert.equal(obj.name, 'test');
assert.closeTo(obj.value, 3.14, 0.0001);
});
});
describe('toJSON()', function() {
it('should return the fields as a stringified JSON object', function() {
var feature = new gdal.Feature(defn);
feature.fields.set([5, 'test', 3.14]);
var obj = JSON.parse(feature.fields.toJSON());
assert.equal(obj.id, 5);
assert.equal(obj.name, 'test');
assert.closeTo(obj.value, 3.14, 0.0001);
});
});
describe('toArray()', function() {
it('should return an array of field values', function() {
var feature = new gdal.Feature(defn);
feature.fields.set([5, 'test', 3.14]);
var array = feature.fields.toArray();
assert.equal(array[0], 5);
assert.equal(array[1], 'test');
assert.closeTo(array[2], 3.14, 0.0001);
});
});
describe('forEach()', function() {
it('should return pass each value and key to callback', function() {
var feature = new gdal.Feature(defn);
var expected_keys = ['id', 'name', 'value'];
var expected_values = [5, 'test', 3.14];
var values = [];
var keys = [];
feature.fields.set(expected_values);
feature.fields.forEach(function(value, key) {
values.push(value);
keys.push(key);
});
assert.equal(keys.length, 3);
assert.deepEqual(keys, expected_keys);
assert.equal(values[0], expected_values[0]);
assert.equal(values[1], expected_values[1]);
assert.closeTo(values[2], expected_values[2], 0.0001);
});
});
describe('getNames()', function() {
it('should return an array of field names', function() {
var feature = new gdal.Feature(defn);
var expected_names = ['id', 'name', 'value'];
assert.deepEqual(feature.fields.getNames(), expected_names);
});
});
describe('indexOf()', function() {
it('should return index of field name', function() {
var feature = new gdal.Feature(defn);
assert.equal(feature.fields.indexOf('name'), 1);
});
});
describe('reset()', function() {
describe('w/no argument', function() {
it('should reset all fields to null', function() {
var feature = new gdal.Feature(defn);
feature.fields.set([5, 'test', 3.14]);
feature.fields.reset();
assert.isNull(feature.fields.get(0));
assert.isNull(feature.fields.get(1));
assert.isNull(feature.fields.get(2));
});
});
describe('w/object argument', function() {
it('should set fields from object and reset others', function() {
var feature = new gdal.Feature(defn);
feature.fields.set([5, 'test', 3.14]);
feature.fields.reset({name: 'reset'});
assert.isNull(feature.fields.get(0));
assert.equal(feature.fields.get(1), 'reset');
assert.isNull(feature.fields.get(2));
});
});
});
});
describe('clone()', function() {
it('should return new Feature', function() {
var feature = new gdal.Feature(defn);
var clone = feature.clone();
assert.instanceOf(clone, gdal.Feature);
assert.notEqual(clone, feature);
});
});
describe('setGeometry()', function() {
it('should set geometry', function() {
var feature = new gdal.Feature(defn);
feature.setGeometry(new gdal.Point(5, 10));
var pt = feature.getGeometry();
assert.equal(pt.x, 5);
assert.equal(pt.y, 10);
});
it('should clear geometry if null is passed', function() {
var feature = new gdal.Feature(defn);
feature.setGeometry(new gdal.Point(5, 10));
feature.setGeometry(null);
assert.isNull(feature.getGeometry());
});
it('should clear geometry if undefined is passed', function() {
var feature = new gdal.Feature(defn);
feature.setGeometry(new gdal.Point(5, 10));
feature.setGeometry(undefined);
assert.isNull(feature.getGeometry());
});
/*
// http://gdal.org/1.11/ogr/classOGRFeature.html#af1181ade837a52129ea25b46dd50cf30
// "checking for geometry type not yet implemented"
it('should throw error if wrong geometry type', function(){
var feature = new gdal.Feature(defn);
assert.throws(function(){
feature.setGeometry(new gdal.LineString());
});
});
*/
});
describe('getGeometry()', function() {
it('should get geometry', function() {
var feature = new gdal.Feature(defn);
feature.setGeometry(new gdal.Point(5, 10));
var pt = feature.getGeometry();
assert.equal(pt.x, 5);
assert.equal(pt.y, 10);
});
it('should return null if geometry is not set', function() {
var feature = new gdal.Feature(defn);
var geom = feature.getGeometry();
assert.isNull(geom);
});
});
describe('setFrom()', function() {
it('should set fields and geometry from other feature', function() {
var feature1 = new gdal.Feature(defn);
var feature2 = new gdal.Feature(defn);
feature1.setGeometry(new gdal.Point(5, 10));
feature1.fields.set([5, 'test', 3.14]);
feature2.setFrom(feature1);
var pt = feature2.getGeometry();
assert.equal(pt.x, 5);
assert.equal(pt.y, 10);
assert.equal(feature2.fields.get(0), 5);
assert.equal(feature2.fields.get(1), 'test');
assert.closeTo(feature2.fields.get(2), 3.14, 0.0001);
});
});
});
});