@bluedot-innovation/models 4.0.0 → 5.0.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.
Files changed (167) hide show
  1. package/.eslintrc.js +42 -0
  2. package/.gitlab-ci.yml +44 -0
  3. package/CHANGELOG.md +156 -0
  4. package/Models.js +55 -10
  5. package/README.MD +36 -0
  6. package/__tests__/account/account.data.json +12 -0
  7. package/__tests__/account/account.rules.js +8 -0
  8. package/__tests__/account/account.spec.js +83 -0
  9. package/__tests__/action/message/messageAction.data.json +5 -0
  10. package/__tests__/action/message/messageAction.rules.json +3 -0
  11. package/__tests__/action/message/messageAction.spec.js +73 -0
  12. package/__tests__/action/url/urlAction.data.json +4 -0
  13. package/__tests__/action/url/urlAction.rules.json +3 -0
  14. package/__tests__/action/url/urlAction.spec.js +74 -0
  15. package/__tests__/beacon/beacon.data.json +15 -0
  16. package/__tests__/beacon/beacon.rules.json +13 -0
  17. package/__tests__/beacon/beacon.spec.js +104 -0
  18. package/__tests__/condition/beacon/beaconFilter.data.json +1 -0
  19. package/__tests__/condition/beacon/beaconFilter.rules.json +3 -0
  20. package/__tests__/condition/beacon/beaconFilter.spec.js +1 -0
  21. package/__tests__/condition/dayRange/dayRange.data.json +5 -0
  22. package/__tests__/condition/dayRange/dayRange.rules.json +3 -0
  23. package/__tests__/condition/dayRange/dayRange.spec.js +95 -0
  24. package/__tests__/condition/percentageCrossed/travelPath.data.json +5 -0
  25. package/__tests__/condition/percentageCrossed/travelPath.rules.json +3 -0
  26. package/__tests__/condition/percentageCrossed/travelPath.spec.js +72 -0
  27. package/__tests__/condition/speed/speed.data.json +5 -0
  28. package/__tests__/condition/speed/speed.rules.json +3 -0
  29. package/__tests__/condition/speed/speed.spec.js +92 -0
  30. package/__tests__/condition/timeActive/timeActive.data.json +5 -0
  31. package/__tests__/condition/timeActive/timeActive.rules.json +14 -0
  32. package/__tests__/condition/timeActive/timeActive.spec.js +103 -0
  33. package/__tests__/condition/travelDirection/travelDirection.data.json +5 -0
  34. package/__tests__/condition/travelDirection/travelDirection.rules.json +3 -0
  35. package/__tests__/condition/travelDirection/travelDirection.spec.js +141 -0
  36. package/__tests__/dailyStats/checkDailyStats.js +31 -0
  37. package/__tests__/geofeature/boundingBox/boundingBox.data.json +12 -0
  38. package/__tests__/geofeature/boundingBox/boundingBox.rules.json +3 -0
  39. package/__tests__/geofeature/boundingBox/boundingBox.spec.js +87 -0
  40. package/__tests__/geofeature/circle/circle.data.json +9 -0
  41. package/__tests__/geofeature/circle/circle.rules.json +3 -0
  42. package/__tests__/geofeature/circle/circle.spec.js +71 -0
  43. package/__tests__/geofeature/geoline/geoline.data.json +46 -0
  44. package/__tests__/geofeature/geoline/geoline.rules.json +3 -0
  45. package/__tests__/geofeature/geoline/geoline.spec.js +70 -0
  46. package/__tests__/geofeature/polygon/polygon.data.json +46 -0
  47. package/__tests__/geofeature/polygon/polygon.rules.json +3 -0
  48. package/__tests__/geofeature/polygon/polygon.spec.js +132 -0
  49. package/__tests__/geofeature/zoneBeacon/zoneBeacon.data.json +16 -0
  50. package/__tests__/geofeature/zoneBeacon/zoneBeacon.rules.json +13 -0
  51. package/__tests__/geofeature/zoneBeacon/zoneBeacon.spec.js +71 -0
  52. package/__tests__/monthlyStats/monthlyStats.data.json +29 -0
  53. package/__tests__/monthlyStats/monthlyStats.spec.js +75 -0
  54. package/__tests__/project/project.data.json +18 -0
  55. package/__tests__/project/project.remote.config.json +122 -0
  56. package/__tests__/project/project.rules.json +9 -0
  57. package/__tests__/project/project.spec.js +100 -0
  58. package/__tests__/ruleRequestAggregation/ruleRequestAggregation.data.json +8 -0
  59. package/__tests__/ruleRequestAggregation/ruleRequestAggregation.spec.js +78 -0
  60. package/__tests__/setup.js +7 -0
  61. package/__tests__/user/user.data.js +19 -0
  62. package/__tests__/user/user.rules.js +8 -0
  63. package/__tests__/user/user.spec.js +130 -0
  64. package/__tests__/utils/utils.spec.js +26 -0
  65. package/__tests__/zone/zone.action.spec.js +50 -0
  66. package/__tests__/zone/zone.condition.spec.js +94 -0
  67. package/__tests__/zone/zone.data.json +157 -0
  68. package/__tests__/zone/zone.geofeatures.spec.js +75 -0
  69. package/__tests__/zone/zone.spec.js +140 -0
  70. package/constants/errorMessages.js +2 -0
  71. package/helpers.js +117 -0
  72. package/models/Account.js +50 -0
  73. package/models/AdminPortalUser.js +10 -11
  74. package/models/Beacon.js +84 -32
  75. package/models/Contract.js +37 -38
  76. package/models/CustomerLogin.js +9 -10
  77. package/models/DBEvent.js +13 -14
  78. package/models/DailyStats.js +7 -7
  79. package/models/DeviceRulesRequest.js +27 -28
  80. package/models/MessageAction.js +26 -26
  81. package/models/MonthlyStats.js +35 -19
  82. package/models/NotificationLogEntry.js +7 -8
  83. package/models/POILocation.js +6 -7
  84. package/models/POILookup.js +8 -9
  85. package/models/POIResult.js +7 -8
  86. package/models/PartnerCustomer.js +12 -13
  87. package/models/PartnerPasswordReset.js +8 -9
  88. package/models/PartnerUser.js +10 -11
  89. package/models/Partners.js +11 -12
  90. package/models/Project.js +63 -0
  91. package/models/RuleRequestAggregation.js +33 -17
  92. package/models/SingleUseToken.js +7 -8
  93. package/models/TestDevice.js +11 -12
  94. package/models/TestDeviceCluster.js +9 -10
  95. package/models/TestInstance.js +9 -10
  96. package/models/TestScenario.js +9 -10
  97. package/models/TimeOfDayRange.js +16 -16
  98. package/models/UrlAction.js +37 -0
  99. package/models/User.js +61 -0
  100. package/models/Zone.js +126 -0
  101. package/models/ZoneUIState.js +18 -18
  102. package/models/conditions/DayRange.js +44 -0
  103. package/models/conditions/Speed.js +42 -0
  104. package/models/conditions/TimeActive.js +50 -0
  105. package/models/conditions/TravelDirection.js +59 -0
  106. package/models/conditions/TravelPath.js +36 -0
  107. package/models/geofeatures/BoundingBox.js +44 -0
  108. package/models/geofeatures/Circle.js +39 -0
  109. package/models/geofeatures/Geoline.js +34 -0
  110. package/models/geofeatures/Polygon.js +45 -0
  111. package/models/geofeatures/ZoneBeacon.js +28 -0
  112. package/package.json +30 -6
  113. package/schemas/AccuracySuppression.js +14 -0
  114. package/schemas/AndroidConfig.js +35 -0
  115. package/schemas/IosConfig.js +38 -0
  116. package/schemas/MultiLineStringGeometry.js +14 -0
  117. package/schemas/Point.js +13 -0
  118. package/schemas/PointGeometry.js +14 -0
  119. package/schemas/PolygonGeometry.js +14 -0
  120. package/schemas/ProjectPermission.js +21 -0
  121. package/schemas/StarvationDetection.js +11 -0
  122. package/schemas/Telemetry.js +36 -0
  123. package/schemas/Webhook.js +22 -0
  124. package/utils/index.js +39 -0
  125. package/utils/validators.js +6 -0
  126. package/validators/checkDistanceBetweenVertices.js +21 -0
  127. package/yarn.lock +2796 -0
  128. package/models/AdminUser.js +0 -10
  129. package/models/Application.js +0 -38
  130. package/models/ApplicationNotificationAction.js +0 -30
  131. package/models/ApplicationUsage.js +0 -11
  132. package/models/BeaconFilter.js +0 -23
  133. package/models/BearingFilter.js +0 -19
  134. package/models/BoundingBox.js +0 -17
  135. package/models/Circle.js +0 -21
  136. package/models/ConfigurationParameters.js +0 -20
  137. package/models/Customer.js +0 -38
  138. package/models/DateRangeFilter.js +0 -20
  139. package/models/Fence.js +0 -21
  140. package/models/FormattedBeacon.js +0 -16
  141. package/models/FormattedCircle.js +0 -12
  142. package/models/FormattedConditions.js +0 -11
  143. package/models/FormattedDateRange.js +0 -11
  144. package/models/FormattedPercentageCrossed.js +0 -12
  145. package/models/FormattedPoint.js +0 -10
  146. package/models/FormattedPolygon.js +0 -11
  147. package/models/FormattedPolyline.js +0 -11
  148. package/models/FormattedRectangle.js +0 -12
  149. package/models/FormattedTimeActive.js +0 -17
  150. package/models/FormattedZone.js +0 -28
  151. package/models/GeoFence.js +0 -23
  152. package/models/Geofeature.js +0 -26
  153. package/models/PercentageCrossedFilter.js +0 -19
  154. package/models/Point.js +0 -20
  155. package/models/Polygon.js +0 -18
  156. package/models/Polyline.js +0 -18
  157. package/models/PropertyMapping.js +0 -17
  158. package/models/Reference.js +0 -19
  159. package/models/SequenceFilter.js +0 -18
  160. package/models/SimpleCheckInZone.js +0 -29
  161. package/models/SoundAction.js +0 -28
  162. package/models/SpeedFilter.js +0 -19
  163. package/models/TimeOfDayRangeFilter.js +0 -19
  164. package/models/URLAction.js +0 -31
  165. package/models/VibrationAction.js +0 -28
  166. package/models/property.js +0 -25
  167. package/package-lock.json.bak +0 -170
