Skip to content

Commit

Permalink
Add tests for helpers and moels to Flutter and Dart SDKs
Browse files Browse the repository at this point in the history
  • Loading branch information
stnguyen90 committed Jun 20, 2023
1 parent fbeead0 commit e427d24
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/SDK/Language/Dart.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,36 @@ public function getFiles(): array
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
'template' => 'dart/docs/example.md.twig',
],
[
'scope' => 'default',
'destination' => '/test/id_test.dart',
'template' => 'dart/test/id_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '/test/permission_test.dart',
'template' => 'dart/test/permission_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '/test/query_test.dart',
'template' => 'dart/test/query_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '/test/role_test.dart',
'template' => 'dart/test/role_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '/test/src/enums_test.dart',
'template' => 'dart/test/src/enums_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '/test/src/response_test.dart',
'template' => 'dart/test/src/response_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '.travis.yml',
Expand Down
35 changes: 35 additions & 0 deletions src/SDK/Language/Flutter.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,41 @@ public function getFiles(): array
'destination' => '/test/services/{{service.name | caseDash}}_test.dart',
'template' => 'flutter/test/services/service_test.dart.twig',
],
[
'scope' => 'definition',
'destination' => '/test/src/models/{{definition.name | caseSnake }}_test.dart',
'template' => 'dart/test/src/models/model_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '/test/id_test.dart',
'template' => 'dart/test/id_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '/test/permission_test.dart',
'template' => 'dart/test/permission_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '/test/query_test.dart',
'template' => 'dart/test/query_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '/test/role_test.dart',
'template' => 'dart/test/role_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '/test/src/enums_test.dart',
'template' => 'dart/test/src/enums_test.dart.twig',
],
[
'scope' => 'default',
'destination' => '/test/src/response_test.dart',
'template' => 'dart/test/src/response_test.dart.twig',
],
[
'scope' => 'method',
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
Expand Down
16 changes: 16 additions & 0 deletions templates/dart/test/id_test.dart.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:{{ language.params.packageName }}/{{ language.params.packageName }}.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('unique()', () {
test('returns unique()', () {
expect(ID.unique(), 'unique()');
});
});

group('custom()', () {
test('returns the custom string', () {
expect(ID.custom('custom'), 'custom');
});
});
}
34 changes: 34 additions & 0 deletions templates/dart/test/permission_test.dart.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:{{ language.params.packageName }}/{{ language.params.packageName }}.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('read()', () {
test('returns read', () {
expect(Permission.read(Role.any()), 'read("any")');
});
});

group('write()', () {
test('returns write', () {
expect(Permission.write(Role.any()), 'write("any")');
});
});

group('create()', () {
test('returns create', () {
expect(Permission.create(Role.any()), 'create("any")');
});
});

group('update()', () {
test('returns update', () {
expect(Permission.update(Role.any()), 'update("any")');
});
});

group('delete()', () {
test('returns delete', () {
expect(Permission.delete(Role.any()), 'delete("any")');
});
});
}
192 changes: 192 additions & 0 deletions templates/dart/test/query_test.dart.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import 'package:{{ language.params.packageName }}/{{ language.params.packageName }}.dart';
import 'package:flutter_test/flutter_test.dart';

class BasicFilterQueryTest {
final String description;
final dynamic value;
final String expectedValues;

BasicFilterQueryTest({
required this.description,
required this.value,
required this.expectedValues,
});
}

