'use strict' const test = require('ava') const { stub } = require('sinon') const Hapi = require('@hapi/hapi') const plugin = require('../lib/plugins/profile') const Aspect = require('../lib/models/aspect') const AspectLabel = require('../lib/models/aspect_label') const Profile = require('../lib/models/profile') const MatchQueue = require('../lib/models/matchqueue') /** * Route parameters */ const params = { profile_id: 1, target_id: 2, reinsert: true, include_profile: false, } const mockReturn = { queue: [ { mocked_profile_id: 1, target_id: 2, is_deleted: false }, { mocked_profile_id: 2, target_id: 3, is_deleted: false }, { mocked_profile_id: 3, target_id: 1, is_deleted: false }, ], labels: [ { aspect_id: 1, a: 100, b: 100 }, { aspect_id: 2, a: 100, b: 200 }, { aspect_id: 3, a: 200, b: 200 }, { aspect_id: 4, a: 200, b: 100 }, ], aspects: [ { aspect_id: 1, 1: 111, 2: 112, 3: 113, 4: 114 }, { aspect_id: 2, 1: 222, 2: 222, 3: 333, 4: 222 }, { aspect_id: 3, 1: 111, 2: 112, 3: 113, 4: 114 }, { aspect_id: 4, 1: 111, 2: 111, 3: 111, 4: 111 }, ], } const pathToTest = { method: 'PATCH', url: `/${params.profile_id}/queue/${params.target_id}/delete?include_profile=${params.include_profile}&reinsert=${params.reinsert}`, } test('path //queue/ should return ok on PATCH', async t => { /** * Create a new server and register services, * models and routes for testing * - * NOTE: We use a mocked registerModel() and register * models manually. Normally this is handled by * Schwifty at runtime. */ const server = Hapi.server() /** * Overload so we don't register any models * using the plugin call (see plugins/profile.js) * and Manually load the model we need for the test */ server.registerModel = () => {} server.models = () => ({ Aspect, AspectLabel, MatchQueue, Profile }) server.registrations = { 'main-app-plugin': { options: {}, }, } /** * Register Routes and Services as usual */ await plugin.register(server) /** * Replace Objection model methods with our own mock functions * !: Janky - might be better to temp knex sqlite instance */ stub(server.models()['AspectLabel'], 'query').returns(mockReturn.labels) stub(server.models()['Aspect'], 'query').returns(mockReturn.aspects) stub(server.models()['MatchQueue'], 'query').returns({ insert: queueRecord => { t.is(queueRecord.profile_id, params.profile_id) t.is(queueRecord.target_id, params.target_id) t.is(queueRecord.is_deleted, !params.reinsert) }, // Mocked for getQueue() where: () => ({ where: (cmd, val) => { return mockReturn.queue }, // Mocked for markAsDeleted() andWhere: () => ({ patch: () => ({ first: () => ({ patch: 'bop', }), }), }), }), }) /** * Test the server with registered models and services */ const { payload } = await server.inject(pathToTest) const res = JSON.parse(payload) t.deepEqual(res.ok, true) t.deepEqual( res.data, mockReturn.queue.map(entry => entry.target_id), ) t.deepEqual(res.data[0], 2) t.deepEqual(res.data[1], 3) })