Add tests for API

This commit is contained in:
Andrew Z Allen
2016-02-02 00:07:53 -07:00
parent b9eb26baff
commit 42f669031d
12 changed files with 2435 additions and 878 deletions

View File

@@ -0,0 +1,98 @@
goog.require('API.Client.PetApi');
angular.module('PetApi', [])
.service('petApi', API.Client.PetApi)
.value('PetApiBasePath', 'https://example.com');
describe('API.Client.PetAPI', function() {
beforeEach(module('ng', 'ngMock', 'PetApi'));
/** @type {!Object} */
var $httpBackend;
/** @type {!API.Client.PetAPI} */
var api;
/** @type {!API.Client.Category} */
var sampleCategory = {
id: 345,
name: 'categoryname',
};
/** @type {!API.Client.Pet} */
var samplePet = {
id: 123,
category: sampleCategory,
name: 'petname',
photoUrls: [],
tags: [sampleTag],
status: API.Client.Pet.StatusEnum.available,
};
/** @type {!API.Client.Tag} */
var sampleTag = {
id: 345,
name: 'categoryname',
};
beforeEach(function() {
inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
api = $injector.get('petApi');
})
});
it('should update pets', function() {
$httpBackend.expectPUT('https://example.com/pet', samplePet)
.respond(200, '');
api.updatePet(samplePet);
$httpBackend.flush();
});
it('should add a pet', function() {
$httpBackend.expectPOST('https://example.com/pet', samplePet)
.respond(200, '');
api.addPet(samplePet);
$httpBackend.flush();
});
it('should find pets by status', function() {
$httpBackend.expectGET('https://example.com/pet/findByStatus?status=sold')
.respond(200, '');
api.findPetsByStatus(API.Client.Pet.StatusEnum.sold);
$httpBackend.flush();
});
it('should find a pet by tag', function() {
$httpBackend.expectGET('https://example.com/pet/findByTags?tags=%7B%22id%22:345,%22name%22:%22categoryname%22%7D')
.respond(200, '');
api.findPetsByTags(sampleTag);
$httpBackend.flush();
});
it('should get pet by id', function() {
$httpBackend.expectGET('https://example.com/pet/789')
.respond(200, '');
api.getPetById(789);
$httpBackend.flush();
});
it('should update pet with form', function() {
$httpBackend.expectPOST('https://example.com/pet/890', "name=newname&status=pending")
.respond(200, '');
api.updatePetWithForm(890, 'newname', API.Client.Pet.StatusEnum.pending);
$httpBackend.flush();
});
it('should delete a pet', function() {
$httpBackend.expectDELETE('https://example.com/pet/234')
.respond(200, '');
api.deletePet(234);
$httpBackend.flush();
});
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
});

View File

@@ -0,0 +1,68 @@
goog.require('API.Client.StoreApi');
angular.module('StoreApi', [])
.service('storeApi', API.Client.StoreApi)
.value('StoreApiBasePath', 'https://example.com');
describe('API.Client.StoreApi', function() {
beforeEach(module('ng', 'ngMock', 'StoreApi'));
/** @type {!Object} */
var $httpBackend;
/** @type {!API.Client.PetAPI} */
var api;
/** @type {!Date} */
fixedDate = new Date();
/** @type {!API.Client.Order} */
var sampleOrder = {
id: 123,
petId: 234,
quantity: 1,
shipDate: fixedDate,
status: API.Client.Order.StatusEnum.placed,
complete: false,
};
beforeEach(function() {
inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
api = $injector.get('storeApi');
})
});
it('should get the inventory', function() {
$httpBackend.expectGET('https://example.com/store/inventory')
.respond(200, 'ok');
api.getInventory();
$httpBackend.flush();
});
it('should place an order', function() {
$httpBackend.expectPOST('https://example.com/store/order', sampleOrder)
.respond(200, 'ok');
api.placeOrder(sampleOrder);
$httpBackend.flush();
});
it('should get an order by id', function() {
$httpBackend.expectGET('https://example.com/store/order/345')
.respond(200, 'ok');
api.getOrderById(345);
$httpBackend.flush();
});
it('should delete an order', function() {
$httpBackend.expectDELETE('https://example.com/store/order/456')
.respond(200, 'ok');
api.deleteOrder(456);
$httpBackend.flush();
});
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
});

View File

@@ -0,0 +1,81 @@
goog.require('API.Client.UserApi');
angular.module('UserApi', [])
.service('UserApi', API.Client.UserApi)
.value('UserApiBasePath', 'https://example.com');
describe('API.Client.PetAPI', function() {
beforeEach(module('ng', 'ngMock', 'UserApi'));
/** @type {!Object} */
var $httpBackend;
/** @type {!API.Client.UserApi} */
var api;
/** @type {!API.Client.User} */
var sampleUser = {
id: 123,
username: 'username',
firstName: 'first',
lastName: 'last',
email: 'email@example.com',
password: 'password',
userStatus: 0,
};
beforeEach(function() {
inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
api = $injector.get('UserApi');
})
});
it('should create a user', function() {
$httpBackend.expectPOST('https://example.com/user', sampleUser)
.respond(200, '');
api.createUser(sampleUser);
$httpBackend.flush();
});
it('should create an array of users', function() {
$httpBackend.expectPOST('https://example.com/user/createWithArray', [sampleUser])
.respond(200, '');
api.createUsersWithArrayInput([sampleUser]);
$httpBackend.flush();
});
it('should create a list of users', function() {
$httpBackend.expectPOST('https://example.com/user/createWithList', [sampleUser])
.respond(200, '');
api.createUsersWithListInput([sampleUser]);
$httpBackend.flush();
});
it('should login a user', function() {
$httpBackend.expectGET('https://example.com/user/login?password=password&username=username')
.respond(200, '');
api.loginUser('username', 'password');
$httpBackend.flush();
});
it('should logout a user', function() {
$httpBackend.expectGET('https://example.com/user/logout')
.respond(200, '');
api.logoutUser();
$httpBackend.flush();
});
it('should get a user by username', function() {
$httpBackend.expectGET('https://example.com/user/username')
.respond(200, '');
api.getUserByName('username');
$httpBackend.flush();
});
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
});