From f767972395ef8d2c7b047af09d4d5bad8e8dc3a8 Mon Sep 17 00:00:00 2001 From: "Vipul Gupta (@vipulgupta2048)" Date: Wed, 15 Feb 2023 01:51:36 +0530 Subject: [PATCH] Add tests for contract partial functions Signed-off-by: Vipul Gupta (@vipulgupta2048) --- DOCUMENTATION.md | 73 +++++++++++++++++--- lib/models/device-type.ts | 17 +++-- lib/types/device-type-json.ts | 2 +- tests/integration/models/device-type.spec.ts | 14 ++++ 4 files changed, 91 insertions(+), 15 deletions(-) diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 33388ce06..e9fc08d1a 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -246,7 +246,7 @@ const sdk = fromSharedOptions(); * [.getDashboardUrl(uuid)](#balena.models.device.getDashboardUrl) ⇒ String * [.getAll([options])](#balena.models.device.getAll) ⇒ Promise * [.getAllByApplication(slugOrUuidOrId, [options])](#balena.models.device.getAllByApplication) ⇒ Promise - * [.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ Promise + * ~~[.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ Promise~~ * [.get(uuidOrId, [options])](#balena.models.device.get) ⇒ Promise * [.getWithServiceDetails(uuidOrId, [options])](#balena.models.device.getWithServiceDetails) ⇒ Promise * [.getByName(name)](#balena.models.device.getByName) ⇒ Promise @@ -315,7 +315,7 @@ const sdk = fromSharedOptions(); * [.getBySlugOrName(slugOrName)](#balena.models.deviceType.getBySlugOrName) ⇒ Promise * [.getName(deviceTypeSlug)](#balena.models.deviceType.getName) ⇒ Promise * [.getSlugByName(deviceTypeName)](#balena.models.deviceType.getSlugByName) ⇒ Promise - * [.getInterpolatedPartials(deviceTypeSlug, initial)](#balena.models.deviceType.getInterpolatedPartials) ⇒ Promise + * [.getInterpolatedPartials(deviceTypeSlug)](#balena.models.deviceType.getInterpolatedPartials) ⇒ Promise * [.getInstructions(deviceTypeSlug)](#balena.models.deviceType.getInstructions) ⇒ Promise * [.getInstallMethod(deviceTypeSlug)](#balena.models.deviceType.getInstallMethod) ⇒ Promise * [.apiKey](#balena.models.apiKey) : object @@ -393,6 +393,9 @@ const sdk = fromSharedOptions(); * [.image](#balena.models.image) : object * [.get(id, [options])](#balena.models.image.get) ⇒ Promise * [.getLogs(id)](#balena.models.image.getLogs) ⇒ Promise + * [.creditBundle](#balena.models.creditBundle) : object + * [.getAllByOrg(orgId, [options])](#balena.models.creditBundle.getAllByOrg) ⇒ Promise + * [.create(orgId, featureId, creditsToPurchase)](#balena.models.creditBundle.create) ⇒ Promise * [.billing](#balena.models.billing) : object * [.getAccount(organization)](#balena.models.billing.getAccount) ⇒ Promise * [.getPlan(organization)](#balena.models.billing.getPlan) ⇒ Promise @@ -644,7 +647,7 @@ balena.models.device.get(123).catch(function (error) { * [.getDashboardUrl(uuid)](#balena.models.device.getDashboardUrl) ⇒ String * [.getAll([options])](#balena.models.device.getAll) ⇒ Promise * [.getAllByApplication(slugOrUuidOrId, [options])](#balena.models.device.getAllByApplication) ⇒ Promise - * [.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ Promise + * ~~[.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ Promise~~ * [.get(uuidOrId, [options])](#balena.models.device.get) ⇒ Promise * [.getWithServiceDetails(uuidOrId, [options])](#balena.models.device.getWithServiceDetails) ⇒ Promise * [.getByName(name)](#balena.models.device.getByName) ⇒ Promise @@ -713,7 +716,7 @@ balena.models.device.get(123).catch(function (error) { * [.getBySlugOrName(slugOrName)](#balena.models.deviceType.getBySlugOrName) ⇒ Promise * [.getName(deviceTypeSlug)](#balena.models.deviceType.getName) ⇒ Promise * [.getSlugByName(deviceTypeName)](#balena.models.deviceType.getSlugByName) ⇒ Promise - * [.getInterpolatedPartials(deviceTypeSlug, initial)](#balena.models.deviceType.getInterpolatedPartials) ⇒ Promise + * [.getInterpolatedPartials(deviceTypeSlug)](#balena.models.deviceType.getInterpolatedPartials) ⇒ Promise * [.getInstructions(deviceTypeSlug)](#balena.models.deviceType.getInstructions) ⇒ Promise * [.getInstallMethod(deviceTypeSlug)](#balena.models.deviceType.getInstallMethod) ⇒ Promise * [.apiKey](#balena.models.apiKey) : object @@ -791,6 +794,9 @@ balena.models.device.get(123).catch(function (error) { * [.image](#balena.models.image) : object * [.get(id, [options])](#balena.models.image.get) ⇒ Promise * [.getLogs(id)](#balena.models.image.getLogs) ⇒ Promise + * [.creditBundle](#balena.models.creditBundle) : object + * [.getAllByOrg(orgId, [options])](#balena.models.creditBundle.getAllByOrg) ⇒ Promise + * [.create(orgId, featureId, creditsToPurchase)](#balena.models.creditBundle.create) ⇒ Promise * [.billing](#balena.models.billing) : object * [.getAccount(organization)](#balena.models.billing.getAccount) ⇒ Promise * [.getPlan(organization)](#balena.models.billing.getPlan) ⇒ Promise @@ -2565,7 +2571,7 @@ balena.models.application.revokeSupportAccess('myorganization/myapp', function(e * [.getDashboardUrl(uuid)](#balena.models.device.getDashboardUrl) ⇒ String * [.getAll([options])](#balena.models.device.getAll) ⇒ Promise * [.getAllByApplication(slugOrUuidOrId, [options])](#balena.models.device.getAllByApplication) ⇒ Promise - * [.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ Promise + * ~~[.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ Promise~~ * [.get(uuidOrId, [options])](#balena.models.device.get) ⇒ Promise * [.getWithServiceDetails(uuidOrId, [options])](#balena.models.device.getWithServiceDetails) ⇒ Promise * [.getByName(name)](#balena.models.device.getByName) ⇒ Promise @@ -3396,7 +3402,9 @@ balena.models.device.getAllByApplication('myorganization/myapp', function(error, ``` -##### device.getAllByParentDevice(parentUuidOrId, [options]) ⇒ Promise +##### ~~device.getAllByParentDevice(parentUuidOrId, [options]) ⇒ Promise~~ +***Deprecated*** + **Kind**: static method of [device](#balena.models.device) **Summary**: Get all devices by parent device **Access**: public @@ -5222,7 +5230,7 @@ balena.models.device.restartService('7cf02a6', 123, function(error) { * [.getBySlugOrName(slugOrName)](#balena.models.deviceType.getBySlugOrName) ⇒ Promise * [.getName(deviceTypeSlug)](#balena.models.deviceType.getName) ⇒ Promise * [.getSlugByName(deviceTypeName)](#balena.models.deviceType.getSlugByName) ⇒ Promise - * [.getInterpolatedPartials(deviceTypeSlug, initial)](#balena.models.deviceType.getInterpolatedPartials) ⇒ Promise + * [.getInterpolatedPartials(deviceTypeSlug)](#balena.models.deviceType.getInterpolatedPartials) ⇒ Promise * [.getInstructions(deviceTypeSlug)](#balena.models.deviceType.getInstructions) ⇒ Promise * [.getInstallMethod(deviceTypeSlug)](#balena.models.deviceType.getInstallMethod) ⇒ Promise @@ -5407,7 +5415,7 @@ balena.models.deviceType.getSlugByName('Raspberry Pi', function(error, deviceTyp ``` -##### deviceType.getInterpolatedPartials(deviceTypeSlug, initial) ⇒ Promise +##### deviceType.getInterpolatedPartials(deviceTypeSlug) ⇒ Promise **Kind**: static method of [deviceType](#balena.models.deviceType) **Summary**: Get a contract with resolved partial templates **Access**: public @@ -5416,7 +5424,6 @@ balena.models.deviceType.getSlugByName('Raspberry Pi', function(error, deviceTyp | Param | Type | Description | | --- | --- | --- | | deviceTypeSlug | String | device type slug | -| initial | any | Other contract values necessary for interpreting contracts | **Example** ```js @@ -7384,6 +7391,54 @@ balena.models.image.getLogs(123, function(error, logs) { console.log(logs); }); ``` + + +#### models.creditBundle : object +**Kind**: static namespace of [models](#balena.models) + +* [.creditBundle](#balena.models.creditBundle) : object + * [.getAllByOrg(orgId, [options])](#balena.models.creditBundle.getAllByOrg) ⇒ Promise + * [.create(orgId, featureId, creditsToPurchase)](#balena.models.creditBundle.create) ⇒ Promise + + + +##### creditBundle.getAllByOrg(orgId, [options]) ⇒ Promise +**Kind**: static method of [creditBundle](#balena.models.creditBundle) +**Summary**: Get all of the credit bundles purchased by the given org +**Access**: public +**Fulfil**: Object[] - credit bundles + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| orgId | String \| Number | | handle (string) or id (number) of the target organization. | +| [options] | Object | {} | extra pine options to use | + +**Example** +```js +balena.models.creditBundle.getAllByOrg(orgId).then(function(creditBundles) { + console.log(creditBundles); +}); +``` + + +##### creditBundle.create(orgId, featureId, creditsToPurchase) ⇒ Promise +**Kind**: static method of [creditBundle](#balena.models.creditBundle) +**Summary**: Purchase a credit bundle for the given feature and org of the given quantity +**Access**: public +**Fulfil**: Object[] - credit bundles + +| Param | Type | Description | +| --- | --- | --- | +| orgId | String \| Number | handle (string) or id (number) of the target organization. | +| featureId | String \| Number | id (number) of the feature for which credits are being purchased. | +| creditsToPurchase | String \| Number | number of credits being purchased. | + +**Example** +```js +balena.models.creditBundle.create(orgId, featureId, creditsToPurchase).then(function(creditBundle) { + console.log(creditBundle); +}); +``` #### models.billing : object diff --git a/lib/models/device-type.ts b/lib/models/device-type.ts index a2f326ff4..eb2e34730 100644 --- a/lib/models/device-type.ts +++ b/lib/models/device-type.ts @@ -30,7 +30,7 @@ const traversingCompile = ( initial: Contract, path: string[], ): Contract => { - let interpolated: Contract = { ...initial } + let interpolated: Contract = { ...initial }; for (const partialKey of Object.keys(partials)) { const current = partials[partialKey]; if (Array.isArray(current)) { @@ -52,7 +52,7 @@ const traversingCompile = ( } } return interpolated; -} +}; const interpolatedPartials = (contract: Contract): Contract => { if (contract.partials) { @@ -417,7 +417,9 @@ const getDeviceTypeModel = function (deps: InjectedDependenciesParam) { $select: 'contract', }); if (!contract) { - throw new Error(`Could not find contract for device type ${deviceTypeSlug}`); + throw new Error( + `Could not find contract for device type ${deviceTypeSlug}`, + ); } return interpolatedPartials(contract); }, @@ -458,8 +460,13 @@ const getDeviceTypeModel = function (deps: InjectedDependenciesParam) { ); } const installMethod = calculateInstallMethod(contract); - const interpolatedDeviceType = interpolatedPartials(contract); - const interpolatedHostOS = interpolatedPartials({...cloneDeep(BalenaOS), ...interpolatedDeviceType}); + const interpolatedDeviceType = { + deviceType: interpolatedPartials(contract), + }; + const interpolatedHostOS = interpolatedPartials({ + ...cloneDeep(BalenaOS), + ...interpolatedDeviceType, + }); return interpolatedHostOS.partials?.[installMethod]; }, diff --git a/lib/types/device-type-json.ts b/lib/types/device-type-json.ts index b61f914fc..8eae44b05 100644 --- a/lib/types/device-type-json.ts +++ b/lib/types/device-type-json.ts @@ -6,7 +6,7 @@ export interface DeviceType { slug: string; name: string; aliases: string[]; - + arch: string; state?: string; community?: boolean; diff --git a/tests/integration/models/device-type.spec.ts b/tests/integration/models/device-type.spec.ts index 4b3d6987e..5e046cdd3 100644 --- a/tests/integration/models/device-type.spec.ts +++ b/tests/integration/models/device-type.spec.ts @@ -100,6 +100,12 @@ describe('Device Type model', function () { ); expect(partials).to.be.an('object'); expect(Object.keys(partials)).to.not.have.length(0); + expect(partials) + .to.have.property('partials') + .to.have.property('bootDevice'); + expect(partials?.partials?.bootDevice[0]).to.equal( + 'Connect power to the Raspberry Pi 2', + ); }); }); @@ -110,6 +116,14 @@ describe('Device Type model', function () { ); expect(partials).to.be.an('Array'); expect(partials).to.not.have.length(0); + expect(partials).to.eql([ + 'Insert the sdcard to the host machine.', + 'Write the balenaOS file you downloaded to the sdcard. We recommend using Etcher.', + 'Wait for writing of balenaOS to complete.', + 'Remove the sdcard from the host machine.', + 'Insert the freshly flashed sdcard into the Raspberry Pi 2.', + 'Connect power to the Raspberry Pi 2 to boot the device.', + ]); }); });