Dart fix template tests (#4015)

* Clean up samples directory before fixing tests

- removed from samples/client/petstore/dart2 :
  - purge_test.sh (doesn't seem to be used and not helpful)
  - openapi folder (is to be re-generated with more meaningful name)
- updated dart2-petstore.sh to generate client library with new name
- used updated shell script to re-generate client library
- updated CI/.drone.yml to use the new client library for tests

* Update petstore tests to use faked http client

- skipped all of the tests that hit a live endpoint
- made a fake http client that can be set to check for expected values
   and/or return a provided response
- added some files with test data recorded from live api calls
- updated the README to reflect changes to tests

* Update .drone.yml so CI will run the tests
This commit is contained in:
Nick Meinhold 2019-10-02 13:16:05 +10:00 committed by William Cheng
parent 8383f26616
commit 41acae19e4
57 changed files with 1018 additions and 17 deletions

View File

@ -13,7 +13,7 @@ steps:
commands:
- (cd samples/client/petstore/dart-jaguar/openapi && pub get && pub run build_runner build --delete-conflicting-outputs)
- (cd samples/client/petstore/dart-jaguar/flutter_petstore/openapi && pub get && pub run build_runner build --delete-conflicting-outputs)
- (cd samples/client/petstore/dart2/openapi && pub get && pub run test)
- (cd samples/client/petstore/dart2/petstore && pub get && pub run test)
# test Java 11 HTTP client
- name: java11-test
image: openjdk:11.0

View File

@ -29,5 +29,5 @@ fi
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
# Generate client
ags="generate -t modules/openapi-generator/src/main/resources/dart2 -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart2/openapi --additional-properties hideGenerationTimestamp=true $@"
ags="generate -t modules/openapi-generator/src/main/resources/dart2 -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart2/petstore_client_lib --additional-properties hideGenerationTimestamp=true $@"
java $JAVA_OPTS -jar $executable $ags

View File

@ -1,3 +1,29 @@
# Background
## Current state of tests
TL;DR currently the only tests are e2e tests that were adapted to use a faked http client. While pushing data around as a smoke test has some value, more testing is required. In particular we need comprehensive unit/integration tests.
- an old set of e2e tests are skipped for CI, as they hit a live endpoint and so are inherently flaky
- `pet_test.dart`
- `store_test.dart`
- `user_test.dart`
- the above set of tests were adapted to use a faked http client
- the tests are not really well suited to being used with a stubbed client, many are basically just testing the endpoint logic
- while not a great set of tests, they do have some value as a smoke test for template changes
- the adapted tests and files that contain test data:
- `pet_test_fake_client.dart`
- `store_test_fake_client.dart`
- `user_test_fake_client.dart`
- `fake_client.dart`
- `file_upload_response.json`
## Assumptions
- the tests will be run as part of CI and so have access to dart:io
# Running
## If not already done, resolve dependencies
`pub get`
@ -8,4 +34,4 @@
## To run all tests in the test folder:
`pub run test test`
`pub run test`

View File

@ -0,0 +1,355 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
analyzer:
dependency: transitive
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "0.38.4"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.2"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.2"
collection:
dependency: "direct dev"
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.12"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
csslib:
dependency: transitive
description:
name: csslib
url: "https://pub.dartlang.org"
source: hosted
version: "0.16.1"
front_end:
dependency: transitive
description:
name: front_end
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.26"
glob:
dependency: transitive
description:
name: glob
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.7"
html:
dependency: transitive
description:
name: html
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+2"
http:
dependency: "direct dev"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.0+2"
http_multi_server:
dependency: transitive
description:
name: http_multi_server
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
io:
dependency: transitive
description:
name: io
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.3"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.1+1"
kernel:
dependency: transitive
description:
name: kernel
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.26"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.5"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.7"
mime:
dependency: transitive
description:
name: mime
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.6+3"
mockito:
dependency: "direct dev"
description:
name: mockito
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.1"
multi_server_socket:
dependency: transitive
description:
name: multi_server_socket
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
node_preamble:
dependency: transitive
description:
name: node_preamble
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.8"
openapi:
dependency: "direct main"
description:
path: "../petstore_client_lib"
relative: true
source: path
version: "1.0.0"
package_config:
dependency: transitive
description:
name: package_config
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
package_resolver:
dependency: transitive
description:
name: package_resolver
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.10"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.4"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0+1"
pool:
dependency: transitive
description:
name: pool
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.0"
pub_semver:
dependency: transitive
description:
name: pub_semver
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.2"
shelf:
dependency: transitive
description:
name: shelf
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.5"
shelf_packages_handler:
dependency: transitive
description:
name: shelf_packages_handler
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
shelf_static:
dependency: transitive
description:
name: shelf_static
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.8"
shelf_web_socket:
dependency: transitive
description:
name: shelf_web_socket
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.3"
source_map_stack_trace:
dependency: transitive
description:
name: source_map_stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.5"
source_maps:
dependency: transitive
description:
name: source_maps
url: "https://pub.dartlang.org"
source: hosted
version: "0.10.8"
source_span:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.5"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.3"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
test:
dependency: "direct dev"
description:
name: test
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.8"
test_core:
dependency: transitive
description:
name: test_core
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.10"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
vm_service:
dependency: transitive
description:
name: vm_service
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
watcher:
dependency: transitive
description:
name: watcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.7+12"
web_socket_channel:
dependency: transitive
description:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.15"
yaml:
dependency: transitive
description:
name: yaml
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
sdks:
dart: ">=2.5.0 <3.0.0"

View File

@ -2,9 +2,12 @@ name: petstore_client
version: 1.0.0
description: Petstore client using OpenAPI library
environment:
sdk: '>=2.0.0 <3.0.0'
sdk: '>=2.5.0 <3.0.0'
dependencies:
openapi:
path: ../openapi
path: ../petstore_client_lib
dev_dependencies:
test: ^1.6.8
test: ^1.8.0
mockito: ^4.1.1
http: ^0.12.0
collection: ^1.14.12

View File

@ -0,0 +1,133 @@
import 'dart:convert';
import 'package:collection/collection.dart';
import 'package:http/http.dart';
import 'package:mockito/mockito.dart';
/// A fake client that checks for expected values and returns given responses
///
/// Checks for the expected values (url, headers, body) and throws if not found
///
/// If exception is non-null the request will throw the exception, after other
/// checks are performed
class FakeClient extends Fake implements Client {
FakeClient({
this.throwException,
this.expectedPostRequestBody,
this.postResponseBody,
this.expectedGetRequestBody,
this.getResponseBody,
this.deleteResponseBody,
this.expectedPutRequestBody,
this.putResponseBody,
this.sendResponseBody,
this.expectedUrl,
this.expectedHeaders = const {'Content-Type': 'application/json'},
});
Exception throwException;
Object expectedPostRequestBody;
String postResponseBody;
String expectedGetRequestBody;
String getResponseBody;
String deleteResponseBody;
String expectedPutRequestBody;
String putResponseBody;
String sendResponseBody;
String expectedUrl;
Map<String, String> expectedHeaders;
@override
Future<Response> post(url,
{Map<String, String> headers, body, Encoding encoding}) async {
// check that the request was made with expected values
if (url != expectedUrl) {
throw StateError(
'POST was called with unexpected url: ${url} should be ${expectedUrl}');
}
if (!MapEquality().equals(headers, expectedHeaders)) {
throw StateError(
'POST was called with unexpected headers: ${headers} should be ${expectedHeaders}');
}
// currently we only expect Map (and subtypes) or Strings
if (body is Map) {
if (!MapEquality().equals(body, expectedPostRequestBody)) {
throw StateError(
'POST was called with unexpected body: ${body} should be ${expectedPostRequestBody}');
}
} else if (body != expectedPostRequestBody) {
throw StateError(
'POST was called with unexpected body: ${body} should be ${expectedPostRequestBody}');
}
// throw if set to throw
if (throwException != null) throw throwException;
return Response(postResponseBody, 200);
}
@override
Future<Response> get(url, {Map<String, String> headers}) async {
// check that the request was made with expected values
if (url != expectedUrl) {
throw StateError(
'GET was called with unexpected url: ${url} should be ${expectedUrl}');
}
if (!MapEquality().equals(headers, expectedHeaders)) {
throw StateError(
'GET was called with unexpected headers: ${headers} should be ${expectedHeaders}');
}
// throw if set to throw
if (throwException != null) throw throwException;
return Response(getResponseBody, 200);
}
@override
Future<Response> delete(url, {Map<String, String> headers}) async {
// check that the request was made with expected values
if (url != expectedUrl) {
throw StateError(
'DELETE was called with unexpected url: ${url} should be ${expectedUrl}');
}
if (!MapEquality().equals(headers, expectedHeaders)) {
throw StateError(
'DELETE was called with unexpected headers: ${headers} should be ${expectedHeaders}');
}
// throw if set to throw
if (throwException != null) throw throwException;
return Response(deleteResponseBody, 200);
}
@override
Future<Response> put(url,
{Map<String, String> headers, body, Encoding encoding}) async {
// check that the request was made with expected values
if (url != expectedUrl) {
throw StateError(
'PUT was called with unexpected url: ${url} should be ${expectedUrl}');
}
if (!MapEquality().equals(headers, expectedHeaders)) {
throw StateError(
'PUT was called with unexpected headers: ${headers} should be ${expectedHeaders}');
}
if (body != expectedPutRequestBody) {
throw StateError(
'PUT was called with unexpected body: ${body} should be ${expectedPutRequestBody}');
}
// throw if set to throw
if (throwException != null) throw throwException;
return Response(putResponseBody, 200);
}
@override
Future<StreamedResponse> send(BaseRequest request) async {
List<int> bytes = utf8.encode(sendResponseBody);
return StreamedResponse(Stream.fromIterable([bytes]), 200);
}
}

View File

@ -0,0 +1 @@
{"code":200,"type":"unknown","message":"additionalMetadata: \nFile uploaded to ./null, 4 bytes"}

View File

@ -0,0 +1 @@
{"mine":1,"sold":18,"string":568,"Dead":2,"test":2,"Nonavailable":1,"custom":3,"pending":20,"available":2212,"notAvailable":26,"avaiflable":1,"AVAILABLE":1,"swimming":1,"availablee":2,"success":1,"105":1,"missing":11,"disabled":1,"Available":1,"]]>":1}

View File

@ -0,0 +1,231 @@
import 'dart:io';
import 'package:http/http.dart';
import 'package:openapi/api.dart';
import 'package:test/test.dart';
import 'fake_client.dart';
import 'random_id.dart';
void main() {
final petApi = PetApi();
Pet makePet({
int id = 1234,
String name = 'Fluffy',
String status = '',
}) {
final category = Category()
..id = 1234
..name = 'eyeColor';
final tags = [
Tag()
..id = 1234
..name = 'New York',
Tag()
..id = 124321
..name = 'Jose'
];
return Pet()
..id = id
..category = category
..tags = tags
..name = name
..status = status
..photoUrls = ['https://petstore.com/sample/photo1.jpg'];
}
/// Setup the fake client then call [petApi.addPet]
Future<dynamic> addPet(Pet pet) {
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet',
expectedPostRequestBody: petApi.apiClient.serialize(pet),
postResponseBody: '',
);
return petApi.addPet(pet);
}
group('Pet API with faked client', () {
test('adds a new pet and gets it by id', () async {
final id = newId();
final newPet = makePet(id: id);
// use the pet api to add a pet
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet',
expectedPostRequestBody: petApi.apiClient.serialize(newPet),
postResponseBody: '',
);
await petApi.addPet(newPet);
// retrieve the same pet by id
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet/$id',
getResponseBody: petApi.apiClient.serialize(newPet),
);
final retrievedPet = await petApi.getPetById(id);
// check that the retrieved id is as expected
expect(retrievedPet.id, equals(id));
});
test('doesn\'t get non-existing pet by id', () {
final id = newId();
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet/$id',
throwException: ApiException(400, 'not found'),
);
expect(
petApi.getPetById(id), throwsA(equals(TypeMatcher<ApiException>())));
});
test('deletes existing pet by id', () async {
final id = newId();
Pet newPet = makePet(id: id);
// add a new pet
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet',
expectedPostRequestBody: petApi.apiClient.serialize(newPet),
postResponseBody: '',
);
await petApi.addPet(newPet);
// delete the pet
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet/$id',
expectedHeaders: {
'Content-Type': 'application/json',
'api_key': 'special-key'
},
deleteResponseBody: '',
);
await petApi.deletePet(id, apiKey: 'special-key');
// check for the deleted pet
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet/$id',
throwException: ApiException(400, 'Not found'),
);
expect(
petApi.getPetById(id), throwsA(equals(TypeMatcher<ApiException>())));
});
test('updates pet with form', () async {
final id = newId();
final newPet = makePet(id: id, name: 'Snowy');
// add a new pet
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet',
expectedPostRequestBody: petApi.apiClient.serialize(newPet),
postResponseBody: '',
);
await petApi.addPet(newPet);
final multipartRequest = MultipartRequest(null, null);
multipartRequest.fields['name'] = 'Doge';
multipartRequest.fields['status'] = '';
// update with form
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet/$id',
expectedHeaders: {
'Content-Type': 'application/x-www-form-urlencoded',
},
expectedPostRequestBody: {'name': 'Doge', 'status': ''},
postResponseBody: '',
);
await petApi.updatePetWithForm(id, name: 'Doge', status: '');
// check update worked
newPet.name = 'Doge';
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet/$id',
getResponseBody: petApi.apiClient.serialize(newPet),
);
final pet = await petApi.getPetById(id);
expect(pet.name, equals('Doge'));
});
test('updates existing pet', () async {
final id = newId();
final name = 'Snowy';
final newPet = makePet(id: id);
final updatePet = makePet(id: id, name: name);
// add a new pet
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet',
expectedPostRequestBody: petApi.apiClient.serialize(newPet),
postResponseBody: '',
);
await petApi.addPet(newPet);
// update the same pet
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet',
expectedPutRequestBody: petApi.apiClient.serialize(updatePet),
putResponseBody: '',
);
await petApi.updatePet(updatePet);
// check update worked
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet/$id',
getResponseBody: petApi.apiClient.serialize(updatePet),
);
final pet = await petApi.getPetById(id);
expect(pet.name, equals(name));
});
test('finds pets by status', () async {
final id1 = newId();
final id2 = newId();
final id3 = newId();
final status = 'available';
final pet1 = makePet(id: id1, status: status);
final pet2 = makePet(id: id2, status: status);
final pet3 = makePet(id: id3, status: 'sold');
return Future.wait([addPet(pet1), addPet(pet2), addPet(pet3)])
.then((_) async {
// retrieve pets by status
petApi.apiClient.client = FakeClient(
expectedUrl:
'http://petstore.swagger.io/v2/pet/findByStatus?status=$status',
getResponseBody: petApi.apiClient.serialize([pet1, pet2]),
);
final pets = await petApi.findPetsByStatus([status]);
final petIds = pets.map((pet) => pet.id).toList();
expect(petIds, contains(id1));
expect(petIds, contains(id2));
expect(petIds, isNot(contains(id3)));
});
});
test('uploads a pet image', () async {
final id = newId();
final newPet = makePet(id: id);
// get some test data (recorded from live response)
final uploadResponse =
await File('test/file_upload_response.json').readAsString();
// add a new pet
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet',
expectedPostRequestBody: petApi.apiClient.serialize(newPet),
postResponseBody: '',
);
await petApi.addPet(newPet);
petApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/pet',
sendResponseBody: uploadResponse,
);
final file =
new MultipartFile.fromBytes('file', [104, 101, 108, 108, 111]);
await petApi.uploadFile(id, additionalMetadata: '', file: file);
});
});
}

