OSDN Git Service

arranged testing frame
authorhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Sat, 23 Jan 2016 02:47:35 +0000 (11:47 +0900)
committerhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Sat, 23 Jan 2016 02:47:35 +0000 (11:47 +0900)
 Please enter the commit message for your changes. Lines starting
 with '' will be ignored, and an empty message aborts the commit.
 On branch master
 Changes to be committed:
modified:   .gitignore
new file:   hoge.js
modified:   package.json
modified:   server/class/DB.js
modified:   test/mocha/DB.test.js

.gitignore
hoge.js [new file with mode: 0644]
package.json
server/class/DB.js
test/mocha/DB.test.js

index 8eba8ff..0e25ccb 100644 (file)
@@ -13,3 +13,4 @@ cli/npm-debug.log*
 user-specific-files/OptionControlFile/
 user-specific-files/meta/
 user-specific-files/workspace/
+user-specific-files/db/
diff --git a/hoge.js b/hoge.js
new file mode 100644 (file)
index 0000000..f00d55f
--- /dev/null
+++ b/hoge.js
@@ -0,0 +1,3 @@
+var fs = require('fs');
+
+fs.writeFileSync(__dirname + '/hoge.db');
index 87a8bf8..ad2c162 100644 (file)
@@ -4,7 +4,7 @@
   "description": "",
   "main": "./server/app.js",
   "scripts": {
-    "test": "mocha test/mocha/*"
+    "test": "mocha test/mocha/DB.test.js"
   },
   "author": "",
   "license": "ISC",
@@ -16,6 +16,7 @@
     "body-parser": "^1.14.2",
     "bootstrap-sass": "^3.3.6",
     "chalk": "^1.1.1",
+    "co": "^4.6.0",
     "commander": "^2.9.0",
     "express": "^4.13.3",
     "sequelize": "^3.18.0",
index c8f3efe..590e9f7 100644 (file)
@@ -1,9 +1,86 @@
-
 /**
  * DBを操作するクラス
  *
  * @returns {object}
  */
 function DB() {
+    'use strict';
+
+    var fs = require('fs');
+    var Sequelize = require('sequelize');
+    var co = require('co');
+
+    var sequelize;
+
+    var dbOption = {
+        dialect: 'sqlite'
+    };
+
+    function sync() {
+        return new Promise(function(resolve) {
+            Promise.all([Directory.sync(), File.sync()])
+            .then(function() {
+                var root = {
+                    name: 'root',
+                    parent: ''
+                };
+                return Directory.insert(root);
+            })
+            .then(function() {
+                return {};
+            });
+        });
+    }
+
+    if(process.env['NODE_ENV'] === 'production') {
+        var dbPath = __dirname + '/../../user-specific-files/db/db.workspace';
+        dbOption.storage = dbPath;
 
+        // if doesn't exist workspace.db, create.
+        try {
+            fs.accessSync(dbPath, fs.R_OK | fs.W_OK);
+        } catch(e) {
+            fs.writeFileSync(dbPath, '');
+        }
+    }
+
+    sequelize = new Sequelize('','','', dbOption);
+
+    var File = sequelize.define('file', {
+        fileId: {
+            type: Sequelize.UUID,
+            field: 'file_id',
+            primaryKey: true
+        },
+        name: {
+            type: Sequelize.STRING,
+            field: 'name',
+            allowNull: false
+        },
+        directoryName: {
+            type: Sequelize.STRING,
+            field: 'directory_name',
+            allowNull: false
+        }
+    });
+
+    var Directory = sequelize.define('directory', {
+        name: {
+            type: Sequelize.STRING,
+            field: 'name',
+            primaryKey: true
+        },
+        parent: {
+            type: Sequelize.STRING,
+            field: 'parent',
+            allowNull: false
+        }
+    });
+
+    Directory.hasMany(File, {foreignKey: 'directoryName'});
+    File.belongsTo(Directory, {foreignKey: 'name'});
+
+    return sync();
 }
+
+module.exports = DB;
index 892d2cc..6a96fee 100644 (file)
@@ -3,7 +3,20 @@
     var DB = require('../../server/class/DB');
     var expect = require('chai').expect;
 
-   describe('DB クラス', function() {
-
-    })
+    //process.env['NODE_ENV'] = 'production';
+    describe('DB クラス', function() {
+        var db;
+        var test;
+        before(function(done) {
+            var promise = new DB();
+            promise.then(function(r) {
+                db = r;
+                done();
+            });
+        });
+        it('test of test', function(done) {
+            db.sayHello();
+            done();
+        });
+    });
 })();