| 
 | 1 | +const test = require('tape')  | 
 | 2 | +const sinon = require('sinon')  | 
 | 3 | +const { parseSelect, applySelect } = require('../sql.js')  | 
 | 4 | + | 
 | 5 | +test('parseSelect - parses simple SQL correctly', (t) => {  | 
 | 6 | +  const subject = "SELECT * FROM 'topic'"  | 
 | 7 | +  const results = parseSelect(subject)  | 
 | 8 | +  t.deepEqual(results.select, [{ field: '*', alias: undefined }])  | 
 | 9 | +  t.equal(results.topic, 'topic')  | 
 | 10 | +  t.equal(results.where, undefined)  | 
 | 11 | +  t.end()  | 
 | 12 | +})  | 
 | 13 | + | 
 | 14 | +test('parseSelect - parses lowercase simple SQL correctly', (t) => {  | 
 | 15 | +  const subject = "select * from 'topic'"  | 
 | 16 | +  const results = parseSelect(subject)  | 
 | 17 | +  t.deepEqual(results.select, [{ field: '*', alias: undefined }])  | 
 | 18 | +  t.equal(results.topic, 'topic')  | 
 | 19 | +  t.equal(results.where, undefined)  | 
 | 20 | +  t.end()  | 
 | 21 | +})  | 
 | 22 | + | 
 | 23 | +test('parseSelect - parses where clause correctly', (t) => {  | 
 | 24 | +  const subject = "SELECT * FROM 'topic' WHERE name='Bob'"  | 
 | 25 | +  const results = parseSelect(subject)  | 
 | 26 | +  t.deepEqual(results.select, [{ field: '*', alias: undefined }])  | 
 | 27 | +  t.equal(results.topic, 'topic')  | 
 | 28 | +  t.equal(results.where, "name='Bob'")  | 
 | 29 | +  t.end()  | 
 | 30 | +})  | 
 | 31 | + | 
 | 32 | +test('parseSelect - parses multiple SELECT properties correctly', (t) => {  | 
 | 33 | +  const subject = "SELECT name, age, maleOrFemale AS gender FROM 'topic'"  | 
 | 34 | +  const results = parseSelect(subject)  | 
 | 35 | +  t.deepEqual(results.select, [  | 
 | 36 | +    { field: 'name', alias: undefined},  | 
 | 37 | +    { field: 'age', alias: undefined },  | 
 | 38 | +    { field: 'maleOrFemale', alias: 'gender'}  | 
 | 39 | +  ])  | 
 | 40 | +  t.end()  | 
 | 41 | +})  | 
 | 42 | + | 
 | 43 | +test('applySelect - Simple select with buffered string handled correctly', (t) => {  | 
 | 44 | +  const select = [{ field: '*', alias: undefined }]  | 
 | 45 | +  const payload = Buffer.from(JSON.stringify({name: 'Bob'}), 'utf8')  | 
 | 46 | +  const context = {}  | 
 | 47 | +  const event = applySelect({ select, payload, context })  | 
 | 48 | +  t.deepEqual(event, { name: 'Bob' })  | 
 | 49 | +  t.end()  | 
 | 50 | +})  | 
 | 51 | + | 
 | 52 | +test('applySelect - Simple select with non-JSON handled correctly', (t) => {  | 
 | 53 | +  const select = [{ field: '*', alias: undefined }]  | 
 | 54 | +  const payload = 'Bob'  | 
 | 55 | +  const context = {}  | 
 | 56 | +  const event = applySelect({ select, payload, context })  | 
 | 57 | +  t.equal(event, 'Bob')  | 
 | 58 | +  t.end()  | 
 | 59 | +})  | 
 | 60 | + | 
 | 61 | +test('applySelect - Aliased wildcard with non-JSON handled correctly', (t) => {  | 
 | 62 | +  const select = [{ field: '*', alias: 'name' }]  | 
 | 63 | +  const payload = 'Bob'  | 
 | 64 | +  const context = {}  | 
 | 65 | +  const event = applySelect({ select, payload, context })  | 
 | 66 | +  t.deepEqual(event, { 'name': 'Bob'})  | 
 | 67 | +  t.end()  | 
 | 68 | +})  | 
 | 69 | + | 
 | 70 | +test('applySelect - Unaliased wildcard plus function results in flattened output', (t) => {  | 
 | 71 | +  const select = [  | 
 | 72 | +    { field: '*', alias: undefined },  | 
 | 73 | +    { field: 'clientid()', alias: undefined }  | 
 | 74 | +  ]  | 
 | 75 | +  const clientIdFunc = sinon.stub().returns(undefined);  | 
 | 76 | +  const payload = Buffer.from(JSON.stringify({name: 'Bob'}), 'utf8')  | 
 | 77 | +  const context = { clientid: clientIdFunc }  | 
 | 78 | +  const event = applySelect({ select, payload, context })  | 
 | 79 | +  t.ok(clientIdFunc.calledOnce)  | 
 | 80 | +  t.deepEqual(event, { name: 'Bob', 'clientid()': undefined })  | 
 | 81 | +  t.end()  | 
 | 82 | +})  | 
 | 83 | + | 
 | 84 | +test('applySelect - Aliased wildcard plus function results in nested output', (t) => {  | 
 | 85 | +  const select = [  | 
 | 86 | +    { field: '*', alias: 'message' },  | 
 | 87 | +    { field: 'clientid()', alias: undefined }  | 
 | 88 | +  ]  | 
 | 89 | +  const clientIdFunc = sinon.stub().returns(undefined);  | 
 | 90 | +  const payload = Buffer.from(JSON.stringify({name: 'Bob'}), 'utf8')  | 
 | 91 | +  const context = { clientid: clientIdFunc }  | 
 | 92 | +  const event = applySelect({ select, payload, context })  | 
 | 93 | +  t.ok(clientIdFunc.calledOnce)  | 
 | 94 | +  t.deepEqual(event, { message: { name: 'Bob' }, 'clientid()': undefined })  | 
 | 95 | +  t.end()  | 
 | 96 | +})  | 
 | 97 | + | 
 | 98 | +test('applySelect - Function results are appeneded to output', (t) => {  | 
 | 99 | +  const select = [  | 
 | 100 | +    { field: '*', alias: 'message' },  | 
 | 101 | +    { field: 'clientid()', alias: 'theClientId' }  | 
 | 102 | +  ]  | 
 | 103 | +  const clientIdFunc = sinon.stub().returns('12345')  | 
 | 104 | +  const payload = Buffer.from(JSON.stringify({name: 'Bob'}), 'utf8')  | 
 | 105 | +  const context = { clientid: clientIdFunc }  | 
 | 106 | +  const event = applySelect({ select, payload, context })  | 
 | 107 | +  t.ok(clientIdFunc.calledOnce)  | 
 | 108 | +  t.deepEqual(event, { message: { name: 'Bob' }, 'theClientId': '12345' })  | 
 | 109 | +  t.end()  | 
 | 110 | +})  | 
0 commit comments