mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-03-14 07:00:01 -04:00
Finished regular query selectors of Local Mongo
This commit is contained in:
parent
20af7b090a
commit
b3bddbd785
2 changed files with 39 additions and 28 deletions
|
@ -1,16 +1,23 @@
|
|||
LocalMongo = module.exports
|
||||
|
||||
# Checks whether func(l, r) is true for at least one value of left for at least one value of right
|
||||
mapred = (left, right, func) ->
|
||||
_.reduce(left, ((result, singleLeft) ->
|
||||
result or (_.reduce (_.map right, (singleRight) -> func(singleLeft, singleRight)),
|
||||
((intermediate, value) -> intermediate or value), false)), false)
|
||||
|
||||
doQuerySelector = (value, operatorObj) ->
|
||||
value = [value] unless _.isArray value # mongo works on arrays too!
|
||||
value = [value] unless _.isArray value # left hand can be an array too
|
||||
for operator, body of operatorObj
|
||||
body = [body] unless _.isArray body # right hand can be an array too
|
||||
switch operator
|
||||
when '$gt' then return false unless _.reduce value, ((result, val) -> result or val > body), false
|
||||
when '$gte' then return false unless _.reduce value, ((result, val) -> result or val >= body), false
|
||||
when '$gt' then return false unless mapred value, body, (l, r) -> l > r
|
||||
when '$gte' then return false unless mapred value, body, (l, r) -> l >= r
|
||||
when '$lt' then return false unless mapred value, body, (l, r) -> l < r
|
||||
when '$lte' then return false unless mapred value, body, (l, r) -> l <= r
|
||||
when '$ne' then return false if mapred value, body, (l, r) -> l == r
|
||||
when '$in' then return false unless _.reduce value, ((result, val) -> result or val in body), false
|
||||
when '$lt' then return false unless _.reduce value, ((result, val) -> result or val < body), false
|
||||
when '$lte' then return false unless _.reduce value, ((result, val) -> result or val <= body), false
|
||||
when '$ne' then return false unless _.reduce value, ((result, val) -> result or val != body), false
|
||||
when '$nin' then return false if _.reduce value, ((result, val) -> result or val of body), false
|
||||
when '$nin' then return false if _.reduce value, ((result, val) -> result or val in body), false
|
||||
true
|
||||
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
describe 'Local Mongo queries', ->
|
||||
LocalMongo = require 'lib/LocalMongo'
|
||||
console.warn 'matchesQuery' in LocalMongo
|
||||
|
||||
beforeEach ->
|
||||
this.fixture1 =
|
||||
'id': 'somestring'
|
||||
'value': 9000
|
||||
'levels': [3, 8, 21]
|
||||
'worth': 6
|
||||
'type': 'unicorn'
|
||||
'likes': ['poptarts', 'popsicles', 'popcorn']
|
||||
|
@ -16,42 +16,46 @@ describe 'Local Mongo queries', ->
|
|||
expect(LocalMongo.matchesQuery(this.fixture1, 'type':'zebra')).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'type':'unicorn', 'id':'somestring')).toBeTruthy()
|
||||
|
||||
xit 'array match of a property', ->
|
||||
it 'array match of a property', ->
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'likes':'poptarts')).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'likes':'walks on the beach')).toBeFalsy()
|
||||
|
||||
xit '$gt selector', ->
|
||||
it '$gt selector', ->
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$gt': 8000)).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$gt': [8000, 10000])).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'levels': '$gt': [10, 20, 30])).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$gt': 9000)).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': {'$gt': 8000}, 'worth': {'$gt': 5})).toBeTruthy()
|
||||
|
||||
xit '$gte selector', ->
|
||||
it '$gte selector', ->
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$gte': 9001)).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$gte': 9000)).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$gte': [9000, 10000])).toBeTruthy()
|
||||
|
||||
xit '$in selector', ->
|
||||
it '$lt selector', ->
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$lt': 9001)).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$lt': 9000)).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$lt': [9001, 9000])).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'levels': '$lt': [10, 20, 30])).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': {'$lt': 9001}, 'worth': {'$lt': 7})).toBeTruthy()
|
||||
|
||||
it '$lte selector', ->
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$lte': 9000)).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$lte': 8000)).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': {'$lte': 9000}, 'worth': {'$lte': [6, 5]})).toBeTruthy()
|
||||
|
||||
it '$ne selector', ->
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$ne': 9000)).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'id': '$ne': 'otherstring')).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'id': '$ne': ['otherstring', 'somestring'])).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'likes': '$ne': ['popcorn', 'chicken'])).toBeFalsy()
|
||||
|
||||
it '$in selector', ->
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'type': '$in': ['unicorn', 'zebra'])).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'type': '$in': ['cats', 'dogs'])).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'likes': '$in': ['popcorn', 'chicken'])).toBeTruthy()
|
||||
|
||||
xit '$lt selector', ->
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$lt': 9001)).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$lt': 9000)).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': {'$lt': 9001}, 'worth': {'$lt': 7})).toBeTruthy()
|
||||
|
||||
xit '$lte selector', ->
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$lte': 9000)).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$lte': 8000)).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': {'$lte': 9000}, 'value': {'$lte': [6, 5]})).toBeTruthy()
|
||||
|
||||
xit '$ne selector', ->
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'value': '$ne': 9000)).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'id': '$ne': 'otherstring')).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'id': '$ne': ['otherstring', 'somestring'])).toBeFalsy()
|
||||
|
||||
xit '$in selector', ->
|
||||
it '$nin selector', ->
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'type': '$nin': ['unicorn', 'zebra'])).toBeFalsy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'type': '$nin': ['cats', 'dogs'])).toBeTruthy()
|
||||
expect(LocalMongo.matchesQuery(this.fixture1, 'likes': '$nin': ['popcorn', 'chicken'])).toBeFalsy()
|
||||
|
|
Loading…
Reference in a new issue