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

Unit Tests - Deno #735

Merged
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
25 changes: 25 additions & 0 deletions src/SDK/Language/Deno.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
12 changes: 6 additions & 6 deletions templates/deno/src/exception.ts.twig
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
export class {{ spec.title | caseUcfirst}}Exception {
message: String;
code: Number;
message: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Good catch! Not sure why we were using these capital Object types

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)}`;
}
}
}
8 changes: 8 additions & 0 deletions templates/deno/test/id.test.ts.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {assertEquals} from "https://deno.land/[email protected]/assert/mod.ts";
import {describe, it as test} from "https://deno.land/[email protected]/testing/bdd.ts";
import {ID} from "../src/id.ts";

describe("ID", () => {
test('unique', () => assertEquals(ID.unique(), 'unique()'));
test('custom', () => assertEquals(ID.custom('custom'), 'custom'));
});
12 changes: 12 additions & 0 deletions templates/deno/test/permission.test.ts.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
import { describe, it as test } from "https://deno.land/[email protected]/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")'));
})
175 changes: 175 additions & 0 deletions templates/deno/test/query.test.ts.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import {describe, it as test} from "https://deno.land/[email protected]/testing/bdd.ts";
import {assertEquals} from "https://deno.land/[email protected]/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)'
));
})
16 changes: 16 additions & 0 deletions templates/deno/test/role.test.ts.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
import { describe, it as test } from "https://deno.land/[email protected]/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'))
})
56 changes: 56 additions & 0 deletions templates/deno/test/services/service.test.ts.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {afterEach, describe, it as test} from "https://deno.land/[email protected]/testing/bdd.ts";
import {restore, stub} from "https://deno.land/[email protected]/testing/mock.ts";
import {assertEquals} from "https://deno.land/[email protected]/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 %}
})