-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from tago-io/feat/unit-test-sdk
adding unit tests for sdk types
- Loading branch information
Showing
30 changed files
with
1,033 additions
and
34 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
packages/tcore-sdk/src/Types/Account/Account.types.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { ZodError } from "zod"; | ||
import { zAccountList, zAccountCreate, zAccountTokenCreate, zAccountListQuery } from "./Account.types"; | ||
|
||
describe("zAccountList", () => { | ||
test("parses simple object", () => { | ||
const data = [ | ||
{ | ||
password: "password", | ||
id: "id", | ||
}, | ||
]; | ||
const parsed = zAccountList.parse(data); | ||
expect(parsed[0].id).toEqual("id"); | ||
}); | ||
|
||
test("check required field", () => { | ||
const data = [ | ||
{ | ||
password: "password", | ||
}, | ||
]; | ||
try { | ||
zAccountList.parse(data); | ||
} catch (error) { | ||
const e = (error as ZodError).flatten(); | ||
expect(e.fieldErrors.id).toBe(undefined); | ||
} | ||
}); | ||
|
||
test("assure it is an array", () => { | ||
const data = { | ||
password: "password", | ||
id: "id", | ||
}; | ||
try { | ||
zAccountList.parse(data); | ||
} catch (error) { | ||
const e = (error as ZodError).flatten(); | ||
expect(e.fieldErrors).toStrictEqual({}); | ||
} | ||
}); | ||
}); | ||
|
||
describe("zAccountListQuery", () => { | ||
test("parses simple object", () => { | ||
const data = { | ||
fields: ["id"], | ||
}; | ||
const parsed = zAccountListQuery.parse(data); | ||
expect(parsed.fields[0]).toEqual("id"); | ||
}); | ||
|
||
test("error if invalid option", () => { | ||
const data = { | ||
fields: [" "], | ||
}; | ||
try { | ||
zAccountListQuery.parse(data); | ||
} catch (error) { | ||
const e = (error as ZodError).flatten(); | ||
expect(e.fieldErrors.fields[0].startsWith("Invalid enum value.")).toBeTruthy(); | ||
} | ||
}); | ||
|
||
test("assure empty query", () => { | ||
const data = { | ||
fields: [], | ||
}; | ||
const parsed = zAccountListQuery.parse(data); | ||
expect(parsed.fields).toContain("id"); | ||
}); | ||
}); | ||
|
||
describe("zAccountCreate", () => { | ||
test("parses simple object", () => { | ||
const data = { | ||
name: "name", | ||
username: "username", | ||
password: "password", | ||
}; | ||
const parsed = zAccountCreate.parse(data); | ||
expect(parsed.name).toEqual("name"); | ||
}); | ||
|
||
test("assure assignment of implicit fields", () => { | ||
const data = { | ||
name: "name", | ||
username: "username", | ||
password: "password", | ||
}; | ||
const parsed = zAccountCreate.parse(data); | ||
expect(parsed.created_at).toBeInstanceOf(Date); | ||
expect(parsed.id).toEqual(expect.any(String)); | ||
expect(parsed.password_hint).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe("zAccountTokenCreate", () => { | ||
test("parses simple object", () => { | ||
const data = { | ||
permission: "full", | ||
}; | ||
const parsed = zAccountTokenCreate.parse(data); | ||
expect(parsed.permission).toEqual("full"); | ||
}); | ||
|
||
test("error if invalid option", () => { | ||
const data = { | ||
permission: " ", | ||
}; | ||
try { | ||
zAccountTokenCreate.parse(data); | ||
} catch (error) { | ||
const e = (error as ZodError).flatten(); | ||
expect(e.fieldErrors.permission[0].startsWith("Invalid enum value.")).toBeTruthy(); | ||
} | ||
}); | ||
|
||
test("assure assignment of implicit fields", () => { | ||
const data = { | ||
permission: "full", | ||
}; | ||
const parsed = zAccountTokenCreate.parse(data); | ||
expect(parsed.created_at).toBeInstanceOf(Date); | ||
expect(parsed.token).toEqual(expect.any(String)); | ||
expect(parsed.expire_time).toEqual("1 month"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/tcore-sdk/src/Types/Hardware/Hardware.types.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { ZodError } from "zod"; | ||
import { zOSInfo, zNetworkInfo, zComputerUsage } from "./Hardware.types"; | ||
|
||
describe("zOSInfo", () => { | ||
test("parses simple object", () => { | ||
const data = { | ||
version: "version", | ||
arch: "arch", | ||
name: "name", | ||
code: "linux", | ||
hardware: "hardware", | ||
hostname: "hostname", | ||
}; | ||
const parsed = zOSInfo.parse(data); | ||
expect(parsed.version).toEqual("version"); | ||
}); | ||
|
||
test("check required fields", () => { | ||
const data = { | ||
version: "version", | ||
}; | ||
try { | ||
zOSInfo.parse(data); | ||
} catch (error) { | ||
const e = (error as ZodError).flatten(); | ||
expect(e.fieldErrors.arch[0]).toBe("Required"); | ||
} | ||
}); | ||
|
||
test("error if invalid code", () => { | ||
const data = { | ||
version: "version", | ||
arch: "arch", | ||
name: "name", | ||
code: " ", | ||
hardware: "hardware", | ||
hostname: "hostname", | ||
}; | ||
try { | ||
zOSInfo.parse(data); | ||
} catch (error) { | ||
const e = (error as ZodError).flatten(); | ||
expect(e.fieldErrors.code[0].startsWith("Invalid enum value.")).toBeTruthy(); | ||
} | ||
}); | ||
}); | ||
|
||
describe("zNetworkInfo", () => { | ||
test("parses simple object", () => { | ||
const data = { | ||
name: "name", | ||
ip: "ip", | ||
bytesTransferred: 0, | ||
bytesDropped: 0, | ||
}; | ||
const parsed = zNetworkInfo.parse(data); | ||
expect(parsed.name).toEqual("name"); | ||
}); | ||
|
||
test("check required fields", () => { | ||
const data = { | ||
name: "name", | ||
}; | ||
try { | ||
zNetworkInfo.parse(data); | ||
} catch (error) { | ||
const e = (error as ZodError).flatten(); | ||
expect(e.fieldErrors.ip[0]).toBe("Required"); | ||
} | ||
}); | ||
|
||
test("assure correct field types", () => { | ||
const data = { | ||
name: "name", | ||
ip: 0, | ||
bytesTransferred: "bytesTransferred", | ||
bytesDropped: 0, | ||
}; | ||
try { | ||
zNetworkInfo.parse(data); | ||
} catch (error) { | ||
const e = (error as ZodError).flatten(); | ||
expect(e.fieldErrors.ip[0]).toBe("Expected string, received number"); | ||
} | ||
}); | ||
}); | ||
|
||
describe("zComputerUsage", () => { | ||
test("parses simple object", () => { | ||
const data = { | ||
description: "string", // undefined | ||
detail: "detail", // undefined | ||
type: "type", | ||
total: 0, | ||
used: 1, | ||
title: "title", | ||
}; | ||
const parsed = zComputerUsage.parse(data); | ||
expect(parsed.type).toEqual("type"); | ||
}); | ||
|
||
test("check required fields", () => { | ||
const data = { | ||
description: "string", // undefined | ||
detail: "detail", // undefined | ||
type: "type", | ||
}; | ||
try { | ||
zComputerUsage.parse(data); | ||
} catch (error) { | ||
const e = (error as ZodError).flatten(); | ||
expect(e.fieldErrors.title[0]).toBe("Required"); | ||
} | ||
}); | ||
|
||
test("parses without optional fields", () => { | ||
const data = { | ||
type: "type", | ||
total: 0, | ||
used: 1, | ||
title: "title", | ||
}; | ||
const parsed = zComputerUsage.parse(data); | ||
expect(parsed.description).toBeUndefined(); | ||
expect(parsed.detail).toBeUndefined(); | ||
}); | ||
}); |
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
packages/tcore-sdk/src/Types/Helpers/parseRelativeDate.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { convertDateToISO, parseRelativeDate } from "./parseRelativeDate"; | ||
|
||
describe("convertDateToISO", () => { | ||
test("convert simple date", () => { | ||
const data = convertDateToISO("01/01/01"); | ||
expect(typeof data).toBe("string"); | ||
}); | ||
}); | ||
|
||
describe("parseRelativeDate", () => { | ||
test("check invalid date", () => { | ||
try { | ||
parseRelativeDate(" ", " "); | ||
} catch (e: any) { | ||
expect(e.message).toBe("Invalid date"); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { parseSafe } from "./parseSafe"; | ||
|
||
describe("parseSafe", () => { | ||
test("assure correct parsing", () => { | ||
const data = '{"value":1}'; | ||
const process = parseSafe(data); | ||
expect(process).toStrictEqual({ value: 1 }); | ||
}); | ||
|
||
test("exception catching", () => { | ||
const data = 0; | ||
const process = parseSafe(data); | ||
expect(process).toStrictEqual({}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Parses a JSON safely. | ||
*/ | ||
export function parseSafe(value: any, fallback: any = {}) { | ||
try { | ||
if (value && typeof value === "object") { | ||
return value; | ||
} | ||
const result = JSON.parse(value); | ||
return result || fallback; | ||
} catch (ex) { | ||
return fallback; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/tcore-sdk/src/Types/Helpers/preprocessBoolean.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import preprocessBoolean from "./preprocessBoolean"; | ||
|
||
describe("preprocessBoolean", () => { | ||
test("process standard boolean", () => { | ||
const data = true; | ||
const process = preprocessBoolean(data); | ||
expect(process).toEqual(data); | ||
}); | ||
|
||
test("process literal boolean", () => { | ||
const data = "true"; | ||
const process = preprocessBoolean(data); | ||
expect(typeof process).toBe("boolean"); | ||
expect(process).toEqual(true); | ||
}); | ||
|
||
test("return true", () => { | ||
const data = "0"; | ||
const process = preprocessBoolean(data); | ||
expect(process).toBeTruthy(); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
packages/tcore-sdk/src/Types/Helpers/preprocessNumber.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import preprocessNumber from "./preprocessNumber"; | ||
|
||
describe("preprocessNumber", () => { | ||
test("process string param", () => { | ||
const data = "123"; | ||
const process = preprocessNumber(data); | ||
expect(typeof process).toBe("number"); | ||
expect(process).toEqual(123); | ||
}); | ||
|
||
test("filter booleans values", () => { | ||
const data = true; | ||
const process = preprocessNumber(data); | ||
expect(typeof process).toBe("string"); | ||
expect(process).toEqual(""); | ||
}); | ||
}); |
Oops, something went wrong.