Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw proper errors when converting device object #53

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/TestIOUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,24 @@ export class TestIOUtil {

static async getDevicePayloadFromPrepareObjectDeviceSpec(device: { category: string; os: string; min: string; max?: string; } ): Promise<any> {
const categoryId = await TestIOUtil.retrieveDeviceCategoryIdByName(device.category);
if (categoryId == -1) {
throw new Error(`Category '${device.category}' is not valid`);
}

const osId = await TestIOUtil.retrieveOperatingSystemIdByDeviceCategoryIdAndName(categoryId, device.os);
if (osId == -1) {
throw new Error(`OS name '${device.os}' is not valid`);
}

const minVersionId = await TestIOUtil.retrieveOsVersionIdByOsIdAndVersion(osId, device.min);
if (minVersionId == -1) {
throw new Error(`Min version '${device.min}' is not valid`);
}

const maxVersionId = (device.max ? await TestIOUtil.retrieveOsVersionIdByOsIdAndVersion(osId, device.max) : null);
if (maxVersionId == -1) {
throw new Error(`Max version '${device.max}' is not valid`);
}

return {
requirements: [
Expand Down
2 changes: 1 addition & 1 deletion src/retrievePayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function createPayload() {

const prepareObject = gha.retrieveValidPrepareObjectFromComment(commentContents);
const prTitle: string = await gha.retrievePrTitle();
gha.createAndPersistTestIoPayload(prepareObject, prTitle);
await gha.createAndPersistTestIoPayload(prepareObject, prTitle);
}

createPayload().then();
46 changes: 46 additions & 0 deletions test/TestIOUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,52 @@ describe("TestIO Device API Util", () => {
expect(devicePayload).toEqual(expectedDevicePayload);
});

it('should validate the device spec correctly', async () => {
let device: { min: string; os: string; max?: string; category: string } = {
os: "wrongOS",
category: "smartphone",
min: "8.0"
};
await expect(async () => await TestIOUtil.getDevicePayloadFromPrepareObjectDeviceSpec(device)).rejects.toThrowError("OS name 'wrongOS' is not valid");

device = {
os: "ios",
category: "wrongCategory",
min: "8.0"
};
await expect(async () => await TestIOUtil.getDevicePayloadFromPrepareObjectDeviceSpec(device)).rejects.toThrowError("Category 'wrongCategory' is not valid");

device = {
os: "android",
category: "smartphone",
min: "8.invalid.0"
};
await expect(async () => await TestIOUtil.getDevicePayloadFromPrepareObjectDeviceSpec(device)).rejects.toThrowError("Min version '8.invalid.0' is not valid");

device = {
os: "android",
category: "smartphone",
min: "8.0"
};
await expect(async () => await TestIOUtil.getDevicePayloadFromPrepareObjectDeviceSpec(device)).resolves;

device = {
os: "android",
category: "smartphone",
min: "8.0",
max: "13.0.invalid"
};
await expect(async () => await TestIOUtil.getDevicePayloadFromPrepareObjectDeviceSpec(device)).rejects.toThrowError("Max version '13.0.invalid' is not valid");

device = {
os: "android",
category: "smartphone",
min: "8.0",
max: "13.0"
};
await expect(async () => await TestIOUtil.getDevicePayloadFromPrepareObjectDeviceSpec(device)).resolves;
});

it('should translate device spec without max version into TestIO device payload', async () => {
const osName = "ios";
const categoryName = "smartphone";
Expand Down