void main() {
group('basic filter tests', () {
final tests = [
BasicFilterQueryTest(
description: 'with a string',
value: 's',
expectedValues: '["s"]',
),
BasicFilterQueryTest(
description: 'with an integer',
value: 1,
expectedValues: '[1]',
),
BasicFilterQueryTest(
description: 'with a double',
value: 1.2,
expectedValues: '[1.2]',
),
BasicFilterQueryTest(
description: 'with a whole number double',
value: 1.0,
expectedValues: '[1.0]',
),
BasicFilterQueryTest(
description: 'with a bool',
value: false,
expectedValues: '[false]',
),
BasicFilterQueryTest(
description: 'with a list',
value: ['a', 'b', 'c'],
expectedValues: '["a","b","c"]',
),
];

group('equal()', () {
for (var t in tests) {
test(t.description, () {
expect(
Query.equal('attr', t.value),
'equal("attr", ${t.expectedValues})',
);
});
}
});

group('notEqual()', () {
for (var t in tests) {
test(t.description, () {
expect(
Query.notEqual('attr', t.value),
'notEqual("attr", ${t.expectedValues})',
);
});
}
});

group('lessThan()', () {
for (var t in tests) {
test(t.description, () {
expect(
Query.lessThan('attr', t.value),
'lessThan("attr", ${t.expectedValues})',
);
});
}
});

group('lessThanEqual()', () {
for (var t in tests) {
test(t.description, () {
expect(
Query.lessThanEqual('attr', t.value),
'lessThanEqual("attr", ${t.expectedValues})',
);
});
}
});

group('greaterThan()', () {
for (var t in tests) {
test(t.description, () {
expect(
Query.greaterThan('attr', t.value),
'greaterThan("attr", ${t.expectedValues})',
);
});
}
});

group('greaterThanEqual()', () {
for (var t in tests) {
test(t.description, () {
expect(
Query.greaterThanEqual('attr', t.value),
'greaterThanEqual("attr", ${t.expectedValues})',
);
});
}
});
});

group('search()', () {
test('returns search', () {
expect(Query.search('attr', 'keyword1 keyword2'), 'search("attr", ["keyword1 keyword2"])');
});
});

group('isNull()', () {
test('returns isNull', () {
expect(Query.isNull('attr'), 'isNull("attr")');
});
});

group('isNotNull()', () {
test('returns isNotNull', () {
expect(Query.isNotNull('attr'), 'isNotNull("attr")');
});
});

group('between()', () {
test('with integers', () {
expect(Query.between('attr', 1, 2), 'between("attr", [1,2])');
});

test('with doubles', () {
expect(Query.between('attr', 1.0, 2.0), 'between("attr", [1.0,2.0])');
});

test('with strings', () {
expect(Query.between('attr', "a", "z"), 'between("attr", ["a","z"])');
});
});

group('select()', () {
test('returns select', () {
expect(Query.select(['attr1', 'attr2']), 'select(["attr1","attr2"])');
});
});

group('orderAsc()', () {
test('returns orderAsc', () {
expect(Query.orderAsc('attr'), 'orderAsc("attr")');
});
});

group('orderDesc()', () {
test('returns orderDesc', () {
expect(Query.orderDesc('attr'), 'orderDesc("attr")');
});
});

group('cursorBefore()', () {
test('returns cursorBefore', () {
expect(Query.cursorBefore(ID.custom('custom')), 'cursorBefore("custom")');
});
});

group('cursorAfter()', () {
test('returns cursorAfter', () {
expect(Query.cursorAfter(ID.custom('custom')), 'cursorAfter("custom")');
});
});

group('limit()', () {
test('returns limit', () {
expect(Query.limit(1), 'limit(1)');
});
});

group('offset()', () {
test('returns offset', () {
expect(Query.offset(1), 'offset(1)');
});
});
}

52 changes: 52 additions & 0 deletions templates/dart/test/role_test.dart.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:{{ language.params.packageName }}/{{ language.params.packageName }}.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('any()', () {
test('returns any', () {
expect(Role.any(), 'any');
});
});

group('user()', () {
test('without status', () {
expect(Role.user('custom'), 'user:custom');
});

test('with status', () {
expect(Role.user('custom', 'verified'), 'user:custom/verified');
});
});

group('users()', () {
test('without status', () {
expect(Role.users(), 'users');
});

test('with status', () {
expect(Role.users('verified'), 'users/verified');
});
});

group('guests()', () {
test('returns guests', () {
expect(Role.guests(), 'guests');
});
});

group('team()', () {
test('without role', () {
expect(Role.team('custom'), 'team:custom');
});

test('with role', () {
expect(Role.team('custom', 'owner'), 'team:custom/owner');
});
});

group('member()', () {
test('returns member', () {
expect(Role.member('custom'), 'member:custom');
});
});
}
12 changes: 12 additions & 0 deletions templates/dart/test/src/enums_test.dart.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:{{ language.params.packageName }}/src/enums.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('name()', () {
for (final method in HttpMethod.values) {
test('returns ${method.toString().split('.').last.toUpperCase()} for $method', () {
expect(method.name(), method.toString().split('.').last.toUpperCase());
});
}
});
}
Loading

0 comments on commit e427d24

Please sign in to comment.