Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: extend a false filter to “not true”
- Loading branch information
Showing
4 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) | ||
}) | ||
}) | ||
}) |
3 changes: 3 additions & 0 deletions
3
test/acceptance/workspace/collections/v1/library/collection.misc.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"fields": { | ||
"boolean": { | ||
"type": "Boolean" | ||
}, | ||
"string": { | ||
"type": "String" | ||
}, | ||
|