Skip to content

Commit

Permalink
new tests and mock structure to drive objects
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusmr13 committed Jun 29, 2016
1 parent 8625131 commit 13b95f8
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 65 deletions.
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "QuickDrive",
"version": "1.0.0",
"description": "Templates with google spreadsheet",
"author": "Matheus Martins do Rego",
"license": "MIT",
"repository": {
"type": "git",
"url": "git@github.com:matheusmr13/quickdrive.git"
},
"scripts": {
"test": "mocha"
},
"dependencies": {
"chai": "^3.5.0",
"mocha": "^2.5.3"
},
"devDependencies": {}
}
15 changes: 7 additions & 8 deletions src/SheetsTemplater.gs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var QuickDrive = (function (config) {
var QuickDrive = function (DriveApp, newConfig) {
var QuickDrive = {};

QuickDrive.annotationFunctions = {
Expand All @@ -12,7 +12,7 @@ var QuickDrive = (function (config) {
'=': QuickDrive.annotationFunctions.REPLACE_TEXT,
'~': QuickDrive.annotationFunctions.FOR_EACH
};
var config = {
this.config = {
folderId: '0ByQE0cDEoa0qLUlPU21xVzNqZVk',
templateId: '1stc2xmCa3QB61bTR52tomteWUOwlVZ4s8OSKWG5dP_8',
newDocumentName: 'My new sheet',
Expand All @@ -23,13 +23,11 @@ var QuickDrive = (function (config) {
permission: DriveApp.Permission.VIEW
}]
};
QuickDrive.setConfigs = function (newConfig) {
for (var propertie in newConfig) {
config[propertie] = newConfig[propertie];
}
for (var propertie in newConfig) {
config[propertie] = newConfig[propertie];
}

QuickDrive.getSheetNewDocument = function (json) {
QuickDrive.getSheetNewDocument = function () {
var templateFile = DriveApp.getFileById(config.templateId);
var newFile = templateFile.makeCopy(config.newDocumentName, DriveApp.getFolderById(config.folderId));

Expand Down Expand Up @@ -160,11 +158,12 @@ var QuickDrive = (function (config) {
};

return QuickDrive;
})({}, {});
};

function doPost(e) {
var json = e ? JSON.parse(e.parameters.data[0]) : {};
var config = e ? (e.parameters.config ? JSON.parse(e.parameters.config[0]) : {}) : {};
var QuickDrive = new QuickDrive(DriveApp, SpreadsheetApp, config);
var newSpreadSheet = QuickDrive.getSheetNewDocument(json);
var sheet = newSpreadSheet.sheet;

Expand Down
143 changes: 143 additions & 0 deletions test/mock/DriveAppMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
var DriveApp = function () {
var DriveFolder = function (newId) {
var files = [],
id = newId || '';
this.getId = function () {
return id;
};
this.addFile = function (file) {
files.push(file);
};
this.getFiles = function () {
return files;
};
};

var DriveFile = function (newId) {
var properties = {
id: newId || '',
name: ''
};

this.setSharing = function () {};
this.getId = function () {
return properties.id;
};
this.makeCopy = function (name, folder) {
var newFile = new DriveFile();
newFile._setProperties(properties);
newFile.setName(name);
folder.addFile(newFile);
return newFile;
};
this.getName = function () {
return properties.name;
};
this.setName = function (newName) {
properties.name = newName;
};
this._setProperties = function(newProperties) {
for (var propertie in newProperties) {
properties[propertie] = newProperties[propertie];
}
};
};

return {
Access: {
ANYONE: 'ANYONE',
ANYONE_WITH_LINK: 'ANYONE_WITH_LINK',
DOMAIN: 'DOMAIN',
DOMAIN_WITH_LINK: 'DOMAIN_WITH_LINK',
PRIVATE: 'PRIVATE'
},
Permission: {
COMMENT: 'COMMENT',
EDIT: 'EDIT',
NONE: 'NONE',
OWNER: 'OWNER',
VIEW: 'VIEW'
},
getFileById: function (id) {
return new DriveFile(id);
},
getFolderById: function (id) {
return new DriveFolder(id);
}
};
};

var SpreadsheetApp = function () {
var SpreadSheetMock = function (mySheets) {
var sheets = mySheets || [];

this.getSheets = function () {
return sheets;
};
var SheetMock = function (myMatrix) {
var matrix = myMatrix || [
[]
];

var CellMock = function () {
var value = "";
this.setValue = function (newValue) {
this.value = newValue;
};

this.getValue = function () {
return value;
};
};

var CellGroupMock = function (matrix) {
var matrix = matrix || [
[]
];

this.getValues = function () {
var values = [];
for (var i = row; i < lines; i++) {
values[i] = [];
for (var j = col; j < cols; j++) {
values[i][j] = matrix[i][j];
}
}
return values;
};

this.setValue = function (value) {
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < matrix.length; j++) {
matrix[i][j] = value;
}
}
};
};

SheetMock.getRange = function (row, col, lines, cols) {
var newMatrix = [
[]
];
for (var i = row; i < lines; i++) {
newMatrix[i] = [];
for (var j = col; j < cols; j++) {
newMatrix[i][j];
}
}
return new CellGroupMock(newMatrix);
};
};
};

return {
open: function (file) {
return new SpreadSheetMock();
}
}
};

exports.DriveAppMock = {
DriveApp: DriveApp,
SpreadsheetApp: SpreadsheetApp
};
1 change: 1 addition & 0 deletions test/mock/docsMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var DocApp = {}
24 changes: 24 additions & 0 deletions test/sheetMockTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var mock = require('./mock/DriveAppMock.js').DriveAppMock;
var SpreadsheetApp = mock.SpreadsheetApp();
var DriveApp = mock.DriveApp();
var assert = require('chai').assert;

describe('DriveApp sheet mock test', function () {
describe('copy of file with name in folder', function () {
it('it should return file with specified id', function () {
var file = DriveApp.getFileById('myFileId');
assert.equal('myFileId', file.getId());
});
it('it should return folder with specified id', function () {
var folder = DriveApp.getFolderById('myFolderId');
assert.equal('myFolderId', folder.getId());
});
it('it should return file and folder should have one file', function () {
var folder = DriveApp.getFolderById('myFolderId');
var file = DriveApp.getFileById('myFileId');
var newFile = file.makeCopy('my new file name', folder);
assert.equal('my new file name', newFile.getName());
assert.equal(1, folder.getFiles().length);
});
});
});
64 changes: 7 additions & 57 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,5 @@

var SheetMock = function(matrix) {
var matrix = matrix || [[]];

var CellMock = function() {
var value = "";
this.setValue = function(newValue) {
this.value = newValue;
};

this.getValue = function() {
return value;
};
};

var CellGroupMock = function(matrix) {
var matrix = matrix || [[]];

this.getValues = function() {
var values = [];
for (var i = row; i < lines;i++) {
values[i] = [];
for (var j = col; j < cols; j++) {
values[i][j] = matrix[i][j];
}
}
return values;
};

this.setValue = function(value) {
for (var i = 0; i < matrix.length;i++) {
for (var j = 0; j < matrix.length; j++) {
matrix[i][j] = value;
}
}
};
};

SheetMock.getRange = function(row, col, lines, cols) {
var newMatrix = [[]];
for (var i = row; i < lines;i++) {
newMatrix[i] = [];
for (var j = col; j < cols; j++) {
newMatrix[i][j];
}
}
return new CellGroupMock(newMatrix);
};
};



DriveApp = {};
DriveApp.Access = {};
DriveApp.Permission = {};

var QuickDrive = require('../SheetsTemplater.gs').QuickDrive;
var DriveApp = require('./mock/DriveAppMock.js').DriveAppMock.DriveApp;
var QuickDrive = require('../src/SheetsTemplater.gs').QuickDrive(DriveApp());
var assert = require('chai').assert;

describe('QuickDrive', function () {
Expand Down Expand Up @@ -85,4 +30,9 @@ describe('QuickDrive', function () {
assert.equal(QuickDrive.annotationFunctions.FOR_EACH, QuickDrive.getAnnotationType('{~foo.bar.with.many.properties.myList}'));
});
});
describe('merge configs', function () {
it('it should keep default config', function () {
var config = QuickDrive.confi
});
});
});

0 comments on commit 13b95f8

Please sign in to comment.