Best JavaScript code snippet using best
storage-test.js
Source:storage-test.js
1/* globals before, after, describe, it */2'use strict';3var assert = require('assert'),4 fs = require('fs'),5 path = require('path'),6 os = require('os');7var mocha = require('mocha'),8 Promise = require('promise'),9 uuid = require('uuid'),10 tempWrite = require('temp-write'),11 async = require('async');12var rimraf = Promise.denodeify(require('rimraf')),13 mkdirp = Promise.denodeify(require('mkdirp'));14var ROOT_TEST_FOLDER = 'node-fu-test';15var Storage = require('../../lib/traditional/storage');16function mktmp_file(size, name){17 return new Promise(function(resolve, reject){18 var content = '';19 for (var i = 0; i < size; i++){20 content = content + '0';21 }22 tempWrite(content, name, function(err, path){23 if (err) return reject(err);24 return resolve(path);25 });26 });27}28function mktmp_files(n, name, size){29 var size = size || 100;30 return new Promise(function(resolve, reject){31 function success(path){32 files.push(path);33 }34 function error(err){35 reject(err);36 }37 async.timesSeries(n, function(i, next){38 mktmp_file(size, name+'_'+i+'.dat').then(function(path){39 next(null, path);40 }, function(err){41 reject(err);42 })43 }, function(err, files){44 resolve(files);45 });46 });47}48function mktmp_dir(name){49 name = name || uuid.v4();50 return path.join(os.tmpdir(), name);51}52function MockChunk(uuid, name, index, path, totalChunks){53 var chunk_mock = { file: {} };54 chunk_mock.uuid = uuid;55 chunk_mock.name = name;56 chunk_mock.chunkIndex = index;57 chunk_mock.file.path = path;58 chunk_mock.totalChunks = totalChunks;59 return chunk_mock;60}61describe('traditional - storage', function(){62 var tmpRoot, chunksPath, uploadsPath, storage;63 beforeEach(function(){64 tmpRoot = mktmp_dir(ROOT_TEST_FOLDER);65 chunksPath = path.join(tmpRoot, 'chunks'),66 uploadsPath = path.join(tmpRoot, 'uploads');67 storage = new Storage(chunksPath, uploadsPath);68 });69 afterEach(function(){70 rimraf(tmpRoot);71 });72 function store_chunks(num_chunks, size){73 return new Promise(function(resolve, reject){74 mktmp_files(num_chunks, 'many-foochunks.dat', size).then(function(chunks){75 var totalChunks = chunks.length,76 chunkUuid = uuid.v4();77 var i = 0,78 newPath, chunk_mock;79 // note that this is an `eachSeries`80 async.eachSeries(chunks, function(chunk, done) {81 chunk_mock = MockChunk(chunkUuid, 'foo.dat', i, chunk,82 totalChunks);83 i++;84 storage.store_chunk(chunk_mock).then(function(p){85 newPath = p;86 done();87 }, done);88 }, function(err) {89 if (err) {90 return reject(err);91 }92 return resolve({93 newPath: newPath,94 chunk_mock: chunk_mock95 });96 });97 });98 });99 }100 it('#store_file', function(done){101 var file_mock = { file: {} };102 file_mock.uuid = uuid.v4();103 file_mock.name = 'foo.dat';104 mktmp_file(100, 'foo.dat', 100).then(function(path){105 file_mock.file.path = path;106 return storage.store_file(file_mock).then(function(newPath){107 var stats = fs.statSync(newPath);108 if (stats.isFile()){109 assert.ok(true);110 } else {111 assert(false);112 }113 done();114 });115 });116 });117 it('#store_chunk', function(){118 //var chunks = mktmp_files(100, 'foo.dat', 100),119 mktmp_file(100, 'foo.dat').then(function(chunk){120 var chunkUuid = uuid.v4();121 var chunk_mock = { file: {} };122 chunk_mock.uuid = chunkUuid;123 chunk_mock.name = 'foochunk.dat';124 chunk_mock.chunkIndex = 0;125 chunk_mock.file.path = chunk;126 chunk_mock.totalChunks = 1;127 storage.store_chunk(chunk_mock).then(function(newPath){128 assert(newPath.indexOf(chunksPath) !== -1, "Chunks not in to chunks path");129 var chunksPath = path.join(chunksPath, chunkUuid);130 var chunks = fs.readdirSync(chunksPath);131 assert(chunks.length === 1, "Missing chunks!");132 done();133 }, function(e) {134 assert(false, "Error storing chunk!");135 done(e);136 });137 });138 });139 describe("#store_chunks (many)", function(){140 this.timeout(10000);141 it("100x10B", function(done) {142 var numChunks = 100,143 chunkSize = 10;144 store_chunks(numChunks, chunkSize).then(function(data){145 var newPath = data.newPath,146 chunk_mock = data.chunk_mock;147 var testChunksPath = path.join(chunksPath, chunk_mock.uuid),148 chunkList = fs.readdirSync(testChunksPath);149 console.log('done?');150 done();151 assert(chunkList.length == numChunks, "Missing " + (totalChunks - chunkList.length) + " chunks!");152 //console.log('done?');153 //done();154 }, function(e) {155 assert(false, "Error storing chunk!");156 done(e);157 }).fail;158 });159 it("10000x1B", function(done) {160 var numChunks = 10000,161 chunkSize = 1;162 store_chunks(numChunks, chunkSize, function(data) {163 var newPath = data.newPath,164 chunk_mock = data.chunk_mock;165 var testChunksPath = path.join(chunksPath, chunk_mock.uuid),166 chunkList = fs.readdirSync(testChunksPath);167 console.log(chunkList.length);168 console.log(numChunks - 1);169 console.log("done?");170 done();171 assert(chunkList.length === numChunks - 1, "Missing " + (totalChunks - chunkList.length) + " chunks!");172 //console.log("done?");173 //done();174 }, function(e) {175 assert(false, "Error storing chunk!");176 done(e);177 });178 });179 });180 describe('#assemble_chunks', function(){181 it.skip("100x100B", function(done){182 store_chunks(100, 100, function(data){183 var newPath = data.newPath,184 chunk_mock = data.chunk_mock;185 console.log(chunk_mock);186 storage.assemble_chunks(chunk_mock).then(function(){187 var uploadedFile = path.join(uploadsPath, chunk_mock.uuid,188 chunk_mock.name);189 var uploadedFileStats = fs.statSync(testUploadsPath);190 console.log(uploadedFileStats);191 assert(uploadedFileStats.size === 100*100,192 "Incorrect assembled chunk size");193 done();194 }, function(e) {195 assert(false, "Error assembling chunks");196 done(e);197 });198 }, function(e) {199 assert(false, "Error storing chunks");200 done(e);201 });202 });203 });204 it.skip('#delete_file', function(){205 });206 it.skip('#verify_upload', function(){207 });...
store-files-option.test.js
Source:store-files-option.test.js
1'use strict';2const fs = require('fs');3const os = require('os');4const http = require('http');5const path = require('path');6const assert = require('assert');7const formidable = require('../../src/index');8const PORT = 13537;9const DEFAULT_UPLOAD_DIR = path.join(10 os.tmpdir(),11 'test-store-files-option-default',12);13const CUSTOM_UPLOAD_FILE_PATH = path.join(DEFAULT_UPLOAD_DIR, 'test-file');14const testFilePath = path.join(15 path.dirname(__dirname),16 'fixture',17 'file',18 'binaryfile.tar.gz',19);20test('store files option', (done) => {21 const server = http.createServer((req, res) => {22 if (!fs.existsSync(DEFAULT_UPLOAD_DIR)) {23 fs.mkdirSync(DEFAULT_UPLOAD_DIR);24 }25 const form = formidable({26 uploadDir: DEFAULT_UPLOAD_DIR,27 fileWriteStreamHandler: () => new fs.WriteStream(CUSTOM_UPLOAD_FILE_PATH),28 });29 form.parse(req, (err, fields, files) => {30 assert.strictEqual(Object.keys(files).length, 1);31 const { file } = files;32 assert.strictEqual(file.size, 301);33 assert.strictEqual(typeof file.filepath, 'string');34 const uploadedFileStats = fs.statSync(CUSTOM_UPLOAD_FILE_PATH);35 assert.ok(uploadedFileStats.size === file.size);36 fs.unlinkSync(CUSTOM_UPLOAD_FILE_PATH);37 res.end();38 server.close();39 done();40 });41 });42 server.listen(PORT, (err) => {43 assert(!err, 'should not have error, but be falsey');44 const request = http.request({45 port: PORT,46 method: 'POST',47 headers: {48 'Content-Type': 'application/octet-stream',49 },50 });51 fs.createReadStream(testFilePath).pipe(request);52 });...
Using AI Code Generation
1var fs = require('fs');2var path = require('path');3var bp = require('bestpractice');4var app = bp.app();5var file = fs.createReadStream(path.join(__dirname, 'test4.js'));6var stats = app.uploadedFileStats(file);7console.log(stats);8app.uploadFile(file, function(err, result) {9 if (err) {10 console.log('Error: ' + err);11 }12 console.log(result);13});14var fs = require('fs');15var path = require('path');16var bp = require('bestpractice');17var app = bp.app();18var file = fs.createReadStream(path.join(__dirname, 'test5.js'));19var stats = app.uploadedFileStats(file);20console.log(stats);21app.uploadFile(file, function(err, result) {22 if (err) {23 console.log('Error: ' + err);24 }25 console.log(result);26});27var fs = require('fs');28var path = require('path');29var bp = require('bestpractice');30var app = bp.app();31var file = fs.createReadStream(path.join(__dirname, 'test6.js'));32var stats = app.uploadedFileStats(file);33console.log(stats);34app.uploadFile(file, function(err, result) {35 if (err) {36 console.log('Error: ' + err);37 }38 console.log(result);39});40var fs = require('fs');41var path = require('path');42var bp = require('bestpractice');43var app = bp.app();44var file = fs.createReadStream(path.join(__dirname, 'test7.js'));45var stats = app.uploadedFileStats(file);46console.log(stats);47app.uploadFile(file, function(err, result) {48 if (err) {
Using AI Code Generation
1var BestPractice = require('./best-practice');2var bestPractice = new BestPractice();3var stats = bestPractice.uploadedFileStats();4console.log(stats);5var BestPractice = require('./best-practice');6var bestPractice = new BestPractice();7var stats = bestPractice.uploadedFileStats();8response.writeHead(200, {"Content-Type": "text/plain"});9response.write("The stats of the file uploaded by the user is: " + JSON.stringify(stats));10response.end();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!