You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
286 lines
8.9 KiB
286 lines
8.9 KiB
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'; |
|
import { sha512 } from 'js-sha512'; |
|
|
|
async function init() { |
|
chai.use(chaiHttp); |
|
const app = express(); |
|
const knex = await helpers.initTestDB(); |
|
|
|
// Add test users. |
|
await knex.insert({ email: "test1@test.com", passwordHash: sha512('pass1') }).into('users'); |
|
await knex.insert({ email: "test2@test.com", passwordHash: sha512('pass2') }).into('users'); |
|
|
|
SetupApp(app, knex, ''); |
|
|
|
// Login as a test user. |
|
var agent = chai.request.agent(app); |
|
await agent |
|
.post('/login?username=' + encodeURIComponent("test1@test.com") + '&password=' + encodeURIComponent('pass1')) |
|
.send({}); |
|
return agent; |
|
} |
|
|
|
describe('POST /query with no songs', () => { |
|
it('should give empty list', async done => { |
|
let agent = await init(); |
|
try { |
|
let res = await agent |
|
.post('/query') |
|
.send({ |
|
'query': {}, |
|
'offsetsLimits': { |
|
'songOffset': 0, |
|
'songLimit': 10, |
|
}, |
|
'ordering': { |
|
'orderBy': { |
|
'type': 0, |
|
}, |
|
'ascending': true |
|
} |
|
}) |
|
expect(res).to.have.status(200); |
|
expect(res.body).to.deep.equal({ |
|
songs: [], |
|
tags: [], |
|
artists: [], |
|
albums: [], |
|
}); |
|
} finally { |
|
agent.close(); |
|
done(); |
|
} |
|
}); |
|
}); |
|
|
|
describe('POST /query with several songs and filters', () => { |
|
it('should give all correct results', async done => { |
|
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: [], |
|
albums: [], |
|
}); |
|
}); |
|
} |
|
|
|
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: [], |
|
albums: [], |
|
}); |
|
}); |
|
} |
|
|
|
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: [], |
|
albums: [], |
|
}); |
|
}); |
|
} |
|
|
|
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: [], |
|
albums: [], |
|
}); |
|
}); |
|
} |
|
|
|
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: [], |
|
albums: [], |
|
}); |
|
}); |
|
} |
|
|
|
let agent = await init(); |
|
let req = agent.keepOpen(); |
|
try { |
|
await helpers.createArtist(req, { name: "Artist1" }, 200); |
|
await helpers.createArtist(req, { name: "Artist2" }, 200); |
|
await helpers.createSong(req, { title: "Song1", artistIds: [1] }, 200); |
|
await helpers.createSong(req, { title: "Song2", artistIds: [1] }, 200); |
|
await helpers.createSong(req, { title: "Song3", artistIds: [2] }, 200); |
|
await checkAllSongs(req); |
|
await checkIdIn(req); |
|
await checkIdNotIn(req); |
|
await checkArtistIdIn(req); |
|
await checkOrRelation(req); |
|
} finally { |
|
req.close(); |
|
agent.close(); |
|
done(); |
|
} |
|
}); |
|
}); |