Skip to content

Commit

Permalink
add Log controller test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
SillyFreak committed Jul 28, 2020
1 parent b9e2b29 commit 193b5c0
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 12 deletions.
18 changes: 7 additions & 11 deletions server/controller/log.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,23 @@ export async function create({ user_id, savelocation }) {
}
}

export async function readAll({}) {
// create filter
var condition = name ? {} : null;

export async function readAll() {
// query database
try {
const log = await Log.findAll({ where: condition });
const log = await Log.findAll();
return log;
} catch (err) {
throw new httpErrors[500](err.message || 'An error occurred...');
}
}

export async function readByUserID({ user_id }) {
// create filter
var condition = user_id ? { user_id: { [Op.like]: `%${user_id}%` } } : null;

export async function readAllByUserId(user_id) {
// query database
try {
const log = await Log.findAll({ where: condition });
return log;
const logs = await Log.findAll({
where: { user_id },
});
return logs;
} catch (err) {
throw new httpErrors[500](err.message || 'An error occurred...');
}
Expand Down
111 changes: 111 additions & 0 deletions test/controller/log.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { initSequelize } from '~/models';
import { Plugin, User, Log } from '~/server/controller';

let plugin_uuid, user_id, user_uuid;

beforeAll(async () => {
await initSequelize();

const plugin = await Plugin.create({
name: 'log_test_plugin',
type: 'log_test_plugin',
config: { foo: 0 },
});

plugin_uuid = plugin.uuid;

const user = await User.create({
plugin_uuid,
native_id: 'log_test_user',
});

user_id = user.id;
user_uuid = user.uuid;
});

afterAll(async () => {
await User.del(user_id);
await Plugin.del(plugin_uuid);
});

describe('Log Controller', () => {
test('it works', async () => {
let id;

// create
{
const log = await Log.create({
user_id,
savelocation: 'foo',
});
// toMatchObject because sequelize model instances are not plain objects
expect(log).toMatchObject({
id: expect.any(Number),
user_id,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
savelocation: 'foo',
});

id = log.id;
}

// read all
{
const logs = await Log.readAll();
// toMatchObject because sequelize model instances are not plain objects
expect(logs).toMatchObject([
{
id: expect.any(Number),
user_id,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
savelocation: 'foo',
},
]);
}

// read all logs for one user ID
{
const logs = await Log.readAllByUserId(user_id);
// toMatchObject because sequelize model instances are not plain objects
expect(logs).toMatchObject([
{
id: expect.any(Number),
user_id,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
savelocation: 'foo',
},
]);
}

// update
{
await Log.update(id, {
user_id,
savelocation: 'bar',
});
}

// read update
{
const log = await Log.read(id);
// toMatchObject because sequelize model instances are not plain objects
expect(log).toMatchObject({
id: expect.any(Number),
user_id,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
savelocation: 'bar',
});
}

// delete
{
await Log.del(id);

expect(await Log.readAll()).toHaveLength(0);
}
});
});
4 changes: 3 additions & 1 deletion test/controller/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ describe('User Controller', () => {
await User.del(id1);
await User.delByUuid(uuid2, plugin_uuid);

expect(await User.readAll()).toHaveLength(0);
const users = await User.readAll();
// manually filter for only those using the test plugin
expect(users.filter(user => user.plugin_uuid === plugin_uuid)).toHaveLength(0);
}
});
});

0 comments on commit 193b5c0

Please sign in to comment.