|
| 1 | +const TestObject = Parse.Object.extend('TestObject'); |
| 2 | +const MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter'); |
| 3 | +const mongoURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase'; |
| 4 | +const rp = require('request-promise'); |
| 5 | +const defaultHeaders = { |
| 6 | + 'X-Parse-Application-Id': 'test', |
| 7 | + 'X-Parse-Rest-API-Key': 'rest' |
| 8 | +} |
| 9 | + |
| 10 | +describe('Parse.Polygon testing', () => { |
| 11 | + it('polygon save open path', (done) => { |
| 12 | + const coords = [[0,0],[0,1],[1,1],[1,0]]; |
| 13 | + const closed = [[0,0],[0,1],[1,1],[1,0],[0,0]]; |
| 14 | + const obj = new TestObject(); |
| 15 | + obj.set('polygon', {__type: 'Polygon', coordinates: coords}); |
| 16 | + return obj.save().then(() => { |
| 17 | + const query = new Parse.Query(TestObject); |
| 18 | + return query.get(obj.id); |
| 19 | + }).then((result) => { |
| 20 | + const polygon = result.get('polygon'); |
| 21 | + equal(polygon.__type, 'Polygon'); |
| 22 | + equal(polygon.coordinates, closed); |
| 23 | + done(); |
| 24 | + }, done.fail); |
| 25 | + }); |
| 26 | + |
| 27 | + it('polygon save closed path', (done) => { |
| 28 | + const coords = [[0,0],[0,1],[1,1],[1,0],[0,0]]; |
| 29 | + const obj = new TestObject(); |
| 30 | + obj.set('polygon', {__type: 'Polygon', coordinates: coords}); |
| 31 | + return obj.save().then(() => { |
| 32 | + const query = new Parse.Query(TestObject); |
| 33 | + return query.get(obj.id); |
| 34 | + }).then((result) => { |
| 35 | + const polygon = result.get('polygon'); |
| 36 | + equal(polygon.__type, 'Polygon'); |
| 37 | + equal(polygon.coordinates, coords); |
| 38 | + done(); |
| 39 | + }, done.fail); |
| 40 | + }); |
| 41 | + |
| 42 | + it('polygon equalTo (open/closed) path', (done) => { |
| 43 | + const openPoints = [[0,0],[0,1],[1,1],[1,0]]; |
| 44 | + const closedPoints = [[0,0],[0,1],[1,1],[1,0],[0,0]]; |
| 45 | + const openPolygon = {__type: 'Polygon', coordinates: openPoints}; |
| 46 | + const closedPolygon = {__type: 'Polygon', coordinates: closedPoints}; |
| 47 | + const obj = new TestObject(); |
| 48 | + obj.set('polygon', openPolygon); |
| 49 | + return obj.save().then(() => { |
| 50 | + const query = new Parse.Query(TestObject); |
| 51 | + query.equalTo('polygon', openPolygon); |
| 52 | + return query.find(); |
| 53 | + }).then((results) => { |
| 54 | + const polygon = results[0].get('polygon'); |
| 55 | + equal(polygon.__type, 'Polygon'); |
| 56 | + equal(polygon.coordinates, closedPoints); |
| 57 | + const query = new Parse.Query(TestObject); |
| 58 | + query.equalTo('polygon', closedPolygon); |
| 59 | + return query.find(); |
| 60 | + }).then((results) => { |
| 61 | + const polygon = results[0].get('polygon'); |
| 62 | + equal(polygon.__type, 'Polygon'); |
| 63 | + equal(polygon.coordinates, closedPoints); |
| 64 | + done(); |
| 65 | + }, done.fail); |
| 66 | + }); |
| 67 | + |
| 68 | + it('polygon update', (done) => { |
| 69 | + const oldCoords = [[0,0],[0,1],[1,1],[1,0]]; |
| 70 | + const oldPolygon = {__type: 'Polygon', coordinates: oldCoords}; |
| 71 | + const newCoords = [[2,2],[2,3],[3,3],[3,2]]; |
| 72 | + const newPolygon = {__type: 'Polygon', coordinates: newCoords}; |
| 73 | + const obj = new TestObject(); |
| 74 | + obj.set('polygon', oldPolygon); |
| 75 | + return obj.save().then(() => { |
| 76 | + obj.set('polygon', newPolygon); |
| 77 | + return obj.save(); |
| 78 | + }).then(() => { |
| 79 | + const query = new Parse.Query(TestObject); |
| 80 | + return query.get(obj.id); |
| 81 | + }).then((result) => { |
| 82 | + const polygon = result.get('polygon'); |
| 83 | + newCoords.push(newCoords[0]); |
| 84 | + equal(polygon.__type, 'Polygon'); |
| 85 | + equal(polygon.coordinates, newCoords); |
| 86 | + done(); |
| 87 | + }, done.fail); |
| 88 | + }); |
| 89 | + |
| 90 | + it('polygon invalid value', (done) => { |
| 91 | + const coords = [['foo','bar'],[0,1],[1,0],[1,1],[0,0]]; |
| 92 | + const obj = new TestObject(); |
| 93 | + obj.set('polygon', {__type: 'Polygon', coordinates: coords}); |
| 94 | + return obj.save().then(() => { |
| 95 | + const query = new Parse.Query(TestObject); |
| 96 | + return query.get(obj.id); |
| 97 | + }).then(done.fail, done); |
| 98 | + }); |
| 99 | + |
| 100 | + it('polygon three points minimum', (done) => { |
| 101 | + const coords = [[0,0]]; |
| 102 | + const obj = new TestObject(); |
| 103 | + obj.set('polygon', {__type: 'Polygon', coordinates: coords}); |
| 104 | + obj.save().then(done.fail, done); |
| 105 | + }); |
| 106 | + |
| 107 | + it('polygon three different points minimum', (done) => { |
| 108 | + const coords = [[0,0],[0,1],[0,0]]; |
| 109 | + const obj = new TestObject(); |
| 110 | + obj.set('polygon', {__type: 'Polygon', coordinates: coords}); |
| 111 | + obj.save().then(done.fail, done); |
| 112 | + }); |
| 113 | + |
| 114 | + it('polygon counterclockwise', (done) => { |
| 115 | + const coords = [[1,1],[0,1],[0,0],[1,0]]; |
| 116 | + const closed = [[1,1],[0,1],[0,0],[1,0],[1,1]]; |
| 117 | + const obj = new TestObject(); |
| 118 | + obj.set('polygon', {__type: 'Polygon', coordinates: coords}); |
| 119 | + obj.save().then(() => { |
| 120 | + const query = new Parse.Query(TestObject); |
| 121 | + return query.get(obj.id); |
| 122 | + }).then((result) => { |
| 123 | + const polygon = result.get('polygon'); |
| 124 | + equal(polygon.__type, 'Polygon'); |
| 125 | + equal(polygon.coordinates, closed); |
| 126 | + done(); |
| 127 | + }, done.fail); |
| 128 | + }); |
| 129 | + |
| 130 | + it('polygonContain query', (done) => { |
| 131 | + const points1 = [[0,0],[0,1],[1,1],[1,0]]; |
| 132 | + const points2 = [[0,0],[0,2],[2,2],[2,0]]; |
| 133 | + const points3 = [[10,10],[10,15],[15,15],[15,10],[10,10]]; |
| 134 | + const polygon1 = {__type: 'Polygon', coordinates: points1}; |
| 135 | + const polygon2 = {__type: 'Polygon', coordinates: points2}; |
| 136 | + const polygon3 = {__type: 'Polygon', coordinates: points3}; |
| 137 | + const obj1 = new TestObject({location: polygon1}); |
| 138 | + const obj2 = new TestObject({location: polygon2}); |
| 139 | + const obj3 = new TestObject({location: polygon3}); |
| 140 | + Parse.Object.saveAll([obj1, obj2, obj3]).then(() => { |
| 141 | + const where = { |
| 142 | + location: { |
| 143 | + $geoIntersects: { |
| 144 | + $point: { __type: 'GeoPoint', latitude: 0.5, longitude: 0.5 } |
| 145 | + } |
| 146 | + } |
| 147 | + }; |
| 148 | + return rp.post({ |
| 149 | + url: Parse.serverURL + '/classes/TestObject', |
| 150 | + json: { where, '_method': 'GET' }, |
| 151 | + headers: { |
| 152 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 153 | + 'X-Parse-Javascript-Key': Parse.javaScriptKey |
| 154 | + } |
| 155 | + }); |
| 156 | + }).then((resp) => { |
| 157 | + expect(resp.results.length).toBe(2); |
| 158 | + done(); |
| 159 | + }, done.fail); |
| 160 | + }); |
| 161 | + |
| 162 | + it('polygonContain invalid input', (done) => { |
| 163 | + const points = [[0,0],[0,1],[1,1],[1,0]]; |
| 164 | + const polygon = {__type: 'Polygon', coordinates: points}; |
| 165 | + const obj = new TestObject({location: polygon}); |
| 166 | + obj.save().then(() => { |
| 167 | + const where = { |
| 168 | + location: { |
| 169 | + $geoIntersects: { |
| 170 | + $point: { __type: 'GeoPoint', latitude: 181, longitude: 181 } |
| 171 | + } |
| 172 | + } |
| 173 | + }; |
| 174 | + return rp.post({ |
| 175 | + url: Parse.serverURL + '/classes/TestObject', |
| 176 | + json: { where, '_method': 'GET' }, |
| 177 | + headers: { |
| 178 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 179 | + 'X-Parse-Javascript-Key': Parse.javaScriptKey |
| 180 | + } |
| 181 | + }); |
| 182 | + }).then(done.fail, done); |
| 183 | + }); |
| 184 | + |
| 185 | + it('polygonContain invalid geoPoint', (done) => { |
| 186 | + const points = [[0,0],[0,1],[1,1],[1,0]]; |
| 187 | + const polygon = {__type: 'Polygon', coordinates: points}; |
| 188 | + const obj = new TestObject({location: polygon}); |
| 189 | + obj.save().then(() => { |
| 190 | + const where = { |
| 191 | + location: { |
| 192 | + $geoIntersects: { |
| 193 | + $point: [] |
| 194 | + } |
| 195 | + } |
| 196 | + }; |
| 197 | + return rp.post({ |
| 198 | + url: Parse.serverURL + '/classes/TestObject', |
| 199 | + json: { where, '_method': 'GET' }, |
| 200 | + headers: { |
| 201 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 202 | + 'X-Parse-Javascript-Key': Parse.javaScriptKey |
| 203 | + } |
| 204 | + }); |
| 205 | + }).then(done.fail, done); |
| 206 | + }); |
| 207 | +}); |
| 208 | + |
| 209 | +describe_only_db('mongo')('Parse.Polygon testing', () => { |
| 210 | + it('support 2d and 2dsphere', (done) => { |
| 211 | + const coords = [[0,0],[0,1],[1,1],[1,0],[0,0]]; |
| 212 | + const polygon = {__type: 'Polygon', coordinates: coords}; |
| 213 | + const location = {__type: 'GeoPoint', latitude:10, longitude:10}; |
| 214 | + const databaseAdapter = new MongoStorageAdapter({ uri: mongoURI }); |
| 215 | + return reconfigureServer({ |
| 216 | + appId: 'test', |
| 217 | + restAPIKey: 'rest', |
| 218 | + publicServerURL: 'http://localhost:8378/1', |
| 219 | + databaseAdapter |
| 220 | + }).then(() => { |
| 221 | + return databaseAdapter.createIndex('TestObject', {location: '2d'}); |
| 222 | + }).then(() => { |
| 223 | + return databaseAdapter.createIndex('TestObject', {polygon: '2dsphere'}); |
| 224 | + }).then(() => { |
| 225 | + return rp.post({ |
| 226 | + url: 'http://localhost:8378/1/classes/TestObject', |
| 227 | + json: { |
| 228 | + '_method': 'POST', |
| 229 | + location, |
| 230 | + polygon, |
| 231 | + polygon2: polygon |
| 232 | + }, |
| 233 | + headers: defaultHeaders |
| 234 | + }); |
| 235 | + }).then((resp) => { |
| 236 | + return rp.post({ |
| 237 | + url: `http://localhost:8378/1/classes/TestObject/${resp.objectId}`, |
| 238 | + json: {'_method': 'GET'}, |
| 239 | + headers: defaultHeaders |
| 240 | + }); |
| 241 | + }).then((resp) => { |
| 242 | + equal(resp.location, location); |
| 243 | + equal(resp.polygon, polygon); |
| 244 | + equal(resp.polygon2, polygon); |
| 245 | + return databaseAdapter.getIndexes('TestObject'); |
| 246 | + }).then((indexes) => { |
| 247 | + equal(indexes.length, 4); |
| 248 | + equal(indexes[0].key, {_id: 1}); |
| 249 | + equal(indexes[1].key, {location: '2d'}); |
| 250 | + equal(indexes[2].key, {polygon: '2dsphere'}); |
| 251 | + equal(indexes[3].key, {polygon2: '2dsphere'}); |
| 252 | + done(); |
| 253 | + }, done.fail); |
| 254 | + }); |
| 255 | + |
| 256 | + it('polygon loop is not valid', (done) => { |
| 257 | + const coords = [[0,0],[0,1],[1,0],[1,1]]; |
| 258 | + const obj = new TestObject(); |
| 259 | + obj.set('polygon', {__type: 'Polygon', coordinates: coords}); |
| 260 | + obj.save().then(done.fail, done); |
| 261 | + }); |
| 262 | +}); |
0 commit comments