|
| 1 | +/* eslint-disable @typescript-eslint/camelcase */ |
1 | 2 | import { expect } from 'chai';
|
2 | 3 | import { CliServiceProvider } from '../../service-provider-server'; // avoid cyclic dep just for test
|
3 | 4 | import ShellInternalState from './shell-internal-state';
|
@@ -66,6 +67,22 @@ describe('Shell API (integration)', function() {
|
66 | 67 | await collection.find( { quantity: { $gte: 5 }, type: 'apparel' } ).toArray();
|
67 | 68 | };
|
68 | 69 |
|
| 70 | + const loadMRExample = async(collection): Promise<any> => { |
| 71 | + const res = await collection.insertMany([ |
| 72 | + { _id: 1, cust_id: 'Ant O. Knee', ord_date: new Date('2020-03-01'), price: 25, items: [ { sku: 'oranges', qty: 5, price: 2.5 }, { sku: 'apples', qty: 5, price: 2.5 } ], status: 'A' }, |
| 73 | + { _id: 2, cust_id: 'Ant O. Knee', ord_date: new Date('2020-03-08'), price: 70, items: [ { sku: 'oranges', qty: 8, price: 2.5 }, { sku: 'chocolates', qty: 5, price: 10 } ], status: 'A' }, |
| 74 | + { _id: 3, cust_id: 'Busby Bee', ord_date: new Date('2020-03-08'), price: 50, items: [ { sku: 'oranges', qty: 10, price: 2.5 }, { sku: 'pears', qty: 10, price: 2.5 } ], status: 'A' }, |
| 75 | + { _id: 4, cust_id: 'Busby Bee', ord_date: new Date('2020-03-18'), price: 25, items: [ { sku: 'oranges', qty: 10, price: 2.5 } ], status: 'A' }, |
| 76 | + { _id: 5, cust_id: 'Busby Bee', ord_date: new Date('2020-03-19'), price: 50, items: [ { sku: 'chocolates', qty: 5, price: 10 } ], status: 'A' }, |
| 77 | + { _id: 6, cust_id: 'Cam Elot', ord_date: new Date('2020-03-19'), price: 35, items: [ { sku: 'carrots', qty: 10, price: 1.0 }, { sku: 'apples', qty: 10, price: 2.5 } ], status: 'A' }, |
| 78 | + { _id: 7, cust_id: 'Cam Elot', ord_date: new Date('2020-03-20'), price: 25, items: [ { sku: 'oranges', qty: 10, price: 2.5 } ], status: 'A' }, |
| 79 | + { _id: 8, cust_id: 'Don Quis', ord_date: new Date('2020-03-20'), price: 75, items: [ { sku: 'chocolates', qty: 5, price: 10 }, { sku: 'apples', qty: 10, price: 2.5 } ], status: 'A' }, |
| 80 | + { _id: 9, cust_id: 'Don Quis', ord_date: new Date('2020-03-20'), price: 55, items: [ { sku: 'carrots', qty: 5, price: 1.0 }, { sku: 'apples', qty: 10, price: 2.5 }, { sku: 'oranges', qty: 10, price: 2.5 } ], status: 'A' }, |
| 81 | + { _id: 10, cust_id: 'Don Quis', ord_date: new Date('2020-03-23'), price: 25, items: [ { sku: 'oranges', qty: 10, price: 2.5 } ], status: 'A' } |
| 82 | + ]); |
| 83 | + expect(res.acknowledged).to.equal(1); |
| 84 | + }; |
| 85 | + |
69 | 86 | before(async() => {
|
70 | 87 | serviceProvider = await CliServiceProvider.connect(connectionString);
|
71 | 88 | });
|
@@ -1348,4 +1365,92 @@ describe('Shell API (integration)', function() {
|
1348 | 1365 | });
|
1349 | 1366 | });
|
1350 | 1367 | });
|
| 1368 | + describe('mapReduce', () => { |
| 1369 | + it('accepts function args and collection name as string', async() => { |
| 1370 | + await loadMRExample(collection); |
| 1371 | + const mapFn = `function() { |
| 1372 | + emit(this.cust_id, this.price); |
| 1373 | + };`; |
| 1374 | + const reduceFn = function(keyCustId, valuesPrices): any { |
| 1375 | + return valuesPrices.reduce((s, t) => s + t); |
| 1376 | + }; |
| 1377 | + const result = await collection.mapReduce(mapFn, reduceFn, 'map_reduce_example'); |
| 1378 | + expect(result.ok).to.equal(1); |
| 1379 | + const outRes = await database.map_reduce_example.find().sort({ _id: 1 }).toArray(); |
| 1380 | + expect(outRes).to.deep.equal([ |
| 1381 | + { '_id': 'Ant O. Knee', 'value': 95 }, |
| 1382 | + { '_id': 'Busby Bee', 'value': 125 }, |
| 1383 | + { '_id': 'Cam Elot', 'value': 60 }, |
| 1384 | + { '_id': 'Don Quis', 'value': 155 } |
| 1385 | + ]); |
| 1386 | + }); |
| 1387 | + it('accepts string args and collection name as string', async() => { |
| 1388 | + await loadMRExample(collection); |
| 1389 | + const mapFn = `function() { |
| 1390 | + emit(this.cust_id, this.price); |
| 1391 | + };`; |
| 1392 | + const reduceFn = function(keyCustId, valuesPrices): any { |
| 1393 | + return valuesPrices.reduce((s, t) => s + t); |
| 1394 | + }; |
| 1395 | + const result = await collection.mapReduce(mapFn, reduceFn.toString(), 'map_reduce_example'); |
| 1396 | + expect(result.ok).to.equal(1); |
| 1397 | + expect(result.result).to.equal('map_reduce_example'); |
| 1398 | + const outRes = await database.map_reduce_example.find().sort({ _id: 1 }).toArray(); |
| 1399 | + expect(outRes).to.deep.equal([ |
| 1400 | + { '_id': 'Ant O. Knee', 'value': 95 }, |
| 1401 | + { '_id': 'Busby Bee', 'value': 125 }, |
| 1402 | + { '_id': 'Cam Elot', 'value': 60 }, |
| 1403 | + { '_id': 'Don Quis', 'value': 155 } |
| 1404 | + ]); |
| 1405 | + }); |
| 1406 | + it('accepts inline as option', async() => { |
| 1407 | + await loadMRExample(collection); |
| 1408 | + const mapFn = `function() { |
| 1409 | + emit(this.cust_id, this.price); |
| 1410 | + };`; |
| 1411 | + const reduceFn = function(keyCustId, valuesPrices): any { |
| 1412 | + return valuesPrices.reduce((s, t) => s + t); |
| 1413 | + }; |
| 1414 | + const result = await collection.mapReduce(mapFn, reduceFn.toString(), { |
| 1415 | + out: { inline: 1 } |
| 1416 | + }); |
| 1417 | + expect(result.ok).to.equal(1); |
| 1418 | + expect(result.results.map(k => k._id).sort()).to.deep.equal([ |
| 1419 | + 'Ant O. Knee', |
| 1420 | + 'Busby Bee', |
| 1421 | + 'Cam Elot', |
| 1422 | + 'Don Quis' |
| 1423 | + ]); |
| 1424 | + expect(result.results.map(k => k.value).sort()).to.deep.equal([ |
| 1425 | + 125, |
| 1426 | + 155, |
| 1427 | + 60, |
| 1428 | + 95 |
| 1429 | + ]); |
| 1430 | + }); |
| 1431 | + it('accepts finalize as option', async() => { |
| 1432 | + await loadMRExample(collection); |
| 1433 | + const mapFn = `function() { |
| 1434 | + emit(this.cust_id, this.price); |
| 1435 | + };`; |
| 1436 | + const reduceFn = function(keyCustId, valuesPrices): any { |
| 1437 | + return valuesPrices.reduce((s, t) => s + t); |
| 1438 | + }; |
| 1439 | + const finalizeFn = function(): any { |
| 1440 | + return 1; |
| 1441 | + }; |
| 1442 | + const result = await collection.mapReduce(mapFn, reduceFn.toString(), { |
| 1443 | + out: { inline: 1 }, |
| 1444 | + finalize: finalizeFn |
| 1445 | + }); |
| 1446 | + expect(result.ok).to.equal(1); |
| 1447 | + expect(result.results.map(k => k._id).sort()).to.deep.equal([ |
| 1448 | + 'Ant O. Knee', |
| 1449 | + 'Busby Bee', |
| 1450 | + 'Cam Elot', |
| 1451 | + 'Don Quis' |
| 1452 | + ]); |
| 1453 | + expect(result.results.map(k => k.value)).to.deep.equal([1, 1, 1, 1]); |
| 1454 | + }); |
| 1455 | + }); |
1351 | 1456 | });
|
0 commit comments