[Groonga-commit] groonga/gcs [master] Add test for file storage

Back to archive index

YUKI Hiroshi null+****@clear*****
Fri Dec 7 15:04:45 JST 2012


YUKI Hiroshi	2012-12-07 15:04:45 +0900 (Fri, 07 Dec 2012)

  New Revision: 0f03dd35605cba2dc618288d8a9c4516649e15aa
  https://github.com/groonga/gcs/commit/0f03dd35605cba2dc618288d8a9c4516649e15aa

  Log:
    Add test for file storage

  Added files:
    test/database-storage.js

  Added: test/database-storage.js (+117 -0) 100644
===================================================================
--- /dev/null
+++ test/database-storage.js    2012-12-07 15:04:45 +0900 (b3a09c4)
@@ -0,0 +1,117 @@
+var utils = require('./test-utils');
+
+var assert = require('chai').assert;
+var path = require('path');
+var crypto = require('crypto');
+
+var FileStorage = require('../lib/database/storage').FileStorage;
+
+function sha1hash(string) {
+  var shasum = crypto.createHash('sha1');
+  shasum.update(string);
+  return shasum.digest('hex');
+}
+
+function createNewDocument() {
+  return {
+    id: Date.now(),
+    date: (new Date()).toString()
+  };
+}
+
+function assertFilesCount(directoryPath, delta, task) {
+  var files = fs.readdirSync(directoryPath);
+  var beforeCount = files.length;
+  task();
+  files = fs.readdirSync(directoryPath);
+  var afterCount = files.length;
+  assert.equal(afterCount - beforeCount, delta);
+}
+
+suite('database', function() {
+  suite('FileStorage', function() {
+    var storage;
+    setup(function() {
+      storage = new FileStorage({ name:     'storage',
+                                  basePath: utils.temporaryDirectory });
+    });
+
+    teardown(function() {
+      storage.clearSync();
+      storage = undefined;
+    });
+
+    test('saveSync for new document', function() {
+      var filePath = path.join(utils.temporaryDirectory, 'storage', sha1hash(document.id));
+      assert.isFalse(path.existsSync(filePath));
+
+      var document = createNewDocument();
+      assertFilesCount(storage.directoryPath, 1, function() {
+        storage.saveSync(document);
+      });
+
+      assert.isTrue(path.existsSync(filePath));
+
+      var contents = fs.readFileSync(filePath, 'UTF-8');
+      contents = JSON.parse(contents);
+      assert.deepEqual(contents, document);
+    });
+
+    test('saveSync for existing document', function() {
+      var document = createNewDocument();
+      storage.saveSync(document);
+
+      var updatedDocument = createNewDocument();
+      updatedDocument.id = document.id;
+      assertFilesCount(storage.directoryPath, 0, function() {
+        storage.saveSync(updatedDocument);
+      });
+
+      var contents = fs.readFileSync(filePath, 'UTF-8');
+      contents = JSON.parse(contents);
+      assert.deepEqual(contents, updatedDocument);
+    });
+
+    test('readSync for existing document', function() {
+      var document = createNewDocument();
+      storage.saveSync(document);
+
+      var readDocument = storage.readSync(document.id);
+      assert.deepEqual(readDocument, document);
+    });
+
+    test('readSync for unknown document', function() {
+      var readDocument = storage.readSync('unknown ' + Date.now());
+      assert.equal(readDocument, null);
+    });
+
+    test('deleteSync for existing document', function() {
+      var document = createNewDocument();
+      storage.saveSync(document);
+
+      var filePath = path.join(utils.temporaryDirectory, 'storage', sha1hash(document.id));
+      assert.isTrue(path.existsSync(filePath));
+
+      assertFilesCount(storage.directoryPath, -1, function() {
+        storage.deleteSync(document.id);
+        assert.isFalse(path.existsSync(filePath));
+      });
+    });
+
+    test('deleteSync for unknown document', function() {
+      assertFilesCount(storage.directoryPath, 0, function() {
+        storage.deleteSync('unknown ' + Date.now());
+      });
+    });
+
+    test('clearSync', function() {
+      assertFilesCount(storage.directoryPath, 3, function() {
+        storage.saveSync(createNewDocument());
+        storage.saveSync(createNewDocument());
+        storage.saveSync(createNewDocument());
+      });
+      storage.clearSync();
+      assert.isFalse(path.existsSync(storage.directoryPath));
+    });
+  });
+});
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index