@@ -0,0 +1,141 @@
1
+ let mongoose = require('mongoose')
2
+ mongoose.Promise = global.Promise
3
+
4
+ const TravelDirectionModel = require('../../../Models').TravelDirection(mongoose)
5
+ const TravelDirectionModelBuiltInMongoose = require('../../../Models').TravelDirection()
6
+ const { isMinimumNinetyApart } = require.main.require('models/conditions/TravelDirection')
7
+ const describe = require('mocha').describe
8
+ const it = require('mocha').it
9
+ const chai = require('chai')
10
+ const assert = chai.assert
11
+ const mocha = require('mocha')
12
+ const beforeEach = mocha.beforeEach
13
+ const rules = require('./travelDirection.rules.json')
14
+
15
+ let travelDirectionCondition
16
+
17
+ beforeEach(() => {
18
+ travelDirectionCondition = JSON.parse(JSON.stringify(require('./travelDirection.data.json')))
19
+ })
20
+
21
+ describe('TravelDirection', () => {
22
+ describe('Valid TravelDirection', () => {
23
+ it('should validate a valid travelDirectionCondition ', (done) => {
24
+ let TravelDirection = new TravelDirectionModel(travelDirectionCondition)
25
+ TravelDirection.validate((err, res) => {
26
+ assert.notExists(res)
27
+ assert(err === null, 'err should equals null')
28
+ done()
29
+ })
30
+ })
31
+ it('should validate a valid travelDirectionCondition ', (done) => {
32
+ let TravelDirection = new TravelDirectionModelBuiltInMongoose(travelDirectionCondition)
33
+ TravelDirection.validate((err, res) => {
34
+ assert.notExists(res)
35
+ assert(err === null, 'err should equals null')
36
+ done()
37
+ })
38
+ })
39
+ })
40
+
41
+ describe('Invalid TravelDirection', () => {
42
+ it('should not validate a travelDirectionCondition with invalid TravelDirection', (done) => {
43
+ delete travelDirectionCondition[rules.requiredFields[0]]
44
+ let TravelDirection = new TravelDirectionModel(travelDirectionCondition)
45
+ TravelDirection.validate((err, res) => {
46
+ assert.exists(err, 'err object should exist')
47
+ assert.isObject(err)
48
+ done()
49
+ })
50
+ })
51
+ it('should throw an error when toAngle === fromAngle', (done) => {
52
+ travelDirectionCondition.toAngle = 10
53
+ travelDirectionCondition.fromAngle = 10
54
+ let TravelDirection = new TravelDirectionModel(travelDirectionCondition)
55
+ TravelDirection.validate((err, res) => {
56
+ assert.exists(err, 'err object should exist')
57
+ assert.isObject(err)
58
+ assert.nestedProperty(err, `errors.toAngle`, 'error should have a toAngle property')
59
+ done()
60
+ })
61
+ })
62
+ it('should throw an error when toAngle < 90 degrees from fromAngle (and toAngle < fromAngle)', (done) => {
63
+ travelDirectionCondition.toAngle = 5
64
+ travelDirectionCondition.fromAngle = 10
65
+ let TravelDirection = new TravelDirectionModel(travelDirectionCondition)
66
+ TravelDirection.validate((err, res) => {
67
+ assert.exists(err, 'err object should exist')
68
+ assert.isObject(err)
69
+ assert.nestedProperty(err, `errors.toAngle`, 'error should have a toAngle property')
70
+ done()
71
+ })
72
+ })
73
+ it('should throw an error when toAngle < 90 degrees from fromAngle (and toAngle > fromAngle)', (done) => {
74
+ travelDirectionCondition.toAngle = 10
75
+ travelDirectionCondition.fromAngle = 5
76
+ let TravelDirection = new TravelDirectionModel(travelDirectionCondition)
77
+ TravelDirection.validate((err, res) => {
78
+ assert.exists(err, 'err object should exist')
79
+ assert.isObject(err)
80
+ assert.nestedProperty(err, `errors.toAngle`, 'error should have a toAngle property')
81
+ done()
82
+ })
83
+ })
84
+ describe('check each required property of the model and validate that it is present', () => {
85
+ let requiredFields = rules.requiredFields
86
+ requiredFields.map((i) => {
87
+ it(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
88
+ // delete i from geofeatures' travelDirectionCondition child
89
+ delete travelDirectionCondition[i]
90
+ let TravelDirection = new TravelDirectionModel(travelDirectionCondition)
91
+ TravelDirection.validate((err, res) => {
92
+ assert.notExists(res)
93
+ assert.exists(err, 'err object should exist')
94
+ assert.isObject(err)
95
+ assert.nestedProperty(err, `errors.${i}`, `error should have a ${i} property`)
96
+ done()
97
+ })
98
+ })
99
+ })
100
+ })
101
+ })
102
+ })
103
+
104
+ describe('isMinimumNinetyApart(fromAngle, toAngle)', () => {
105
+ describe('should return true if the angles are at least 90 degrees apart in both directions', () => {
106
+ const angles = [
107
+ [360, 90],
108
+ [0, 90],
109
+ [90, 180],
110
+ [50, 140],
111
+ [315, 50],
112
+ [300, 30],
113
+ [211, 301]
114
+ ]
115
+
116
+ angles.forEach(([fromAngle, toAngle]) => {
117
+ it(`fromAngle ${fromAngle} and toAngle ${toAngle} should be true`, () => {
118
+ const result = isMinimumNinetyApart(fromAngle, toAngle)
119
+ assert.isTrue(result)
120
+ })
121
+ })
122
+ })
123
+
124
+ describe('should return false if the angles are NOT at least 90 degrees apart in both directions', () => {
125
+ const angles = [
126
+ [359, 2],
127
+ [50, 90],
128
+ [315, 360],
129
+ [180, 181],
130
+ [0, 89],
131
+ [360, 89]
132
+ ]
133
+
134
+ angles.forEach(([fromAngle, toAngle]) => {
135
+ it(`fromAngle ${fromAngle} and toAngle ${toAngle} should be false`, () => {
136
+ const result = isMinimumNinetyApart(fromAngle, toAngle)
137
+ assert.isFalse(result)
138
+ })
139
+ })
140
+ })
141
+ })
@@ -0,0 +1,31 @@
1
+ const assert = require('chai').assert
2
+ const DailyStats = require('../../models/DailyStats')
3
+ let testCase = require('mocha').describe
4
+ let assertions = require('mocha').it
5
+
6
+ const dailyStats = {
7
+ totalEndUsers: 4626,
8
+ totalIos: 2196,
9
+ totalAndroid: 2430
10
+ }
11
+
12
+ testCase('Creating new dailyStats document ', () => {
13
+ assertions('validates data types and structure of dailyStats document', (done) => {
14
+ const createDailyStats = new DailyStats(dailyStats)
15
+ assert(createDailyStats._id)
16
+ assert(createDailyStats._id.match(/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/))
17
+ createDailyStats.validate()
18
+ .then(() => {
19
+ console.log('validate DailyStats')
20
+ assert(true)
21
+ done()
22
+ }).catch(err => {
23
+ console.log('Errors found:')
24
+ for (let key of Object.keys(err.errors)) {
25
+ console.log(err.errors[key].message)
26
+ }
27
+ assert(false)
28
+ done(err)
29
+ })
30
+ })
31
+ })
@@ -0,0 +1,12 @@
1
+ {
2
+ "type": "boundingBox",
3
+ "name": "Test BoundingBox",
4
+ "northEast": {
5
+ "latitude": -37.81794619852986,
6
+ "longitude": 144.97999548912048
7
+ },
8
+ "southWest": {
9
+ "latitude": -37.81846319511329,
10
+ "longitude": 144.97921228408813
11
+ }
12
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "requiredFields":["name", "northEast", "southWest"]
3
+ }
@@ -0,0 +1,87 @@
1
+ let mongoose = require('mongoose')
2
+
3
+ mongoose.Promise = global.Promise
4
+
5
+ let BoundingBoxModel = require('../../../Models').BoundingBox(mongoose)
6
+ let BoundingBoxModelBuiltInMongoose = require('../../../Models').BoundingBox()
7
+ let describe = require('mocha').describe
8
+ let it = require('mocha').it
9
+ let chai = require('chai')
10
+
11
+ let assert = chai.assert
12
+ let mocha = require('mocha')
13
+ let beforeEach = mocha.beforeEach
14
+
15
+ let rules = require('./boundingBox.rules.json')
16
+
17
+ let boundingBox
18
+
19
+ beforeEach(() => {
20
+ boundingBox = JSON.parse(JSON.stringify(require('./boundingBox.data.json')))
21
+ })
22
+
23
+ describe('BoundingBox', () => {
24
+ describe('Valid BoundingBox', () => {
25
+ it('should validate a valid boundingBox ', (done) => {
26
+ let BoundingBox = new BoundingBoxModel(boundingBox)
27
+ BoundingBox.validate((err, res) => {
28
+ assert.notExists(res)
29
+ assert(err === null, 'err should equals null')
30
+ done()
31
+ })
32
+ })
33
+ it('should validate a valid boundingBox ', (done) => {
34
+ let BoundingBox = new BoundingBoxModelBuiltInMongoose(boundingBox)
35
+ BoundingBox.validate((err, res) => {
36
+ assert.notExists(res)
37
+ assert(err === null, 'err should equals null')
38
+ done()
39
+ })
40
+ })
41
+ })
42
+
43
+ describe('Invalid BoundingBox', () => {
44
+ it('should not validate a boundingBox with invalid BoundingBox', (done) => {
45
+ delete boundingBox[rules.requiredFields[0]]
46
+ let BoundingBox = new BoundingBoxModel(boundingBox)
47
+ BoundingBox.validate((err, res) => {
48
+ assert.exists(err, 'err object should exist')
49
+ assert.isObject(err)
50
+ done()
51
+ })
52
+ })
53
+
54
+ describe('check each required property of the model and validate that it is present', () => {
55
+ let requiredFields = rules.requiredFields
56
+ requiredFields.map((i) => {
57
+ it(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
58
+ // delete i from geofeatures' boundingBox child
59
+ delete boundingBox[i]
60
+ let BoundingBox = new BoundingBoxModel(boundingBox)
61
+ BoundingBox.validate((err, res) => {
62
+ assert.notExists(res)
63
+ assert.exists(err, 'err object should exist')
64
+ assert.isObject(err)
65
+ assert.nestedProperty(err, `errors.${i}`, `error should have a ${i} property`)
66
+ done()
67
+ })
68
+ })
69
+ })
70
+ })
71
+
72
+ it('should be invalid when southWest and NorthEast are the same', (done) => {
73
+ boundingBox.southWest = {
74
+ latitude: -37.81794619852986,
75
+ longitude: 144.97999548912048
76
+ }
77
+
78
+ const BoundingBox = new BoundingBoxModel(boundingBox)
79
+
80
+ BoundingBox.validate((err, res) => {
81
+ assert.exists(err, 'err object should exist')
82
+ assert.isObject(err)
83
+ done()
84
+ })
85
+ })
86
+ })
87
+ })
@@ -0,0 +1,9 @@
1
+ {
2
+ "type": "circle",
3
+ "name": "Test Circle",
4
+ "radius": 10,
5
+ "center": {
6
+ "latitude": -37.8159544565362,
7
+ "longitude": 144.9723565578461
8
+ }
9
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "requiredFields":["name", "radius", "center"]
3
+ }
@@ -0,0 +1,71 @@
1
+ let mongoose = require('mongoose')
2
+
3
+ mongoose.Promise = global.Promise
4
+
5
+ let CircleModel = require('../../../Models').Circle(mongoose)
6
+ let CircleModelBuiltInMongoose = require('../../../Models').Circle()
7
+ let testCase = require('mocha').describe
8
+ let assertions = require('mocha').it
9
+ let chai = require('chai')
10
+
11
+ let assert = chai.assert
12
+ let beforeEach = require('mocha').beforeEach
13
+
14
+ let rules = require('./circle.rules.json')
15
+
16
+ let circle
17
+
18
+ beforeEach(() => {
19
+ circle = JSON.parse(JSON.stringify(require('./circle.data.json')))
20
+ })
21
+
22
+ testCase('Circle', () => {
23
+ testCase('Valid Circle', () => {
24
+ assertions('should validate zone with Circle ', (done) => {
25
+ let Circle = new CircleModel(circle)
26
+ Circle.validate((err, res) => {
27
+ assert.notExists(res)
28
+ assert(err === null, 'err should equals null')
29
+ done()
30
+ })
31
+ })
32
+ assertions('should validate zone with Circle (built in mongoose)', (done) => {
33
+ let Circle = new CircleModelBuiltInMongoose(circle)
34
+ Circle.validate((err, res) => {
35
+ assert.notExists(res)
36
+ assert(err === null, 'err should equals null')
37
+ done()
38
+ })
39
+ })
40
+ })
41
+
42
+ testCase('Invalid Circle', () => {
43
+ assertions('should not validate a Circle with no radius', (done) => {
44
+ delete circle.radius
45
+ let Circle = new CircleModel(circle)
46
+ Circle.validate((err, res) => {
47
+ assert.exists(err, 'err object should exist')
48
+ assert.isObject(err)
49
+ done()
50
+ })
51
+ })
52
+
53
+ testCase('recursive property check', () => {
54
+ let requiredFields = rules.requiredFields
55
+ requiredFields.map((i) => {
56
+ assertions(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
57
+ // delete i from geofeatures' circle child
58
+ delete circle[i]
59
+ let Circle = new CircleModel(circle)
60
+ Circle.validate((err, res) => {
61
+ assert.notExists(res)
62
+ assert.exists(err, 'err object should exist')
63
+ assert.isObject(err)
64
+ assert.nestedProperty(err, `errors.${i}`, `error should have a ${i} property`)
65
+ done()
66
+ })
67
+ })
68
+ })
69
+ })
70
+ })
71
+ })
@@ -0,0 +1,46 @@
1
+ {
2
+ "type": "geoline",
3
+ "name": "Test geoline",
4
+ "vertices": [
5
+ {
6
+ "latitude": -37.81902680201739,
7
+ "longitude": 144.9821037054062
8
+ },
9
+ {
10
+ "latitude": -37.8196878692931,
11
+ "longitude": 144.98161554336548
12
+ },
13
+ {
14
+ "latitude": -37.82052267015026,
15
+ "longitude": 144.9816906452179
16
+ },
17
+ {
18
+ "latitude": -37.82128118660006,
19
+ "longitude": 144.98312830924988
20
+ },
21
+ {
22
+ "latitude": -37.82102693614463,
23
+ "longitude": 144.98458743095398
24
+ },
25
+ {
26
+ "latitude": -37.820196378553405,
27
+ "longitude": 144.98524725437164
28
+ },
29
+ {
30
+ "latitude": -37.81895899991077,
31
+ "longitude": 144.9847161769867
32
+ },
33
+ {
34
+ "latitude": -37.81865812731128,
35
+ "longitude": 144.9839222431183
36
+ },
37
+ {
38
+ "latitude": -37.81866660261256,
39
+ "longitude": 144.98308539390564
40
+ },
41
+ {
42
+ "latitude": -37.81902680201739,
43
+ "longitude": 144.9821037054062
44
+ }
45
+ ]
46
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "requiredFields":["type", "name", "vertices"]
3
+ }
@@ -0,0 +1,70 @@
1
+ let mongoose = require('mongoose')
2
+
3
+ mongoose.Promise = global.Promise
4
+
5
+ let GeolineModel = require('../../../Models').Geoline(mongoose)
6
+ let GeolineModelBuiltInMongoose = require('../../../Models').Geoline()
7
+ let testCase = require('mocha').describe
8
+ let assertions = require('mocha').it
9
+ let chai = require('chai')
10
+
11
+ let assert = chai.assert
12
+ let mocha = require('mocha')
13
+ let beforeEach = mocha.beforeEach
14
+
15
+ let rules = require('./geoline.rules.json')
16
+
17
+ let geoline
18
+
19
+ beforeEach(() => {
20
+ geoline = JSON.parse(JSON.stringify(require('./geoline.data.json')))
21
+ })
22
+ testCase('Geoline', () => {
23
+ testCase('Valid Geoline', () => {
24
+ assertions('should validate a valid geoline ', (done) => {
25
+ let Geoline = new GeolineModel(geoline)
26
+ Geoline.validate((err, res) => {
27
+ assert.notExists(res)
28
+ assert(err === null, 'err should equals null')
29
+ done()
30
+ })
31
+ })
32
+ assertions('should validate a valid geoline (built in mongoose)', (done) => {
33
+ let Geoline = new GeolineModelBuiltInMongoose(geoline)
34
+ Geoline.validate((err, res) => {
35
+ assert.notExists(res)
36
+ assert(err === null, 'err should equals null')
37
+ done()
38
+ })
39
+ })
40
+ })
41
+
42
+ testCase('Invalid Geoline', () => {
43
+ assertions('should not validate a geoline with invalid geoline', (done) => {
44
+ delete geoline[rules.requiredFields[0]]
45
+ let Geoline = new GeolineModel(geoline)
46
+ Geoline.validate((err, res) => {
47
+ assert.exists(err, 'err object should exist')
48
+ assert.isObject(err)
49
+ done()
50
+ })
51
+ })
52
+ testCase('check each required property of the model and validate that it is present', () => {
53
+ let requiredFields = rules.requiredFields
54
+ requiredFields.map((i) => {
55
+ assertions(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
56
+ // delete i from geofeatures' geoline child
57
+ delete geoline[i]
58
+ let Geoline = new GeolineModel(geoline)
59
+ Geoline.validate((err, res) => {
60
+ assert.notExists(res)
61
+ assert.exists(err, 'err object should exist')
62
+ assert.isObject(err)
63
+ assert.nestedProperty(err, `errors.${i}`, `error should have a ${i} property`)
64
+ done()
65
+ })
66
+ })
67
+ })
68
+ })
69
+ })
70
+ })
@@ -0,0 +1,46 @@
1
+ {
2
+ "type": "polygon",
3
+ "name": "Test Polygon",
4
+ "vertices": [
5
+ {
6
+ "latitude": -37.81902680201739,
7
+ "longitude": 144.9821037054062
8
+ },
9
+ {
10
+ "latitude": -37.8196878692931,
11
+ "longitude": 144.98161554336548
12
+ },
13
+ {
14
+ "latitude": -37.82052267015026,
15
+ "longitude": 144.9816906452179
16
+ },
17
+ {
18
+ "latitude": -37.82128118660006,
19
+ "longitude": 144.98312830924988
20
+ },
21
+ {
22
+ "latitude": -37.82102693614463,
23
+ "longitude": 144.98458743095398
24
+ },
25
+ {
26
+ "latitude": -37.820196378553405,
27
+ "longitude": 144.98524725437164
28
+ },
29
+ {
30
+ "latitude": -37.81895899991077,
31
+ "longitude": 144.9847161769867
32
+ },
33
+ {
34
+ "latitude": -37.81865812731128,
35
+ "longitude": 144.9839222431183
36
+ },
37
+ {
38
+ "latitude": -37.81866660261256,
39
+ "longitude": 144.98308539390564
40
+ },
41
+ {
42
+ "latitude": -37.81902680201739,
43
+ "longitude": 144.9821037054062
44
+ }
45
+ ]
46
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "requiredFields":["type", "name", "vertices"]
3
+ }
@@ -0,0 +1,132 @@
1
+ let mongoose = require('mongoose')
2
+ mongoose.Promise = global.Promise
3
+ let PolygonModel = require('../../../Models').Polygon(mongoose)
4
+ let PolygonModelBuiltInMongoose = require('../../../Models').Polygon()
5
+ const { VERTICES_TOO_CLOSE, CANT_HAVE_THREE_OR_MORE_VERTICES } = require('../../../constants/errorMessages')
6
+ let rules = require('./polygon.rules.json')
7
+ let chai = require('chai')
8
+ let assert = chai.assert
9
+
10
+
11
+ let polygon
12
+
13
+ beforeEach(() => {
14
+ polygon = JSON.parse(JSON.stringify(require('./polygon.data.json')))
15
+ })
16
+
17
+ describe('Polygon', () => {
18
+ describe('Valid Polygon', () => {
19
+ it('should validate a valid polygon ', (done) => {
20
+ let Polygon = new PolygonModel(polygon)
21
+ Polygon.validate((err, res) => {
22
+ assert.notExists(res)
23
+ assert(err === null, 'err should equals null')
24
+ done()
25
+ })
26
+ })
27
+ it('should validate a valid polygon (built in mongoose)', (done) => {
28
+ let Polygon = new PolygonModelBuiltInMongoose(polygon)
29
+ Polygon.validate((err, res) => {
30
+ assert.notExists(res)
31
+ assert(err === null, 'err should equals null')
32
+ done()
33
+ })
34
+ })
35
+ })
36
+
37
+ describe('Invalid Polygon', () => {
38
+ it('should not validate a polygon with invalid Polygon', (done) => {
39
+ delete polygon[rules.requiredFields[0]]
40
+ let Polygon = new PolygonModel(polygon)
41
+ Polygon.validate((err, res) => {
42
+ assert.exists(err, 'err object should exist')
43
+ assert.isObject(err)
44
+ done()
45
+ })
46
+ })
47
+
48
+ describe('check each required property of the model and validate that it is present', () => {
49
+ let requiredFields = rules.requiredFields
50
+ requiredFields.map((i) => {
51
+ it(`should throw an error with property '${i}' if '${i}' is not present`, (done) => {
52
+ // delete i from geofeatures' polygon child
53
+ delete polygon[i]
54
+ let Polygon = new PolygonModel(polygon)
55
+ Polygon.validate((err, res) => {
56
+ assert.notExists(res)
57
+ assert.exists(err, 'err object should exist')
58
+ assert.isObject(err)
59
+ assert.nestedProperty(err, `errors.${i}`, `error should have a ${i} property`)
60
+ done()
61
+ })
62
+ })
63
+ })
64
+ })
65
+
66
+ it('when it has less than 3 vertices, it should error', (done) => {
67
+ polygon.vertices = [{
68
+ "latitude": -37.81902680201739,
69
+ "longitude": 144.9821037054062
70
+ },
71
+ {
72
+ "latitude": -37.8196878692931,
73
+ "longitude": 144.98161554336548
74
+ }]
75
+
76
+ let Polygon = new PolygonModel(polygon)
77
+ Polygon.validate((err, res) => {
78
+ const regex = new RegExp(CANT_HAVE_THREE_OR_MORE_VERTICES)
79
+ assert.match(err, regex)
80
+ done()
81
+ })
82
+ })
83
+
84
+ describe('For any given vertex, if any of the next 2 vertices (accurate to 6 decimal points) are the same, it should error', (done) => {
85
+ it('next vertice is the same', (done) => {
86
+ polygon.vertices = [{
87
+ "latitude": -37.81902680201733,
88
+ "longitude": 144.9821037054044
89
+ }, {
90
+ "latitude": -37.81902680201731,
91
+ "longitude": 144.9821037054062
92
+ }, {
93
+ "latitude": -36.81902680201745,
94
+ "longitude": 143.9821037054032
95
+ }, {
96
+ "latitude": -37.23968786929315,
97
+ "longitude": 144.1216155433654
98
+ }]
99
+
100
+ let Polygon = new PolygonModel(polygon)
101
+ Polygon.validate((err, res) => {
102
+ const regex = new RegExp(VERTICES_TOO_CLOSE)
103
+ assert.match(err, regex)
104
+ done()
105
+ })
106
+ })
107
+
108
+ it('the one after next is the same', (done) => {
109
+ polygon.vertices = [{
110
+ "latitude": -37.81902680201739,
111
+ "longitude": 144.9821037054062
112
+ }, {
113
+ "latitude": -36.81902680201745,
114
+ "longitude": 143.9821037054033
115
+ }, {
116
+ "latitude": -37.81902680201739,
117
+ "longitude": 144.9821037054062
118
+ }, {
119
+ "latitude": -37.23968786929314,
120
+ "longitude": 144.1216155433654
121
+ }]
122
+
123
+ let Polygon = new PolygonModel(polygon)
124
+ Polygon.validate((err, res) => {
125
+ const regex = new RegExp(VERTICES_TOO_CLOSE)
126
+ assert.match(err, regex)
127
+ done()
128
+ })
129
+ })
130
+ })
131
+ })
132
+ })