-
-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathParsePolygonTest.js
196 lines (182 loc) · 4.93 KB
/
ParsePolygonTest.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
const assert = require('assert');
const Parse = require('../../node');
describe('Polygon', () => {
it('can save polygon with points', done => {
const openPoints = [
[0, 0],
[0, 1],
[1, 1],
[1, 0],
];
const closedPoints = [
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
];
const polygon = new Parse.Polygon(openPoints);
const obj = new TestObject({ polygon });
obj
.save()
.then(() => {
const query = new Parse.Query(TestObject);
query.equalTo('polygon', polygon);
return query.find();
})
.then(results => {
assert.equal(results.length, 1);
assert.deepEqual(results[0].get('polygon').coordinates, closedPoints);
const closedPolygon = new Parse.Polygon(closedPoints);
const query = new Parse.Query(TestObject);
query.equalTo('polygon', closedPolygon);
return query.find();
})
.then(results => {
assert.equal(results.length, 1);
assert.deepEqual(results[0].get('polygon').coordinates, closedPoints);
done();
}, done.fail);
});
it('can save polygon with GeoPoints', done => {
const p1 = new Parse.GeoPoint(0, 0);
const p2 = new Parse.GeoPoint(0, 1);
const p3 = new Parse.GeoPoint(1, 1);
const p4 = new Parse.GeoPoint(1, 0);
const p5 = new Parse.GeoPoint(0, 0);
const closedPoints = [
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
];
const polygon = new Parse.Polygon([p1, p2, p3, p4, p5]);
const obj = new TestObject({ polygon });
obj
.save()
.then(() => {
const query = new Parse.Query(TestObject);
query.equalTo('polygon', polygon);
return query.find();
})
.then(results => {
assert.equal(results.length, 1);
assert.deepEqual(results[0].get('polygon').coordinates, closedPoints);
const closedPolygon = new Parse.Polygon(closedPoints);
const query = new Parse.Query(TestObject);
query.equalTo('polygon', closedPolygon);
return query.find();
})
.then(results => {
assert.deepEqual(results[0].get('polygon').coordinates, closedPoints);
done();
}, done.fail);
});
it('fail save with 3 point minumum', () => {
expect(function () {
new Parse.Polygon([[0, 0]]);
}).toThrow();
});
it('fail save with non array', () => {
expect(function () {
new Parse.Polygon(123);
}).toThrow();
});
it('fail save with invalid array', () => {
expect(function () {
new Parse.Polygon([['str1'], ['str2'], ['str3']]);
}).toThrow();
});
it('containsPoint', () => {
const points = [
[0, 0],
[0, 1],
[1, 1],
[1, 0],
];
const inside = new Parse.GeoPoint(0.5, 0.5);
const outside = new Parse.GeoPoint(10, 10);
const polygon = new Parse.Polygon(points);
assert.equal(polygon.containsPoint(inside), true);
assert.equal(polygon.containsPoint(outside), false);
});
it('equality', () => {
const points = [
[0, 0],
[0, 1],
[1, 1],
[1, 0],
];
const diff = [
[0, 0],
[0, 2],
[2, 2],
[2, 0],
];
const polygonA = new Parse.Polygon(points);
const polygonB = new Parse.Polygon(points);
const polygonC = new Parse.Polygon(diff);
assert.equal(polygonA.equals(polygonA), true);
assert.equal(polygonA.equals(polygonB), true);
assert.equal(polygonB.equals(polygonA), true);
assert.equal(polygonA.equals(true), false);
assert.equal(polygonA.equals(polygonC), false);
});
it('supports polygonContains', async () => {
const p1 = [
[0, 0],
[0, 1],
[1, 1],
[1, 0],
];
const p2 = [
[0, 0],
[0, 2],
[2, 2],
[2, 0],
];
const p3 = [
[10, 10],
[10, 15],
[15, 15],
[15, 10],
[10, 10],
];
const polygon1 = new Parse.Polygon(p1);
const polygon2 = new Parse.Polygon(p2);
const polygon3 = new Parse.Polygon(p3);
const obj1 = new TestObject({ polygon: polygon1 });
const obj2 = new TestObject({ polygon: polygon2 });
const obj3 = new TestObject({ polygon: polygon3 });
await Parse.Object.saveAll([obj1, obj2, obj3]);
const point = new Parse.GeoPoint(0.5, 0.5);
const query = new Parse.Query(TestObject);
query.polygonContains('polygon', point);
const results = await query.find();
assert.equal(results.length, 2);
});
it('polygonContains invalid input', done => {
const points = [
[0, 0],
[0, 1],
[1, 1],
[1, 0],
];
const polygon = new Parse.Polygon(points);
const obj = new TestObject({ polygon });
obj
.save()
.then(() => {
const query = new Parse.Query(TestObject);
query.polygonContains('polygon', 1234);
return query.find();
})
.then(() => {
fail();
})
.catch(() => {
done();
});
});
});