const chai = require('chai'); const chaiHttp = require('chai-http'); const express = require('express'); const models = require('../../../models'); import { SetupApp } from '../../../app'; import { expect } from 'chai'; import * as helpers from './helpers'; async function init() { chai.use(chaiHttp); const app = express(); SetupApp(app); await models.sequelize.sync({ force: true }); return app; } describe('POST /song/query with no songs', () => { it('should give empty list', done => { init().then((app) => { chai .request(app) .post('/song/query') .send({ 'query': {} }) .end((err, res) => { expect(err).to.be.null; expect(res).to.have.status(200); expect(res.body).to.deep.equal({ ids: [] }); }); }) .then(done); }); }); describe('POST /song/query with several songs and filters', () => { it('should give all correct results', done => { init().then((app) => { async function checkAllSongs(req) { await req .post('/song/query') .send({ "query": {} }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ ids: [1, 2, 3] }); }); } async function checkIdIn(req) { await req .post('/song/query') .send({ "query": { "prop": "id", "propOperator": "IN", "propOperand": [1, 3, 5] } }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ ids: [1, 3] }); }); } async function checkIdNotIn(req) { await req .post('/song/query') .send({ "query": { "prop": "id", "propOperator": "NOTIN", "propOperand": [1, 3, 5] } }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ ids: [2] }); }); } async function checkArtistIdIn(req) { await req .post('/song/query') .send({ "query": { "prop": "artistIds", "propOperator": "IN", "propOperand": [1] } }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ ids: [1, 2] }); }); } async function checkOrRelation(req) { await req .post('/song/query') .send({ "query": { "childrenOperator": "OR", "children": [ { "prop": "artistIds", "propOperator": "IN", "propOperand": [2] }, { "prop": "id", "propOperator": "EQ", "propOperand": 1 } ] } }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ ids: [1, 3] }); }); } var req = chai.request(app).keepOpen(); helpers.createArtist(req, { name: "Artist1" }, 200) .then(() => helpers.createArtist(req, { name: "Artist2" }, 200)) .then(() => helpers.createSong(req, { title: "Song1", artistIds: [1] }, 200)) .then(() => helpers.createSong(req, { title: "Song2", artistIds: [1] }, 200)) .then(() => helpers.createSong(req, { title: "Song3", artistIds: [2] }, 200)) .then(() => checkAllSongs(req)) .then(() => checkIdIn(req)) .then(() => checkIdNotIn(req)) .then(() => checkArtistIdIn(req)) .then(() => checkOrRelation(req)) .then(req.close) .then(done) }) }); });