2014-05-13 16:46:56 -04:00
|
|
|
describe 'Local Mongo queries', ->
|
|
|
|
LocalMongo = require 'lib/LocalMongo'
|
|
|
|
|
|
|
|
beforeEach ->
|
2014-06-04 14:47:32 -04:00
|
|
|
@fixture1 =
|
2014-05-13 16:46:56 -04:00
|
|
|
'id': 'somestring'
|
|
|
|
'value': 9000
|
2014-05-14 08:47:44 -04:00
|
|
|
'levels': [3, 8, 21]
|
2014-05-13 16:46:56 -04:00
|
|
|
'worth': 6
|
|
|
|
'type': 'unicorn'
|
|
|
|
'likes': ['poptarts', 'popsicles', 'popcorn']
|
|
|
|
|
2014-06-04 14:47:32 -04:00
|
|
|
@fixture2 = this: is: so: 'deep'
|
2014-05-17 11:19:57 -04:00
|
|
|
|
2014-05-13 16:46:56 -04:00
|
|
|
it 'regular match of a property', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'gender': 'unicorn')).toBeFalsy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'type':'unicorn')).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'type':'zebra')).toBeFalsy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'type':'unicorn', 'id':'somestring')).toBeTruthy()
|
2014-05-13 16:46:56 -04:00
|
|
|
|
2014-05-14 08:47:44 -04:00
|
|
|
it 'array match of a property', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'likes':'poptarts')).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'likes':'walks on the beach')).toBeFalsy()
|
2014-05-13 16:46:56 -04:00
|
|
|
|
2014-05-17 11:19:57 -04:00
|
|
|
it 'nested match', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture2, 'this.is.so':'deep')).toBeTruthy()
|
2014-05-17 11:19:57 -04:00
|
|
|
|
2014-05-14 08:47:44 -04:00
|
|
|
it '$gt selector', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$gt': 8000)).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$gt': [8000, 10000])).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'levels': '$gt': [10, 20, 30])).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$gt': 9000)).toBeFalsy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': {'$gt': 8000}, 'worth': {'$gt': 5})).toBeTruthy()
|
2014-05-13 16:46:56 -04:00
|
|
|
|
2014-05-14 08:47:44 -04:00
|
|
|
it '$gte selector', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$gte': 9001)).toBeFalsy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$gte': 9000)).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$gte': [9000, 10000])).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'levels': '$gte': [21, 30])).toBeTruthy()
|
2014-05-13 16:46:56 -04:00
|
|
|
|
2014-05-14 08:47:44 -04:00
|
|
|
it '$lt selector', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$lt': 9001)).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$lt': 9000)).toBeFalsy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$lt': [9001, 9000])).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'levels': '$lt': [10, 20, 30])).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': {'$lt': 9001}, 'worth': {'$lt': 7})).toBeTruthy()
|
2014-05-13 16:46:56 -04:00
|
|
|
|
2014-05-14 08:47:44 -04:00
|
|
|
it '$lte selector', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$lte': 9000)).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$lte': 8000)).toBeFalsy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': {'$lte': 9000}, 'worth': {'$lte': [6, 5]})).toBeTruthy()
|
2014-05-13 16:46:56 -04:00
|
|
|
|
2014-05-14 08:47:44 -04:00
|
|
|
it '$ne selector', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'value': '$ne': 9000)).toBeFalsy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'id': '$ne': 'otherstring')).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'id': '$ne': ['otherstring', 'somestring'])).toBeFalsy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'likes': '$ne': ['popcorn', 'chicken'])).toBeFalsy()
|
2014-05-14 08:47:44 -04:00
|
|
|
|
|
|
|
it '$in selector', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'type': '$in': ['unicorn', 'zebra'])).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'type': '$in': ['cats', 'dogs'])).toBeFalsy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'likes': '$in': ['popcorn', 'chicken'])).toBeTruthy()
|
2014-05-13 16:46:56 -04:00
|
|
|
|
2014-05-14 08:47:44 -04:00
|
|
|
it '$nin selector', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'type': '$nin': ['unicorn', 'zebra'])).toBeFalsy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'type': '$nin': ['cats', 'dogs'])).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, 'likes': '$nin': ['popcorn', 'chicken'])).toBeFalsy()
|
2014-05-13 16:46:56 -04:00
|
|
|
|
2014-05-14 11:12:33 -04:00
|
|
|
it '$or operator', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, $or: [{value:9000}, {type:'zebra'}])).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, $or: [{value:9001}, {worth:$lt:10}])).toBeTruthy()
|
2014-05-14 11:12:33 -04:00
|
|
|
|
|
|
|
it '$and operator', ->
|
2014-06-04 14:47:32 -04:00
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, $and: [{value:9000}, {type:'zebra'}])).toBeFalsy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, $and: [{value:9000}, {type:'unicorn'}])).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, $and: [{value:$gte:9000}, {worth:$lt:10}])).toBeTruthy()
|
|
|
|
|
|
|
|
it '$exists operator', ->
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, type: $exists: true)).toBeTruthy()
|
|
|
|
expect(LocalMongo.matchesQuery(@fixture1, interesting: $exists: false)).toBeTruthy()
|
2014-05-17 11:19:57 -04:00
|
|
|
|