@bluedot-innovation/models 2.0.19 → 2.0.20-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -0
- package/__tests__/geofeature/polygon/polygon.spec.js +29 -10
- package/helpers.js +4 -0
- package/models/Polygon.js +14 -11
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,8 +4,8 @@ mongoose.Promise = global.Promise
|
|
|
4
4
|
|
|
5
5
|
let PolygonModel = require('../../../Models').Polygon(mongoose)
|
|
6
6
|
let PolygonModelBuiltInMongoose = require('../../../Models').Polygon()
|
|
7
|
-
let
|
|
8
|
-
let
|
|
7
|
+
let describe = require('mocha').describe
|
|
8
|
+
let it = require('mocha').it
|
|
9
9
|
let chai = require('chai')
|
|
10
10
|
|
|
11
11
|
let assert = chai.assert
|
|
@@ -19,9 +19,10 @@ let polygon
|
|
|
19
19
|
beforeEach(() => {
|
|
20
20
|
polygon = JSON.parse(JSON.stringify(require('./polygon.data.json')))
|
|
21
21
|
})
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
|
|
23
|
+
describe('Polygon', () => {
|
|
24
|
+
describe('Valid Polygon', () => {
|
|
25
|
+
it('should validate a valid polygon ', (done) => {
|
|
25
26
|
let Polygon = new PolygonModel(polygon)
|
|
26
27
|
Polygon.validate((err, res) => {
|
|
27
28
|
assert.notExists(res)
|
|
@@ -29,7 +30,7 @@ testCase('Polygon', () => {
|
|
|
29
30
|
done()
|
|
30
31
|
})
|
|
31
32
|
})
|
|
32
|
-
|
|
33
|
+
it('should validate a valid polygon (built in mongoose)', (done) => {
|
|
33
34
|
let Polygon = new PolygonModelBuiltInMongoose(polygon)
|
|
34
35
|
Polygon.validate((err, res) => {
|
|
35
36
|
assert.notExists(res)
|
|
@@ -39,8 +40,8 @@ testCase('Polygon', () => {
|
|
|
39
40
|
})
|
|
40
41
|
})
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
describe('Invalid Polygon', () => {
|
|
44
|
+
it('should not validate a polygon with invalid Polygon', (done) => {
|
|
44
45
|
delete polygon[rules.requiredFields[0]]
|
|
45
46
|
let Polygon = new PolygonModel(polygon)
|
|
46
47
|
Polygon.validate((err, res) => {
|
|
@@ -49,10 +50,11 @@ testCase('Polygon', () => {
|
|
|
49
50
|
done()
|
|
50
51
|
})
|
|
51
52
|
})
|
|
52
|
-
|
|
53
|
+
|
|
54
|
+
describe('check each required property of the model and validate that it is present', () => {
|
|
53
55
|
let requiredFields = rules.requiredFields
|
|
54
56
|
requiredFields.map((i) => {
|
|
55
|
-
|
|
57
|
+
it(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
|
|
56
58
|
// delete i from geofeatures' polygon child
|
|
57
59
|
delete polygon[i]
|
|
58
60
|
let Polygon = new PolygonModel(polygon)
|
|
@@ -66,5 +68,22 @@ testCase('Polygon', () => {
|
|
|
66
68
|
})
|
|
67
69
|
})
|
|
68
70
|
})
|
|
71
|
+
|
|
72
|
+
it('when it only has less than 3 vertices, it should error', (done) => {
|
|
73
|
+
polygon.vertices = [{
|
|
74
|
+
"latitude": -37.81902680201739,
|
|
75
|
+
"longitude": 144.9821037054062
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
"latitude": -37.8196878692931,
|
|
79
|
+
"longitude": 144.98161554336548
|
|
80
|
+
}]
|
|
81
|
+
|
|
82
|
+
let Polygon = new PolygonModel(polygon)
|
|
83
|
+
Polygon.validate((err, res) => {
|
|
84
|
+
assert.match(err, /Must have more than 2 vertices/i)
|
|
85
|
+
done()
|
|
86
|
+
})
|
|
87
|
+
})
|
|
69
88
|
})
|
|
70
89
|
})
|
package/helpers.js
CHANGED
package/models/Polygon.js
CHANGED
|
@@ -2,29 +2,32 @@ let mongoose = require('mongoose')
|
|
|
2
2
|
let StateEnum = require('@bluedot-innovation/enums').StateEnum
|
|
3
3
|
let Point = require('../schemas/Point')
|
|
4
4
|
let uuidv4 = require('uuid/v4')
|
|
5
|
+
let helpers = require('../helpers')
|
|
5
6
|
|
|
6
7
|
const schema = {
|
|
7
8
|
_id: {type: String, default: uuidv4},
|
|
8
9
|
geometry: {type: mongoose.Schema.Types.Mixed},
|
|
9
|
-
name: {type: String, required: [true, 'name is required.
|
|
10
|
+
name: {type: String, required: [true, 'name is required.']},
|
|
10
11
|
color: String,
|
|
11
|
-
vertices: {
|
|
12
|
+
vertices: {
|
|
13
|
+
type: [Point], required: [true, 'vertices is required.'],
|
|
14
|
+
validate: {
|
|
15
|
+
validator: helpers.validatePolygonVertices,
|
|
16
|
+
msg: 'A polygon must have 3 or more vertices.'
|
|
17
|
+
}
|
|
18
|
+
},
|
|
12
19
|
creationTime: {type: Date, default: Date.now},
|
|
13
20
|
lastUpdateTime: {type: Date, default: Date.now},
|
|
14
21
|
state: {type: String, default: StateEnum.active, select: false}
|
|
15
22
|
}
|
|
16
|
-
const PolygonSchema = new mongoose.Schema(
|
|
17
|
-
schema,
|
|
18
|
-
{
|
|
19
|
-
_id: false,
|
|
20
|
-
versionKey: false
|
|
21
|
-
})
|
|
22
23
|
|
|
23
|
-
PolygonSchema.
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const PolygonSchema = new mongoose.Schema(schema, {
|
|
25
|
+
_id: false,
|
|
26
|
+
versionKey: false
|
|
26
27
|
})
|
|
27
28
|
|
|
29
|
+
PolygonSchema.add({type: {type: String, required: true}})
|
|
30
|
+
|
|
28
31
|
// optional mongoose
|
|
29
32
|
let model = (mongooseInstance) => {
|
|
30
33
|
let Polygon
|