From f53646926f7ae1c2c7f22cdb7e001c187e91144f Mon Sep 17 00:00:00 2001 From: "Aydrian J. Howard" Date: Tue, 4 Aug 2015 10:58:00 -0400 Subject: [PATCH 1/2] Modified toApiFormat spec test file to fit standard naming convention. Added tests for code coverage. --- lib/toApiFormat.js | 9 +- test/spec/toApiFormat.js | 74 ----------------- test/spec/toApiFormat.spec.js | 152 ++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 78 deletions(-) delete mode 100644 test/spec/toApiFormat.js create mode 100644 test/spec/toApiFormat.spec.js diff --git a/lib/toApiFormat.js b/lib/toApiFormat.js index 5142f66..0faa2d9 100644 --- a/lib/toApiFormat.js +++ b/lib/toApiFormat.js @@ -8,16 +8,17 @@ module.exports = function toApiFormat(source) { var excludeList = ['substitution_data', 'tags', 'metadata', 'attributes', 'headers']; try{ - // Handle arrays - if(Array.isArray(source) && _.isObject(source[0])) { + // Handle arrays (Only need to handle arrays of objects for our use case.) + if(Array.isArray(source) && _.isPlainObject(source[0])) { dest = []; for(var i = 0; i < source.length; i++) { // Exclude appropriately - if( -1 === excludeList.indexOf(source[i])) { + /*if( -1 === excludeList.indexOf(source[i])) { dest.push(toApiFormat(source[i])); } else { dest.push(source[i]); - } + }*/ + dest.push(toApiFormat(source[i])); } return dest; } diff --git a/test/spec/toApiFormat.js b/test/spec/toApiFormat.js deleted file mode 100644 index 921a16f..0000000 --- a/test/spec/toApiFormat.js +++ /dev/null @@ -1,74 +0,0 @@ -'use strict'; - -var chai = require('chai') - , toApiFormat = require('../../lib/toApiFormat') - , expect = chai.expect - ; - -describe('toApiFormat', function() { - it('should recursively convert complex object keys to snake_case', function(done) { - var testObj = { - simpleString: "foo" - , boolVal: true - , numBers: 201 - , bangObj: { - strTwo: "bar" - , string_three: "skip" - } - , recipients: [ - {name: 'John Doe', address: 'j.doe@sparkpost.com'} - , {name: 'Sam Jones', address: 's.jones@sparkpost.com'} - ] - , fizzArr: [ - { - buzzInga: "buzz" - , bang: { - bellHop: "bell" - } - } - , { - bilBo: "baggins" - , alias: { - misTer: "underhill" - } - } - ] - , good_name: null - }; - - var validationObj = { - simple_string: "foo" - , bool_val: true - , num_bers: 201 - , bang_obj: { - str_two: "bar" - , string_three: "skip" - } - , recipients: [ - {name: 'John Doe', address: 'j.doe@sparkpost.com'} - , {name: 'Sam Jones', address: 's.jones@sparkpost.com'} - ] - , fizz_arr: [ - { - buzz_inga: "buzz" - , bang: { - bell_hop: "bell" - } - } - , { - bil_bo: "baggins" - , alias: { - mis_ter: "underhill" - } - } - ] - , good_name: null - }; - - var out = toApiFormat(testObj); - - expect(out).to.deep.equal(validationObj); - - done(); - }); -}); diff --git a/test/spec/toApiFormat.spec.js b/test/spec/toApiFormat.spec.js new file mode 100644 index 0000000..a0a3ca6 --- /dev/null +++ b/test/spec/toApiFormat.spec.js @@ -0,0 +1,152 @@ +'use strict'; + +var chai = require('chai') + , sinon = require('sinon') + , sinonChai = require('sinon-chai') + , toApiFormat = require('../../lib/toApiFormat') + , expect = chai.expect + , _ = require('lodash') + ; + +chai.use(sinonChai); + +describe('toApiFormat', function() { + it('should recursively convert complex object keys to snake_case', function(done) { + var testObj = { + simpleString: "foo" + , boolVal: true + , numBers: 201 + , bangObj: { + strTwo: "bar" + , string_three: "skip" + } + , recipients: [ + {name: 'John Doe', address: 'j.doe@sparkpost.com'} + , {name: 'Sam Jones', address: 's.jones@sparkpost.com'} + ] + , fizzArr: [ + { + buzzInga: "buzz" + , bang: { + bellHop: "bell" + } + } + , { + bilBo: "baggins" + , alias: { + misTer: "underhill" + } + } + ] + , good_name: null + }; + + var validationObj = { + simple_string: "foo" + , bool_val: true + , num_bers: 201 + , bang_obj: { + str_two: "bar" + , string_three: "skip" + } + , recipients: [ + {name: 'John Doe', address: 'j.doe@sparkpost.com'} + , {name: 'Sam Jones', address: 's.jones@sparkpost.com'} + ] + , fizz_arr: [ + { + buzz_inga: "buzz" + , bang: { + bell_hop: "bell" + } + } + , { + bil_bo: "baggins" + , alias: { + mis_ter: "underhill" + } + } + ] + , good_name: null + }; + + var out = toApiFormat(testObj); + + expect(out).to.deep.equal(validationObj); + + done(); + }); + + it('should throw an error if it encounters one', function(done) { + var stub = sinon.stub(_, 'snakeCase').throws(); + + var testObj = { + simpleString: "foo" + , substitution_data: { + key1: 'value1', + key_2: 'value_2' + } + , sampleArray: [ + 'oneTag', + 'two_tag', + 'substitution_data', + 'tags' + ] + }; + + try { + var out = toApiFormat(testObj); + } catch(e) { + expect(e).to.not.be.null; + } + + stub.restore(); + done(); + }); + + it('should ignore sub-properties of properties on exclude list', function(done) { + var testObj = { + simpleString: "foo" + , substitution_data: { + key1: 'value1', + key_2: 'value_2' + } + , sampleArray: [ + { + goodValue: 'hello', + tags: [ + 'oneTag', + 'two_tag', + 'substitution_data', + 'tags' + ] + } + ] + }; + + var validationObj = { + simple_string: "foo" + , substitution_data: { + key1: 'value1', + key_2: 'value_2' + } + , sample_array: [ + { + good_value: 'hello', + tags: [ + 'oneTag', + 'two_tag', + 'substitution_data', + 'tags' + ] + } + ] + }; + + var out = toApiFormat(testObj); + + expect(out).to.deep.equal(validationObj); + + done(); + }); +}); From 3fa3a232deec88a9f9fe0bf05d7b32bf96257977 Mon Sep 17 00:00:00 2001 From: "Aydrian J. Howard" Date: Thu, 6 Aug 2015 10:35:19 -0400 Subject: [PATCH 2/2] Removed comments. --- lib/toApiFormat.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/toApiFormat.js b/lib/toApiFormat.js index 0faa2d9..c9799d0 100644 --- a/lib/toApiFormat.js +++ b/lib/toApiFormat.js @@ -12,12 +12,6 @@ module.exports = function toApiFormat(source) { if(Array.isArray(source) && _.isPlainObject(source[0])) { dest = []; for(var i = 0; i < source.length; i++) { - // Exclude appropriately - /*if( -1 === excludeList.indexOf(source[i])) { - dest.push(toApiFormat(source[i])); - } else { - dest.push(source[i]); - }*/ dest.push(toApiFormat(source[i])); } return dest;