-
Notifications
You must be signed in to change notification settings - Fork 174
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
lohanidamodar
merged 2 commits into
appwrite:master
from
mvarendorff:feat-680-add-unit-tests-deno
Jan 2, 2024
Merged
Unit Tests - Deno #735
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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; | ||
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)}`; | ||
} | ||
} | ||
} |
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,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')); | ||
}); |
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,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")')); | ||
}) |
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,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)' | ||
)); | ||
}) |
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,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')) | ||
}) |
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,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 %} | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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