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 with no title', () => { it('should fail', done => { init().then((app) => { chai .request(app) .post('/song') .send({}) .then((res) => { expect(res).to.have.status(400); done(); }); }) }); }); describe('POST /song with only a title', () => { it('should return the first available id', done => { init().then(async(app) => { chai .request(app) .post('/song') .send({ title: "MySong" }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ id: 1 }); done(); }); }) }); }); describe('POST /song with a nonexistent artist Id', () => { it('should fail', done => { init().then(async (app) => { chai .request(app) .post('/song') .send({ title: "MySong", artistIds: [1] }) .then((res) => { expect(res).to.have.status(400); done(); }); }) }); }); describe('POST /song with an existing artist Id', () => { it('should succeed', done => { init().then((app) => { var req = chai.request(app).keepOpen(); helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 }) .then(() => helpers.createSong(req, { title: "MySong", artistIds: [ 1 ] }, 200, { id: 1 }) ) .then(req.close) .then(done); }); }); }); describe('POST /song with two existing artist Ids', () => { it('should succeed', done => { init().then((app) => { var req = chai.request(app).keepOpen(); helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) .then(() => helpers.createArtist(req, { name: "Artist2" }, 200, { id: 2 }) ) .then(() => helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 200, { id: 1 }) ) .then(req.close) .then(done); }); }); }); describe('POST /song with an existent and a nonexistent artist Id', () => { it('should fail', done => { init().then((app) => { var req = chai.request(app).keepOpen(); helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) .then(() => helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 400) ) .then(req.close) .then(done); }); }); }); describe('POST /song/query with no songs', () => { it('should give empty list', done => { init().then((app) => { chai .request(app) .post('/song/query') .send({ 'query': {} }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ ids: [] }); 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) }) }); });