Skip to content

Commit

Permalink
add system tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Apr 15, 2015
1 parent 1108321 commit f871220
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 21 deletions.
19 changes: 17 additions & 2 deletions lib/storage/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ Bucket.prototype.file = function(name, options) {
* return.
* @param {string} query.pageToken - A previously-returned page token
* representing part of the larger set of results to view.
* @param {bool} query.versions - If true, returns File objects scoped to their
* versions.
* @param {function} callback - The callback function.
*
* @example
Expand All @@ -368,25 +370,38 @@ Bucket.prototype.file = function(name, options) {
* }, function(err, files, nextQuery) {});
*/
Bucket.prototype.getFiles = function(query, callback) {
var that = this;
var self = this;

if (!callback) {
callback = query;
query = {};
}

this.makeReq_('GET', '/o', query, true, function(err, resp) {
if (err) {
callback(err);
return;
}

var files = (resp.items || []).map(function(item) {
var file = that.file(item.name);
var options = {};

if (query.versions) {
options.generation = item.generation;
}

var file = self.file(item.name, options);
file.metadata = item;

return file;
});

var nextQuery = null;

if (resp.nextPageToken) {
nextQuery = extend({}, query, { pageToken: resp.nextPageToken });
}

callback(null, files, nextQuery);
});
};
Expand Down
124 changes: 105 additions & 19 deletions regression/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

/*global describe, it, before, after, beforeEach, afterEach */

'use strict';

var assert = require('assert');
Expand Down Expand Up @@ -44,6 +42,17 @@ var files = {
}
};

function deleteAllFiles(bucket, callback) {
bucket.getFiles({ versions: true }, function(err, files) {
if (err) {
callback(err);
return;
}

async.each(files, deleteFile, callback);
});
}

function deleteFiles(bucket, callback) {
bucket.getFiles(function(err, files) {
if (err) {
Expand All @@ -58,6 +67,13 @@ function deleteFile(file, callback) {
file.delete(callback);
}

function writeToFile(file, contents, callback) {
var writeStream = file.createWriteStream();
writeStream.once('error', callback);
writeStream.once('complete', callback.bind(null, null));
writeStream.end(contents);
}

function generateBucketName() {
return 'gcloud-test-bucket-temp-' + uuid.v1();
}
Expand Down Expand Up @@ -214,12 +230,7 @@ describe('storage', function() {
});

function createFileWithContent(content, callback) {
bucket.file(uuid() + '.txt').createWriteStream()
.on('error', callback)
.on('complete', function() {
callback();
})
.end(content);
writeToFile(bucket.file(uuid() + '.txt'), content, callback);
}

function isFilePublic(file, callback) {
Expand Down Expand Up @@ -274,12 +285,7 @@ describe('storage', function() {
});

function createFileWithContent(content, callback) {
bucket.file(uuid() + '.txt').createWriteStream()
.on('error', callback)
.on('complete', function() {
callback();
})
.end(content);
writeToFile(bucket.file(uuid() + '.txt'), content, callback);
}

function isFilePrivate(file, callback) {
Expand Down Expand Up @@ -670,11 +676,8 @@ describe('storage', function() {
});
});

function createFile(fileObject, cb) {
var ws = fileObject.file.createWriteStream();
ws.on('error', cb);
ws.on('complete', cb.bind(null, null));
ws.end(fileObject.contents);
function createFile(fileObject, callback) {
writeToFile(fileObject.file, fileObject.contents, callback);
}
});
});
Expand Down Expand Up @@ -732,6 +735,89 @@ describe('storage', function() {
});
});

describe('file generations', function() {
var VERSIONED_BUCKET_NAME = generateBucketName();
var versionedBucket;

before(function(done) {
var opts = { versioning: { enabled: true } };

storage.createBucket(VERSIONED_BUCKET_NAME, opts, function(err, bucket) {
assert.ifError(err);
versionedBucket = bucket;
done();
});
});

afterEach(function(done) {
deleteAllFiles(versionedBucket, done);
});

after(function(done) {
versionedBucket.delete(done);
});

it('should overwrite file, then get older version', function(done) {
var VERSIONED_FILE_NAME = Date.now();
var versionedFile = versionedBucket.file(VERSIONED_FILE_NAME);

writeToFile(versionedFile, 'a', function(err) {
assert.ifError(err);

versionedFile.getMetadata(function(err, metadata) {
assert.ifError(err);

var initialGeneration = metadata.generation;

writeToFile(versionedFile, 'b', function(err) {
assert.ifError(err);

var firstGenFile = versionedBucket.file(VERSIONED_FILE_NAME, {
generation: initialGeneration
});

firstGenFile.download(function(err, contents) {
assert.ifError(err);
assert.equal(contents, 'a');
done();
});
});
});
});

});

it('should get all files scoped to their version', function(done) {
var filesToCreate = [
{ file: versionedBucket.file('file-one.txt'), contents: '123' },
{ file: versionedBucket.file('file-one.txt'), contents: '456' }
];

async.each(filesToCreate, createFile, function(err) {
assert.ifError(err);

versionedBucket.getFiles({ versions: true }, function(err, files) {
assert.ifError(err);

// same file.
assert.equal(files[0].name, files[1].name);

// different generations.
assert.notEqual(
files[0].metadata.generation,
files[1].metadata.generation
);

done();
});
});

function createFile(fileObject, callback) {
writeToFile(fileObject.file, fileObject.contents, callback);
}
});
});

describe('sign urls', function() {
var localFile = fs.readFileSync(files.logo.path);
var file;
Expand Down

0 comments on commit f871220

Please sign in to comment.