View File

@ -7,7 +7,7 @@ import 'package:test/test.dart';
import 'random_id.dart';
void main() {
var petApi = new PetApi();
var petApi = PetApi();
Pet makePet({
int id = 1234,
@ -34,7 +34,7 @@ void main() {
..photoUrls = ['https://petstore.com/sample/photo1.jpg'];
}
group('Pet API ', () {
group('Pet API with live client', () {
test('adds a new pet and gets it by id', () async {
var id = newId();
await petApi.addPet(makePet(id: id));
@ -99,5 +99,5 @@ void main() {
var file = new MultipartFile.fromBytes('file', [104, 101, 108, 108, 111]);
await petApi.uploadFile(id, additionalMetadata: '', file: file);
});
});
}, skip: 'e2e tests for CI');
}

View File

@ -0,0 +1,85 @@
import 'dart:io';
import 'package:openapi/api.dart';
import 'package:test/test.dart';
import 'fake_client.dart';
import 'random_id.dart';
void main() {
var storeApi = new StoreApi();
Order makeOrder({int id}) {
return Order()
..id = id
..petId = 1234
..quantity = 1
..shipDate = DateTime.now()
..status
..complete = false;
}
group('Store API with faked client', () {
test('places an order and gets it by id', () async {
final id = newId();
final newOrder = makeOrder(id: id);
// use the store api to add an order
storeApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/store/order',
expectedPostRequestBody: storeApi.apiClient.serialize(newOrder),
postResponseBody: storeApi.apiClient.serialize(newOrder),
);
await storeApi.placeOrder(newOrder);
// retrieve the same order by id
storeApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/store/order/$id',
getResponseBody: storeApi.apiClient.serialize(newOrder),
);
final placedOrder = await storeApi.getOrderById(id);
expect(placedOrder.id, equals(id));
});
test('deletes an order', () async {
final id = newId();
final newOrder = makeOrder(id: id);
// use the store api to add an order
storeApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/store/order',
expectedPostRequestBody: storeApi.apiClient.serialize(newOrder),
postResponseBody: storeApi.apiClient.serialize(newOrder),
);
await storeApi.placeOrder(newOrder);
// delete the same order
storeApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/store/order/$id',
deleteResponseBody: '',
);
await storeApi.deleteOrder(id.toString());
// try and retrieve the order
storeApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/store/order/$id',
throwException: ApiException(400, 'Not found'),
);
expect(storeApi.getOrderById(id),
throwsA(equals(TypeMatcher<ApiException>())));
});
test('gets the store inventory', () async {
// get some test data (recorded from live response)
final inventoryResponse =
await File('test/inventory_response.json').readAsString();
// use the store api to get the inventory
storeApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/store/inventory',
getResponseBody: inventoryResponse,
);
Map<String, int> inventory = await storeApi.getInventory();
expect(inventory.length, isNot(equals(0)));
});
});
}

View File

@ -16,7 +16,7 @@ void main() {
..complete = false;
}
group('Store API ', () {
group('Store API with live client', () {
test('places an order and gets it by id', () async {
var id = newId();
@ -38,5 +38,5 @@ void main() {
Map<String, int> inventory = await storeApi.getInventory();
expect(inventory.length, isNot(equals(0)));
});
});
}); // , skip: 'e2e tests for CI'
}

View File

@ -0,0 +1,170 @@
import 'package:openapi/api.dart';
import 'package:test/test.dart';
import 'fake_client.dart';
import 'random_id.dart';
void main() {
var userApi = new UserApi();
User makeUser(
{int id, String userName = 'username', String password = 'password'}) {
return User()
..id = id
..username = userName
..firstName = 'firstname'
..lastName = 'lastname'
..email = 'email'
..password = password
..phone = 'phone'
..userStatus = 0;
}
group('User API with faked client', () {
test('creates a user', () async {
final id = newId();
final username = 'Mally45';
final newUser = makeUser(id: id, userName: username);
// use the user api to create a user
userApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/user',
expectedPostRequestBody: userApi.apiClient.serialize(newUser),
postResponseBody: userApi.apiClient.serialize(newUser),
);
await userApi.createUser(newUser);
// retrieve the same user
userApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/user/$username',
getResponseBody: userApi.apiClient.serialize(newUser),
);
var user = await userApi.getUserByName(username);
expect(user.id, equals(id));
});
test('creates users with list input', () async {
final firstId = newId();
final joe = 'Joe';
final sally = 'Sally';
final secondId = newId();
final users = [
makeUser(id: firstId, userName: joe),
makeUser(id: secondId, userName: sally),
];
// use the user api to create a list of users
userApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/user/createWithList',
expectedPostRequestBody: userApi.apiClient.serialize(users),
postResponseBody: userApi.apiClient.serialize(users),
);
await userApi.createUsersWithListInput(users);
// retrieve the users
userApi.apiClient.client = FakeClient(
expectedUrl:
'http://petstore.swagger.io/v2/user/${users.elementAt(0).username}',
getResponseBody: userApi.apiClient.serialize(
users.elementAt(0),
),
);
final firstUser = await userApi.getUserByName(joe);
userApi.apiClient.client = FakeClient(
expectedUrl:
'http://petstore.swagger.io/v2/user/${users.elementAt(1).username}',
getResponseBody: userApi.apiClient.serialize(
users.elementAt(1),
),
);
final secondUser = await userApi.getUserByName(sally);
expect(firstUser.id, equals(firstId));
expect(secondUser.id, equals(secondId));
});
test('updates a user', () async {
final username = 'Arkjam89';
final email = 'test@example.com';
final newUser = makeUser(id: newId(), userName: username);
// use the user api to create a user
userApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/user',
expectedPostRequestBody: userApi.apiClient.serialize(newUser),
postResponseBody: userApi.apiClient.serialize(newUser),
);
await userApi.createUser(newUser);
newUser.email = email;
// use the user api to update the user
userApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/user/${newUser.username}',
expectedPutRequestBody: userApi.apiClient.serialize(newUser),
putResponseBody: userApi.apiClient.serialize(newUser),
);
await userApi.updateUser(username, newUser);
// retrieve the same user
userApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/user/${newUser.username}',
getResponseBody: userApi.apiClient.serialize(newUser),
);
var foundUser = await userApi.getUserByName(username);
expect(foundUser.email, equals(email));
});
test('deletes a user', () async {
final username = 'Riddlem325';
final newUser = makeUser(id: newId(), userName: username);
// use the user api to create a user
userApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/user',
expectedPostRequestBody: userApi.apiClient.serialize(newUser),
postResponseBody: userApi.apiClient.serialize(newUser),
);
await userApi.createUser(newUser);
// delete the same user
userApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/user/${newUser.username}',
deleteResponseBody: '',
);
await userApi.deleteUser(username);
// try and retrieve the user
userApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/user/${newUser.username}',
throwException: ApiException(400, 'Not found'),
);
expect(userApi.getUserByName(username),
throwsA(TypeMatcher<ApiException>()));
});
test('logs a user in', () async {
final username = 'sgarad625';
final password = 'lokimoki1';
final newUser =
makeUser(id: newId(), userName: username, password: password);
// use the user api to create a user
userApi.apiClient.client = FakeClient(
expectedUrl: 'http://petstore.swagger.io/v2/user',
expectedPostRequestBody: userApi.apiClient.serialize(newUser),
postResponseBody: userApi.apiClient.serialize(newUser),
);
await userApi.createUser(newUser);
// use the user api to login
userApi.apiClient.client = FakeClient(
expectedUrl:
'http://petstore.swagger.io/v2/user/login?username=${newUser.username}&password=${newUser.password}',
getResponseBody: 'logged in user session:',
);
final result = await userApi.loginUser(username, password);
expect(result, contains('logged in user session:'));
});
});
}

View File

@ -19,7 +19,7 @@ void main() {
..userStatus = 0;
}
group('User API ', () {
group('User API with live client', () {
test('creates a user', () async {
var id = newId();
var username = 'Mally45';
@ -76,5 +76,5 @@ void main() {
var result = await userApi.loginUser(username, password);
expect(result, contains('logged in user session:'));
});
});
}, skip: 'e2e tests for CI');
}

View File

@ -1,4 +0,0 @@
#!/bin/sh
echo "purge test fils under 'test' folder"
rm -Rf openapi/test/