Ohm-Management - Projektarbeit B-ME
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.

operations-test.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import * as assert from 'assert';
  2. import sift from '..';
  3. var ObjectID = require('bson').ObjectID;
  4. describe(__filename + '#', function () {
  5. [
  6. // $eq
  7. [{$eq:5}, [5,'5', 6], [5]],
  8. ['5', [5,'5', 6], ['5']],
  9. [false, [false,'false', true], [false]],
  10. [true, [1, true], [true]],
  11. [0, [0,'0'], [0]],
  12. [null, [null], [null]],
  13. [void 0, [void 0, null], [void 0]],
  14. [1, [2,3,4,5], []],
  15. [1, [[1]], [[1]]],
  16. [new Date(1), [new Date(), new Date(1), new Date(2), new Date(3)], [new Date(1)]],
  17. [/^a/, ['a','ab','abc','b','bc'], ['a','ab','abc']],
  18. [function(b) { return b === 1; }, [1,2,3],[1]],
  19. [ObjectID('54dd5546b1d296a54d152e84'),[ObjectID(),ObjectID('54dd5546b1d296a54d152e84')],[ObjectID('54dd5546b1d296a54d152e84')]],
  20. // $ne
  21. [{$ne:5}, [5, '5', 6], ['5', 6]],
  22. [{$ne:'5'}, ['5', 6], [6]],
  23. [{$ne:false}, [false], []],
  24. [{$ne:void 0}, [false, 0, '0', void 0], [false, 0, '0']],
  25. [{$ne:/^a/}, ['a','ab','abc','b','bc'], ['b','bc']],
  26. [{$ne:1}, [[2],[1]], [[2]]],
  27. [{groups:{$ne:111}}, [{groups:[111,222,333,444]},{groups:[222,333,444]}],[{groups:[222,333,444]}]],
  28. // $lt
  29. [{$lt:5}, [3,4,5,6],[3,4]],
  30. [{$lt:'c'}, ['a','b','c'],['a','b']],
  31. [{$lt:null}, [-3,-4], []],
  32. [{$lt:new Date(3)}, [new Date(1), new Date(2), new Date(3)],[new Date(1), new Date(2)]],
  33. // $lte
  34. [{$lte:5}, [3,4,5,6],[3,4,5]],
  35. [{groups:{$lt:5}}, [{groups:[1,2,3,4]}, {groups:[7,8]}], [{groups:[1,2,3,4]}]],
  36. // $gt
  37. [{$gt:5}, [3,4,5,6],[6]],
  38. [{$gt:null}, [3,4], []],
  39. [{groups:{$gt:5}}, [{groups:[1,2,3,4]}, {groups:[7,8]}], [{groups:[7,8]}]],
  40. // $gte
  41. [{$gte:5}, [3,4,5,6],[5, 6]],
  42. [{groups:{$gte:5}}, [{groups:[1,2,3,4]}, {groups:[7,8]}], [{groups:[7,8]}]],
  43. // $mod
  44. [{$mod:[2,1]}, [1,2,3,4,5,6],[1,3,5]],
  45. [{groups:{$mod:[2,0]}}, [{groups:[1,2,3,4]}, {groups:[7,9]}], [{groups:[1,2,3,4]}]],
  46. // $exists
  47. [{$exists:false}, [0,false,void 0, null],[]],
  48. [{$exists:true}, [0,false,void 0, 1, {}],[0, false, void 0, 1, {}]],
  49. [{'a.b': {$exists: true}}, [{a: {b: 'exists'}}, {a: {c: 'does not exist'}}], [{a: {b: 'exists'}}]],
  50. [{field: { $exists: false }}, [{a: 1}, {a: 2, field: 5}, {a: 3, field: 0}, {a: 4, field: undefined}, {a: 5}],[{a: 1}, {a: 5}]],
  51. // $in
  52. // TODO - {$in:[Date]} doesn't work - make it work?
  53. [{$in:[0,false,1,'1']},[0,1,2,3,4,false],[0,1,false]],
  54. [{$in:[1,'1','2']},['1','2','3'],['1','2']],
  55. [{$in:[new Date(1)]},[new Date(1), new Date(2)],[new Date(1)]],
  56. [{'a.b.status':{'$in': [0]}}, [{'a':{'b':[{'status':0}]}},{'a':{'b':[{'status':2}]}}],[{'a':{'b':[{'status':0}]}}]],
  57. [{'a.b.status':{'$in': [0, 2]}}, [{'a':{'b':[{'status':0}]}},{'a':{'b':[{'status':2}]}}], [{'a':{'b':[{'status':0}]}},{'a':{'b':[{'status':2}]}}]],
  58. [{'x': {$in: [{$regex: '.*aaa.*'}, {$regex: '.*bbb.*'}]}}, [{'x': {'b': 'aaa'}}, {'x': 'bbb'}, {'x': 'ccc'}, {'x': 'aaa'}], [{'x': 'bbb'}, {'x': 'aaa'}]],
  59. [{'x': {$in: [/.*aaa.*/, /.*bbb.*/]}}, [{'x': {'b': 'aaa'}}, {'x': 'bbb'}, {'x': 'ccc'}, {'x': 'aaa'}], [{'x': 'bbb'}, {'x': 'aaa'}]],
  60. // $nin
  61. [{$nin:[0,false,1,'1']},[0,1,2,3,4,false],[2,3,4]],
  62. [{$nin:[1,'1','2']},['1','2','3'],['3']],
  63. [{$nin:[new Date(1)]},[new Date(1), new Date(2)],[new Date(2)]],
  64. [{'root.notDefined': {$nin: [1, 2, 3]}}, [{'root': {'defined': 1337}}], [{'root': {'defined': 1337}}]],
  65. [{'root.notDefined': {$nin: [1, 2, 3, null]}}, [{'root': {'defined': 1337}}], []],
  66. [{'x': {$nin: [{$regex: '.*aaa.*'}, {$regex: '.*bbb.*'}]}}, [{'x': {'b': 'aaa'}}, {'x': 'bbb'}, {'x': 'ccc'}, {'x': 'aaa'}], [{'x': {'b': 'aaa'}},{'x': 'ccc'}]],
  67. [{'x': {$nin: [/.*aaa.*/, /.*bbb.*/]}}, [{'x': {'b': 'aaa'}}, {'x': 'bbb'}, {'x': 'ccc'}, {'x': 'aaa'}], [{'x': {'b': 'aaa'}},{'x': 'ccc'}]],
  68. // $not
  69. [{$not:false},[0,false],[0]],
  70. [{$not:0},[0, false, 1, 2, 3],[false, 1, 2, 3]],
  71. [{$not:{$in:[1,2,3]}},[1,2,3,4,5,6],[4,5,6]], // with expressions
  72. // $type
  73. [{$type:Date}, [0,new Date(1)],[new Date(1)]],
  74. [{$type:Number}, [0,false,1],[0,1]],
  75. [{$type:Boolean}, [0,false, void 0],[false]],
  76. [{$type:String}, ['1',1,false],['1']],
  77. // $all
  78. [{$all:[1,2,3]},[[1,2,3,4],[1,2,4]],[[1,2,3,4]]],
  79. [{$all:[0,false]},[[0,1,2],[0,false],['0','false'],void 0],[[0,false]]],
  80. [{$all:['1']},[[1]],[]],
  81. [{$all:[new Date(1),new Date(2)]},[[new Date(1), new Date(2)],[new Date(1)]],[[new Date(1), new Date(2)]]],
  82. // $size
  83. [{$size:3},['123',[1,2,3],'1'],['123',[1,2,3]]],
  84. [{$size:1},['123',[1,2,3],'1', void 0],['1']],
  85. // $or
  86. [{$or:[1,2,3]},[1,2,3,4],[1,2,3]],
  87. [{$or:[{$ne:1},2]},[1,2,3,4,5,6],[2,3,4,5,6]],
  88. // $nor
  89. [{$nor:[1,2,3]},[1,2,3,4],[4]],
  90. [{$nor:[{$ne:1},2]},[1,2,3,4,5,6],[1]],
  91. // $and
  92. [{$and:[{$gt:1},{$lt:4}]},[1,2,3,4],[2,3]],
  93. [{$and: [{field: {$not: {$type: String}}}, {field: {$ne: null}}]}, [{a: 1, field: 1}, {a: 2, field: '2'}], [{a: 1, field: 1}]],
  94. // $regex
  95. [{$regex:'^a'},['a','ab','abc','bc','bcd'],['a','ab','abc']],
  96. [{a:{$regex:'b|c'}}, [{a:['b']},{a:['c']},{a:'c'},{a:'d'}], [{a:['b']},{a:['c']},{a:'c'}]],
  97. [{ folder: { $regex:'^[0-9]{4}$' }}, [{ folder:['1234','3212'] }], [{ folder:['1234','3212'] }]],
  98. // $options
  99. [{$regex:'^a', $options: 'i'},['a','Ab','abc','bc','bcd'],['a','Ab','abc']],
  100. [{'text':{'$regex':'.*lis.*','$options':'i'}}, [{text:['Bob','Melissa','Joe','Sherry']}], [{text:['Bob','Melissa','Joe','Sherry']}]],
  101. // undefined
  102. [{$regex:'a'},[undefined, null, true, false, 0, 'aa'],['aa']],
  103. [/a/,[undefined, null, true, false, 0, 'aa'],['aa']],
  104. [/.+/,[undefined, null, true, false, 0, 'aa', {}],['aa']],
  105. // Multiple conditions on an undefined root
  106. [{'a.b': {$exists: true, $nin: [null]}}, [{a: {b: 'exists'}}, {a: {c: 'does not exist'}}], [{a: {b: 'exists'}}]],
  107. // $where
  108. [{$where:function () { return this.v === 1 }}, [{v:1},{v:2}],[{v:1}]],
  109. [{$where:'this.v === 1'}, [{v:1},{v:2}],[{v:1}]],
  110. [{$where:'obj.v === 1'}, [{v:1},{v:2}],[{v:1}]],
  111. // $elemMatch
  112. //{'person': {'$elemMatch': {'gender': 'male', 'age': {'$lt': 30}}}}
  113. [
  114. {a:{$elemMatch:{b:1,c:2}}},
  115. [{a:{b:1,c:2}},{a:[{b:1,c:2,d:3}]},{a:{b:2,c:3}}], [{a:{b:1,c:2}},{a:[{b:1,c:2,d:3}]}]
  116. ],
  117. [{a:{$elemMatch:{b:2,c:{$gt:2}}}}, [{a:{b:1,c:2}},{a:{b:1,c:2,d:3}},[{a:{b:2,c:3}}]], [[{a:{b:2,c:3}}]]],
  118. [
  119. {tags: {$all: [{$elemMatch: {a: 1}}]}},
  120. [{tags: [{a: 1}]}, {tags: [{a: 1}, {b: 1}]}], [{tags: [{a: 1}]}, {tags: [{a: 1}, {b: 1}]}]
  121. ],
  122. // dot-notation
  123. [
  124. {'a.b': /c/ },
  125. [{a:{b:'c'}}, {a:{b:'cd'}}, {'a.b':'c'},{a:{b:'e'}}],
  126. [{a:{b:'c'}}, {a:{b:'cd'}}]
  127. ],
  128. [
  129. {'foo.0': 'baz' },
  130. [{foo:['bar', 'baz']}, {foo:['baz', 'bar']}],
  131. [{foo:['baz', 'bar']}]
  132. ],
  133. [
  134. {'foo.0.name': 'baz' },
  135. [{foo:[{ name: 'bar' }, { name: 'baz' }]}, {foo:[{ name: 'baz' }, { name: 'bar' }]}],
  136. [{foo:[{ name: 'baz' }, { name: 'bar' }]}]
  137. ],
  138. // object.toString() tests
  139. [
  140. { $in: [{ toString: function(){ return 'a'; }}]},
  141. [{toString: function(){ return 'a'; }}, {toString: function(){ return 'b' }}],
  142. [{toString: function(){ return 'a'; }}]
  143. ],
  144. [
  145. { $in: [{}]},
  146. [{}, {}],
  147. []
  148. ],
  149. // various comparisons
  150. [
  151. { c: { d: 'd' }},
  152. [{ a: 'b', b: 'c', c: { d: 'd', e: 'e' }}, { c: { d: 'e' }}],
  153. [{ a: 'b', b: 'c', c: { d: 'd', e: 'e' }}]
  154. ],
  155. // based on https://gist.github.com/jdnichollsc/00ea8cf1204b17d9fb9a991fbd1dfee6
  156. [
  157. { $and: [{ 'a.s': { $lte: new Date('2017-01-29T05:00:00.000Z') }}, {'a.e': { $gte: new Date('2017-01-08T05:00:00.000Z') }}]},
  158. [{ a: { s: new Date('2017-01-13T05:00:00.000Z'), e: new Date('2017-01-31T05:00:00.000Z') }}],
  159. [{ a: { s: new Date('2017-01-13T05:00:00.000Z'), e: new Date('2017-01-31T05:00:00.000Z') }}]
  160. ],
  161. ].forEach(function (operation, i) {
  162. var filter = operation[0];
  163. var array = operation[1];
  164. var matchArray = operation[2];
  165. it(i + ': ' + JSON.stringify(filter), function() {
  166. assert.equal(JSON.stringify(array.filter(sift(filter))), JSON.stringify(matchArray));
  167. });
  168. });
  169. });