Skip to content

Commit

Permalink
Add tests for contract partial functions
Browse files Browse the repository at this point in the history
Signed-off-by: Vipul Gupta (@vipulgupta2048) <vipul@balena.io>
  • Loading branch information
vipulgupta2048 committed Feb 15, 2023
1 parent 424fc46 commit f767972
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 15 deletions.
73 changes: 64 additions & 9 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ const sdk = fromSharedOptions();
* [.getDashboardUrl(uuid)](#balena.models.device.getDashboardUrl) ⇒ <code>String</code>
* [.getAll([options])](#balena.models.device.getAll) ⇒ <code>Promise</code>
* [.getAllByApplication(slugOrUuidOrId, [options])](#balena.models.device.getAllByApplication) ⇒ <code>Promise</code>
* [.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ <code>Promise</code>
* ~~[.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ <code>Promise</code>~~
* [.get(uuidOrId, [options])](#balena.models.device.get) ⇒ <code>Promise</code>
* [.getWithServiceDetails(uuidOrId, [options])](#balena.models.device.getWithServiceDetails) ⇒ <code>Promise</code>
* [.getByName(name)](#balena.models.device.getByName) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -315,7 +315,7 @@ const sdk = fromSharedOptions();
* [.getBySlugOrName(slugOrName)](#balena.models.deviceType.getBySlugOrName) ⇒ <code>Promise</code>
* [.getName(deviceTypeSlug)](#balena.models.deviceType.getName) ⇒ <code>Promise</code>
* [.getSlugByName(deviceTypeName)](#balena.models.deviceType.getSlugByName) ⇒ <code>Promise</code>
* [.getInterpolatedPartials(deviceTypeSlug, initial)](#balena.models.deviceType.getInterpolatedPartials) ⇒ <code>Promise</code>
* [.getInterpolatedPartials(deviceTypeSlug)](#balena.models.deviceType.getInterpolatedPartials) ⇒ <code>Promise</code>
* [.getInstructions(deviceTypeSlug)](#balena.models.deviceType.getInstructions) ⇒ <code>Promise</code>
* [.getInstallMethod(deviceTypeSlug)](#balena.models.deviceType.getInstallMethod) ⇒ <code>Promise</code>
* [.apiKey](#balena.models.apiKey) : <code>object</code>
Expand Down Expand Up @@ -393,6 +393,9 @@ const sdk = fromSharedOptions();
* [.image](#balena.models.image) : <code>object</code>
* [.get(id, [options])](#balena.models.image.get) ⇒ <code>Promise</code>
* [.getLogs(id)](#balena.models.image.getLogs) ⇒ <code>Promise</code>
* [.creditBundle](#balena.models.creditBundle) : <code>object</code>
* [.getAllByOrg(orgId, [options])](#balena.models.creditBundle.getAllByOrg) ⇒ <code>Promise</code>
* [.create(orgId, featureId, creditsToPurchase)](#balena.models.creditBundle.create) ⇒ <code>Promise</code>
* [.billing](#balena.models.billing) : <code>object</code>
* [.getAccount(organization)](#balena.models.billing.getAccount) ⇒ <code>Promise</code>
* [.getPlan(organization)](#balena.models.billing.getPlan) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -644,7 +647,7 @@ balena.models.device.get(123).catch(function (error) {
* [.getDashboardUrl(uuid)](#balena.models.device.getDashboardUrl) ⇒ <code>String</code>
* [.getAll([options])](#balena.models.device.getAll) ⇒ <code>Promise</code>
* [.getAllByApplication(slugOrUuidOrId, [options])](#balena.models.device.getAllByApplication) ⇒ <code>Promise</code>
* [.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ <code>Promise</code>
* ~~[.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ <code>Promise</code>~~
* [.get(uuidOrId, [options])](#balena.models.device.get) ⇒ <code>Promise</code>
* [.getWithServiceDetails(uuidOrId, [options])](#balena.models.device.getWithServiceDetails) ⇒ <code>Promise</code>
* [.getByName(name)](#balena.models.device.getByName) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -713,7 +716,7 @@ balena.models.device.get(123).catch(function (error) {
* [.getBySlugOrName(slugOrName)](#balena.models.deviceType.getBySlugOrName) ⇒ <code>Promise</code>
* [.getName(deviceTypeSlug)](#balena.models.deviceType.getName) ⇒ <code>Promise</code>
* [.getSlugByName(deviceTypeName)](#balena.models.deviceType.getSlugByName) ⇒ <code>Promise</code>
* [.getInterpolatedPartials(deviceTypeSlug, initial)](#balena.models.deviceType.getInterpolatedPartials) ⇒ <code>Promise</code>
* [.getInterpolatedPartials(deviceTypeSlug)](#balena.models.deviceType.getInterpolatedPartials) ⇒ <code>Promise</code>
* [.getInstructions(deviceTypeSlug)](#balena.models.deviceType.getInstructions) ⇒ <code>Promise</code>
* [.getInstallMethod(deviceTypeSlug)](#balena.models.deviceType.getInstallMethod) ⇒ <code>Promise</code>
* [.apiKey](#balena.models.apiKey) : <code>object</code>
Expand Down Expand Up @@ -791,6 +794,9 @@ balena.models.device.get(123).catch(function (error) {
* [.image](#balena.models.image) : <code>object</code>
* [.get(id, [options])](#balena.models.image.get) ⇒ <code>Promise</code>
* [.getLogs(id)](#balena.models.image.getLogs) ⇒ <code>Promise</code>
* [.creditBundle](#balena.models.creditBundle) : <code>object</code>
* [.getAllByOrg(orgId, [options])](#balena.models.creditBundle.getAllByOrg) ⇒ <code>Promise</code>
* [.create(orgId, featureId, creditsToPurchase)](#balena.models.creditBundle.create) ⇒ <code>Promise</code>
* [.billing](#balena.models.billing) : <code>object</code>
* [.getAccount(organization)](#balena.models.billing.getAccount) ⇒ <code>Promise</code>
* [.getPlan(organization)](#balena.models.billing.getPlan) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -2565,7 +2571,7 @@ balena.models.application.revokeSupportAccess('myorganization/myapp', function(e
* [.getDashboardUrl(uuid)](#balena.models.device.getDashboardUrl) ⇒ <code>String</code>
* [.getAll([options])](#balena.models.device.getAll) ⇒ <code>Promise</code>
* [.getAllByApplication(slugOrUuidOrId, [options])](#balena.models.device.getAllByApplication) ⇒ <code>Promise</code>
* [.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ <code>Promise</code>
* ~~[.getAllByParentDevice(parentUuidOrId, [options])](#balena.models.device.getAllByParentDevice) ⇒ <code>Promise</code>~~
* [.get(uuidOrId, [options])](#balena.models.device.get) ⇒ <code>Promise</code>
* [.getWithServiceDetails(uuidOrId, [options])](#balena.models.device.getWithServiceDetails) ⇒ <code>Promise</code>
* [.getByName(name)](#balena.models.device.getByName) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -3396,7 +3402,9 @@ balena.models.device.getAllByApplication('myorganization/myapp', function(error,
```
<a name="balena.models.device.getAllByParentDevice"></a>

##### device.getAllByParentDevice(parentUuidOrId, [options]) ⇒ <code>Promise</code>
##### ~~device.getAllByParentDevice(parentUuidOrId, [options]) ⇒ <code>Promise</code>~~
***Deprecated***

**Kind**: static method of [<code>device</code>](#balena.models.device)
**Summary**: Get all devices by parent device
**Access**: public
Expand Down Expand Up @@ -5222,7 +5230,7 @@ balena.models.device.restartService('7cf02a6', 123, function(error) {
* [.getBySlugOrName(slugOrName)](#balena.models.deviceType.getBySlugOrName) ⇒ <code>Promise</code>
* [.getName(deviceTypeSlug)](#balena.models.deviceType.getName) ⇒ <code>Promise</code>
* [.getSlugByName(deviceTypeName)](#balena.models.deviceType.getSlugByName) ⇒ <code>Promise</code>
* [.getInterpolatedPartials(deviceTypeSlug, initial)](#balena.models.deviceType.getInterpolatedPartials) ⇒ <code>Promise</code>
* [.getInterpolatedPartials(deviceTypeSlug)](#balena.models.deviceType.getInterpolatedPartials) ⇒ <code>Promise</code>
* [.getInstructions(deviceTypeSlug)](#balena.models.deviceType.getInstructions) ⇒ <code>Promise</code>
* [.getInstallMethod(deviceTypeSlug)](#balena.models.deviceType.getInstallMethod) ⇒ <code>Promise</code>

Expand Down Expand Up @@ -5407,7 +5415,7 @@ balena.models.deviceType.getSlugByName('Raspberry Pi', function(error, deviceTyp
```
<a name="balena.models.deviceType.getInterpolatedPartials"></a>

##### deviceType.getInterpolatedPartials(deviceTypeSlug, initial) ⇒ <code>Promise</code>
##### deviceType.getInterpolatedPartials(deviceTypeSlug) ⇒ <code>Promise</code>
**Kind**: static method of [<code>deviceType</code>](#balena.models.deviceType)
**Summary**: Get a contract with resolved partial templates
**Access**: public
Expand All @@ -5416,7 +5424,6 @@ balena.models.deviceType.getSlugByName('Raspberry Pi', function(error, deviceTyp
| Param | Type | Description |
| --- | --- | --- |
| deviceTypeSlug | <code>String</code> | device type slug |
| initial | <code>any</code> | Other contract values necessary for interpreting contracts |

**Example**
```js
Expand Down Expand Up @@ -7384,6 +7391,54 @@ balena.models.image.getLogs(123, function(error, logs) {
console.log(logs);
});
```
<a name="balena.models.creditBundle"></a>

#### models.creditBundle : <code>object</code>
**Kind**: static namespace of [<code>models</code>](#balena.models)

* [.creditBundle](#balena.models.creditBundle) : <code>object</code>
* [.getAllByOrg(orgId, [options])](#balena.models.creditBundle.getAllByOrg) ⇒ <code>Promise</code>
* [.create(orgId, featureId, creditsToPurchase)](#balena.models.creditBundle.create) ⇒ <code>Promise</code>

<a name="balena.models.creditBundle.getAllByOrg"></a>

##### creditBundle.getAllByOrg(orgId, [options]) ⇒ <code>Promise</code>
**Kind**: static method of [<code>creditBundle</code>](#balena.models.creditBundle)
**Summary**: Get all of the credit bundles purchased by the given org
**Access**: public
**Fulfil**: <code>Object[]</code> - credit bundles

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| orgId | <code>String</code> \| <code>Number</code> | | handle (string) or id (number) of the target organization. |
| [options] | <code>Object</code> | <code>{}</code> | extra pine options to use |

**Example**
```js
balena.models.creditBundle.getAllByOrg(orgId).then(function(creditBundles) {
console.log(creditBundles);
});
```
<a name="balena.models.creditBundle.create"></a>

##### creditBundle.create(orgId, featureId, creditsToPurchase) ⇒ <code>Promise</code>
**Kind**: static method of [<code>creditBundle</code>](#balena.models.creditBundle)
**Summary**: Purchase a credit bundle for the given feature and org of the given quantity
**Access**: public
**Fulfil**: <code>Object[]</code> - credit bundles

| Param | Type | Description |
| --- | --- | --- |
| orgId | <code>String</code> \| <code>Number</code> | handle (string) or id (number) of the target organization. |
| featureId | <code>String</code> \| <code>Number</code> | id (number) of the feature for which credits are being purchased. |
| creditsToPurchase | <code>String</code> \| <code>Number</code> | number of credits being purchased. |

**Example**
```js
balena.models.creditBundle.create(orgId, featureId, creditsToPurchase).then(function(creditBundle) {
console.log(creditBundle);
});
```
<a name="balena.models.billing"></a>

#### models.billing : <code>object</code>
Expand Down
17 changes: 12 additions & 5 deletions lib/models/device-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -52,7 +52,7 @@ const traversingCompile = (
}
}
return interpolated;
}
};

const interpolatedPartials = (contract: Contract): Contract => {
if (contract.partials) {
Expand Down Expand Up @@ -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);
},
Expand Down Expand Up @@ -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];
},
Expand Down
2 changes: 1 addition & 1 deletion lib/types/device-type-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface DeviceType {
slug: string;
name: string;
aliases: string[];

arch: string;
state?: string;
community?: boolean;
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/models/device-type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
});
});

Expand All @@ -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 <a href="http://www.etcher.io/">Etcher</a>.',
'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.',
]);
});
});

Expand Down

0 comments on commit f767972

Please sign in to comment.