From 0302a40e2969d13594dd51d6082ccafce784fa48 Mon Sep 17 00:00:00 2001 From: himetani_cafe Date: Sat, 23 Jan 2016 11:47:35 +0900 Subject: [PATCH] arranged testing frame 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 | 1 + hoge.js | 3 ++ package.json | 3 +- server/class/DB.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++- test/mocha/DB.test.js | 19 +++++++++++-- 5 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 hoge.js diff --git a/.gitignore b/.gitignore index 8eba8ff..0e25ccb 100644 --- a/.gitignore +++ b/.gitignore @@ -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 index 0000000..f00d55f --- /dev/null +++ b/hoge.js @@ -0,0 +1,3 @@ +var fs = require('fs'); + +fs.writeFileSync(__dirname + '/hoge.db'); diff --git a/package.json b/package.json index 87a8bf8..ad2c162 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/class/DB.js b/server/class/DB.js index c8f3efe..590e9f7 100644 --- a/server/class/DB.js +++ b/server/class/DB.js @@ -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; diff --git a/test/mocha/DB.test.js b/test/mocha/DB.test.js index 892d2cc..6a96fee 100644 --- a/test/mocha/DB.test.js +++ b/test/mocha/DB.test.js @@ -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(); + }); + }); })(); -- 2.11.0