From 2ddb9cf8fc3ea90c05ad76ac11ac90bbe8fcfe18 Mon Sep 17 00:00:00 2001 From: geisterfurz007 Date: Sat, 28 Oct 2023 13:31:32 +0200 Subject: [PATCH] feat: deno tests --- src/SDK/Language/Deno.php | 25 +++ templates/deno/src/exception.ts.twig | 12 +- templates/deno/test/id.test.ts.twig | 8 + templates/deno/test/permission.test.ts.twig | 12 ++ templates/deno/test/query.test.ts.twig | 175 ++++++++++++++++++ templates/deno/test/role.test.ts.twig | 16 ++ .../deno/test/services/service.test.ts.twig | 56 ++++++ 7 files changed, 298 insertions(+), 6 deletions(-) create mode 100644 templates/deno/test/id.test.ts.twig create mode 100644 templates/deno/test/permission.test.ts.twig create mode 100644 templates/deno/test/query.test.ts.twig create mode 100644 templates/deno/test/role.test.ts.twig create mode 100644 templates/deno/test/services/service.test.ts.twig diff --git a/src/SDK/Language/Deno.php b/src/SDK/Language/Deno.php index 1fdef0c0da..3027ed5033 100644 --- a/src/SDK/Language/Deno.php +++ b/src/SDK/Language/Deno.php @@ -33,21 +33,41 @@ public function getFiles(): array 'destination' => 'src/permission.ts', 'template' => 'deno/src/permission.ts.twig', ], + [ + 'scope' => 'default', + 'destination' => 'test/permission.test.ts', + 'template' => 'deno/test/permission.test.ts.twig', + ], [ 'scope' => 'default', 'destination' => 'src/role.ts', 'template' => 'deno/src/role.ts.twig', ], + [ + 'scope' => 'default', + 'destination' => 'test/role.test.ts', + 'template' => 'deno/test/role.test.ts.twig', + ], [ 'scope' => 'default', 'destination' => 'src/id.ts', 'template' => 'deno/src/id.ts.twig', ], + [ + 'scope' => 'default', + 'destination' => 'test/id.test.ts', + 'template' => 'deno/test/id.test.ts.twig', + ], [ 'scope' => 'default', 'destination' => 'src/query.ts', 'template' => 'deno/src/query.ts.twig', ], + [ + 'scope' => 'default', + 'destination' => 'test/query.test.ts', + 'template' => 'deno/test/query.test.ts.twig', + ], [ 'scope' => 'default', 'destination' => 'src/inputFile.ts', @@ -73,6 +93,11 @@ public function getFiles(): array 'destination' => '/src/services/{{service.name | caseDash}}.ts', 'template' => 'deno/src/services/service.ts.twig', ], + [ + 'scope' => 'service', + 'destination' => '/test/services/{{service.name | caseDash}}.test.ts', + 'template' => 'deno/test/services/service.test.ts.twig', + ], [ 'scope' => 'default', 'destination' => 'README.md', diff --git a/templates/deno/src/exception.ts.twig b/templates/deno/src/exception.ts.twig index 61b90cff5f..aaa5e5f411 100644 --- a/templates/deno/src/exception.ts.twig +++ b/templates/deno/src/exception.ts.twig @@ -1,17 +1,17 @@ export class {{ spec.title | caseUcfirst}}Exception { - message: String; - code: Number; + message: string; + code: number; response: any; - type: String; + type: string; - constructor(message: String, code: Number = 0, type: String = "", response: any = "") { + constructor(message: string, code: number = 0, type: string = "", response: any = "") { this.message = message; this.code = code; this.type = type; this.response = response; } - public toString(): String { + public toString(): string { return `${this.message} - ${this.code} - ${this.type} - ${JSON.stringify(this.response)}`; } -} \ No newline at end of file +} diff --git a/templates/deno/test/id.test.ts.twig b/templates/deno/test/id.test.ts.twig new file mode 100644 index 0000000000..6e7dcbed03 --- /dev/null +++ b/templates/deno/test/id.test.ts.twig @@ -0,0 +1,8 @@ +import {assertEquals} from "https://deno.land/std@0.204.0/assert/mod.ts"; +import {describe, it as test} from "https://deno.land/std@0.149.0/testing/bdd.ts"; +import {ID} from "../src/id.ts"; + +describe("ID", () => { + test('unique', () => assertEquals(ID.unique(), 'unique()')); + test('custom', () => assertEquals(ID.custom('custom'), 'custom')); +}); diff --git a/templates/deno/test/permission.test.ts.twig b/templates/deno/test/permission.test.ts.twig new file mode 100644 index 0000000000..a9366fd43f --- /dev/null +++ b/templates/deno/test/permission.test.ts.twig @@ -0,0 +1,12 @@ +import { assertEquals } from "https://deno.land/std@0.204.0/assert/mod.ts"; +import { describe, it as test } from "https://deno.land/std@0.149.0/testing/bdd.ts"; +import {Permission} from "../src/permission.ts"; +import {Role} from "../src/role.ts"; + +describe('Permission', () => { + test('read', () => assertEquals(Permission.read(Role.any()), 'read("any")')); + test('write', () => assertEquals(Permission.write(Role.any()), 'write("any")')); + test('create', () => assertEquals(Permission.create(Role.any()), 'create("any")')); + test('update', () => assertEquals(Permission.update(Role.any()), 'update("any")')); + test('delete', () => assertEquals(Permission.delete(Role.any()), 'delete("any")')); +}) diff --git a/templates/deno/test/query.test.ts.twig b/templates/deno/test/query.test.ts.twig new file mode 100644 index 0000000000..e38db77ad6 --- /dev/null +++ b/templates/deno/test/query.test.ts.twig @@ -0,0 +1,175 @@ +import {describe, it as test} from "https://deno.land/std@0.149.0/testing/bdd.ts"; +import {assertEquals} from "https://deno.land/std@0.204.0/assert/assert_equals.ts"; +import {Query, QueryTypes} from "../src/query.ts"; + +type BasicFilterQueryTest = { + description: string; + value: QueryTypes; + expectedValues: string; +} + +const tests: BasicFilterQueryTest[] = [ + { + description: 'with a string', + value: 's', + expectedValues: '["s"]' + }, + { + description: 'with a integer', + value: 1, + expectedValues: '[1]' + }, + { + description: 'with a double', + value: 1.2, + expectedValues: '[1.2]' + }, + { + description: 'with a whole number double', + value: 1.0, + expectedValues: '[1]' + }, + { + description: 'with a bool', + value: false, + expectedValues: '[false]' + }, + { + description: 'with a list', + value: ['a', 'b', 'c'], + expectedValues: '["a","b","c"]' + } +]; + +describe('Query', () => { + describe('basic filter equal', () => { + for (const t of tests) { + test(t.description, () => + assertEquals( + Query.equal("attr", t.value), + `equal("attr", ${t.expectedValues})`, + ) + ) + } + }) + + describe('basic filter notEqual', () => { + for (const t of tests) { + test(t.description, () => + assertEquals( + Query.notEqual("attr", t.value), + `notEqual("attr", ${t.expectedValues})`, + ) + ) + } + }); + + describe('basic filter lessThan', () => { + for (const t of tests) { + test(t.description, () => + assertEquals( + Query.lessThan("attr", t.value), + `lessThan("attr", ${t.expectedValues})`, + ) + ) + } + }); + + describe('basic filter lessThanEqual', () => { + for (const t of tests) { + test(t.description, () => + assertEquals( + Query.lessThanEqual("attr", t.value), + `lessThanEqual("attr", ${t.expectedValues})`, + ) + ) + } + }); + + describe('basic filter greaterThan', () => { + for (const t of tests) { + test(t.description, () => + assertEquals( + Query.greaterThan("attr", t.value), + `greaterThan("attr", ${t.expectedValues})`, + ) + ) + } + }); + + describe('basic filter greaterThanEqual', () => { + for (const t of tests) { + test(t.description, () => + assertEquals( + Query.greaterThanEqual("attr", t.value), + `greaterThanEqual("attr", ${t.expectedValues})`, + ) + ) + } + }); + + test('search', () => assertEquals( + Query.search('attr', 'keyword1 keyword2'), + 'search("attr", ["keyword1 keyword2"])', + )); + + test('isNull', () => assertEquals( + Query.isNull('attr'), + 'isNull("attr")', + )); + + test('isNotNull', () => assertEquals( + Query.isNotNull('attr'), + 'isNotNull("attr")', + )); + + describe('between', () => { + test('with integers', () => assertEquals( + Query.between('attr', 1, 2), + 'between("attr", [1,2])' + )); + test('with doubles', () => assertEquals( + Query.between('attr', 1.2, 2.2), + 'between("attr", [1.2,2.2])' + )); + test('with strings', () => assertEquals( + Query.between('attr', "a", "z"), + 'between("attr", ["a","z"])' + )); + }); + + test('select', () => assertEquals( + Query.select(['attr1', 'attr2']), + 'select(["attr1","attr2"])', + )); + + test('orderAsc', () => assertEquals( + Query.orderAsc('attr'), + 'orderAsc("attr")', + )); + + test('orderDesc', () => assertEquals( + Query.orderDesc('attr'), + 'orderDesc("attr")', + )); + + test('cursorBefore', () => assertEquals( + Query.cursorBefore('attr'), + 'cursorBefore("attr")', + )); + + test('cursorAfter', () => assertEquals( + Query.cursorAfter('attr'), + 'cursorAfter("attr")', + )); + + test('limit', () => assertEquals( + Query.limit(1), + 'limit(1)' + )); + + test('offset', () => assertEquals( + Query.offset(1), + 'offset(1)' + )); +}) diff --git a/templates/deno/test/role.test.ts.twig b/templates/deno/test/role.test.ts.twig new file mode 100644 index 0000000000..e3f155da0d --- /dev/null +++ b/templates/deno/test/role.test.ts.twig @@ -0,0 +1,16 @@ +import { assertEquals } from "https://deno.land/std@0.204.0/assert/mod.ts"; +import { describe, it as test } from "https://deno.land/std@0.149.0/testing/bdd.ts"; +import {Role} from "../src/role.ts"; + +describe('Role', () => { + test('any', () => assertEquals(Role.any(), 'any')); + test('user without status', () => assertEquals(Role.user('custom'), 'user:custom')); + test('user with status', () => assertEquals(Role.user('custom', 'verified'), 'user:custom/verified')); + test('users without status', () => assertEquals(Role.users(), 'users')); + test('users with status', () => assertEquals(Role.users('verified'), 'users/verified')); + test('guests', () => assertEquals(Role.guests(), 'guests')); + test('team without role', () => assertEquals(Role.team('custom'), 'team:custom')) + test('team with role', () => assertEquals(Role.team('custom', 'owner'), 'team:custom/owner')) + test('member', () => assertEquals(Role.member('custom'), 'member:custom')) + test('label', () => assertEquals(Role.label('admin'), 'label:admin')) +}) diff --git a/templates/deno/test/services/service.test.ts.twig b/templates/deno/test/services/service.test.ts.twig new file mode 100644 index 0000000000..00743c1658 --- /dev/null +++ b/templates/deno/test/services/service.test.ts.twig @@ -0,0 +1,56 @@ +import {afterEach, describe, it as test} from "https://deno.land/std@0.204.0/testing/bdd.ts"; +import {restore, stub} from "https://deno.land/std@0.204.0/testing/mock.ts"; +import {assertEquals} from "https://deno.land/std@0.204.0/assert/assert_equals.ts"; +import { {{ service.name | caseUcfirst }} } from "../../src/services/{{ service.name | caseCamel }}.ts"; +import {Client} from "../../src/client.ts"; +import {InputFile} from "../../src/inputFile.ts" + +describe('{{ service.name | caseUcfirst }} service', () => { + const client = new Client(); + const {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client); + + afterEach(() => restore()) + + {% for method in service.methods ~%} + test('test method {{ method.name | caseCamel }}()', async () => { + {%~ if method.type == 'webAuth' %} + const data = ''; + {%~ elseif method.type == 'location' %} + const data = new Uint8Array(0); + {%~ else %} + {%- if method.responseModel and method.responseModel != 'any' %} + const data = { + {%- for definition in spec.definitions ~%}{%~ if definition.name == method.responseModel -%}{%~ for property in definition.properties | filter((param) => param.required) ~%} + '{{property.name | escapeDollarSign}}': {% if property.type == 'object' %}{}{% elseif property.type == 'array' %}[]{% elseif property.type == 'string' %}'{{property.example | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property.example}}{% endif %},{%~ endfor ~%}{% set break = true %}{%- else -%}{% set continue = true %}{%- endif -%}{%~ endfor -%} + }; + {%~ else %} + const data = ''; + {%~ endif %} + {%~ endif %} + + {%~ if method.type == 'location' %} + const stubbedFetch = stub(globalThis, 'fetch', () => Promise.resolve(new Response(data.buffer))); + {%~ elseif method.responseModel and method.responseModel != 'any' %} + const stubbedFetch = stub(globalThis, 'fetch', () => Promise.resolve(Response.json(data))); + {%~ else %} + const stubbedFetch = stub(globalThis, 'fetch', () => Promise.resolve(new Response(data))) + {%~ endif %} + + const response = await {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%} + {% if parameter.type == 'object' %}{}{% elseif parameter.type == 'array' %}[]{% elseif parameter.type == 'file' %}InputFile.fromBuffer(new Uint8Array(0), 'image.png'){% elseif parameter.type == 'boolean' %}true{% elseif parameter.type == 'string' %}'{% if parameter.example is not empty %}{{parameter.example | escapeDollarSign}}{% endif %}'{% elseif parameter.type == 'integer' and parameter['x-example'] is empty %}1{% elseif parameter.type == 'number' and parameter['x-example'] is empty %}1.0{% else %}{{parameter.example}}{%~ endif ~%},{%~ endfor ~%} + ); + + {%~ if method.type == 'location' %} + const buffer = await response.arrayBuffer(); + assertEquals(buffer.byteLength, 0); + {%~ elseif not method.responseModel or method.responseModel == 'any' %} + const text = await response.text(); + assertEquals(text, data); + {%~ else %} + assertEquals(response, data); + {%~ endif %} + stubbedFetch.restore(); + }); + + {% endfor %} +})