Skip to content

Commit 17e9c9e

Browse files
committed
feat: deno tests
1 parent 4d99588 commit 17e9c9e

File tree

7 files changed

+298
-6
lines changed

7 files changed

+298
-6
lines changed

src/SDK/Language/Deno.php

+25
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,41 @@ public function getFiles(): array
3333
'destination' => 'src/permission.ts',
3434
'template' => 'deno/src/permission.ts.twig',
3535
],
36+
[
37+
'scope' => 'default',
38+
'destination' => 'test/permission.test.ts',
39+
'template' => 'deno/test/permission.test.ts.twig',
40+
],
3641
[
3742
'scope' => 'default',
3843
'destination' => 'src/role.ts',
3944
'template' => 'deno/src/role.ts.twig',
4045
],
46+
[
47+
'scope' => 'default',
48+
'destination' => 'test/role.test.ts',
49+
'template' => 'deno/test/role.test.ts.twig',
50+
],
4151
[
4252
'scope' => 'default',
4353
'destination' => 'src/id.ts',
4454
'template' => 'deno/src/id.ts.twig',
4555
],
56+
[
57+
'scope' => 'default',
58+
'destination' => 'test/id.test.ts',
59+
'template' => 'deno/test/id.test.ts.twig',
60+
],
4661
[
4762
'scope' => 'default',
4863
'destination' => 'src/query.ts',
4964
'template' => 'deno/src/query.ts.twig',
5065
],
66+
[
67+
'scope' => 'default',
68+
'destination' => 'test/query.test.ts',
69+
'template' => 'deno/test/query.test.ts.twig',
70+
],
5171
[
5272
'scope' => 'default',
5373
'destination' => 'src/inputFile.ts',
@@ -73,6 +93,11 @@ public function getFiles(): array
7393
'destination' => '/src/services/{{service.name | caseDash}}.ts',
7494
'template' => 'deno/src/services/service.ts.twig',
7595
],
96+
[
97+
'scope' => 'service',
98+
'destination' => '/test/services/{{service.name | caseDash}}.test.ts',
99+
'template' => 'deno/test/services/service.test.ts.twig',
100+
],
76101
[
77102
'scope' => 'default',
78103
'destination' => 'README.md',

templates/deno/src/exception.ts.twig

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
export class {{ spec.title | caseUcfirst}}Exception {
2-
message: String;
3-
code: Number;
2+
message: string;
3+
code: number;
44
response: any;
5-
type: String;
5+
type: string;
66

7-
constructor(message: String, code: Number = 0, type: String = "", response: any = "") {
7+
constructor(message: string, code: number = 0, type: string = "", response: any = "") {
88
this.message = message;
99
this.code = code;
1010
this.type = type;
1111
this.response = response;
1212
}
1313

14-
public toString(): String {
14+
public toString(): string {
1515
return `${this.message} - ${this.code} - ${this.type} - ${JSON.stringify(this.response)}`;
1616
}
17-
}
17+
}

templates/deno/test/id.test.ts.twig

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {assertEquals} from "https://deno.land/[email protected]/assert/mod.ts";
2+
import {describe, it as test} from "https://deno.land/[email protected]/testing/bdd.ts";
3+
import {ID} from "../src/id.ts";
4+
5+
describe("ID", () => {
6+
test('unique', () => assertEquals(ID.unique(), 'unique()'));
7+
test('custom', () => assertEquals(ID.custom('custom'), 'custom'));
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
2+
import { describe, it as test } from "https://deno.land/[email protected]/testing/bdd.ts";
3+
import {Permission} from "../src/permission.ts";
4+
import {Role} from "../src/role.ts";
5+
6+
describe('Permission', () => {
7+
test('read', () => assertEquals(Permission.read(Role.any()), 'read("any")'));
8+
test('write', () => assertEquals(Permission.write(Role.any()), 'write("any")'));
9+
test('create', () => assertEquals(Permission.create(Role.any()), 'create("any")'));
10+
test('update', () => assertEquals(Permission.update(Role.any()), 'update("any")'));
11+
test('delete', () => assertEquals(Permission.delete(Role.any()), 'delete("any")'));
12+
})
+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
import {describe, it as test} from "https://deno.land/[email protected]/testing/bdd.ts";
2+
import {assertEquals} from "https://deno.land/[email protected]/assert/assert_equals.ts";
3+
import {Query, QueryTypes} from "../src/query.ts";
4+
5+
type BasicFilterQueryTest = {
6+
description: string;
7+
value: QueryTypes;
8+
expectedValues: string;
9+
}
10+
11+
const tests: BasicFilterQueryTest[] = [
12+
{
13+
description: 'with a string',
14+
value: 's',
15+
expectedValues: '["s"]'
16+
},
17+
{
18+
description: 'with a integer',
19+
value: 1,
20+
expectedValues: '[1]'
21+
},
22+
{
23+
description: 'with a double',
24+
value: 1.2,
25+
expectedValues: '[1.2]'
26+
},
27+
{
28+
description: 'with a whole number double',
29+
value: 1.0,
30+
expectedValues: '[1]'
31+
},
32+
{
33+
description: 'with a bool',
34+
value: false,
35+
expectedValues: '[false]'
36+
},
37+
{
38+
description: 'with a list',
39+
value: ['a', 'b', 'c'],
40+
expectedValues: '["a","b","c"]'
41+
}
42+
];
43+
44+
describe('Query', () => {
45+
describe('basic filter equal', () => {
46+
for (const t of tests) {
47+
test(t.description, () =>
48+
assertEquals(
49+
Query.equal("attr", t.value),
50+
`equal("attr", ${t.expectedValues})`,
51+
)
52+
)
53+
}
54+
})
55+
56+
describe('basic filter notEqual', () => {
57+
for (const t of tests) {
58+
test(t.description, () =>
59+
assertEquals(
60+
Query.notEqual("attr", t.value),
61+
`notEqual("attr", ${t.expectedValues})`,
62+
)
63+
)
64+
}
65+
});
66+
67+
describe('basic filter lessThan', () => {
68+
for (const t of tests) {
69+
test(t.description, () =>
70+
assertEquals(
71+
Query.lessThan("attr", t.value),
72+
`lessThan("attr", ${t.expectedValues})`,
73+
)
74+
)
75+
}
76+
});
77+
78+
describe('basic filter lessThanEqual', () => {
79+
for (const t of tests) {
80+
test(t.description, () =>
81+
assertEquals(
82+
Query.lessThanEqual("attr", t.value),
83+
`lessThanEqual("attr", ${t.expectedValues})`,
84+
)
85+
)
86+
}
87+
});
88+
89+
describe('basic filter greaterThan', () => {
90+
for (const t of tests) {
91+
test(t.description, () =>
92+
assertEquals(
93+
Query.greaterThan("attr", t.value),
94+
`greaterThan("attr", ${t.expectedValues})`,
95+
)
96+
)
97+
}
98+
});
99+
100+
describe('basic filter greaterThanEqual', () => {
101+
for (const t of tests) {
102+
test(t.description, () =>
103+
assertEquals(
104+
Query.greaterThanEqual("attr", t.value),
105+
`greaterThanEqual("attr", ${t.expectedValues})`,
106+
)
107+
)
108+
}
109+
});
110+
111+
test('search', () => assertEquals(
112+
Query.search('attr', 'keyword1 keyword2'),
113+
'search("attr", ["keyword1 keyword2"])',
114+
));
115+
116+
test('isNull', () => assertEquals(
117+
Query.isNull('attr'),
118+
'isNull("attr")',
119+
));
120+
121+
test('isNotNull', () => assertEquals(
122+
Query.isNotNull('attr'),
123+
'isNotNull("attr")',
124+
));
125+
126+
describe('between', () => {
127+
test('with integers', () => assertEquals(
128+
Query.between('attr', 1, 2),
129+
'between("attr", [1,2])'
130+
));
131+
test('with doubles', () => assertEquals(
132+
Query.between('attr', 1.2, 2.2),
133+
'between("attr", [1.2,2.2])'
134+
));
135+
test('with strings', () => assertEquals(
136+
Query.between('attr', "a", "z"),
137+
'between("attr", ["a","z"])'
138+
));
139+
});
140+
141+
test('select', () => assertEquals(
142+
Query.select(['attr1', 'attr2']),
143+
'select(["attr1","attr2"])',
144+
));
145+
146+
test('orderAsc', () => assertEquals(
147+
Query.orderAsc('attr'),
148+
'orderAsc("attr")',
149+
));
150+
151+
test('orderDesc', () => assertEquals(
152+
Query.orderDesc('attr'),
153+
'orderDesc("attr")',
154+
));
155+
156+
test('cursorBefore', () => assertEquals(
157+
Query.cursorBefore('attr'),
158+
'cursorBefore("attr")',
159+
));
160+
161+
test('cursorAfter', () => assertEquals(
162+
Query.cursorAfter('attr'),
163+
'cursorAfter("attr")',
164+
));
165+
166+
test('limit', () => assertEquals(
167+
Query.limit(1),
168+
'limit(1)'
169+
));
170+
171+
test('offset', () => assertEquals(
172+
Query.offset(1),
173+
'offset(1)'
174+
));
175+
})

templates/deno/test/role.test.ts.twig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
2+
import { describe, it as test } from "https://deno.land/[email protected]/testing/bdd.ts";
3+
import {Role} from "../src/role.ts";
4+
5+
describe('Role', () => {
6+
test('any', () => assertEquals(Role.any(), 'any'));
7+
test('user without status', () => assertEquals(Role.user('custom'), 'user:custom'));
8+
test('user with status', () => assertEquals(Role.user('custom', 'verified'), 'user:custom/verified'));
9+
test('users without status', () => assertEquals(Role.users(), 'users'));
10+
test('users with status', () => assertEquals(Role.users('verified'), 'users/verified'));
11+
test('guests', () => assertEquals(Role.guests(), 'guests'));
12+
test('team without role', () => assertEquals(Role.team('custom'), 'team:custom'))
13+
test('team with role', () => assertEquals(Role.team('custom', 'owner'), 'team:custom/owner'))
14+
test('member', () => assertEquals(Role.member('custom'), 'member:custom'))
15+
test('label', () => assertEquals(Role.label('admin'), 'label:admin'))
16+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {afterEach, describe, it as test} from "https://deno.land/[email protected]/testing/bdd.ts";
2+
import {restore, stub} from "https://deno.land/[email protected]/testing/mock.ts";
3+
import {assertEquals} from "https://deno.land/[email protected]/assert/assert_equals.ts";
4+
import { {{ service.name | caseUcfirst }} } from "../../src/services/{{ service.name | caseCamel }}.ts";
5+
import {Client} from "../../src/client.ts";
6+
import {InputFile} from "../../src/inputFile.ts"
7+
8+
describe('{{ service.name | caseUcfirst }} service', () => {
9+
const client = new Client();
10+
const {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client);
11+
12+
afterEach(() => restore())
13+
14+
{% for method in service.methods ~%}
15+
test('test method {{ method.name | caseCamel }}()', async () => {
16+
{%~ if method.type == 'webAuth' %}
17+
const data = '';
18+
{%~ elseif method.type == 'location' %}
19+
const data = new Uint8Array(0);
20+
{%~ else %}
21+
{%- if method.responseModel and method.responseModel != 'any' %}
22+
const data = {
23+
{%- for definition in spec.definitions ~%}{%~ if definition.name == method.responseModel -%}{%~ for property in definition.properties | filter((param) => param.required) ~%}
24+
'{{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 -%}
25+
};
26+
{%~ else %}
27+
const data = '';
28+
{%~ endif %}
29+
{%~ endif %}
30+
31+
{%~ if method.type == 'location' %}
32+
const stubbedFetch = stub(globalThis, 'fetch', () => Promise.resolve(new Response(data.buffer)));
33+
{%~ elseif method.responseModel and method.responseModel != 'any' %}
34+
const stubbedFetch = stub(globalThis, 'fetch', () => Promise.resolve(Response.json(data)));
35+
{%~ else %}
36+
const stubbedFetch = stub(globalThis, 'fetch', () => Promise.resolve(new Response(data)))
37+
{%~ endif %}
38+
39+
const response = await {{ service.name | caseCamel }}.{{ method.name | caseCamel }}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%}
40+
{% 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 ~%}
41+
);
42+
43+
{%~ if method.type == 'location' %}
44+
const buffer = await response.arrayBuffer();
45+
assertEquals(buffer.byteLength, 0);
46+
{%~ elseif not method.responseModel or method.responseModel == 'any' %}
47+
const text = await response.text();
48+
assertEquals(text, data);
49+
{%~ else %}
50+
assertEquals(response, data);
51+
{%~ endif %}
52+
stubbedFetch.restore();
53+
});
54+
55+
{% endfor %}
56+
})

0 commit comments

Comments
 (0)