1 | var assert = require ('assert'),
|
2 | path = require ('path'),
|
3 | rm = require ('rimraf'),
|
4 | fs = require ('fs');
|
5 |
|
6 | describe ('fs-storage', function(){
|
7 | var temp_folder = path.join(process.cwd(), "test/tmp");
|
8 | var storagePath = path.join(temp_folder, "tmp-storage.json");
|
9 |
|
10 | var fsStorageClass = require ('../lib/job-dependencies/storage/implementations/fs-storage');
|
11 |
|
12 | beforeEach(function(done){
|
13 | rm(temp_folder, done);
|
14 | });
|
15 |
|
16 | afterEach(function(done){
|
17 | rm(temp_folder, done);
|
18 | });
|
19 |
|
20 | it('should set value to storage', function(done){
|
21 | var fsStorage = new fsStorageClass('jobkey1', {storagePath : storagePath});
|
22 | fs.mkdir(temp_folder, function (err, data){
|
23 | fsStorage.set('key1', { foo: 'bar'}, function(error, data){
|
24 | assert.ifError(error);
|
25 | fsStorage.get('key1', function(err, data){
|
26 | assert.ifError(error);
|
27 | assert.equal(data.foo, 'bar');
|
28 | done();
|
29 | });
|
30 | });
|
31 | });
|
32 | });
|
33 |
|
34 | it('should not interfere with other jobs (isolated storage)', function(done){
|
35 | var fsStorage1 = new fsStorageClass('jobkey1', {storagePath : storagePath});
|
36 | var fsStorage2 = new fsStorageClass('jobkey2', {storagePath : storagePath});
|
37 |
|
38 | fs.mkdir(temp_folder, function (err, data){
|
39 |
|
40 | fsStorage1.set('key1', { foo: 'bar'}, function(error, data){
|
41 | assert.ifError(error);
|
42 | fsStorage2.set('key1', { foo: 'foo'}, function(error, data){
|
43 |
|
44 | fsStorage1.get('key1', function(err, data){
|
45 | assert.ifError(error);
|
46 | assert.equal(data.foo, 'bar');
|
47 | fsStorage2.get('key1', function(err, data){
|
48 | assert.equal(data.foo, 'foo');
|
49 | done();
|
50 | });
|
51 | });
|
52 | });
|
53 | });
|
54 | });
|
55 | });
|
56 |
|
57 | it('should be able to select custom storage path', function(done){
|
58 | var newStoragePath = path.join(temp_folder, "tmp-storage2.json");
|
59 | var fsStorage = new fsStorageClass('jobkey1', {storagePath : newStoragePath});
|
60 | fs.mkdir(temp_folder, function (err, data){
|
61 | fsStorage.set('key1', { foo: 'bar'}, function(error, data){
|
62 | assert.ifError(error);
|
63 | assert.ok(fs.existsSync(newStoragePath));
|
64 | done();
|
65 | });
|
66 | });
|
67 | });
|
68 |
|
69 | it('should be able to handle reading from corrupted JSON files', function(done){
|
70 | var corruptedStoragePath = path.join(temp_folder, "tmp-storage2.json");
|
71 | fs.mkdir(temp_folder, function (err, data){
|
72 | fs.writeFile(corruptedStoragePath, "bad json", function(err, data){
|
73 | var fsStorage = new fsStorageClass('jobkey1', {storagePath : corruptedStoragePath});
|
74 | fsStorage.get('key1', function(error, data){
|
75 | assert.ok(error);
|
76 | assert.equal(null, data);
|
77 | done();
|
78 | });
|
79 | });
|
80 | });
|
81 | });
|
82 |
|
83 | it('should be able to handle reading from valid JSON files with wrong structure', function(done){
|
84 | var invalidStorageFilePath = path.join(temp_folder, "tmp-storage.json");
|
85 | var jobkey = 'jobkey1';
|
86 | fs.mkdir(temp_folder, function (err, data){
|
87 | var fileData = "{\"" + jobkey + "\": { \"daaata\" : \"hello\"}}";
|
88 | fs.writeFile(invalidStorageFilePath, fileData, function(err, data){
|
89 | var fsStorage = new fsStorageClass(jobkey, {storagePath : invalidStorageFilePath});
|
90 | fsStorage.get('key1', function(error, data){
|
91 | assert.ok(error);
|
92 | assert.equal(null, data);
|
93 | done();
|
94 | });
|
95 | });
|
96 | });
|
97 | });
|
98 |
|
99 | it('should be able to handle writing to corrupted JSON files', function(done){
|
100 | var corruptedStoragePath = path.join(temp_folder, "tmp-storage2.json");
|
101 | fs.mkdir(temp_folder, function (err, data){
|
102 | fs.writeFile(corruptedStoragePath, "bad json", function(err, data){
|
103 | var fsStorage = new fsStorageClass('jobkey1', {storagePath : corruptedStoragePath});
|
104 | fsStorage.set('key1', 'test', function(error, data){
|
105 | assert.ifError(error);
|
106 | fsStorage.get('key1', function(error, data){
|
107 | assert.equal('test', data);
|
108 | done();
|
109 | });
|
110 | });
|
111 | });
|
112 | });
|
113 | });
|
114 |
|
115 | it('should be able to handle writing to valid JSON files with wrong structure', function(done){
|
116 | var invalidStorageFilePath = path.join(temp_folder, "tmp-storage.json");
|
117 | var jobkey = 'jobkey1';
|
118 | fs.mkdir(temp_folder, function (err, data){
|
119 | var fileData = "{\"" + jobkey + "\": { \"key1\" : { \"daaata\" : \"hello\"}}}";
|
120 | fs.writeFile(invalidStorageFilePath, fileData, function(err, data){
|
121 | var fsStorage = new fsStorageClass(jobkey, {storagePath : invalidStorageFilePath});
|
122 | fsStorage.set('key1', 'test', function(error, data){
|
123 | assert.equal(data.jobkey1.key1.data, 'test');
|
124 | assert.ifError(error);
|
125 | fsStorage.get('key1', function(error, data){
|
126 | assert.equal('test', data);
|
127 | done();
|
128 | });
|
129 | });
|
130 | });
|
131 | });
|
132 | });
|
133 |
|
134 | }); |
\ | No newline at end of file |