const chai = require('chai'); const chaiHttp = require('chai-http'); const express = require('express'); 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 helpers.initTestDB());; return app; } describe('POST /query with no songs', () => { it('should give empty list', done => { init().then((app) => { chai .request(app) .post('/query') .send({ 'query': {}, 'offsetsLimits': { 'songOffset': 0, 'songLimit': 10, }, 'ordering': { 'orderBy': { 'type': 0, }, 'ascending': true } }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [], tags: [], artists: [], }); done(); }); }) }); }); describe('POST /query with several songs and filters', () => { it('should give all correct results', done => { init().then((app) => { const song1 = { songId: 1, title: 'Song1', storeLinks: [], artists: [ { artistId: 1, name: 'Artist1', storeLinks: [], } ], tags: [], albums: [] }; const song2 = { songId: 2, title: 'Song2', storeLinks: [], artists: [ { artistId: 1, name: 'Artist1', storeLinks: [], } ], tags: [], albums: [] }; const song3 = { songId: 3, title: 'Song3', storeLinks: [], artists: [ { artistId: 2, name: 'Artist2', storeLinks: [], } ], tags: [], albums: [] }; async function checkAllSongs(req) { await req .post('/query') .send({ "query": {}, 'offsetsLimits': { 'songOffset': 0, 'songLimit': 10, }, 'ordering': { 'orderBy': { 'type': 0, }, 'ascending': true } }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [ song1, song2, song3 ], artists: [], tags: [] }); }); } async function checkIdIn(req) { await req .post('/query') .send({ "query": { "prop": "songId", "propOperator": "IN", "propOperand": [1, 3, 5] }, 'offsetsLimits': { 'songOffset': 0, 'songLimit': 10, }, 'ordering': { 'orderBy': { 'type': 0, }, 'ascending': true } }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [ song1, song3 ], artists: [], tags: [] }); }); } async function checkIdNotIn(req) { await req .post('/query') .send({ "query": { "prop": "songId", "propOperator": "NOTIN", "propOperand": [1, 3, 5] }, 'offsetsLimits': { 'songOffset': 0, 'songLimit': 10, }, 'ordering': { 'orderBy': { 'type': 0, }, 'ascending': true } }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [ song2 ], artists: [], tags: [] }); }); } async function checkArtistIdIn(req) { await req .post('/query') .send({ "query": { "prop": "artistId", "propOperator": "IN", "propOperand": [1] }, 'offsetsLimits': { 'songOffset': 0, 'songLimit': 10, }, 'ordering': { 'orderBy': { 'type': 0, }, 'ascending': true } }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [ song1, song2 ], artists: [], tags: [] }); }); } async function checkOrRelation(req) { await req .post('/query') .send({ "query": { "childrenOperator": "OR", "children": [ { "prop": "artistId", "propOperator": "IN", "propOperand": [2] }, { "prop": "songId", "propOperator": "EQ", "propOperand": 1 } ] }, 'offsetsLimits': { 'songOffset': 0, 'songLimit': 10, }, 'ordering': { 'orderBy': { 'type': 0, }, 'ascending': true } }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [ song1, song3 ], artists: [], tags: [] }); }); } 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) }) }); });