From 8448e577313be573e0978e39d40b90de694d1e9b Mon Sep 17 00:00:00 2001 From: geisterfurz007 Date: Sat, 28 Oct 2023 15:22:08 +0200 Subject: [PATCH 1/2] feat: node tests --- src/SDK/Language/Node.php | 25 +++ templates/node/package.json.twig | 3 +- templates/node/test/id.test.js.twig | 6 + templates/node/test/permission.test.js.twig | 10 ++ templates/node/test/query.test.js.twig | 155 ++++++++++++++++++ templates/node/test/role.test.js.twig | 14 ++ .../node/test/services/service.test.js.twig | 38 +++++ 7 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 templates/node/test/id.test.js.twig create mode 100644 templates/node/test/permission.test.js.twig create mode 100644 templates/node/test/query.test.js.twig create mode 100644 templates/node/test/role.test.js.twig create mode 100644 templates/node/test/services/service.test.js.twig diff --git a/src/SDK/Language/Node.php b/src/SDK/Language/Node.php index 435e1cce0..d7a2d3f4b 100644 --- a/src/SDK/Language/Node.php +++ b/src/SDK/Language/Node.php @@ -61,21 +61,41 @@ public function getFiles(): array 'destination' => 'lib/permission.js', 'template' => 'node/lib/permission.js.twig', ], + [ + 'scope' => 'default', + 'destination' => 'test/permission.test.js', + 'template' => 'node/test/permission.test.js.twig', + ], [ 'scope' => 'default', 'destination' => 'lib/role.js', 'template' => 'node/lib/role.js.twig', ], + [ + 'scope' => 'default', + 'destination' => 'test/role.test.js', + 'template' => 'node/test/role.test.js.twig', + ], [ 'scope' => 'default', 'destination' => 'lib/id.js', 'template' => 'node/lib/id.js.twig', ], + [ + 'scope' => 'default', + 'destination' => 'test/id.test.js', + 'template' => 'node/test/id.test.js.twig', + ], [ 'scope' => 'default', 'destination' => 'lib/query.js', 'template' => 'node/lib/query.js.twig', ], + [ + 'scope' => 'default', + 'destination' => 'test/query.test.js', + 'template' => 'node/test/query.test.js.twig', + ], [ 'scope' => 'default', 'destination' => 'lib/inputFile.js', @@ -91,6 +111,11 @@ public function getFiles(): array 'destination' => '/lib/services/{{service.name | caseDash}}.js', 'template' => 'node/lib/services/service.js.twig', ], + [ + 'scope' => 'service', + 'destination' => '/test/services/{{service.name | caseDash}}.test.js', + 'template' => 'node/test/services/service.test.js.twig', + ], [ 'scope' => 'default', 'destination' => 'README.md', diff --git a/templates/node/package.json.twig b/templates/node/package.json.twig index 6a8eae40c..ae589e61b 100644 --- a/templates/node/package.json.twig +++ b/templates/node/package.json.twig @@ -11,7 +11,8 @@ "url": "{{ sdk.gitURL }}" }, "devDependencies": { - "@types/node": "^18.16.1" + "@types/node": "^18.16.1", + "jest": "^29.7.0" }, "dependencies": { "axios": "^1.4.0", diff --git a/templates/node/test/id.test.js.twig b/templates/node/test/id.test.js.twig new file mode 100644 index 000000000..d28fab758 --- /dev/null +++ b/templates/node/test/id.test.js.twig @@ -0,0 +1,6 @@ +const ID = require("../lib/id"); + +describe("ID", () => { + test('unique', () => expect(ID.unique()).toEqual('unique()')); + test('custom', () => expect(ID.custom('custom')).toEqual('custom')); +}); diff --git a/templates/node/test/permission.test.js.twig b/templates/node/test/permission.test.js.twig new file mode 100644 index 000000000..8fb854e78 --- /dev/null +++ b/templates/node/test/permission.test.js.twig @@ -0,0 +1,10 @@ +const Permission = require("../lib/permission"); +const Role = require("../lib/role"); + +describe('Permission', () => { + test('read', () => expect(Permission.read(Role.any())).toEqual('read("any")')); + test('write', () => expect(Permission.write(Role.any())).toEqual('write("any")')); + test('create', () => expect(Permission.create(Role.any())).toEqual('create("any")')); + test('update', () => expect(Permission.update(Role.any())).toEqual('update("any")')); + test('delete', () => expect(Permission.delete(Role.any())).toEqual('delete("any")')); +}) diff --git a/templates/node/test/query.test.js.twig b/templates/node/test/query.test.js.twig new file mode 100644 index 000000000..c1dd7e632 --- /dev/null +++ b/templates/node/test/query.test.js.twig @@ -0,0 +1,155 @@ +const Query = require("../lib/query"); + +const tests = [ + { + 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, () => + expect(Query.equal("attr", t.value)) + .toEqual(`equal("attr", ${t.expectedValues})`) + ) + } + }) + + describe('basic filter notEqual', () => { + for (const t of tests) { + test(t.description, () => + expect(Query.notEqual("attr", t.value)) + .toEqual(`notEqual("attr", ${t.expectedValues})`) + ) + } + }); + + describe('basic filter lessThan', () => { + for (const t of tests) { + test(t.description, () => + expect(Query.lessThan("attr", t.value)) + .toEqual(`lessThan("attr", ${t.expectedValues})`) + ) + } + }); + + describe('basic filter lessThanEqual', () => { + for (const t of tests) { + test(t.description, () => + expect(Query.lessThanEqual("attr", t.value)) + .toEqual(`lessThanEqual("attr", ${t.expectedValues})`) + ) + } + }); + + describe('basic filter greaterThan', () => { + for (const t of tests) { + test(t.description, () => + expect(Query.greaterThan("attr", t.value)) + .toEqual(`greaterThan("attr", ${t.expectedValues})`) + ) + } + }); + + describe('basic filter greaterThanEqual', () => { + for (const t of tests) { + test(t.description, () => + expect(Query.greaterThanEqual("attr", t.value)) + .toEqual(`greaterThanEqual("attr", ${t.expectedValues})`) + ) + } + }); + + test('search', () => + expect(Query.search('attr', 'keyword1 keyword2')) + .toEqual('search("attr", ["keyword1 keyword2"])') + ); + + test('isNull', () => + expect(Query.isNull('attr')) + .toEqual('isNull("attr")') + ); + + test('isNotNull', () => + expect(Query.isNotNull('attr')) + .toEqual('isNotNull("attr")') + ); + + describe('between', () => { + test('with integers', () => + expect(Query.between('attr', 1, 2)) + .toEqual('between("attr", [1,2])') + ); + test('with doubles', () => + expect(Query.between('attr', 1.2, 2.2)) + .toEqual('between("attr", [1.2,2.2])') + ); + test('with strings', () => + expect(Query.between('attr', "a", "z")) + .toEqual('between("attr", ["a","z"])') + ); + }); + + test('select', () => + expect(Query.select(['attr1', 'attr2'])) + .toEqual('select(["attr1","attr2"])') + ); + + test('orderAsc', () => + expect(Query.orderAsc('attr')) + .toEqual('orderAsc("attr")') + ); + + test('orderDesc', () => + expect(Query.orderDesc('attr')) + .toEqual('orderDesc("attr")') + ); + + test('cursorBefore', () => + expect(Query.cursorBefore('attr')) + .toEqual('cursorBefore("attr")') + ); + + test('cursorAfter', () => + expect(Query.cursorAfter('attr')) + .toEqual('cursorAfter("attr")') + ); + + test('limit', () => + expect(Query.limit(1)) + .toEqual('limit(1)') + ); + + test('offset', () => + expect(Query.offset(1)) + .toEqual('offset(1)') + ); +}) diff --git a/templates/node/test/role.test.js.twig b/templates/node/test/role.test.js.twig new file mode 100644 index 000000000..61dd4231f --- /dev/null +++ b/templates/node/test/role.test.js.twig @@ -0,0 +1,14 @@ +const Role = require("../lib/role"); + +describe('Role', () => { + test('any', () => expect(Role.any()).toEqual('any')); + test('user without status', () => expect(Role.user('custom')).toEqual('user:custom')); + test('user with status', () => expect(Role.user('custom', 'verified')).toEqual('user:custom/verified')); + test('users without status', () => expect(Role.users()).toEqual('users')); + test('users with status', () => expect(Role.users('verified')).toEqual('users/verified')); + test('guests', () => expect(Role.guests()).toEqual('guests')); + test('team without role', () => expect(Role.team('custom')).toEqual('team:custom')) + test('team with role', () => expect(Role.team('custom', 'owner')).toEqual('team:custom/owner')) + test('member', () => expect(Role.member('custom')).toEqual('member:custom')) + test('label', () => expect(Role.label('admin')).toEqual('label:admin')) +}) diff --git a/templates/node/test/services/service.test.js.twig b/templates/node/test/services/service.test.js.twig new file mode 100644 index 000000000..8d77ddf5d --- /dev/null +++ b/templates/node/test/services/service.test.js.twig @@ -0,0 +1,38 @@ +const Client = require("../../lib/client"); +const InputFile = require("../../lib/inputFile"); +const {{ service.name | caseUcfirst }} = require("../../lib/services/{{ service.name | caseCamel }}"); + +const mockedAxios = require("axios"); +jest.mock('axios', () => jest.fn()); + +describe('{{ service.name | caseUcfirst }}', () => { + const client = new Client(); + const {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client); + + {% 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 %} + + mockedAxios.mockImplementation(() => Promise.resolve({data: data})); + + 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 ~%} + ); + + expect(response).toEqual(data); + }); + {% endfor %} +}) From e4dbb20ac9ca68cd3284a54985cceb4a7369a935 Mon Sep 17 00:00:00 2001 From: geisterfurz007 Date: Sun, 19 Nov 2023 16:38:10 +0100 Subject: [PATCH 2/2] fix: updated between serialization --- templates/node/test/query.test.js.twig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/node/test/query.test.js.twig b/templates/node/test/query.test.js.twig index c1dd7e632..0dff5eb5f 100644 --- a/templates/node/test/query.test.js.twig +++ b/templates/node/test/query.test.js.twig @@ -106,15 +106,15 @@ describe('Query', () => { describe('between', () => { test('with integers', () => expect(Query.between('attr', 1, 2)) - .toEqual('between("attr", [1,2])') + .toEqual('between("attr", 1, 2)') ); test('with doubles', () => expect(Query.between('attr', 1.2, 2.2)) - .toEqual('between("attr", [1.2,2.2])') + .toEqual('between("attr", 1.2, 2.2)') ); test('with strings', () => expect(Query.between('attr', "a", "z")) - .toEqual('between("attr", ["a","z"])') + .toEqual('between("attr", "a" "z")') ); });