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.

objects-test.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import assert from 'assert';
  2. import sift from '..';
  3. describe(__filename + '#', function () {
  4. var topic = [
  5. {
  6. name: 'craig',
  7. age: 90001,
  8. tags: ['coder', 'programmer', 'traveler', 'photographer'],
  9. address: {
  10. city: 'Minneapolis',
  11. state: 'MN',
  12. phone: '9999999999'
  13. },
  14. tags: ['photos', 'cook'],
  15. hobbies: [
  16. {
  17. name: 'programming',
  18. description: 'some desc'
  19. },
  20. {
  21. name: 'cooking'
  22. },
  23. {
  24. name: 'photography',
  25. places: ['haiti', 'brazil', 'costa rica']
  26. },
  27. {
  28. name: 'backpacking'
  29. }
  30. ]
  31. },
  32. {
  33. name: 'tim',
  34. age: 90001,
  35. tags: ['traveler', 'photographer'],
  36. address: {
  37. city: 'St. Paul',
  38. state: 'MN',
  39. phone: '765765756765'
  40. },
  41. tags: ['dj'],
  42. hobbies: [
  43. {
  44. name: 'biking',
  45. description: 'some desc'
  46. },
  47. {
  48. name: 'DJ'
  49. },
  50. {
  51. name: 'photography',
  52. places: ['costa rica']
  53. }
  54. ]
  55. }
  56. ];
  57. xit('throws error if $not is incorrect', function () {
  58. assert.throws(function () {
  59. sift({
  60. $not: ['abc']
  61. }, topic);
  62. }, Error);
  63. });
  64. it('has sifted through photography in brazil count of 1', function () {
  65. var sifted = sift({
  66. hobbies: {
  67. name: 'photography',
  68. places: {
  69. $in: ['brazil']
  70. }
  71. }
  72. }, topic);
  73. assert.equal(sifted.length, 1);
  74. });
  75. it('has sifted through photography in brazil, haiti, and costa rica count of 1', function () {
  76. var sifted = sift({
  77. hobbies: {
  78. name: 'photography',
  79. places: {
  80. $all: ['brazil', 'haiti', 'costa rica']
  81. }
  82. }
  83. }, topic);
  84. assert.equal(sifted.length, 1);
  85. assert.equal(sifted[0], topic[0]);
  86. });
  87. it('has a sifted hobbies of photography, cooking, or biking count of 2', function () {
  88. var sifted = sift({
  89. hobbies: {
  90. name: {
  91. $in: ['photography', 'cooking', 'biking']
  92. }
  93. }
  94. }, topic);
  95. assert.equal(sifted.length, 2);
  96. });
  97. it('has sifted to complex count of 2', function () {
  98. var sifted = sift({
  99. hobbies: {
  100. name: 'photography',
  101. places: {
  102. $in: ['costa rica']
  103. }
  104. },
  105. address: {
  106. state: 'MN',
  107. phone: {
  108. $exists: true
  109. }
  110. }
  111. }, topic);
  112. assert.equal(sifted.length, 2);
  113. });
  114. it('has sifted to complex count of 0', function () {
  115. var sifted = sift({
  116. hobbies: {
  117. name: 'photos',
  118. places: {
  119. $in: ['costa rica']
  120. }
  121. }
  122. }, topic);
  123. assert.equal(sifted.length, 0);
  124. });
  125. it('has sifted subobject hobbies count of 3', function () {
  126. var sifted = sift({
  127. 'hobbies.name': 'photography'
  128. }, topic);
  129. assert.equal(sifted.length, 2);
  130. });
  131. it('has sifted dot-notation hobbies of photography, cooking, and biking count of 3', function () {
  132. var sifted = sift({
  133. 'hobbies.name': {
  134. $in: ['photography', 'cooking', 'biking']
  135. }
  136. }, topic);
  137. assert.equal(sifted.length, 2);
  138. });
  139. it('has sifted to complex dot-search count of 2', function () {
  140. var sifted = sift({
  141. 'hobbies.name': 'photography',
  142. 'hobbies.places': {
  143. $in: ['costa rica']
  144. },
  145. 'address.state': 'MN',
  146. 'address.phone': {
  147. $exists: true
  148. }
  149. }, topic);
  150. assert.equal(sifted.length, 2);
  151. });
  152. it('has sifted with selector function count of 2', function () {
  153. var sifted = sift({
  154. 'name': 'photography',
  155. 'places': {
  156. $in: ['costa rica']
  157. }
  158. }, topic, function (item) {
  159. return item.hobbies;
  160. });
  161. assert.equal(sifted.length, 2);
  162. });
  163. describe('nesting', function () {
  164. it('$eq for nested object', function () {
  165. var sifted = sift({'sub.num': {'$eq': 10}}, loremArr);
  166. assert(sifted.length > 0);
  167. sifted.forEach(function (v) {
  168. assert.equal(10, v.sub.num);
  169. });
  170. });
  171. it('$ne for nested object', function () {
  172. var sifted = sift({'sub.num': {'$ne': 10}}, loremArr);
  173. assert(sifted.length > 0);
  174. sifted.forEach(function (v) {
  175. assert.notEqual(10, v.sub.num);
  176. });
  177. });
  178. it('$regex for nested object (one missing key)', function () {
  179. var persons = [{
  180. id: 1,
  181. prof: 'Mr. Moriarty'
  182. }, {
  183. id: 2,
  184. prof: 'Mycroft Holmes'
  185. }, {
  186. id: 3,
  187. name: 'Dr. Watson',
  188. prof: 'Doctor'
  189. }, {
  190. id: 4,
  191. name: 'Mr. Holmes',
  192. prof: 'Detective'
  193. }];
  194. var q = { 'name': { '$regex': 'n' } };
  195. var sifted = sift(q, persons);
  196. assert.deepEqual(sifted, [{
  197. id: 3,
  198. name: 'Dr. Watson',
  199. prof: 'Doctor'
  200. }]);
  201. });
  202. });
  203. describe('arrays of objects', function () {
  204. var objects = [
  205. {
  206. things: [
  207. {
  208. id: 123
  209. }, {
  210. id: 456
  211. }
  212. ]
  213. }, {
  214. things: [
  215. {
  216. id: 123
  217. },
  218. {
  219. id: 789
  220. }
  221. ]
  222. }
  223. ];
  224. it('$eq for array of objects, matches if at least one exists', function () {
  225. let q = {
  226. 'things.id': 123
  227. }
  228. var sifted = sift(q, objects)
  229. assert.deepEqual(sifted, objects)
  230. let q2 = {
  231. 'things.id': 789
  232. }
  233. var sifted2 = sift(q2, objects)
  234. assert.deepEqual(sifted2, [objects[1]])
  235. })
  236. it('$ne for array of objects, returns if none of the array elements match the query', function () {
  237. let q = {
  238. 'things.id': {
  239. $ne: 123
  240. }
  241. }
  242. var sifted = sift(q, objects)
  243. assert.deepEqual(sifted, [])
  244. let q2 = {
  245. 'things.id': {
  246. $ne: 789
  247. }
  248. }
  249. var sifted2 = sift(q2, objects)
  250. assert.deepEqual(sifted2, [objects[0]])
  251. })
  252. })
  253. describe('$where', function() {
  254. var couples = [{
  255. name: 'SMITH',
  256. person: [{
  257. firstName: 'craig',
  258. gender: 'female',
  259. age: 29
  260. }, {
  261. firstName: 'tim',
  262. gender: 'male',
  263. age: 32
  264. }
  265. ]
  266. }, {
  267. name: 'JOHNSON',
  268. person: [{
  269. firstName: 'emily',
  270. gender: 'female',
  271. age: 35
  272. }, {
  273. firstName: 'jacob',
  274. gender: 'male',
  275. age: 32
  276. }
  277. ]
  278. }];
  279. it('can filter people', function() {
  280. var results = sift({'person': {$elemMatch: { 'gender': 'female', 'age': {'$lt': 30}}}}, couples);
  281. assert.equal(results[0].name, 'SMITH');
  282. var results = sift({'person': {$elemMatch: { 'gender': 'male', 'age': {'$lt': 30}}}}, [couples[0]]);
  283. assert.equal(results.length, 0);
  284. });
  285. });
  286. describe('keypath', function () {
  287. var arr = [
  288. {
  289. a: {
  290. b: {
  291. c: 1,
  292. c2: 1
  293. }
  294. }
  295. }
  296. ]
  297. it('can be used', function () {
  298. assert.equal(sift({'a.b.c':1})(arr[0]), true);
  299. });
  300. });
  301. });
  302. var loremArr = [
  303. {
  304. 'num': 1,
  305. 'pum': 1,
  306. 'sub': {
  307. 'num': 1,
  308. 'pum': 1
  309. }
  310. },
  311. {
  312. 'num': 2,
  313. 'pum': 2,
  314. 'sub': {
  315. 'num': 2,
  316. 'pum': 2
  317. }
  318. },
  319. {
  320. 'num': 3,
  321. 'pum': 3,
  322. 'sub': {
  323. 'num': 3,
  324. 'pum': 3
  325. }
  326. },
  327. {
  328. 'num': 4,
  329. 'pum': 4,
  330. 'sub': {
  331. 'num': 4,
  332. 'pum': 4
  333. }
  334. },
  335. {
  336. 'num': 5,
  337. 'pum': 5,
  338. 'sub': {
  339. 'num': 5,
  340. 'pum': 5
  341. }
  342. },
  343. {
  344. 'num': 6,
  345. 'pum': 6,
  346. 'sub': {
  347. 'num': 6,
  348. 'pum': 6
  349. }
  350. },
  351. {
  352. 'num': 7,
  353. 'pum': 7,
  354. 'sub': {
  355. 'num': 7,
  356. 'pum': 7
  357. }
  358. },
  359. {
  360. 'num': 8,
  361. 'pum': 8,
  362. 'sub': {
  363. 'num': 8,
  364. 'pum': 8
  365. }
  366. },
  367. {
  368. 'num': 9,
  369. 'pum': 9,
  370. 'sub': {
  371. 'num': 9,
  372. 'pum': 9
  373. }
  374. },
  375. {
  376. 'num': 10,
  377. 'pum': 10,
  378. 'sub': {
  379. 'num': 10,
  380. 'pum': 10
  381. }
  382. },
  383. {
  384. 'num': 11,
  385. 'pum': 11,
  386. 'sub': {
  387. 'num': 10,
  388. 'pum': 10
  389. }
  390. }
  391. ];