Test add destroyScope tests

This commit is contained in:
Joey Baker 2016-10-27 21:01:32 -07:00
parent 3b962ce4d8
commit 01081db7a3
3 changed files with 79 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import setup from '../test/integration-setup.js';
const STATUS_OK = 200; const STATUS_OK = 200;
const STATUS_NOT_FOUND = 404; const STATUS_NOT_FOUND = 404;
const STATUS_BAD_REQUEST = 400;
setup(test); setup(test);
@ -112,3 +113,65 @@ test('not found /notamodel', async (t) => {
const { statusCode } = await server.inject({ url, method }); const { statusCode } = await server.inject({ url, method });
t.is(statusCode, STATUS_NOT_FOUND); t.is(statusCode, STATUS_NOT_FOUND);
}); });
test('destroyScope /players/returnsOne', async (t) => {
const { server, instances, sequelize: { models: { Player } } } = t.context;
const { player1, player2 } = instances;
// this doesn't exist in our fixtures
const url = '/players/returnsOne';
const method = 'DELETE';
const presentPlayers = await Player.findAll();
const playerIds = presentPlayers.map(({ id }) => id);
t.truthy(playerIds.includes(player1.id));
t.truthy(playerIds.includes(player2.id));
const { result, statusCode } = await server.inject({ url, method });
t.is(statusCode, STATUS_OK);
t.is(result.id, player1.id);
const nonDeletedPlayers = await Player.findAll();
t.is(nonDeletedPlayers.length, presentPlayers.length - 1);
});
test('destroyScope /players/returnsNone', async (t) => {
const { server, instances, sequelize: { models: { Player } } } = t.context;
const { player1, player2 } = instances;
// this doesn't exist in our fixtures
const url = '/players/returnsNone';
const method = 'DELETE';
const presentPlayers = await Player.findAll();
const playerIds = presentPlayers.map(({ id }) => id);
t.truthy(playerIds.includes(player1.id));
t.truthy(playerIds.includes(player2.id));
const { statusCode } = await server.inject({ url, method });
t.is(statusCode, STATUS_NOT_FOUND);
const nonDeletedPlayers = await Player.findAll();
const nonDeletedPlayerIds = nonDeletedPlayers.map(({ id }) => id);
t.truthy(nonDeletedPlayerIds.includes(player1.id));
t.truthy(nonDeletedPlayerIds.includes(player2.id));
});
test('destroyScope invalid scope /players/invalid', async (t) => {
const { server, instances, sequelize: { models: { Player } } } = t.context;
const { player1, player2 } = instances;
// this doesn't exist in our fixtures
const url = '/players/invalid';
const method = 'DELETE';
const presentPlayers = await Player.findAll();
const playerIds = presentPlayers.map(({ id }) => id);
t.truthy(playerIds.includes(player1.id));
t.truthy(playerIds.includes(player2.id));
const { statusCode } = await server.inject({ url, method });
t.is(statusCode, STATUS_BAD_REQUEST);
const nonDeletedPlayers = await Player.findAll();
const nonDeletedPlayerIds = nonDeletedPlayers.map(({ id }) => id);
t.truthy(nonDeletedPlayerIds.includes(player1.id));
t.truthy(nonDeletedPlayerIds.includes(player2.id));
});

View File

@ -7,6 +7,7 @@ export default (sequelize, DataTypes) => {
}, },
name: DataTypes.STRING, name: DataTypes.STRING,
teamId: DataTypes.INTEGER, teamId: DataTypes.INTEGER,
active: DataTypes.BOOLEAN,
}, { }, {
classMethods: { classMethods: {
associate: (models) => { associate: (models) => {
@ -15,5 +16,17 @@ export default (sequelize, DataTypes) => {
}); });
}, },
}, },
scopes: {
returnsOne: {
where: {
active: true,
},
},
returnsNone: {
where: {
name: 'notaname',
},
},
},
}); });
}; };

View File

@ -58,7 +58,9 @@ export default (test) => {
const { Player, Team, City } = t.context.sequelize.models; const { Player, Team, City } = t.context.sequelize.models;
const city1 = await City.create({ name: 'Healdsburg' }); const city1 = await City.create({ name: 'Healdsburg' });
const team1 = await Team.create({ name: 'Baseballs', cityId: city1.id }); const team1 = await Team.create({ name: 'Baseballs', cityId: city1.id });
const player1 = await Player.create({ name: 'Pinot', teamId: team1.id }); const player1 = await Player.create({
name: 'Pinot', teamId: team1.id, active: true,
});
const player2 = await Player.create({ name: 'Syrah', teamId: team1.id }); const player2 = await Player.create({ name: 'Syrah', teamId: team1.id });
t.context.instances = { city1, team1, player1, player2 }; t.context.instances = { city1, team1, player1, player2 };
}); });