Skip to content

Commit

Permalink
fix: extend a false filter to “not true”
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambie committed Mar 21, 2019
1 parent f82f6db commit 0fd6894
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
14 changes: 14 additions & 0 deletions dadi/lib/fields/boolean.js
@@ -0,0 +1,14 @@
module.exports.type = 'boolean'

module.exports.beforeQuery = function ({config, field, input, options}) {
// If the query is "where field is false", modify it
// to "where field is not true" to ensure that records
// that don't have the specified field are also returned
if ((input[field]) !== true) {
return {
[field]: { '$ne': true }
}
}

return input
}
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -4,7 +4,6 @@
"main": "main.js",
"scripts": {
"create-client": "cd ../../.. && node ./node_modules/@dadi/api/utils/create-client.js",
"docs": "jsdoc -c ./docs/conf.json -R README.md -r dadi/lib -d docs",
"commitmsg": "commitlint -e",
"test:prepare": "rm -rf cache && rm -rf config/config.test.json && rm -rf test/acceptance/temp-workspace && cp -R test/acceptance/workspace test/acceptance/temp-workspace",
"test": "npm run test:prepare && standard --fix 'dadi/**/*.js' | snazzy && env NODE_ENV=test ./node_modules/.bin/istanbul cover --report cobertura --report text --report html --report lcov ./node_modules/mocha/bin/_mocha && npm run test:cleanup",
Expand Down
129 changes: 129 additions & 0 deletions test/acceptance/fields/boolean.js
@@ -0,0 +1,129 @@
const should = require('should')
const request = require('supertest')
const config = require(__dirname + '/../../../config')
const help = require(__dirname + '/../help')
const app = require(__dirname + '/../../../dadi/lib/')

let bearerToken
let configBackup = config.get()
let connectionString = 'http://' + config.get('server.host') + ':' + config.get('server.port')

describe('Boolean Field', () => {
beforeEach(done => {
config.set('paths.collections', 'test/acceptance/temp-workspace/collections')

help.dropDatabase('library', 'misc', err => {
app.start(() => {
help.getBearerToken(function (err, token) {
if (err) return done(err)
bearerToken = token
done()
})
})
})
})

afterEach(done => {
config.set('paths.collections', configBackup.paths.collections)
app.stop(done)
})

it('should create and retrieve', done => {
let client = request(connectionString)

client
.post('/v1/library/misc')
.set('Authorization', 'Bearer ' + bearerToken)
.send({boolean: true})
.expect(200)
.end((err, res) => {
if (err) return done(err)

res.body.results[0].boolean.should.eql(true)

client
.get(`/v1/library/misc/${res.body.results[0]._id}`)
.set('Authorization', 'Bearer ' + bearerToken)
.expect(200)
.end((err, res) => {
res.body.results[0].boolean.should.eql(true)

done()
})
})
})

it('should retrieve all documents where the field is truthy', done => {
let client = request(connectionString)
let docs = [
{
boolean: true
},
{
boolean: false
},
{
boolean: true
},
{
string: 'hello'
}
]

client
.post('/v1/library/misc')
.set('Authorization', 'Bearer ' + bearerToken)
.send(docs)
.expect(200)
.end((err, res) => {
if (err) return done(err)

client
.get(`/v1/library/misc?filter={"boolean":true}`)
.set('Authorization', 'Bearer ' + bearerToken)
.expect(200)
.end((err, res) => {
res.body.results.length.should.eql(2)

done()
})
})
})

it('should retrieve all documents where the field is falsy', done => {
let client = request(connectionString)
let docs = [
{
boolean: true
},
{
boolean: false
},
{
boolean: true
},
{
string: 'hello'
}
]

client
.post('/v1/library/misc')
.set('Authorization', 'Bearer ' + bearerToken)
.send(docs)
.expect(200)
.end((err, res) => {
if (err) return done(err)

client
.get(`/v1/library/misc?filter={"boolean":false}`)
.set('Authorization', 'Bearer ' + bearerToken)
.expect(200)
.end((err, res) => {
res.body.results.length.should.eql(2)

done()
})
})
})
})
@@ -1,5 +1,8 @@
{
"fields": {
"boolean": {
"type": "Boolean"
},
"string": {
"type": "String"
},
Expand Down

0 comments on commit 0fd6894

Please sign in to comment.