|
| 1 | +const assert = require('assert'); |
| 2 | +const clear = require('./clear'); |
| 3 | +const Parse = require('../../node'); |
| 4 | + |
| 5 | +const TestObject = Parse.Object.extend('TestObject'); |
| 6 | + |
| 7 | +describe('Polygon', () => { |
| 8 | + before(() => { |
| 9 | + Parse.initialize('integration'); |
| 10 | + Parse.CoreManager.set('SERVER_URL', 'http://localhost:1337/parse'); |
| 11 | + Parse.Storage._clear(); |
| 12 | + }); |
| 13 | + |
| 14 | + beforeEach((done) => { |
| 15 | + clear().then(() => { |
| 16 | + done(); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + it('can save polygon with points', (done) => { |
| 21 | + const openPoints = [[0,0], [0,1], [1,1], [1,0]]; |
| 22 | + const closedPoints = [[0,0], [0,1], [1,1], [1,0], [0,0]]; |
| 23 | + const polygon = new Parse.Polygon(openPoints); |
| 24 | + const obj = new TestObject({ polygon }); |
| 25 | + obj.save().then(() => { |
| 26 | + const query = new Parse.Query(TestObject); |
| 27 | + query.equalTo('polygon', polygon); |
| 28 | + return query.find(); |
| 29 | + }).then((results) => { |
| 30 | + assert.equal(results.length, 1); |
| 31 | + assert.deepEqual(results[0].get('polygon').coordinates, closedPoints); |
| 32 | + const closedPolygon = new Parse.Polygon(closedPoints); |
| 33 | + const query = new Parse.Query(TestObject); |
| 34 | + query.equalTo('polygon', closedPolygon); |
| 35 | + return query.find(); |
| 36 | + }).then((results) => { |
| 37 | + assert.equal(results.length, 1); |
| 38 | + assert.deepEqual(results[0].get('polygon').coordinates, closedPoints); |
| 39 | + done(); |
| 40 | + }, done.fail); |
| 41 | + }); |
| 42 | + |
| 43 | + it('can save polygon with GeoPoints', (done) => { |
| 44 | + const p1 = new Parse.GeoPoint(0, 0); |
| 45 | + const p2 = new Parse.GeoPoint(0, 1); |
| 46 | + const p3 = new Parse.GeoPoint(1, 1); |
| 47 | + const p4 = new Parse.GeoPoint(1, 0); |
| 48 | + const p5 = new Parse.GeoPoint(0, 0); |
| 49 | + const closedPoints = [[0,0], [0,1], [1,1], [1,0], [0,0]]; |
| 50 | + const polygon = new Parse.Polygon([p1, p2, p3, p4, p5]); |
| 51 | + const obj = new TestObject({ polygon }); |
| 52 | + obj.save().then(() => { |
| 53 | + const query = new Parse.Query(TestObject); |
| 54 | + query.equalTo('polygon', polygon); |
| 55 | + return query.find(); |
| 56 | + }).then((results) => { |
| 57 | + assert.equal(results.length, 1); |
| 58 | + assert.deepEqual(results[0].get('polygon').coordinates, closedPoints); |
| 59 | + const closedPolygon = new Parse.Polygon(closedPoints); |
| 60 | + const query = new Parse.Query(TestObject); |
| 61 | + query.equalTo('polygon', closedPolygon); |
| 62 | + return query.find(); |
| 63 | + }).then((results) => { |
| 64 | + assert.deepEqual(results[0].get('polygon').coordinates, closedPoints); |
| 65 | + done(); |
| 66 | + }, done.fail); |
| 67 | + }); |
| 68 | + |
| 69 | + it('fail save with 3 point minumum', (done) => { |
| 70 | + try { |
| 71 | + const polygon = new Parse.Polygon([[0, 0]]); |
| 72 | + } catch (e) { |
| 73 | + done(); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it('fail save with non array', (done) => { |
| 78 | + try { |
| 79 | + const polygon = new Parse.Polygon(123); |
| 80 | + } catch (e) { |
| 81 | + done(); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('fail save with invalid array', (done) => { |
| 86 | + try { |
| 87 | + const polygon = new Parse.Polygon([['str1'], ['str2'], ['str3']]); |
| 88 | + } catch (e) { |
| 89 | + done(); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it('containsPoint', (done) => { |
| 94 | + const points = [[0,0], [0,1], [1,1], [1,0]]; |
| 95 | + const inside = new Parse.GeoPoint(0.5, 0.5); |
| 96 | + const outside = new Parse.GeoPoint(10, 10); |
| 97 | + const polygon = new Parse.Polygon(points); |
| 98 | + |
| 99 | + assert.equal(polygon.containsPoint(inside), true); |
| 100 | + assert.equal(polygon.containsPoint(outside), false); |
| 101 | + done(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('equality', (done) => { |
| 105 | + const points = [[0,0], [0,1], [1,1], [1,0]]; |
| 106 | + const diff = [[0,0], [0,2], [2,2], [2,0]]; |
| 107 | + |
| 108 | + const polygonA = new Parse.Polygon(points); |
| 109 | + const polygonB = new Parse.Polygon(points); |
| 110 | + const polygonC = new Parse.Polygon(diff); |
| 111 | + |
| 112 | + assert.equal(polygonA.equals(polygonA), true); |
| 113 | + assert.equal(polygonA.equals(polygonB), true); |
| 114 | + assert.equal(polygonB.equals(polygonA), true); |
| 115 | + |
| 116 | + assert.equal(polygonA.equals(true), false); |
| 117 | + assert.equal(polygonA.equals(polygonC), false); |
| 118 | + |
| 119 | + done(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('supports polygonContains', (done) => { |
| 123 | + const p1 = [[0,0], [0,1], [1,1], [1,0]]; |
| 124 | + const p2 = [[0,0], [0,2], [2,2], [2,0]]; |
| 125 | + const p3 = [[10,10], [10,15], [15,15], [15,10], [10,10]]; |
| 126 | + |
| 127 | + const polygon1 = new Parse.Polygon(p1); |
| 128 | + const polygon2 = new Parse.Polygon(p2); |
| 129 | + const polygon3 = new Parse.Polygon(p3); |
| 130 | + |
| 131 | + const obj1 = new TestObject({ polygon: polygon1 }); |
| 132 | + const obj2 = new TestObject({ polygon: polygon2 }); |
| 133 | + const obj3 = new TestObject({ polygon: polygon3 }); |
| 134 | + |
| 135 | + Parse.Object.saveAll([obj1, obj2, obj3]).then(() => { |
| 136 | + const point = new Parse.GeoPoint(0.5, 0.5); |
| 137 | + const query = new Parse.Query(TestObject); |
| 138 | + query.polygonContains('polygon', point); |
| 139 | + return query.find(); |
| 140 | + }).then((results) => { |
| 141 | + assert.equal(results.length, 2); |
| 142 | + done(); |
| 143 | + }, done.fail); |
| 144 | + }); |
| 145 | + |
| 146 | + it('polygonContains invalid input', (done) => { |
| 147 | + const points = [[0,0], [0,1], [1,1], [1,0]]; |
| 148 | + const polygon = new Parse.Polygon(points); |
| 149 | + const obj = new TestObject({ polygon }); |
| 150 | + obj.save().then(() => { |
| 151 | + const query = new Parse.Query(TestObject); |
| 152 | + query.polygonContains('polygon', 1234); |
| 153 | + return query.find(); |
| 154 | + }).then(() => { |
| 155 | + fail(); |
| 156 | + }).catch(() => { |
| 157 | + done(); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments