[Dart 2] Fix petstore tests (#954)

* properly initialized models in tests

* Revert "move dart2 test to the end"

This reverts commit 7d2edf4713c5c3bfb2d1698c4fd110e5b2a07bd9.
This commit is contained in:
Yimin Lin 2018-09-02 15:43:23 +08:00 committed by William Cheng
parent d4c0de2876
commit 8ccb7135d3
4 changed files with 75 additions and 46 deletions

View File

@ -1005,6 +1005,7 @@
</activation>
<modules>
<!-- clients -->
<module>samples/client/petstore/dart2/petstore</module>
<module>samples/client/petstore/haskell-http-client</module>
<module>samples/client/petstore/elm</module>
<module>samples/client/petstore/groovy</module>
@ -1036,7 +1037,6 @@
<!--<module>samples/client/petstore/bash</module>-->
<module>samples/client/petstore/ruby</module>
<module>samples/server/petstore/rust-server</module>
<module>samples/client/petstore/dart2/petstore</module>
</modules>
</profile>
<profile>

View File

@ -9,31 +9,56 @@ import 'random_id.dart';
void main() {
var petApi = new 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'];
}
group('Pet API ', () {
test('adds a new pet and gets it by id', () async {
var id = newId();
await petApi.addPet(new Pet()..id = id);
await petApi.addPet(makePet(id: id));
var pet = await petApi.getPetById(id);
expect(pet.id, equals(id));
});
test('doesn\'t get non-existing pet by id', () {
expect(petApi.getPetById(newId()), throwsA(equals(TypeMatcher<ApiException>())));
expect(petApi.getPetById(newId()),
throwsA(equals(TypeMatcher<ApiException>())));
});
test('deletes existing pet by id', () async {
var id = newId();
await petApi.addPet(new Pet()..id = id);
await petApi.addPet(makePet(id: id));
await petApi.deletePet(id, apiKey: 'special-key');
expect(petApi.getPetById(id), throwsA(equals(TypeMatcher<ApiException>())));
expect(
petApi.getPetById(id), throwsA(equals(TypeMatcher<ApiException>())));
});
test('updates pet with form', () async {
var id = newId();
await petApi.addPet(new Pet()
..id = id
..name = 'Snowy');
await petApi.addPet(makePet(id: id, name: 'Snowy'));
await petApi.updatePetWithForm(id, name: 'Doge', status: '');
var pet = await petApi.getPetById(id);
expect(pet.name, equals('Doge'));
@ -43,10 +68,8 @@ void main() {
var id = newId();
var name = 'Snowy';
await petApi.addPet(new Pet()..id = id);
await petApi.updatePet(new Pet()
..id = id
..name = name);
await petApi.addPet(makePet(id: id));
await petApi.updatePet(makePet(id: id, name: name));
var pet = await petApi.getPetById(id);
expect(pet.name, equals(name));
});
@ -58,15 +81,9 @@ void main() {
var status = 'available';
return Future.wait([
petApi.addPet(new Pet()
..id = id1
..status = status),
petApi.addPet(new Pet()
..id = id2
..status = status),
petApi.addPet(new Pet()
..id = id3
..status = 'sold')
petApi.addPet(makePet(id: id1, status: status)),
petApi.addPet(makePet(id: id2, status: status)),
petApi.addPet(makePet(id: id3, status: 'sold'))
]).then((_) async {
var pets = await petApi.findPetsByStatus([status]);
var petIds = pets.map((pet) => pet.id).toList();
@ -78,7 +95,7 @@ void main() {
test('uploads a pet image', () async {
var id = newId();
await petApi.addPet(new Pet()..id = id);
await petApi.addPet(makePet(id: id));
var file = new MultipartFile.fromBytes('file', [104, 101, 108, 108, 111]);
await petApi.uploadFile(id, additionalMetadata: '', file: file);
});

View File

@ -6,11 +6,21 @@ 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 ', () {
test('places an order and gets it by id', () async {
var id = newId();
await storeApi.placeOrder(new Order()..id = id);
await storeApi.placeOrder(makeOrder(id: id));
var order = await storeApi.getOrderById(id);
expect(order.id, equals(id));
});
@ -18,9 +28,10 @@ void main() {
test('deletes an order', () async {
var id = newId();
await storeApi.placeOrder(new Order()..id = id);
await storeApi.placeOrder(makeOrder(id: id));
await storeApi.deleteOrder(id.toString());
expect(storeApi.getOrderById(id), throwsA(equals(TypeMatcher<ApiException>())));
expect(storeApi.getOrderById(id),
throwsA(equals(TypeMatcher<ApiException>())));
});
test('gets the store inventory', () async {

View File

@ -6,13 +6,24 @@ 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 ', () {
test('creates a user', () async {
var id = newId();
var username = 'Mally45';
await userApi.createUser(new User()
..id = id
..username = username);
await userApi.createUser(makeUser(id: id, userName: username));
var user = await userApi.getUserByName(username);
expect(user.id, equals(id));
});
@ -25,12 +36,8 @@ void main() {
var secondId = newId();
var users = [
new User()
..id = firstId
..username = joe,
new User()
..id = secondId
..username = sally
makeUser(id: firstId, userName: joe),
makeUser(id: secondId, userName: sally),
];
await userApi.createUsersWithListInput(users);
@ -43,9 +50,7 @@ void main() {
test('updates a user', () async {
var username = 'Arkjam89';
var email = 'test@example.com';
var user = new User()
..id = newId()
..username = username;
var user = makeUser(id: newId(), userName: username);
await userApi.createUser(user);
user.email = email;
@ -56,20 +61,16 @@ void main() {
test('deletes a user', () async {
var username = 'Riddlem325';
await userApi.createUser(new User()
..id = newId()
..username = username);
await userApi.createUser(makeUser(id: newId(), userName: username));
await userApi.deleteUser(username);
expect(userApi.getUserByName(username), throwsA(TypeMatcher<ApiException>()));
expect(userApi.getUserByName(username),
throwsA(TypeMatcher<ApiException>()));
});
test('logs a user in', () async {
var username = 'sgarad625';
var password = 'lokimoki1';
var user = new User()
..id = newId()
..username = username
..password = password;
var user = makeUser(id: newId(), userName: username, password: password);
await userApi.createUser(user);
var result = await userApi.loginUser(username, password);