@ndustrial/contxt-sdk 4.3.4 → 5.1.0
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/.gitattributes +1 -0
- package/.nvmrc +1 -1
- package/CHANGELOG.md +8 -0
- package/docs/Facilities.md +0 -144
- package/docs/FacilityGroupings.md +0 -160
- package/docs/Nionic.md +63 -0
- package/docs/README.md +3 -29
- package/docs/Typedefs.md +0 -101
- package/esm/config/audiences.js +13 -13
- package/esm/config/audiences.js.map +1 -1
- package/esm/index.js +3 -6
- package/esm/index.js.map +1 -1
- package/esm/nionic/index.js +126 -0
- package/esm/nionic/index.js.map +1 -0
- package/lib/config/audiences.js +13 -13
- package/lib/config/audiences.js.map +1 -1
- package/lib/index.js +19 -13
- package/lib/index.js.map +1 -1
- package/lib/nionic/index.js +130 -0
- package/lib/nionic/index.js.map +1 -0
- package/meta.yaml +11 -0
- package/package.json +9 -10
- package/src/bus/index.spec.js +3 -2
- package/src/bus/webSocketConnection.spec.js +31 -31
- package/src/config/audiences.js +13 -13
- package/src/config/index.spec.js +6 -6
- package/src/coordinator/applications.spec.js +8 -8
- package/src/coordinator/consent.spec.js +1 -1
- package/src/coordinator/edgeNodes.spec.js +3 -3
- package/src/coordinator/organizations.spec.js +4 -4
- package/src/coordinator/permissions.spec.js +4 -4
- package/src/coordinator/roles.spec.js +3 -3
- package/src/coordinator/users.spec.js +14 -15
- package/src/events/index.spec.js +16 -16
- package/src/files/index.spec.js +7 -7
- package/src/index.js +3 -6
- package/src/index.spec.js +8 -22
- package/src/iot/feeds.spec.js +4 -4
- package/src/iot/fieldCategories.spec.js +4 -4
- package/src/iot/fieldGroupings.spec.js +5 -5
- package/src/iot/outputs.spec.js +6 -6
- package/src/nionic/index.js +151 -0
- package/src/request.spec.js +3 -3
- package/src/sessionTypes/auth0WebAuth.spec.js +2 -2
- package/src/sessionTypes/index.spec.js +3 -3
- package/src/sessionTypes/machineAuth.spec.js +3 -3
- package/src/sessionTypes/passwordGrantAuth.spec.js +1 -1
- package/src/utils/iot/formatOutputFieldDataFromServer.spec.js +6 -6
- package/src/utils/iot/parseOutputFieldNextPageUrlMetadata.spec.js +2 -2
- package/src/utils/pagination/formatPaginatedDataFromServer.spec.js +2 -2
- package/src/utils/url/stringifyParams.spec.js +2 -3
- package/support/fixtures/factories/IOSDevice.js +3 -3
- package/support/fixtures/factories/applicationGrouping.js +4 -4
- package/support/fixtures/factories/applicationModule.js +2 -2
- package/support/fixtures/factories/asset.js +1 -1
- package/support/fixtures/factories/assetAttribute.js +2 -2
- package/support/fixtures/factories/assetAttributeValue.js +2 -2
- package/support/fixtures/factories/assetMetric.js +1 -1
- package/support/fixtures/factories/assetMetricValue.js +3 -3
- package/support/fixtures/factories/assetMetricValueCompact.js +2 -2
- package/support/fixtures/factories/assetType.js +1 -1
- package/support/fixtures/factories/authResults.js +3 -3
- package/support/fixtures/factories/channel.js +1 -1
- package/support/fixtures/factories/contxtApplication.js +5 -5
- package/support/fixtures/factories/contxtOrganization.js +2 -2
- package/support/fixtures/factories/contxtOrganizationFeaturedApplication.js +3 -3
- package/support/fixtures/factories/contxtProject.js +2 -2
- package/support/fixtures/factories/contxtProjectEnvironment.js +2 -2
- package/support/fixtures/factories/contxtRole.js +3 -3
- package/support/fixtures/factories/contxtRoleApplication.js +1 -1
- package/support/fixtures/factories/contxtRoleProjectEnvironment.js +1 -1
- package/support/fixtures/factories/contxtUser.js +2 -2
- package/support/fixtures/factories/contxtUserApplication.js +1 -1
- package/support/fixtures/factories/contxtUserFavoriteApplication.js +2 -2
- package/support/fixtures/factories/contxtUserPermissions.js +5 -5
- package/support/fixtures/factories/contxtUserProjectEnvironment.js +1 -1
- package/support/fixtures/factories/contxtUserRole.js +2 -2
- package/support/fixtures/factories/costCenter.js +2 -2
- package/support/fixtures/factories/costCenterFacility.js +3 -3
- package/support/fixtures/factories/edgeNode.js +1 -1
- package/support/fixtures/factories/event.js +5 -5
- package/support/fixtures/factories/eventType.js +4 -4
- package/support/fixtures/factories/eventUser.js +5 -5
- package/support/fixtures/factories/facility.js +4 -4
- package/support/fixtures/factories/facilityGrouping.js +3 -3
- package/support/fixtures/factories/facilityGroupingFacility.js +3 -3
- package/support/fixtures/factories/facilityInfo.js +2 -2
- package/support/fixtures/factories/facilityTag.js +1 -1
- package/support/fixtures/factories/feedStatus.js +1 -1
- package/support/fixtures/factories/feedType.js +1 -1
- package/support/fixtures/factories/feeds.js +5 -5
- package/support/fixtures/factories/fieldCategory.js +3 -3
- package/support/fixtures/factories/fieldGrouping.js +4 -4
- package/support/fixtures/factories/fieldGroupingField.js +3 -3
- package/support/fixtures/factories/fieldGroupingStatus.js +1 -1
- package/support/fixtures/factories/file.js +1 -1
- package/support/fixtures/factories/fileUploadInfo.js +1 -1
- package/support/fixtures/factories/organization.js +1 -1
- package/support/fixtures/factories/outputField.js +10 -10
- package/support/fixtures/factories/outputFieldData.js +1 -1
- package/support/fixtures/factories/owner.js +2 -2
- package/support/fixtures/factories/paginationMetadata.js +2 -2
- package/support/fixtures/factories/triggeredEvent.js +4 -4
- package/support/fixtures/factories/userEventSubscription.js +1 -1
- package/support/fixtures/factories/userMobileNumber.js +1 -1
- package/support/fixtures/factories/userPermissionsMap.js +2 -2
- package/.envrc +0 -0
- package/.huskyrc.js +0 -11
- package/.nycrc +0 -12
- package/esm/assets/assetAttributes.js +0 -574
- package/esm/assets/assetAttributes.js.map +0 -1
- package/esm/assets/assetMetrics.js +0 -690
- package/esm/assets/assetMetrics.js.map +0 -1
- package/esm/assets/assetTypes.js +0 -273
- package/esm/assets/assetTypes.js.map +0 -1
- package/esm/assets/index.js +0 -283
- package/esm/assets/index.js.map +0 -1
- package/esm/facilities/costCenters.js +0 -306
- package/esm/facilities/costCenters.js.map +0 -1
- package/esm/facilities/groupings.js +0 -323
- package/esm/facilities/groupings.js.map +0 -1
- package/esm/facilities/index.js +0 -343
- package/esm/facilities/index.js.map +0 -1
- package/esm/health/index.js +0 -229
- package/esm/health/index.js.map +0 -1
- package/esm/utils/facilities/formatFacilityWithInfoFromServer.js +0 -65
- package/esm/utils/facilities/formatFacilityWithInfoFromServer.js.map +0 -1
- package/esm/utils/facilities/index.js +0 -4
- package/esm/utils/facilities/index.js.map +0 -1
- package/lib/assets/assetAttributes.js +0 -588
- package/lib/assets/assetAttributes.js.map +0 -1
- package/lib/assets/assetMetrics.js +0 -705
- package/lib/assets/assetMetrics.js.map +0 -1
- package/lib/assets/assetTypes.js +0 -287
- package/lib/assets/assetTypes.js.map +0 -1
- package/lib/assets/index.js +0 -302
- package/lib/assets/index.js.map +0 -1
- package/lib/facilities/costCenters.js +0 -316
- package/lib/facilities/costCenters.js.map +0 -1
- package/lib/facilities/groupings.js +0 -333
- package/lib/facilities/groupings.js.map +0 -1
- package/lib/facilities/index.js +0 -360
- package/lib/facilities/index.js.map +0 -1
- package/lib/health/index.js +0 -232
- package/lib/health/index.js.map +0 -1
- package/lib/utils/facilities/formatFacilityWithInfoFromServer.js +0 -71
- package/lib/utils/facilities/formatFacilityWithInfoFromServer.js.map +0 -1
- package/lib/utils/facilities/index.js +0 -15
- package/lib/utils/facilities/index.js.map +0 -1
- package/src/assets/assetAttributes.js +0 -619
- package/src/assets/assetAttributes.spec.js +0 -1143
- package/src/assets/assetMetrics.js +0 -736
- package/src/assets/assetMetrics.spec.js +0 -1180
- package/src/assets/assetTypes.js +0 -260
- package/src/assets/assetTypes.spec.js +0 -556
- package/src/assets/index.js +0 -271
- package/src/assets/index.spec.js +0 -515
- package/src/facilities/costCenters.js +0 -295
- package/src/facilities/costCenters.spec.js +0 -529
- package/src/facilities/groupings.js +0 -320
- package/src/facilities/groupings.spec.js +0 -567
- package/src/facilities/index.js +0 -343
- package/src/facilities/index.spec.js +0 -551
- package/src/health/index.js +0 -200
- package/src/health/index.spec.js +0 -501
- package/src/utils/facilities/formatFacilityWithInfoFromServer.js +0 -62
- package/src/utils/facilities/formatFacilityWithInfoFromServer.spec.js +0 -73
- package/src/utils/facilities/index.js +0 -3
- package/tmp/package.json +0 -11
|
@@ -1,1180 +0,0 @@
|
|
|
1
|
-
import omit from 'lodash.omit';
|
|
2
|
-
import AssetMetrics from './assetMetrics';
|
|
3
|
-
import * as objectUtils from '../utils/objects';
|
|
4
|
-
import * as paginationUtils from '../utils/pagination';
|
|
5
|
-
import * as urlUtils from '../utils/url';
|
|
6
|
-
|
|
7
|
-
describe('Assets/Metrics', function() {
|
|
8
|
-
let baseRequest;
|
|
9
|
-
let baseSdk;
|
|
10
|
-
let expectedHost;
|
|
11
|
-
|
|
12
|
-
beforeEach(function() {
|
|
13
|
-
baseRequest = {
|
|
14
|
-
delete: sinon.stub().resolves(),
|
|
15
|
-
get: sinon.stub().resolves(),
|
|
16
|
-
post: sinon.stub().resolves(),
|
|
17
|
-
put: sinon.stub().resolves()
|
|
18
|
-
};
|
|
19
|
-
baseSdk = {
|
|
20
|
-
config: {
|
|
21
|
-
audiences: {
|
|
22
|
-
facilities: fixture.build('audience')
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
expectedHost = faker.internet.url();
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
afterEach(function() {
|
|
30
|
-
sinon.restore();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
describe('constructor', function() {
|
|
34
|
-
let assetMetrics;
|
|
35
|
-
|
|
36
|
-
beforeEach(function() {
|
|
37
|
-
assetMetrics = new AssetMetrics(baseSdk, baseRequest, expectedHost);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('sets a base url for the class instance', function() {
|
|
41
|
-
expect(assetMetrics._baseUrl).to.equal(expectedHost);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('appends the supplied request module to the class instance', function() {
|
|
45
|
-
expect(assetMetrics._request).to.deep.equal(baseRequest);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('appends the supplied sdk to the class instance', function() {
|
|
49
|
-
expect(assetMetrics._sdk).to.deep.equal(baseSdk);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
describe('create', function() {
|
|
54
|
-
context('when all required information is supplied', function() {
|
|
55
|
-
let assetMetricFromServerAfterFormat;
|
|
56
|
-
let assetMetricFromServerBeforeFormat;
|
|
57
|
-
let assetMetricToServerAfterFormat;
|
|
58
|
-
let assetMetricToServerBeforeFormat;
|
|
59
|
-
let assetTypeId;
|
|
60
|
-
let promise;
|
|
61
|
-
let request;
|
|
62
|
-
let toCamelCase;
|
|
63
|
-
let toSnakeCase;
|
|
64
|
-
|
|
65
|
-
beforeEach(function() {
|
|
66
|
-
assetMetricFromServerAfterFormat = fixture.build('assetMetric');
|
|
67
|
-
assetMetricFromServerBeforeFormat = fixture.build('assetMetric', null, {
|
|
68
|
-
fromServer: true
|
|
69
|
-
});
|
|
70
|
-
assetMetricToServerAfterFormat = fixture.build('assetMetric', null, {
|
|
71
|
-
fromServer: true
|
|
72
|
-
});
|
|
73
|
-
assetMetricToServerBeforeFormat = fixture.build('assetMetric');
|
|
74
|
-
|
|
75
|
-
assetTypeId = fixture.build('assetType').id;
|
|
76
|
-
|
|
77
|
-
request = {
|
|
78
|
-
...baseRequest,
|
|
79
|
-
post: sinon.stub().resolves(assetMetricFromServerBeforeFormat)
|
|
80
|
-
};
|
|
81
|
-
toCamelCase = sinon
|
|
82
|
-
.stub(objectUtils, 'toCamelCase')
|
|
83
|
-
.returns(assetMetricFromServerAfterFormat);
|
|
84
|
-
toSnakeCase = sinon
|
|
85
|
-
.stub(objectUtils, 'toSnakeCase')
|
|
86
|
-
.returns(assetMetricToServerAfterFormat);
|
|
87
|
-
|
|
88
|
-
const assetMetrics = new AssetMetrics(baseSdk, request, expectedHost);
|
|
89
|
-
|
|
90
|
-
promise = assetMetrics.create(
|
|
91
|
-
assetTypeId,
|
|
92
|
-
assetMetricToServerBeforeFormat
|
|
93
|
-
);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('formats the submitted asset metric object to send to the server', function() {
|
|
97
|
-
expect(toSnakeCase).to.be.deep.calledWith(
|
|
98
|
-
assetMetricToServerBeforeFormat
|
|
99
|
-
);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('creates a new asset metric', function() {
|
|
103
|
-
expect(request.post).to.be.deep.calledWith(
|
|
104
|
-
`${expectedHost}/assets/types/${assetTypeId}/metrics`,
|
|
105
|
-
assetMetricToServerAfterFormat
|
|
106
|
-
);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('formats the returned asset metric object', function() {
|
|
110
|
-
return promise.then(() => {
|
|
111
|
-
expect(toCamelCase).to.be.deep.calledWith(
|
|
112
|
-
assetMetricFromServerBeforeFormat
|
|
113
|
-
);
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('returns a fulfilled promise with the new asset metric information', function() {
|
|
118
|
-
return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
|
|
119
|
-
assetMetricFromServerAfterFormat
|
|
120
|
-
);
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
context('when there is missing required information', function() {
|
|
125
|
-
let assetMetric;
|
|
126
|
-
let assetMetrics;
|
|
127
|
-
let assetTypeId;
|
|
128
|
-
let promise;
|
|
129
|
-
|
|
130
|
-
beforeEach(function() {
|
|
131
|
-
assetMetric = fixture.build('assetMetric');
|
|
132
|
-
assetMetrics = new AssetMetrics(baseSdk, baseRequest, expectedHost);
|
|
133
|
-
assetTypeId = fixture.build('assetType').id;
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('throws an error if there is no asset type ID provided', function() {
|
|
137
|
-
promise = assetMetrics.create(null, assetMetric);
|
|
138
|
-
|
|
139
|
-
return expect(promise).to.be.rejectedWith(
|
|
140
|
-
'An asset type ID is required to create an asset metric.'
|
|
141
|
-
);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
['description', 'label', 'organizationId', 'timeInterval'].forEach(
|
|
145
|
-
(field) => {
|
|
146
|
-
it(`throws an error when ${field} is missing`, function() {
|
|
147
|
-
promise = assetMetrics.create(
|
|
148
|
-
assetTypeId,
|
|
149
|
-
omit(assetMetric, [field])
|
|
150
|
-
);
|
|
151
|
-
|
|
152
|
-
return expect(promise).to.be.rejectedWith(
|
|
153
|
-
`A ${field} is required to create a new asset metric.`
|
|
154
|
-
);
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
);
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
describe('delete', function() {
|
|
162
|
-
context('when all required information is supplied', function() {
|
|
163
|
-
let expectedAssetMetricId;
|
|
164
|
-
let promise;
|
|
165
|
-
|
|
166
|
-
beforeEach(function() {
|
|
167
|
-
expectedAssetMetricId = fixture.build('assetMetric').id;
|
|
168
|
-
|
|
169
|
-
const assetMetrics = new AssetMetrics(
|
|
170
|
-
baseSdk,
|
|
171
|
-
baseRequest,
|
|
172
|
-
expectedHost
|
|
173
|
-
);
|
|
174
|
-
|
|
175
|
-
promise = assetMetrics.delete(expectedAssetMetricId);
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it('requests to delete the asset metric', function() {
|
|
179
|
-
expect(baseRequest.delete).to.be.calledWith(
|
|
180
|
-
`${expectedHost}/assets/metrics/${expectedAssetMetricId}`
|
|
181
|
-
);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it('returns a fulfilled promise', function() {
|
|
185
|
-
return expect(promise).to.be.fulfilled;
|
|
186
|
-
});
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
context('when there is missing required information', function() {
|
|
190
|
-
it('throws an error when the asset metric ID is missing', function() {
|
|
191
|
-
const assetMetrics = new AssetMetrics(
|
|
192
|
-
baseSdk,
|
|
193
|
-
baseRequest,
|
|
194
|
-
expectedHost
|
|
195
|
-
);
|
|
196
|
-
|
|
197
|
-
const promise = assetMetrics.delete();
|
|
198
|
-
|
|
199
|
-
return expect(promise).to.be.rejectedWith(
|
|
200
|
-
'An asset metric ID is required for deleting an asset metric.'
|
|
201
|
-
);
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
describe('get', function() {
|
|
207
|
-
context('the asset metric ID is provided', function() {
|
|
208
|
-
let assetMetricFromServerAfterFormat;
|
|
209
|
-
let assetMetricFromServerBeforeFormat;
|
|
210
|
-
let expectedAssetMetricId;
|
|
211
|
-
let promise;
|
|
212
|
-
let request;
|
|
213
|
-
let toCamelCase;
|
|
214
|
-
|
|
215
|
-
beforeEach(function() {
|
|
216
|
-
expectedAssetMetricId = fixture.build('assetMetric').id;
|
|
217
|
-
assetMetricFromServerAfterFormat = fixture.build('assetMetric', {
|
|
218
|
-
id: expectedAssetMetricId
|
|
219
|
-
});
|
|
220
|
-
assetMetricFromServerBeforeFormat = fixture.build(
|
|
221
|
-
'assetMetric',
|
|
222
|
-
assetMetricFromServerAfterFormat,
|
|
223
|
-
{ fromServer: true }
|
|
224
|
-
);
|
|
225
|
-
|
|
226
|
-
request = {
|
|
227
|
-
...baseRequest,
|
|
228
|
-
get: sinon.stub().resolves(assetMetricFromServerBeforeFormat)
|
|
229
|
-
};
|
|
230
|
-
toCamelCase = sinon
|
|
231
|
-
.stub(objectUtils, 'toCamelCase')
|
|
232
|
-
.returns(assetMetricFromServerAfterFormat);
|
|
233
|
-
|
|
234
|
-
const assetMetrics = new AssetMetrics(baseSdk, request, expectedHost);
|
|
235
|
-
|
|
236
|
-
promise = assetMetrics.get(expectedAssetMetricId);
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
it('gets the asset metric from the server', function() {
|
|
240
|
-
expect(request.get).to.be.calledWith(
|
|
241
|
-
`${expectedHost}/assets/metrics/${expectedAssetMetricId}`
|
|
242
|
-
);
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
it('formats the asset metric object', function() {
|
|
246
|
-
return promise.then(() => {
|
|
247
|
-
expect(toCamelCase).to.be.deep.calledWith(
|
|
248
|
-
assetMetricFromServerBeforeFormat
|
|
249
|
-
);
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
it('returns the requested asset metric', function() {
|
|
254
|
-
return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
|
|
255
|
-
assetMetricFromServerAfterFormat
|
|
256
|
-
);
|
|
257
|
-
});
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
context('the asset metric ID is not provided', function() {
|
|
261
|
-
it('throws an error', function() {
|
|
262
|
-
const assetMetrics = new AssetMetrics(
|
|
263
|
-
baseSdk,
|
|
264
|
-
baseRequest,
|
|
265
|
-
expectedHost
|
|
266
|
-
);
|
|
267
|
-
const promise = assetMetrics.get();
|
|
268
|
-
|
|
269
|
-
return expect(promise).to.be.rejectedWith(
|
|
270
|
-
'An asset metric ID is required for getting information about an asset metric.'
|
|
271
|
-
);
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
describe('getByAssetId', function() {
|
|
277
|
-
context('when all required information is supplied', function() {
|
|
278
|
-
let asset;
|
|
279
|
-
let formatPaginatedDataFromServer;
|
|
280
|
-
let numberOfAssetMetrics;
|
|
281
|
-
let metricsFiltersAfterFormat;
|
|
282
|
-
let metricsFiltersBeforeFormat;
|
|
283
|
-
let promise;
|
|
284
|
-
let request;
|
|
285
|
-
let toSnakeCase;
|
|
286
|
-
let valuesFromServerAfterFormat;
|
|
287
|
-
let valuesFromServerBeforeFormat;
|
|
288
|
-
|
|
289
|
-
beforeEach(function() {
|
|
290
|
-
numberOfAssetMetrics = faker.random.number({ min: 1, max: 10 });
|
|
291
|
-
asset = fixture.build('asset');
|
|
292
|
-
metricsFiltersBeforeFormat = {
|
|
293
|
-
limit: faker.random.number({ min: 10, max: 1000 }),
|
|
294
|
-
offset: faker.random.number({ max: 1000 })
|
|
295
|
-
};
|
|
296
|
-
metricsFiltersAfterFormat = {
|
|
297
|
-
limit: faker.random.number({ min: 10, max: 1000 }),
|
|
298
|
-
offset: faker.random.number({ max: 1000 })
|
|
299
|
-
};
|
|
300
|
-
valuesFromServerAfterFormat = {
|
|
301
|
-
_metadata: fixture.build('paginationMetadata'),
|
|
302
|
-
records: fixture.buildList('assetMetric', numberOfAssetMetrics, {
|
|
303
|
-
assetTypeId: asset.assetTypeId
|
|
304
|
-
})
|
|
305
|
-
};
|
|
306
|
-
valuesFromServerBeforeFormat = {
|
|
307
|
-
...valuesFromServerAfterFormat,
|
|
308
|
-
records: valuesFromServerAfterFormat.records.map((values) =>
|
|
309
|
-
fixture.build('assetMetric', values, { fromServer: true })
|
|
310
|
-
)
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
formatPaginatedDataFromServer = sinon
|
|
314
|
-
.stub(paginationUtils, 'formatPaginatedDataFromServer')
|
|
315
|
-
.returns(valuesFromServerAfterFormat);
|
|
316
|
-
request = {
|
|
317
|
-
...baseRequest,
|
|
318
|
-
get: sinon.stub().resolves(valuesFromServerBeforeFormat)
|
|
319
|
-
};
|
|
320
|
-
toSnakeCase = sinon
|
|
321
|
-
.stub(objectUtils, 'toSnakeCase')
|
|
322
|
-
.returns(metricsFiltersAfterFormat);
|
|
323
|
-
|
|
324
|
-
const assetMetrics = new AssetMetrics(baseSdk, request, expectedHost);
|
|
325
|
-
promise = assetMetrics.getByAssetId(
|
|
326
|
-
asset.id,
|
|
327
|
-
metricsFiltersBeforeFormat
|
|
328
|
-
);
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
it('formats the pagination options sent to the server', function() {
|
|
332
|
-
expect(toSnakeCase).to.be.calledWith(metricsFiltersBeforeFormat);
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
it('gets a list of the asset metrics from the server', function() {
|
|
336
|
-
expect(request.get).to.be.calledWith(
|
|
337
|
-
`${expectedHost}/assets/${asset.id}/metrics`,
|
|
338
|
-
{ params: metricsFiltersAfterFormat }
|
|
339
|
-
);
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
it('formats the asset metric data', function() {
|
|
343
|
-
return promise.then(() => {
|
|
344
|
-
expect(formatPaginatedDataFromServer).to.be.calledWith(
|
|
345
|
-
valuesFromServerBeforeFormat
|
|
346
|
-
);
|
|
347
|
-
});
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it('resolves with a list of asset metrics', function() {
|
|
351
|
-
return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
|
|
352
|
-
valuesFromServerAfterFormat
|
|
353
|
-
);
|
|
354
|
-
});
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
context('when there is missing required information', function() {
|
|
358
|
-
let promise;
|
|
359
|
-
|
|
360
|
-
beforeEach(function() {
|
|
361
|
-
const assetMetrics = new AssetMetrics(
|
|
362
|
-
baseSdk,
|
|
363
|
-
baseRequest,
|
|
364
|
-
expectedHost
|
|
365
|
-
);
|
|
366
|
-
|
|
367
|
-
promise = assetMetrics.getByAssetId();
|
|
368
|
-
});
|
|
369
|
-
|
|
370
|
-
it('throws an error when the asset type ID is missing', function() {
|
|
371
|
-
return expect(promise).to.be.rejectedWith(
|
|
372
|
-
'An asset ID is required to get a list of all asset metrics.'
|
|
373
|
-
);
|
|
374
|
-
});
|
|
375
|
-
});
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
describe('getByAssetTypeId', function() {
|
|
379
|
-
context('when all required information is supplied', function() {
|
|
380
|
-
let assetTypeId;
|
|
381
|
-
let formatPaginatedDataFromServer;
|
|
382
|
-
let numberOfAssetMetrics;
|
|
383
|
-
let metricsFiltersAfterFormat;
|
|
384
|
-
let metricsFiltersBeforeFormat;
|
|
385
|
-
let promise;
|
|
386
|
-
let request;
|
|
387
|
-
let toSnakeCase;
|
|
388
|
-
let valuesFromServerAfterFormat;
|
|
389
|
-
let valuesFromServerBeforeFormat;
|
|
390
|
-
|
|
391
|
-
beforeEach(function() {
|
|
392
|
-
numberOfAssetMetrics = faker.random.number({ min: 1, max: 10 });
|
|
393
|
-
assetTypeId = fixture.build('assetType').id;
|
|
394
|
-
metricsFiltersBeforeFormat = {
|
|
395
|
-
limit: faker.random.number({ min: 10, max: 1000 }),
|
|
396
|
-
offset: faker.random.number({ max: 1000 })
|
|
397
|
-
};
|
|
398
|
-
metricsFiltersAfterFormat = {
|
|
399
|
-
limit: faker.random.number({ min: 10, max: 1000 }),
|
|
400
|
-
offset: faker.random.number({ max: 1000 })
|
|
401
|
-
};
|
|
402
|
-
valuesFromServerAfterFormat = {
|
|
403
|
-
_metadata: fixture.build('paginationMetadata'),
|
|
404
|
-
records: fixture.buildList('assetMetric', numberOfAssetMetrics, {
|
|
405
|
-
assetTypeId
|
|
406
|
-
})
|
|
407
|
-
};
|
|
408
|
-
valuesFromServerBeforeFormat = {
|
|
409
|
-
...valuesFromServerAfterFormat,
|
|
410
|
-
records: valuesFromServerAfterFormat.records.map((values) =>
|
|
411
|
-
fixture.build('assetMetric', values, { fromServer: true })
|
|
412
|
-
)
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
formatPaginatedDataFromServer = sinon
|
|
416
|
-
.stub(paginationUtils, 'formatPaginatedDataFromServer')
|
|
417
|
-
.returns(valuesFromServerAfterFormat);
|
|
418
|
-
request = {
|
|
419
|
-
...baseRequest,
|
|
420
|
-
get: sinon.stub().resolves(valuesFromServerBeforeFormat)
|
|
421
|
-
};
|
|
422
|
-
toSnakeCase = sinon
|
|
423
|
-
.stub(objectUtils, 'toSnakeCase')
|
|
424
|
-
.returns(metricsFiltersAfterFormat);
|
|
425
|
-
|
|
426
|
-
const assetMetrics = new AssetMetrics(baseSdk, request, expectedHost);
|
|
427
|
-
promise = assetMetrics.getByAssetTypeId(
|
|
428
|
-
assetTypeId,
|
|
429
|
-
metricsFiltersBeforeFormat
|
|
430
|
-
);
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
it('formats the pagination options sent to the server', function() {
|
|
434
|
-
expect(toSnakeCase).to.be.calledWith(metricsFiltersBeforeFormat);
|
|
435
|
-
});
|
|
436
|
-
|
|
437
|
-
it('gets a list of the asset metrics from the server', function() {
|
|
438
|
-
expect(request.get).to.be.calledWith(
|
|
439
|
-
`${expectedHost}/assets/types/${assetTypeId}/metrics`,
|
|
440
|
-
{ params: metricsFiltersAfterFormat }
|
|
441
|
-
);
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
it('formats the asset metric data', function() {
|
|
445
|
-
return promise.then(() => {
|
|
446
|
-
expect(formatPaginatedDataFromServer).to.be.calledWith(
|
|
447
|
-
valuesFromServerBeforeFormat
|
|
448
|
-
);
|
|
449
|
-
});
|
|
450
|
-
});
|
|
451
|
-
|
|
452
|
-
it('resolves with a list of asset metrics', function() {
|
|
453
|
-
return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
|
|
454
|
-
valuesFromServerAfterFormat
|
|
455
|
-
);
|
|
456
|
-
});
|
|
457
|
-
});
|
|
458
|
-
|
|
459
|
-
context('when there is missing required information', function() {
|
|
460
|
-
let promise;
|
|
461
|
-
|
|
462
|
-
beforeEach(function() {
|
|
463
|
-
const assetMetrics = new AssetMetrics(
|
|
464
|
-
baseSdk,
|
|
465
|
-
baseRequest,
|
|
466
|
-
expectedHost
|
|
467
|
-
);
|
|
468
|
-
|
|
469
|
-
promise = assetMetrics.getByAssetTypeId();
|
|
470
|
-
});
|
|
471
|
-
|
|
472
|
-
it('throws an error when the asset type ID is missing', function() {
|
|
473
|
-
return expect(promise).to.be.rejectedWith(
|
|
474
|
-
'An asset type ID is required to get a list of all asset metrics.'
|
|
475
|
-
);
|
|
476
|
-
});
|
|
477
|
-
});
|
|
478
|
-
});
|
|
479
|
-
|
|
480
|
-
describe('getValuesByMetricIdsAssetIds', function() {
|
|
481
|
-
let numberOfAssetMetricValues;
|
|
482
|
-
let numberOfAssetMetrics;
|
|
483
|
-
let numberOfAssets;
|
|
484
|
-
|
|
485
|
-
let assetMetricValuesCompact;
|
|
486
|
-
let assetMetricValuesCompactFromServer;
|
|
487
|
-
let assetMetricObjects;
|
|
488
|
-
let assets;
|
|
489
|
-
|
|
490
|
-
let metricsFiltersAfterFormat;
|
|
491
|
-
let metricsFiltersBeforeFormat;
|
|
492
|
-
let promise;
|
|
493
|
-
let request;
|
|
494
|
-
let toSnakeCase;
|
|
495
|
-
let strigifyParamsWithCommaSeparatedArrays;
|
|
496
|
-
|
|
497
|
-
let valuesFromServer;
|
|
498
|
-
let valuesFromServerFormatted;
|
|
499
|
-
|
|
500
|
-
beforeEach(function() {
|
|
501
|
-
numberOfAssetMetricValues = faker.random.number({ min: 1, max: 10 });
|
|
502
|
-
numberOfAssetMetrics = faker.random.number({ min: 1, max: 3 });
|
|
503
|
-
numberOfAssets = faker.random.number({ min: 1, max: 3 });
|
|
504
|
-
|
|
505
|
-
// camel case
|
|
506
|
-
assetMetricValuesCompact = fixture.buildList(
|
|
507
|
-
'assetMetricValueCompact',
|
|
508
|
-
numberOfAssetMetricValues
|
|
509
|
-
);
|
|
510
|
-
|
|
511
|
-
// snake case
|
|
512
|
-
assetMetricValuesCompactFromServer = fixture.buildList(
|
|
513
|
-
'assetMetricValueCompact',
|
|
514
|
-
numberOfAssetMetricValues,
|
|
515
|
-
null,
|
|
516
|
-
{ fromServer: true }
|
|
517
|
-
);
|
|
518
|
-
|
|
519
|
-
assetMetricObjects = fixture.buildList(
|
|
520
|
-
'assetMetric',
|
|
521
|
-
numberOfAssetMetrics
|
|
522
|
-
);
|
|
523
|
-
assets = fixture.buildList('asset', numberOfAssets);
|
|
524
|
-
|
|
525
|
-
const metricObjects = assetMetricObjects.reduce((acc, metric) => {
|
|
526
|
-
return { ...acc, [metric.label]: assetMetricValuesCompactFromServer };
|
|
527
|
-
}, {});
|
|
528
|
-
|
|
529
|
-
valuesFromServer = assets.reduce((acc, asset) => {
|
|
530
|
-
return { ...acc, [asset.id]: { ...metricObjects } };
|
|
531
|
-
}, {});
|
|
532
|
-
|
|
533
|
-
const metricObjectsFormatted = assetMetricObjects.reduce(
|
|
534
|
-
(acc, metric) => {
|
|
535
|
-
return { ...acc, [metric.label]: assetMetricValuesCompact };
|
|
536
|
-
},
|
|
537
|
-
{}
|
|
538
|
-
);
|
|
539
|
-
|
|
540
|
-
valuesFromServerFormatted = assets.reduce((acc, asset) => {
|
|
541
|
-
return { ...acc, [asset.id]: { ...metricObjectsFormatted } };
|
|
542
|
-
}, {});
|
|
543
|
-
|
|
544
|
-
request = {
|
|
545
|
-
...baseRequest,
|
|
546
|
-
get: sinon.stub().resolves(valuesFromServer)
|
|
547
|
-
};
|
|
548
|
-
|
|
549
|
-
metricsFiltersBeforeFormat = {
|
|
550
|
-
assetIds: assets.map((asset) => asset.id),
|
|
551
|
-
labels: assetMetricObjects.map((assetMetric) => assetMetric.label)
|
|
552
|
-
};
|
|
553
|
-
|
|
554
|
-
metricsFiltersAfterFormat = {
|
|
555
|
-
asset_ids: metricsFiltersBeforeFormat.assetIds,
|
|
556
|
-
labels: metricsFiltersBeforeFormat.labels
|
|
557
|
-
};
|
|
558
|
-
|
|
559
|
-
toSnakeCase = sinon
|
|
560
|
-
.stub(objectUtils, 'toSnakeCase')
|
|
561
|
-
.returns(metricsFiltersAfterFormat);
|
|
562
|
-
|
|
563
|
-
strigifyParamsWithCommaSeparatedArrays = sinon.stub(
|
|
564
|
-
urlUtils,
|
|
565
|
-
'stringifyParamsWithCommaSeparatedArrays'
|
|
566
|
-
);
|
|
567
|
-
});
|
|
568
|
-
|
|
569
|
-
context('when all required information is supplied', function() {
|
|
570
|
-
beforeEach(function() {
|
|
571
|
-
const assetMetrics = new AssetMetrics(baseSdk, request, expectedHost);
|
|
572
|
-
promise = assetMetrics.getValuesByMetricIdsAssetIds(
|
|
573
|
-
metricsFiltersBeforeFormat
|
|
574
|
-
);
|
|
575
|
-
});
|
|
576
|
-
|
|
577
|
-
it('called with the correctly formatted params', function() {
|
|
578
|
-
expect(request.get).to.be.calledWith(
|
|
579
|
-
`${expectedHost}/assets/metrics/values`,
|
|
580
|
-
{
|
|
581
|
-
params: metricsFiltersAfterFormat,
|
|
582
|
-
paramsSerializer: strigifyParamsWithCommaSeparatedArrays
|
|
583
|
-
}
|
|
584
|
-
);
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
it('resolves with a list of the asset metrics by asset id and label from the server', function() {
|
|
588
|
-
return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
|
|
589
|
-
valuesFromServerFormatted
|
|
590
|
-
);
|
|
591
|
-
});
|
|
592
|
-
|
|
593
|
-
it('formats the url request params into the right format', function() {
|
|
594
|
-
expect(toSnakeCase).to.be.deep.calledWith(metricsFiltersBeforeFormat);
|
|
595
|
-
});
|
|
596
|
-
});
|
|
597
|
-
|
|
598
|
-
context('when any parameter is missing from filters', function() {
|
|
599
|
-
const missingParam = faker.random.arrayElement(['assetIds', 'labels']);
|
|
600
|
-
|
|
601
|
-
beforeEach(function() {
|
|
602
|
-
const assetMetrics = new AssetMetrics(baseSdk, request, expectedHost);
|
|
603
|
-
delete metricsFiltersBeforeFormat[missingParam];
|
|
604
|
-
promise = assetMetrics.getValuesByMetricIdsAssetIds(
|
|
605
|
-
metricsFiltersBeforeFormat
|
|
606
|
-
);
|
|
607
|
-
});
|
|
608
|
-
|
|
609
|
-
it('throws an Error', function() {
|
|
610
|
-
return expect(promise).to.be.eventually.rejectedWith(
|
|
611
|
-
`The ${missingParam} param is required to fetch data.`
|
|
612
|
-
);
|
|
613
|
-
});
|
|
614
|
-
});
|
|
615
|
-
});
|
|
616
|
-
|
|
617
|
-
describe('update', function() {
|
|
618
|
-
context('when all required information is supplied', function() {
|
|
619
|
-
let assetMetricToServerAfterFormat;
|
|
620
|
-
let assetMetricToServerBeforeFormat;
|
|
621
|
-
let toSnakeCase;
|
|
622
|
-
let promise;
|
|
623
|
-
|
|
624
|
-
beforeEach(function() {
|
|
625
|
-
assetMetricToServerAfterFormat = fixture.build('assetMetric', null, {
|
|
626
|
-
fromServer: true
|
|
627
|
-
});
|
|
628
|
-
assetMetricToServerBeforeFormat = fixture.build('assetMetric');
|
|
629
|
-
|
|
630
|
-
toSnakeCase = sinon
|
|
631
|
-
.stub(objectUtils, 'toSnakeCase')
|
|
632
|
-
.returns(assetMetricToServerAfterFormat);
|
|
633
|
-
|
|
634
|
-
const assetMetrics = new AssetMetrics(
|
|
635
|
-
baseSdk,
|
|
636
|
-
baseRequest,
|
|
637
|
-
expectedHost
|
|
638
|
-
);
|
|
639
|
-
|
|
640
|
-
promise = assetMetrics.update(
|
|
641
|
-
assetMetricToServerBeforeFormat.id,
|
|
642
|
-
assetMetricToServerBeforeFormat
|
|
643
|
-
);
|
|
644
|
-
});
|
|
645
|
-
|
|
646
|
-
it('formats the data into the right format', function() {
|
|
647
|
-
expect(toSnakeCase).to.be.deep.calledWith(
|
|
648
|
-
assetMetricToServerBeforeFormat,
|
|
649
|
-
{
|
|
650
|
-
excludeKeys: [
|
|
651
|
-
'createdAt',
|
|
652
|
-
'id',
|
|
653
|
-
'label',
|
|
654
|
-
'organizationId',
|
|
655
|
-
'updatedAt'
|
|
656
|
-
]
|
|
657
|
-
}
|
|
658
|
-
);
|
|
659
|
-
});
|
|
660
|
-
|
|
661
|
-
it('updates the asset metric', function() {
|
|
662
|
-
expect(baseRequest.put).to.be.deep.calledWith(
|
|
663
|
-
`${expectedHost}/assets/metrics/${
|
|
664
|
-
assetMetricToServerBeforeFormat.id
|
|
665
|
-
}`,
|
|
666
|
-
assetMetricToServerAfterFormat
|
|
667
|
-
);
|
|
668
|
-
});
|
|
669
|
-
|
|
670
|
-
it('returns a fulfilled promise', function() {
|
|
671
|
-
return expect(promise).to.be.fulfilled;
|
|
672
|
-
});
|
|
673
|
-
});
|
|
674
|
-
|
|
675
|
-
context(
|
|
676
|
-
'when there is missing or malformed required information',
|
|
677
|
-
function() {
|
|
678
|
-
let assetMetrics;
|
|
679
|
-
|
|
680
|
-
beforeEach(function() {
|
|
681
|
-
assetMetrics = new AssetMetrics(baseSdk, baseRequest, expectedHost);
|
|
682
|
-
});
|
|
683
|
-
|
|
684
|
-
it('throws an error when there is no provided asset metric ID', function() {
|
|
685
|
-
const assetAttributeUpdate = fixture.build('assetMetric');
|
|
686
|
-
const promise = assetMetrics.update(null, assetAttributeUpdate);
|
|
687
|
-
|
|
688
|
-
return expect(promise).to.be.rejectedWith(
|
|
689
|
-
'An asset metric ID is required to update an asset metric.'
|
|
690
|
-
);
|
|
691
|
-
});
|
|
692
|
-
|
|
693
|
-
it('throws an error when there is no update provided', function() {
|
|
694
|
-
const assetAttributeUpdate = fixture.build('assetMetric');
|
|
695
|
-
const promise = assetMetrics.update(assetAttributeUpdate.id);
|
|
696
|
-
|
|
697
|
-
return expect(promise).to.be.rejectedWith(
|
|
698
|
-
'An update is required to update an asset metric.'
|
|
699
|
-
);
|
|
700
|
-
});
|
|
701
|
-
|
|
702
|
-
it('throws an error when the update is not a well-formed object', function() {
|
|
703
|
-
const assetAttributeUpdate = fixture.build('assetMetric');
|
|
704
|
-
const promise = assetMetrics.update(assetAttributeUpdate.id, [
|
|
705
|
-
assetAttributeUpdate
|
|
706
|
-
]);
|
|
707
|
-
|
|
708
|
-
return expect(promise).to.be.rejectedWith(
|
|
709
|
-
'The asset metric update must be a well-formed object with the data you wish to update.'
|
|
710
|
-
);
|
|
711
|
-
});
|
|
712
|
-
}
|
|
713
|
-
);
|
|
714
|
-
});
|
|
715
|
-
|
|
716
|
-
describe('createValue', function() {
|
|
717
|
-
context('when all required information is supplied', function() {
|
|
718
|
-
let assetId;
|
|
719
|
-
let promise;
|
|
720
|
-
let request;
|
|
721
|
-
let toCamelCase;
|
|
722
|
-
let toSnakeCase;
|
|
723
|
-
let valueFromServerAfterFormat;
|
|
724
|
-
let valueFromServerBeforeFormat;
|
|
725
|
-
let valueToServerAfterFormat;
|
|
726
|
-
let valueToServerBeforeFormat;
|
|
727
|
-
|
|
728
|
-
beforeEach(function() {
|
|
729
|
-
valueToServerBeforeFormat = fixture.build('assetMetricValue');
|
|
730
|
-
valueToServerAfterFormat = fixture.build(
|
|
731
|
-
'assetMetricValue',
|
|
732
|
-
valueToServerBeforeFormat,
|
|
733
|
-
{
|
|
734
|
-
fromServer: true
|
|
735
|
-
}
|
|
736
|
-
);
|
|
737
|
-
valueFromServerAfterFormat = fixture.build('assetMetricValue');
|
|
738
|
-
valueFromServerBeforeFormat = fixture.build(
|
|
739
|
-
'assetMetricValue',
|
|
740
|
-
valueFromServerAfterFormat,
|
|
741
|
-
{ fromServer: true }
|
|
742
|
-
);
|
|
743
|
-
assetId = fixture.build('asset').id;
|
|
744
|
-
|
|
745
|
-
request = {
|
|
746
|
-
...baseRequest,
|
|
747
|
-
post: sinon.stub().resolves(valueFromServerBeforeFormat)
|
|
748
|
-
};
|
|
749
|
-
toCamelCase = sinon
|
|
750
|
-
.stub(objectUtils, 'toCamelCase')
|
|
751
|
-
.returns(valueFromServerAfterFormat);
|
|
752
|
-
toSnakeCase = sinon
|
|
753
|
-
.stub(objectUtils, 'toSnakeCase')
|
|
754
|
-
.returns(valueToServerAfterFormat);
|
|
755
|
-
|
|
756
|
-
const assetMetrics = new AssetMetrics(baseSdk, request, expectedHost);
|
|
757
|
-
|
|
758
|
-
promise = assetMetrics.createValue(assetId, valueToServerBeforeFormat);
|
|
759
|
-
});
|
|
760
|
-
|
|
761
|
-
it('formats the submitted asset metric value object to send to the server ', function() {
|
|
762
|
-
expect(toSnakeCase).to.be.calledWith(valueToServerBeforeFormat);
|
|
763
|
-
});
|
|
764
|
-
|
|
765
|
-
it('creates a new asset metric value', function() {
|
|
766
|
-
expect(request.post).to.be.calledWith(
|
|
767
|
-
`${expectedHost}/assets/${assetId}/metrics/${
|
|
768
|
-
valueToServerBeforeFormat.assetMetricId
|
|
769
|
-
}/values`,
|
|
770
|
-
valueToServerAfterFormat
|
|
771
|
-
);
|
|
772
|
-
});
|
|
773
|
-
|
|
774
|
-
it('formats the returned asset metric value object', function() {
|
|
775
|
-
return promise.then(() => {
|
|
776
|
-
expect(toCamelCase).to.be.calledWith(valueFromServerBeforeFormat);
|
|
777
|
-
});
|
|
778
|
-
});
|
|
779
|
-
|
|
780
|
-
it('returns a fulfilled promise with the new asset metric value information', function() {
|
|
781
|
-
return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
|
|
782
|
-
valueFromServerAfterFormat
|
|
783
|
-
);
|
|
784
|
-
});
|
|
785
|
-
});
|
|
786
|
-
|
|
787
|
-
context('when there is missing required information', function() {
|
|
788
|
-
let value;
|
|
789
|
-
let assetMetrics;
|
|
790
|
-
|
|
791
|
-
beforeEach(function() {
|
|
792
|
-
value = fixture.build('assetMetricValue');
|
|
793
|
-
|
|
794
|
-
assetMetrics = new AssetMetrics(baseSdk, baseRequest, expectedHost);
|
|
795
|
-
});
|
|
796
|
-
|
|
797
|
-
it('throws an error if there is no asset ID provided', function() {
|
|
798
|
-
const promise = assetMetrics.createValue(null, value);
|
|
799
|
-
|
|
800
|
-
return expect(promise).to.be.rejectedWith(
|
|
801
|
-
'An asset ID is required to create a new asset metric value.'
|
|
802
|
-
);
|
|
803
|
-
});
|
|
804
|
-
|
|
805
|
-
[
|
|
806
|
-
'assetMetricId',
|
|
807
|
-
'effectiveEndDate',
|
|
808
|
-
'effectiveStartDate',
|
|
809
|
-
'value'
|
|
810
|
-
].forEach(function(field) {
|
|
811
|
-
it(`throws an error when ${field} is missing`, function() {
|
|
812
|
-
const promise = assetMetrics.createValue(
|
|
813
|
-
value.id,
|
|
814
|
-
omit(value, [field])
|
|
815
|
-
);
|
|
816
|
-
|
|
817
|
-
return expect(promise).to.be.rejectedWith(
|
|
818
|
-
`A ${field} is required to create a new asset metric value.`
|
|
819
|
-
);
|
|
820
|
-
});
|
|
821
|
-
});
|
|
822
|
-
});
|
|
823
|
-
});
|
|
824
|
-
|
|
825
|
-
describe('deleteValue', function() {
|
|
826
|
-
context('when all required information is supplied', function() {
|
|
827
|
-
let valueId;
|
|
828
|
-
let promise;
|
|
829
|
-
|
|
830
|
-
beforeEach(function() {
|
|
831
|
-
valueId = fixture.build('assetMetricValue').id;
|
|
832
|
-
|
|
833
|
-
const assetMetrics = new AssetMetrics(
|
|
834
|
-
baseSdk,
|
|
835
|
-
baseRequest,
|
|
836
|
-
expectedHost
|
|
837
|
-
);
|
|
838
|
-
|
|
839
|
-
promise = assetMetrics.deleteValue(valueId);
|
|
840
|
-
});
|
|
841
|
-
|
|
842
|
-
it('requests to delete the asset metric value', function() {
|
|
843
|
-
expect(baseRequest.delete).to.be.calledWith(
|
|
844
|
-
`${expectedHost}/assets/metrics/values/${valueId}`
|
|
845
|
-
);
|
|
846
|
-
});
|
|
847
|
-
|
|
848
|
-
it('returns a fulfilled promise', function() {
|
|
849
|
-
return expect(promise).to.be.fulfilled;
|
|
850
|
-
});
|
|
851
|
-
});
|
|
852
|
-
|
|
853
|
-
context('when there is missing required information', function() {
|
|
854
|
-
it('throws an error when the asset metric value ID is missing', function() {
|
|
855
|
-
const assetMetrics = new AssetMetrics(
|
|
856
|
-
baseSdk,
|
|
857
|
-
baseRequest,
|
|
858
|
-
expectedHost
|
|
859
|
-
);
|
|
860
|
-
|
|
861
|
-
const promise = assetMetrics.deleteValue();
|
|
862
|
-
|
|
863
|
-
return expect(promise).to.be.rejectedWith(
|
|
864
|
-
'An asset metric value ID is required for deleting an asset metric value.'
|
|
865
|
-
);
|
|
866
|
-
});
|
|
867
|
-
});
|
|
868
|
-
});
|
|
869
|
-
|
|
870
|
-
describe('getValuesByAssetId', function() {
|
|
871
|
-
context('when all required information is supplied', function() {
|
|
872
|
-
let assetId;
|
|
873
|
-
let formatPaginatedDataFromServer;
|
|
874
|
-
let metricValuesFiltersBeforeFormat;
|
|
875
|
-
let metricValuesFiltersAfterFormat;
|
|
876
|
-
let promise;
|
|
877
|
-
let request;
|
|
878
|
-
let toSnakeCase;
|
|
879
|
-
let valuesFromServerAfterFormat;
|
|
880
|
-
let valuesFromServerBeforeFormat;
|
|
881
|
-
|
|
882
|
-
beforeEach(function() {
|
|
883
|
-
assetId = fixture.build('asset').id;
|
|
884
|
-
metricValuesFiltersBeforeFormat = {
|
|
885
|
-
limit: faker.random.number({ min: 10, max: 1000 }),
|
|
886
|
-
offset: faker.random.number({ max: 1000 })
|
|
887
|
-
};
|
|
888
|
-
metricValuesFiltersAfterFormat = {
|
|
889
|
-
limit: faker.random.number({ min: 10, max: 1000 }),
|
|
890
|
-
offset: faker.random.number({ max: 1000 })
|
|
891
|
-
};
|
|
892
|
-
valuesFromServerAfterFormat = {
|
|
893
|
-
_metadata: fixture.build('paginationMetadata'),
|
|
894
|
-
records: fixture.buildList(
|
|
895
|
-
'assetMetricValue',
|
|
896
|
-
faker.random.number({ min: 1, max: 20 }),
|
|
897
|
-
{ assetId }
|
|
898
|
-
)
|
|
899
|
-
};
|
|
900
|
-
valuesFromServerBeforeFormat = {
|
|
901
|
-
...valuesFromServerAfterFormat,
|
|
902
|
-
records: valuesFromServerAfterFormat.records.map((values) =>
|
|
903
|
-
fixture.build('assetMetricValue', values, { fromServer: true })
|
|
904
|
-
)
|
|
905
|
-
};
|
|
906
|
-
|
|
907
|
-
formatPaginatedDataFromServer = sinon
|
|
908
|
-
.stub(paginationUtils, 'formatPaginatedDataFromServer')
|
|
909
|
-
.returns(valuesFromServerAfterFormat);
|
|
910
|
-
request = {
|
|
911
|
-
...baseRequest,
|
|
912
|
-
get: sinon.stub().resolves(valuesFromServerBeforeFormat)
|
|
913
|
-
};
|
|
914
|
-
toSnakeCase = sinon
|
|
915
|
-
.stub(objectUtils, 'toSnakeCase')
|
|
916
|
-
.returns(metricValuesFiltersAfterFormat);
|
|
917
|
-
|
|
918
|
-
const assetMetrics = new AssetMetrics(baseSdk, request, expectedHost);
|
|
919
|
-
promise = assetMetrics.getValuesByAssetId(
|
|
920
|
-
assetId,
|
|
921
|
-
metricValuesFiltersBeforeFormat
|
|
922
|
-
);
|
|
923
|
-
});
|
|
924
|
-
|
|
925
|
-
it('formats the pagination options sent to the server', function() {
|
|
926
|
-
expect(toSnakeCase).to.be.calledWith(metricValuesFiltersBeforeFormat);
|
|
927
|
-
});
|
|
928
|
-
|
|
929
|
-
it('gets a list of asset metric values from the server', function() {
|
|
930
|
-
expect(request.get).to.be.calledWith(
|
|
931
|
-
`${expectedHost}/assets/${assetId}/metrics/values`,
|
|
932
|
-
{ params: metricValuesFiltersAfterFormat }
|
|
933
|
-
);
|
|
934
|
-
});
|
|
935
|
-
|
|
936
|
-
it('formats the asset metric value data', function() {
|
|
937
|
-
return promise.then(() => {
|
|
938
|
-
expect(formatPaginatedDataFromServer).to.be.calledWith(
|
|
939
|
-
valuesFromServerBeforeFormat
|
|
940
|
-
);
|
|
941
|
-
});
|
|
942
|
-
});
|
|
943
|
-
|
|
944
|
-
it('resolves with a list of asset metric values', function() {
|
|
945
|
-
return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
|
|
946
|
-
valuesFromServerAfterFormat
|
|
947
|
-
);
|
|
948
|
-
});
|
|
949
|
-
});
|
|
950
|
-
|
|
951
|
-
context('when there is missing required information', function() {
|
|
952
|
-
it('throws an error when the asset ID is missing', function() {
|
|
953
|
-
const assetMetrics = new AssetMetrics(
|
|
954
|
-
baseSdk,
|
|
955
|
-
baseRequest,
|
|
956
|
-
expectedHost
|
|
957
|
-
);
|
|
958
|
-
const promise = assetMetrics.getValuesByMetricId();
|
|
959
|
-
|
|
960
|
-
return expect(promise).to.be.rejectedWith(
|
|
961
|
-
'An asset ID is required to get a list of asset metric values.'
|
|
962
|
-
);
|
|
963
|
-
});
|
|
964
|
-
});
|
|
965
|
-
});
|
|
966
|
-
|
|
967
|
-
describe('getValuesByMetricId', function() {
|
|
968
|
-
context('when all required information is supplied', function() {
|
|
969
|
-
let assetId;
|
|
970
|
-
let metricId;
|
|
971
|
-
let formatPaginatedDataFromServer;
|
|
972
|
-
let metricValuesFiltersBeforeFormat;
|
|
973
|
-
let metricValuesFiltersAfterFormat;
|
|
974
|
-
let promise;
|
|
975
|
-
let request;
|
|
976
|
-
let toSnakeCase;
|
|
977
|
-
let valuesFromServerAfterFormat;
|
|
978
|
-
let valuesFromServerBeforeFormat;
|
|
979
|
-
|
|
980
|
-
beforeEach(function() {
|
|
981
|
-
assetId = fixture.build('asset').id;
|
|
982
|
-
metricId = fixture.build('assetMetric').id;
|
|
983
|
-
metricValuesFiltersBeforeFormat = {
|
|
984
|
-
limit: faker.random.number({ min: 10, max: 1000 }),
|
|
985
|
-
offset: faker.random.number({ max: 1000 })
|
|
986
|
-
};
|
|
987
|
-
metricValuesFiltersAfterFormat = {
|
|
988
|
-
limit: faker.random.number({ min: 10, max: 1000 }),
|
|
989
|
-
offset: faker.random.number({ max: 1000 })
|
|
990
|
-
};
|
|
991
|
-
valuesFromServerAfterFormat = {
|
|
992
|
-
_metadata: fixture.build('paginationMetadata'),
|
|
993
|
-
records: fixture.buildList(
|
|
994
|
-
'assetMetricValue',
|
|
995
|
-
faker.random.number({ min: 1, max: 20 }),
|
|
996
|
-
{
|
|
997
|
-
assetId,
|
|
998
|
-
assetMetricId: metricId
|
|
999
|
-
}
|
|
1000
|
-
)
|
|
1001
|
-
};
|
|
1002
|
-
valuesFromServerBeforeFormat = {
|
|
1003
|
-
...valuesFromServerAfterFormat,
|
|
1004
|
-
records: valuesFromServerAfterFormat.records.map((values) =>
|
|
1005
|
-
fixture.build('assetMetricValue', values, { fromServer: true })
|
|
1006
|
-
)
|
|
1007
|
-
};
|
|
1008
|
-
|
|
1009
|
-
formatPaginatedDataFromServer = sinon
|
|
1010
|
-
.stub(paginationUtils, 'formatPaginatedDataFromServer')
|
|
1011
|
-
.returns(valuesFromServerAfterFormat);
|
|
1012
|
-
request = {
|
|
1013
|
-
...baseRequest,
|
|
1014
|
-
get: sinon.stub().resolves(valuesFromServerBeforeFormat)
|
|
1015
|
-
};
|
|
1016
|
-
toSnakeCase = sinon
|
|
1017
|
-
.stub(objectUtils, 'toSnakeCase')
|
|
1018
|
-
.returns(metricValuesFiltersAfterFormat);
|
|
1019
|
-
|
|
1020
|
-
const assetMetrics = new AssetMetrics(baseSdk, request, expectedHost);
|
|
1021
|
-
promise = assetMetrics.getValuesByMetricId(
|
|
1022
|
-
assetId,
|
|
1023
|
-
metricId,
|
|
1024
|
-
metricValuesFiltersBeforeFormat
|
|
1025
|
-
);
|
|
1026
|
-
});
|
|
1027
|
-
|
|
1028
|
-
it('formats the pagination options sent to the server', function() {
|
|
1029
|
-
expect(toSnakeCase).to.be.calledWith(metricValuesFiltersBeforeFormat);
|
|
1030
|
-
});
|
|
1031
|
-
|
|
1032
|
-
it('gets a list of asset metric values from the server', function() {
|
|
1033
|
-
expect(request.get).to.be.calledWith(
|
|
1034
|
-
`${expectedHost}/assets/${assetId}/metrics/${metricId}/values`,
|
|
1035
|
-
{ params: metricValuesFiltersAfterFormat }
|
|
1036
|
-
);
|
|
1037
|
-
});
|
|
1038
|
-
|
|
1039
|
-
it('formats the asset metric value data', function() {
|
|
1040
|
-
return promise.then(() => {
|
|
1041
|
-
expect(formatPaginatedDataFromServer).to.be.calledWith(
|
|
1042
|
-
valuesFromServerBeforeFormat
|
|
1043
|
-
);
|
|
1044
|
-
});
|
|
1045
|
-
});
|
|
1046
|
-
|
|
1047
|
-
it('resolves with a list of asset metric values', function() {
|
|
1048
|
-
return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
|
|
1049
|
-
valuesFromServerAfterFormat
|
|
1050
|
-
);
|
|
1051
|
-
});
|
|
1052
|
-
});
|
|
1053
|
-
|
|
1054
|
-
context('when there is missing required information', function() {
|
|
1055
|
-
it('throws an error when the asset ID is missing', function() {
|
|
1056
|
-
const assetMetrics = new AssetMetrics(
|
|
1057
|
-
baseSdk,
|
|
1058
|
-
baseRequest,
|
|
1059
|
-
expectedHost
|
|
1060
|
-
);
|
|
1061
|
-
const promise = assetMetrics.getValuesByMetricId(
|
|
1062
|
-
null,
|
|
1063
|
-
fixture.build('assetMetric').id
|
|
1064
|
-
);
|
|
1065
|
-
|
|
1066
|
-
return expect(promise).to.be.rejectedWith(
|
|
1067
|
-
'An asset ID is required to get a list of asset metric values.'
|
|
1068
|
-
);
|
|
1069
|
-
});
|
|
1070
|
-
|
|
1071
|
-
it('throws an error when the asset metric ID is missing', function() {
|
|
1072
|
-
const assetMetrics = new AssetMetrics(
|
|
1073
|
-
baseSdk,
|
|
1074
|
-
baseRequest,
|
|
1075
|
-
expectedHost
|
|
1076
|
-
);
|
|
1077
|
-
const promise = assetMetrics.getValuesByMetricId(
|
|
1078
|
-
fixture.build('asset').id,
|
|
1079
|
-
null
|
|
1080
|
-
);
|
|
1081
|
-
|
|
1082
|
-
return expect(promise).to.be.rejectedWith(
|
|
1083
|
-
'An asset metric ID is required to get a list of asset metric values.'
|
|
1084
|
-
);
|
|
1085
|
-
});
|
|
1086
|
-
});
|
|
1087
|
-
});
|
|
1088
|
-
|
|
1089
|
-
describe('updateValue', function() {
|
|
1090
|
-
context('when all required information is supplied', function() {
|
|
1091
|
-
let promise;
|
|
1092
|
-
let toSnakeCase;
|
|
1093
|
-
let valueToServerAfterFormat;
|
|
1094
|
-
let valueToServerBeforeFormat;
|
|
1095
|
-
|
|
1096
|
-
beforeEach(function() {
|
|
1097
|
-
valueToServerBeforeFormat = fixture.build('assetMetricValue');
|
|
1098
|
-
valueToServerAfterFormat = fixture.build(
|
|
1099
|
-
'assetMetricValue',
|
|
1100
|
-
valueToServerBeforeFormat,
|
|
1101
|
-
{ fromServer: true }
|
|
1102
|
-
);
|
|
1103
|
-
|
|
1104
|
-
toSnakeCase = sinon
|
|
1105
|
-
.stub(objectUtils, 'toSnakeCase')
|
|
1106
|
-
.returns(valueToServerAfterFormat);
|
|
1107
|
-
|
|
1108
|
-
const assetMetrics = new AssetMetrics(
|
|
1109
|
-
baseSdk,
|
|
1110
|
-
baseRequest,
|
|
1111
|
-
expectedHost
|
|
1112
|
-
);
|
|
1113
|
-
|
|
1114
|
-
promise = assetMetrics.updateValue(
|
|
1115
|
-
valueToServerBeforeFormat.id,
|
|
1116
|
-
valueToServerBeforeFormat
|
|
1117
|
-
);
|
|
1118
|
-
});
|
|
1119
|
-
|
|
1120
|
-
it('formats the data into the right format', function() {
|
|
1121
|
-
expect(toSnakeCase).to.calledWith(valueToServerBeforeFormat, {
|
|
1122
|
-
excludeKeys: ['assetId', 'assetMetricId', 'id']
|
|
1123
|
-
});
|
|
1124
|
-
});
|
|
1125
|
-
|
|
1126
|
-
it('updates the asset metric value', function() {
|
|
1127
|
-
expect(baseRequest.put).to.be.calledWith(
|
|
1128
|
-
`${expectedHost}/assets/metrics/values/${
|
|
1129
|
-
valueToServerAfterFormat.id
|
|
1130
|
-
}`,
|
|
1131
|
-
valueToServerAfterFormat
|
|
1132
|
-
);
|
|
1133
|
-
});
|
|
1134
|
-
|
|
1135
|
-
it('returns a fulfilled promise', function() {
|
|
1136
|
-
return expect(promise).to.be.fulfilled;
|
|
1137
|
-
});
|
|
1138
|
-
});
|
|
1139
|
-
|
|
1140
|
-
context(
|
|
1141
|
-
'when there is missing or malformed required information',
|
|
1142
|
-
function() {
|
|
1143
|
-
let assetMetrics;
|
|
1144
|
-
|
|
1145
|
-
beforeEach(function() {
|
|
1146
|
-
assetMetrics = new AssetMetrics(baseSdk, baseRequest, expectedHost);
|
|
1147
|
-
});
|
|
1148
|
-
|
|
1149
|
-
it('throws an error when there is not provided asset metric value ID', function() {
|
|
1150
|
-
const valueUpdate = fixture.build('assetMetricValue');
|
|
1151
|
-
const promise = assetMetrics.updateValue(null, valueUpdate);
|
|
1152
|
-
|
|
1153
|
-
return expect(promise).to.be.rejectedWith(
|
|
1154
|
-
'An asset metric value ID is required to update an asset metric value.'
|
|
1155
|
-
);
|
|
1156
|
-
});
|
|
1157
|
-
|
|
1158
|
-
it('throws an error when there is no update provided', function() {
|
|
1159
|
-
const valueUpdate = fixture.build('assetMetricValue');
|
|
1160
|
-
const promise = assetMetrics.updateValue(valueUpdate.id);
|
|
1161
|
-
|
|
1162
|
-
return expect(promise).to.be.rejectedWith(
|
|
1163
|
-
'An update is required to update an asset metric value.'
|
|
1164
|
-
);
|
|
1165
|
-
});
|
|
1166
|
-
|
|
1167
|
-
it('throws an error when the update is not a well-formed object', function() {
|
|
1168
|
-
const valueUpdate = fixture.build('assetMetricValue');
|
|
1169
|
-
const promise = assetMetrics.updateValue(valueUpdate.id, [
|
|
1170
|
-
valueUpdate
|
|
1171
|
-
]);
|
|
1172
|
-
|
|
1173
|
-
return expect(promise).to.be.rejectedWith(
|
|
1174
|
-
'The asset metric value update must be a well-formed object with the data you wish to update.'
|
|
1175
|
-
);
|
|
1176
|
-
});
|
|
1177
|
-
}
|
|
1178
|
-
);
|
|
1179
|
-
});
|
|
1180
|
-
});
|