loopback-datasource-juggler/test/geo.test.js

147 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-04-01 22:25:16 +00:00
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2014-12-10 22:41:49 +00:00
/*global describe,it*/
/*jshint expr:true */
2014-12-10 22:41:49 +00:00
2016-08-22 19:55:22 +00:00
'use strict';
2014-12-10 22:41:49 +00:00
require('should');
var GeoPoint = require('../lib/geo').GeoPoint;
2015-01-05 18:53:26 +00:00
var DELTA = 0.0000001;
2014-12-10 22:41:49 +00:00
2016-04-01 11:48:17 +00:00
describe('GeoPoint', function() {
describe('constructor', function() {
2016-04-01 11:48:17 +00:00
it('should support a valid array', function() {
var point = new GeoPoint([-34, 150]);
point.lat.should.equal(-34);
point.lng.should.equal(150);
});
2016-04-01 11:48:17 +00:00
it('should support a valid object', function() {
2016-08-19 17:46:59 +00:00
var point = new GeoPoint({lat: -34, lng: 150});
point.lat.should.equal(-34);
point.lng.should.equal(150);
});
2016-04-01 11:48:17 +00:00
it('should support valid string geo coordinates', function() {
var point = new GeoPoint('-34,150');
point.lat.should.equal(-34);
point.lng.should.equal(150);
});
2016-04-01 11:48:17 +00:00
it('should support coordinates as inline parameters', function() {
var point = new GeoPoint(-34, 150);
point.lat.should.equal(-34);
point.lng.should.equal(150);
});
2016-04-01 11:48:17 +00:00
it('should reject invalid parameters', function() {
/*jshint -W024 */
var fn = function() {
new GeoPoint('150,-34');
};
fn.should.throw();
fn = function() {
new GeoPoint('invalid_string');
};
fn.should.throw();
fn = function() {
new GeoPoint([150, -34]);
};
fn.should.throw();
fn = function() {
new GeoPoint({
lat: 150,
2016-04-01 11:48:17 +00:00
lng: null,
});
};
fn.should.throw();
fn = function() {
new GeoPoint(150, -34);
};
fn.should.throw();
fn = function() {
new GeoPoint();
};
fn.should.throw();
});
});
2014-12-11 00:43:49 +00:00
describe('toString()', function() {
it('should return a string in the form "lat,lng"', function() {
2016-08-19 17:46:59 +00:00
var point = new GeoPoint({lat: -34, lng: 150});
2014-12-11 00:43:49 +00:00
point.toString().should.equal('-34,150');
});
});
2016-04-01 11:48:17 +00:00
describe('distance calculation between two points', function() {
2014-12-10 22:41:49 +00:00
2016-08-19 17:46:59 +00:00
var here = new GeoPoint({lat: 40.77492964101182, lng: -73.90950187151662});
var there = new GeoPoint({lat: 40.7753227, lng: -73.909217});
2014-12-10 22:41:49 +00:00
2016-04-01 11:48:17 +00:00
it('should return value in miles by default', function() {
2014-12-10 22:41:49 +00:00
2014-12-10 22:47:32 +00:00
var distance = GeoPoint.distanceBetween(here, there);
2014-12-10 22:41:49 +00:00
distance.should.be.a.Number;
2015-01-05 18:53:26 +00:00
distance.should.be.approximately(0.03097916611592679, DELTA);
2014-12-10 22:41:49 +00:00
});
2016-04-01 11:48:17 +00:00
it('should return value using specified unit', function() {
2014-12-10 22:41:49 +00:00
/* Supported units:
* - `radians`
* - `kilometers`
* - `meters`
* - `miles`
* - `feet`
* - `degrees`
*/
2016-08-19 17:46:59 +00:00
var distance = here.distanceTo(there, {type: 'radians'});
2014-12-10 22:41:49 +00:00
distance.should.be.a.Number;
2015-01-05 18:53:26 +00:00
distance.should.be.approximately(0.000007825491914348416, DELTA);
2014-12-10 22:41:49 +00:00
2016-08-19 17:46:59 +00:00
distance = here.distanceTo(there, {type: 'kilometers'});
2014-12-10 22:41:49 +00:00
distance.should.be.a.Number;
2015-01-05 18:53:26 +00:00
distance.should.be.approximately(0.04985613511367009, DELTA);
2014-12-10 22:41:49 +00:00
2016-08-19 17:46:59 +00:00
distance = here.distanceTo(there, {type: 'meters'});
2014-12-10 22:41:49 +00:00
distance.should.be.a.Number;
2015-01-05 18:53:26 +00:00
distance.should.be.approximately(49.856135113670085, DELTA);
2014-12-10 22:41:49 +00:00
2016-08-19 17:46:59 +00:00
distance = here.distanceTo(there, {type: 'miles'});
2014-12-10 22:41:49 +00:00
distance.should.be.a.Number;
2015-01-05 18:53:26 +00:00
distance.should.be.approximately(0.03097916611592679, DELTA);
2014-12-10 22:41:49 +00:00
2016-08-19 17:46:59 +00:00
distance = here.distanceTo(there, {type: 'feet'});
2014-12-10 22:41:49 +00:00
distance.should.be.a.Number;
2015-01-05 18:53:26 +00:00
distance.should.be.approximately(163.56999709209347, DELTA);
2014-12-10 22:41:49 +00:00
2016-08-19 17:46:59 +00:00
distance = here.distanceTo(there, {type: 'degrees'});
2014-12-10 22:41:49 +00:00
distance.should.be.a.Number;
2015-01-05 18:53:26 +00:00
distance.should.be.approximately(0.0004483676593058972, DELTA);
2014-12-10 22:41:49 +00:00
});
2014-12-11 00:43:49 +00:00
2014-12-10 22:47:32 +00:00
});
2014-12-11 00:43:49 +00:00
2014-12-10 22:41:49 +00:00
});