Skip to content

Commit a55bd2b

Browse files
committed
chore(flutter): fix tests
1 parent 20a0470 commit a55bd2b

File tree

5 files changed

+150
-25
lines changed

5 files changed

+150
-25
lines changed

src/SDK/Language/Flutter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function getFiles(): array
253253
[
254254
'scope' => 'service',
255255
'destination' => '/test/services/{{service.name | caseDash}}_test.dart',
256-
'template' => 'dart/test/services/service_test.dart.twig',
256+
'template' => 'flutter/test/services/service_test.dart.twig',
257257
],
258258
[
259259
'scope' => 'definition',

templates/dart/test/query_test.dart.twig

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,16 @@ void main() {
6767

6868
group('notEqual()', () {
6969
for (var t in tests) {
70-
test(t.description, () {
71-
final query = jsonDecode(Query.notEqual('attr', t.value));
72-
expect(query['attribute'], 'attr');
73-
expect(query['values'], t.expectedValues);
74-
expect(query['method'], 'notEqual');
75-
});
70+
test(
71+
t.description,
72+
() {
73+
final query = jsonDecode(Query.notEqual('attr', t.value));
74+
expect(query['attribute'], 'attr');
75+
expect(query['values'], t.expectedValues);
76+
expect(query['method'], 'notEqual');
77+
},
78+
skip: t.value is List,
79+
);
7680
}
7781
});
7882

@@ -189,28 +193,28 @@ void main() {
189193
test('returns cursorBefore', () {
190194
final query = jsonDecode(Query.cursorBefore('custom'));
191195
expect(query['attribute'], null);
192-
expect(query['values'], 'custom');
196+
expect(query['values'], ['custom']);
193197
expect(query['method'], 'cursorBefore');
194198
});
195199

196200
test('returns cursorAfter', () {
197201
final query = jsonDecode(Query.cursorAfter('custom'));
198202
expect(query['attribute'], null);
199-
expect(query['values'], 'custom');
203+
expect(query['values'], ['custom']);
200204
expect(query['method'], 'cursorAfter');
201205
});
202206

203207
test('returns limit', () {
204208
final query = jsonDecode(Query.limit(1));
205209
expect(query['attribute'], null);
206-
expect(query['values'], 1);
210+
expect(query['values'], [1]);
207211
expect(query['method'], 'limit');
208212
});
209213

210214
test('returns offset', () {
211215
final query = jsonDecode(Query.offset(1));
212216
expect(query['attribute'], null);
213-
expect(query['values'], 1);
217+
expect(query['values'], [1]);
214218
expect(query['method'], 'offset');
215219
});
216220

@@ -268,28 +272,28 @@ void main() {
268272
test('returns createdBefore', () {
269273
final query = jsonDecode(Query.createdBefore('2023-01-01'));
270274
expect(query['attribute'], null);
271-
expect(query['values'], '2023-01-01');
275+
expect(query['values'], ['2023-01-01']);
272276
expect(query['method'], 'createdBefore');
273277
});
274278

275279
test('returns createdAfter', () {
276280
final query = jsonDecode(Query.createdAfter('2023-01-01'));
277281
expect(query['attribute'], null);
278-
expect(query['values'], '2023-01-01');
282+
expect(query['values'], ['2023-01-01']);
279283
expect(query['method'], 'createdAfter');
280284
});
281285

282286
test('returns updatedBefore', () {
283287
final query = jsonDecode(Query.updatedBefore('2023-01-01'));
284288
expect(query['attribute'], null);
285-
expect(query['values'], '2023-01-01');
289+
expect(query['values'], ['2023-01-01']);
286290
expect(query['method'], 'updatedBefore');
287291
});
288292

289293
test('returns updatedAfter', () {
290294
final query = jsonDecode(Query.updatedAfter('2023-01-01'));
291295
expect(query['attribute'], null);
292-
expect(query['values'], '2023-01-01');
296+
expect(query['values'], ['2023-01-01']);
293297
expect(query['method'], 'updatedAfter');
294298
});
295299
}

templates/dart/test/services/service_test.dart.twig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
{% macro sub_schema(definitions, property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<<String, dynamic>>{% else %}<String, dynamic>{
2+
{% if definitions[property.sub_schema] %}{% for property in definitions[property.sub_schema].properties | filter(p => p.required) %}
3+
'{{property.name | escapeDollarSign}}': {% if property.type == 'object' %}{% if property.sub_schema and (property.sub_schema != 'prefs' and property.sub_schema != 'preferences') %}{{_self.sub_schema(spec.definitions, property)}}{% else %}<String, dynamic>{}{% endif %}{% elseif property.type == 'array' %}[]{% elseif property.type == 'string' %}'{{property.example | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property.example}}{% endif %},
4+
{% endfor %}{% endif %}}{% endif %}{% else %}{% if property.type == 'object' and property.additionalProperties %}Map<String, dynamic>{% else %}{{property | typeName}}{% endif %}{% endif %}{% endmacro %}
15
{% import 'flutter/base/utils.twig' as utils %}
26
{% if 'dart' in language.params.packageName %}
37
import 'package:test/test.dart';
@@ -6,6 +10,7 @@ import 'package:flutter_test/flutter_test.dart';
610
{% endif %}
711
import 'package:mockito/mockito.dart';
812
import 'package:{{ language.params.packageName }}/models.dart' as models;
13+
import 'package:{{ language.params.packageName }}/enums.dart' as enums;
914
import 'package:{{ language.params.packageName }}/src/enums.dart';
1015
import 'package:{{ language.params.packageName }}/src/response.dart';
1116
import 'dart:typed_data';
@@ -27,12 +32,7 @@ class MockClient extends Mock implements Client {
2732
}
2833

2934
@override
30-
Future webAuth(
31-
Uri? url,
32-
{
33-
String? callbackUrlScheme,
34-
}
35-
) async {
35+
Future<String?> webAuth(Uri url) async {
3636
return super.noSuchMethod(Invocation.method(#webAuth, [url]), returnValue: 'done');
3737
}
3838

@@ -69,7 +69,7 @@ void main() {
6969
{%~ if method.responseModel and method.responseModel != 'any' ~%}
7070
final Map<String, dynamic> data = {
7171
{%- for definition in spec.definitions ~%}{%~ if definition.name == method.responseModel -%}{%~ for property in definition.properties | filter((param) => param.required) ~%}
72-
'{{property.name | escapeDollarSign}}': {% if property.type == 'object' %}<String, dynamic>{}{% 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 -%}
72+
'{{property.name | escapeDollarSign}}': {% if property.type == 'object' %}{% if property.sub_schema and (property.sub_schema != 'prefs' and property.sub_schema != 'preferences') %}{{_self.sub_schema(spec.definitions, property)}}{% else %}<String, dynamic>{}{% endif %}{% 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 -%}
7373

7474
};
7575
{%~ else ~%}
@@ -96,7 +96,7 @@ void main() {
9696
{%~ endif ~%}
9797

9898
final response = await {{service.name | caseCamel}}.{{method.name | caseCamel}}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%}
99-
{{parameter.name | escapeKeyword | caseCamel}}: {% if parameter.type == 'object' %}{}{% elseif parameter.type == 'array' %}[]{% elseif parameter.type == 'file' %}InputFile.fromPath(path: './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 ~%}
99+
{{parameter.name | escapeKeyword | caseCamel}}: {% if parameter.enumValues | length > 0%}{{parameter | typeName}}.{{ (parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseEnumKey }}{% elseif parameter.type == 'object' %}{}{% elseif parameter.type == 'array' %}[]{% elseif parameter.type == 'file' %}InputFile.fromPath(path: './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 ~%}
100100
);
101101

102102
{%- if method.type == 'location' ~%}

templates/dart/test/src/models/model_test.dart.twig

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier }}{% endif %}{% else %}{% if property.type == 'object' and property.additionalProperties %}Map<String, dynamic>{% else %}{{property | typeName}}{% endif %}{% endif %}{% endmacro %}
1+
{% macro sub_schema(definitions, property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier }}(
2+
{% if definitions[property.sub_schema] %}{% for property in definitions[property.sub_schema].properties | filter(p => p.required) %}
3+
{{ property.name | escapeKeyword }}: {% if property.type == 'array' %}[]{% elseif property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}Preferences(data: {}){% elseif property.type == 'object' and property.sub_schema %}{{_self.sub_schema(definitions, property)}}{% elseif property.type == 'string' %}'{{property['x-example'] | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property['x-example']}}{% endif %},
4+
{% endfor %}{% endif %}){% endif %}{% else %}{% if property.type == 'object' and property.additionalProperties %}Map<String, dynamic>{% else %}{{property | typeName}}{% endif %}{% endif %}{% endmacro %}
25
import 'package:{{ language.params.packageName }}/models.dart';
36
{% if 'dart' in language.params.packageName %}
47
import 'package:test/test.dart';
@@ -11,7 +14,7 @@ void main() {
1114
test('model', () {
1215
final model = {{ definition.name | caseUcfirst | overrideIdentifier }}(
1316
{% for property in definition.properties | filter(p => p.required) %}
14-
{{ property.name | escapeKeyword }}: {% if property.type == 'array' %}[]{% elseif property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}Preferences(data: {}){% elseif property.type == 'object' %}{}{% elseif property.type == 'string' %}'{{property['x-example'] | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property['x-example']}}{% endif %},
17+
{{ property.name | escapeKeyword }}: {% if property.type == 'array' %}[]{% elseif property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}Preferences(data: {}){% elseif property.type == 'object' and property.sub_schema %}{{_self.sub_schema(spec.definitions, property)}}{% elseif property.type == 'object' %}{}{% elseif property.type == 'string' %}'{{property['x-example'] | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property['x-example']}}{% endif %},
1518
{% endfor %}
1619
{% if definition.additionalProperties %}
1720
data: {},
@@ -22,7 +25,9 @@ void main() {
2225
final result = {{ definition.name | caseUcfirst | overrideIdentifier }}.fromMap(map);
2326

2427
{% for property in definition.properties | filter(p => p.required) %}
28+
{% if property.type != 'object' or not property.sub_schema or (property.sub_schema == 'prefs' and property.sub_schema == 'preferences') %}
2529
expect(result.{{ property.name | escapeKeyword }}{% if property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}.data{% endif %}, {% if property.type == 'array' %}[]{% elseif property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}{"data": {}}{% elseif property.type == 'object' %}{}{% elseif property.type == 'string' %}'{{property['x-example'] | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property['x-example']}}{% endif %});
30+
{% endif %}
2631
{% endfor %}
2732
});
2833
});
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{% macro sub_schema(definitions, property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<<String, dynamic>>{% else %}<String, dynamic>{
2+
{% if definitions[property.sub_schema] %}{% for property in definitions[property.sub_schema].properties | filter(p => p.required) %}
3+
'{{property.name | escapeDollarSign}}': {% if property.type == 'object' %}{% if property.sub_schema and (property.sub_schema != 'prefs' and property.sub_schema != 'preferences') %}{{_self.sub_schema(spec.definitions, property)}}{% else %}<String, dynamic>{}{% endif %}{% elseif property.type == 'array' %}[]{% elseif property.type == 'string' %}'{{property.example | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property.example}}{% endif %},
4+
{% endfor %}{% endif %}}{% endif %}{% else %}{% if property.type == 'object' and property.additionalProperties %}Map<String, dynamic>{% else %}{{property | typeName}}{% endif %}{% endif %}{% endmacro %}
5+
{% import 'flutter/base/utils.twig' as utils %}
6+
{% if 'dart' in language.params.packageName %}
7+
import 'package:test/test.dart';
8+
{% else %}
9+
import 'package:flutter_test/flutter_test.dart';
10+
{% endif %}
11+
import 'package:mockito/mockito.dart';
12+
import 'package:{{ language.params.packageName }}/models.dart' as models;
13+
import 'package:{{ language.params.packageName }}/enums.dart' as enums;
14+
import 'package:{{ language.params.packageName }}/src/enums.dart';
15+
import 'package:{{ language.params.packageName }}/src/response.dart';
16+
import 'dart:typed_data';
17+
import 'package:{{ language.params.packageName }}/{{ language.params.packageName }}.dart';
18+
19+
class MockClient extends Mock implements Client {
20+
Map<String, String> config = {'project': 'testproject'};
21+
String endPoint = 'https://localhost/v1';
22+
@override
23+
Future<Response> call(
24+
HttpMethod? method, {
25+
String path = '',
26+
Map<String, String> headers = const {},
27+
Map<String, dynamic> params = const {},
28+
ResponseType? responseType,
29+
}) async {
30+
return super.noSuchMethod(Invocation.method(#call, [method]),
31+
returnValue: Response());
32+
}
33+
34+
@override
35+
Future webAuth(
36+
Uri? url,
37+
{
38+
String? callbackUrlScheme,
39+
}
40+
) async {
41+
return super.noSuchMethod(Invocation.method(#webAuth, [url]), returnValue: 'done');
42+
}
43+
44+
@override
45+
Future<Response> chunkedUpload({
46+
String? path,
47+
Map<String, dynamic>? params,
48+
String? paramName,
49+
String? idParamName,
50+
Map<String, String>? headers,
51+
Function(UploadProgress)? onProgress,
52+
}) async {
53+
return super.noSuchMethod(Invocation.method(#chunkedUpload, [path, params, paramName, idParamName, headers]), returnValue: Response(data: {}));
54+
}
55+
}
56+
57+
void main() {
58+
group('{{service.name | caseUcfirst}} test', () {
59+
late MockClient client;
60+
late {{service.name | caseUcfirst}} {{service.name | caseCamel}};
61+
62+
setUp(() {
63+
client = MockClient();
64+
{{service.name | caseCamel}} = {{service.name | caseUcfirst}}(client);
65+
});
66+
67+
{% for method in service.methods %}
68+
test('test method {{method.name | caseCamel}}()', () async {
69+
{%- if method.type == 'webAuth' -%}
70+
{%~ elseif method.type == 'location' -%}
71+
final Uint8List data = Uint8List.fromList([]);
72+
{%- else -%}
73+
74+
{%~ if method.responseModel and method.responseModel != 'any' ~%}
75+
final Map<String, dynamic> data = {
76+
{%- for definition in spec.definitions ~%}{%~ if definition.name == method.responseModel -%}{%~ for property in definition.properties | filter((param) => param.required) ~%}
77+
'{{property.name | escapeDollarSign}}': {% if property.type == 'object' %}{% if property.sub_schema and (property.sub_schema != 'prefs' and property.sub_schema != 'preferences') %}{{_self.sub_schema(spec.definitions, property)}}{% else %}<String, dynamic>{}{% endif %}{% 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 -%}
78+
79+
};
80+
{%~ else ~%}
81+
final data = '';
82+
{%- endif -%}
83+
{% endif %}
84+
85+
{%~ if method.type == 'webAuth' ~%}
86+
when(client.webAuth(
87+
Uri(),
88+
)).thenAnswer((_) async => 'done');
89+
{%~ elseif 'multipart/form-data' in method.consumes ~%}
90+
when(client.chunkedUpload(
91+
path: argThat(isNotNull),
92+
params: argThat(isNotNull),
93+
paramName: argThat(isNotNull),
94+
idParamName: argThat(isNotNull),
95+
headers: argThat(isNotNull),
96+
)).thenAnswer((_) async => Response(data: data));
97+
{%~ else ~%}
98+
when(client.call(
99+
HttpMethod.{{method.method | caseLower}},
100+
)).thenAnswer((_) async => Response(data: data));
101+
{%~ endif ~%}
102+
103+
final response = await {{service.name | caseCamel}}.{{method.name | caseCamel}}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%}
104+
{{parameter.name | escapeKeyword | caseCamel}}: {% if parameter.enumValues | length > 0%}{{ parameter | typeName }}.{{ (parameter.enumKeys[0] ?? parameter.enumValues[0]) | caseEnumKey }}{% elseif parameter.type == 'object' %}{}{% elseif parameter.type == 'array' %}[]{% elseif parameter.type == 'file' %}InputFile.fromPath(path: './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 ~%}
105+
);
106+
107+
{%- if method.type == 'location' ~%}
108+
expect(response, isA<Uint8List>());
109+
{%~ endif ~%}{%~ if method.responseModel and method.responseModel != 'any' ~%}
110+
expect(response, isA<models.{{method.responseModel | caseUcfirst | overrideIdentifier}}>());
111+
{%~ endif ~%}
112+
});
113+
114+
{% endfor %}
115+
});
116+
}

0 commit comments

Comments
 (0)