|
1 | 1 | // This is a port of the test suite:
|
2 | 2 | // hungry/js/test/parse_geo_point_test.js
|
3 | 3 |
|
| 4 | +const rp = require('request-promise'); |
4 | 5 | var TestObject = Parse.Object.extend('TestObject');
|
5 | 6 |
|
6 | 7 | describe('Parse.GeoPoint testing', () => {
|
@@ -374,4 +375,158 @@ describe('Parse.GeoPoint testing', () => {
|
374 | 375 | }
|
375 | 376 | });
|
376 | 377 | });
|
| 378 | + |
| 379 | + it('supports withinPolygon', (done) => { |
| 380 | + const point1 = new Parse.GeoPoint(1.5, 1.5); |
| 381 | + const point2 = new Parse.GeoPoint(2, 8); |
| 382 | + const point3 = new Parse.GeoPoint(20, 20); |
| 383 | + const obj1 = new Parse.Object('Polygon', {location: point1}); |
| 384 | + const obj2 = new Parse.Object('Polygon', {location: point2}); |
| 385 | + const obj3 = new Parse.Object('Polygon', {location: point3}); |
| 386 | + Parse.Object.saveAll([obj1, obj2, obj3]).then(() => { |
| 387 | + const where = { |
| 388 | + location: { |
| 389 | + $geoWithin: { |
| 390 | + $polygon: [ |
| 391 | + { __type: 'GeoPoint', latitude: 0, longitude: 0 }, |
| 392 | + { __type: 'GeoPoint', latitude: 0, longitude: 10 }, |
| 393 | + { __type: 'GeoPoint', latitude: 10, longitude: 10 }, |
| 394 | + { __type: 'GeoPoint', latitude: 10, longitude: 0 } |
| 395 | + ] |
| 396 | + } |
| 397 | + } |
| 398 | + }; |
| 399 | + return rp.post({ |
| 400 | + url: Parse.serverURL + '/classes/Polygon', |
| 401 | + json: { where, '_method': 'GET' }, |
| 402 | + headers: { |
| 403 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 404 | + 'X-Parse-Javascript-Key': Parse.javaScriptKey |
| 405 | + } |
| 406 | + }); |
| 407 | + }).then((resp) => { |
| 408 | + expect(resp.results.length).toBe(2); |
| 409 | + done(); |
| 410 | + }, done.fail); |
| 411 | + }); |
| 412 | + |
| 413 | + it('invalid input withinPolygon', (done) => { |
| 414 | + const point = new Parse.GeoPoint(1.5, 1.5); |
| 415 | + const obj = new Parse.Object('Polygon', {location: point}); |
| 416 | + obj.save().then(() => { |
| 417 | + const where = { |
| 418 | + location: { |
| 419 | + $geoWithin: { |
| 420 | + $polygon: 1234 |
| 421 | + } |
| 422 | + } |
| 423 | + }; |
| 424 | + return rp.post({ |
| 425 | + url: Parse.serverURL + '/classes/Polygon', |
| 426 | + json: { where, '_method': 'GET' }, |
| 427 | + headers: { |
| 428 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 429 | + 'X-Parse-Javascript-Key': Parse.javaScriptKey |
| 430 | + } |
| 431 | + }); |
| 432 | + }).then((resp) => { |
| 433 | + fail(`no request should succeed: ${JSON.stringify(resp)}`); |
| 434 | + done(); |
| 435 | + }).catch((err) => { |
| 436 | + expect(err.error.code).toEqual(Parse.Error.INVALID_JSON); |
| 437 | + done(); |
| 438 | + }); |
| 439 | + }); |
| 440 | + |
| 441 | + it('invalid geoPoint withinPolygon', (done) => { |
| 442 | + const point = new Parse.GeoPoint(1.5, 1.5); |
| 443 | + const obj = new Parse.Object('Polygon', {location: point}); |
| 444 | + obj.save().then(() => { |
| 445 | + const where = { |
| 446 | + location: { |
| 447 | + $geoWithin: { |
| 448 | + $polygon: [ |
| 449 | + {} |
| 450 | + ] |
| 451 | + } |
| 452 | + } |
| 453 | + }; |
| 454 | + return rp.post({ |
| 455 | + url: Parse.serverURL + '/classes/Polygon', |
| 456 | + json: { where, '_method': 'GET' }, |
| 457 | + headers: { |
| 458 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 459 | + 'X-Parse-Javascript-Key': Parse.javaScriptKey |
| 460 | + } |
| 461 | + }); |
| 462 | + }).then((resp) => { |
| 463 | + fail(`no request should succeed: ${JSON.stringify(resp)}`); |
| 464 | + done(); |
| 465 | + }).catch((err) => { |
| 466 | + expect(err.error.code).toEqual(Parse.Error.INVALID_JSON); |
| 467 | + done(); |
| 468 | + }); |
| 469 | + }); |
| 470 | + |
| 471 | + it('invalid latitude withinPolygon', (done) => { |
| 472 | + const point = new Parse.GeoPoint(1.5, 1.5); |
| 473 | + const obj = new Parse.Object('Polygon', {location: point}); |
| 474 | + obj.save().then(() => { |
| 475 | + const where = { |
| 476 | + location: { |
| 477 | + $geoWithin: { |
| 478 | + $polygon: [ |
| 479 | + { __type: 'GeoPoint', latitude: 0, longitude: 0 }, |
| 480 | + { __type: 'GeoPoint', latitude: 181, longitude: 0 } |
| 481 | + ] |
| 482 | + } |
| 483 | + } |
| 484 | + }; |
| 485 | + return rp.post({ |
| 486 | + url: Parse.serverURL + '/classes/Polygon', |
| 487 | + json: { where, '_method': 'GET' }, |
| 488 | + headers: { |
| 489 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 490 | + 'X-Parse-Javascript-Key': Parse.javaScriptKey |
| 491 | + } |
| 492 | + }); |
| 493 | + }).then((resp) => { |
| 494 | + fail(`no request should succeed: ${JSON.stringify(resp)}`); |
| 495 | + done(); |
| 496 | + }).catch((err) => { |
| 497 | + expect(err.error.code).toEqual(1); |
| 498 | + done(); |
| 499 | + }); |
| 500 | + }); |
| 501 | + |
| 502 | + it('invalid longitude withinPolygon', (done) => { |
| 503 | + const point = new Parse.GeoPoint(1.5, 1.5); |
| 504 | + const obj = new Parse.Object('Polygon', {location: point}); |
| 505 | + obj.save().then(() => { |
| 506 | + const where = { |
| 507 | + location: { |
| 508 | + $geoWithin: { |
| 509 | + $polygon: [ |
| 510 | + { __type: 'GeoPoint', latitude: 0, longitude: 0 }, |
| 511 | + { __type: 'GeoPoint', latitude: 0, longitude: 181 } |
| 512 | + ] |
| 513 | + } |
| 514 | + } |
| 515 | + }; |
| 516 | + return rp.post({ |
| 517 | + url: Parse.serverURL + '/classes/Polygon', |
| 518 | + json: { where, '_method': 'GET' }, |
| 519 | + headers: { |
| 520 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 521 | + 'X-Parse-Javascript-Key': Parse.javaScriptKey |
| 522 | + } |
| 523 | + }); |
| 524 | + }).then((resp) => { |
| 525 | + fail(`no request should succeed: ${JSON.stringify(resp)}`); |
| 526 | + done(); |
| 527 | + }).catch((err) => { |
| 528 | + expect(err.error.code).toEqual(1); |
| 529 | + done(); |
| 530 | + }); |
| 531 | + }); |
377 | 532 | });
|
0 commit comments