Procházet zdrojové kódy

test case + bluebird

TonyKang před 8 roky
rodič
revize
c201a2e855

+ 2 - 0
config/db/db_manager.js

@@ -2,6 +2,8 @@
  * Created by Tony on 2017/3/9.
  */
 var mg = require('mongoose');
+//var Promise = require('bluebird');
+mg.Promise = require('bluebird');
 //mg.connect('mongodb://localhost/Demo');
 module.exports = {
     getConnection : function(server, port, dbName) {

+ 3 - 0
package.json

@@ -14,5 +14,8 @@
     "express-session": "^1.15.1",
     "request": "^2.79.0",
     "tape": "^4.6.3"
+  },
+  "dependencies": {
+    "bluebird": "^3.5.0"
   }
 }

+ 16 - 5
public/cache/cacheUtil.js

@@ -4,11 +4,22 @@
 var cache = {};
 
 module.exports = {
-    setCache: function(cacheKey, cacheValue) {
-        cache[cacheKey] = cacheValue;
-        return true;
+    setCache: function(groupKey, cacheKey, cacheValue) {
+        if (groupKey && cacheKey) {
+            if (!(cache[groupKey])) {
+                cache[groupKey] = {};
+            }
+            cache[groupKey][cacheKey] = cacheValue;
+            return true;
+        } else {
+            return false;
+        }
     },
-    getCache: function(cacheKey) {
-        return cache[cacheKey];
+    getCache: function(groupKey, cacheKey) {
+        var rst = null;
+        if (cache[groupKey]) {
+            rst = cache[groupKey][cacheKey];
+        }
+        return rst;
     }
 }

+ 55 - 0
public/counter/counter.js

@@ -0,0 +1,55 @@
+/**
+ * Created by Tony on 2017/3/21.
+ */
+var mongoose = require('mongoose');
+var dbm = require("../../config/db/db_manager");
+var projectdb = dbm.getCfgConnection("scConstruct");
+
+var Schema = mongoose.Schema;
+var counterSchema = new Schema({
+    _id: String,
+    sequence_value: Number
+});
+counterSchema.statics.findAndModify = function (query, sort, doc, options, callback) {
+    return this.collection.findAndModify(query, sort, doc, options, callback);
+};
+var counterModel = projectdb.model("counters", counterSchema);
+
+const PROJECT_COUNTER = 'projects', USER_COUNTER = 'users', BILL_COUNTER = 'bills', RATION_COUNTER = 'rations',
+    REPORT_COUNTER = 'rptTemplates', FEE_COUNTER = 'fees'
+
+var counterDAO = function(){};
+
+counterDAO.prototype.getIDAfterCount = function(moduleName, stepCount, callback) {
+    //counterModel.findAndModify({_id: moduleName}, [], { $inc: { sequence_value: stepCount } }, {'new':true},
+    //    function (err, result) {
+    //        if (err) return err
+    //        else {
+    //            console.log('result: ' + result.value.sequence_value);
+    //            return result.value.sequence_value;
+    //        }
+    //    }
+    //);
+    //var rst = counterModel.findOne({_id: moduleName}).exec()
+    //    .then(function(result, err) {
+    //        console.log('thenable');
+    //        return result;
+    //    }
+    //);
+    var rst = counterModel.findAndModify({_id: moduleName}, [], { $inc: { sequence_value: stepCount } }, {'new':true}, callback);
+    //projectdb.close();
+    return rst;
+}
+
+counterDAO.prototype.getID = function(moduleName) {
+    var rst = counterModel.findOne({_id: moduleName}).exec()
+        .then(function(result, err) {
+            console.log('thenable');
+            //projectdb.close();
+            return result;
+        }
+    );
+    return rst;
+}
+
+module.exports = new counterDAO();

+ 4 - 2
test/unit/cache/testCacheUsage.js

@@ -5,7 +5,9 @@ var test = require('tape');
 var cache = require('../../../public/cache/cacheUtil');
 
 test('test cache usage:', function(t) {
-    cache.setCache("unit_TestKey","hello my cache!");
-    t.equal(cache.getCache("unit_TestKey"), "hello my cache!")
+    cache.setCache("unit_Test_Grp","unit_TestKey","hello my cache!");
+    var msg = cache.getCache("unit_Test_Grp","unit_TestKey");
+    console.log(msg);
+    t.equal(msg, "hello my cache!")
     t.end();
 })

+ 29 - 0
test/unit/counter/testCounter.js

@@ -0,0 +1,29 @@
+/**
+ * Created by Tony on 2017/3/21.
+ */
+var test = require('tape');
+var counter = require('../../../public/counter/counter');
+var mongoose = require('mongoose');
+
+test('test counter\'s usage:', function(t) {
+    var promise = counter.getID("rptTemplates",1);
+    promise.then(function(rst){
+        console.log('result: ' + rst.sequence_value);
+        t.equal(rst.sequence_value, 6);
+        t.end();
+    });
+})
+
+test('test counter\'s usage 2:', function(t) {
+    counter.getIDAfterCount("rptTemplates",1, function(err, result){
+        console.log('result: ' + result.value.sequence_value);
+        t.equal(result.value.sequence_value, 7);
+        t.end();
+    });
+})
+
+test('finish', function (t) {
+    mongoose.disconnect();
+    t.pass('closing db connection');
+    t.end();
+});

+ 7 - 0
test/unit/reports/testRpt.js

@@ -5,6 +5,7 @@ var test = require('tape');
 var cmn_ctrl = require('../../../modules/reports/models/cfg_control');
 var cmn_font = require('../../../modules/reports/models/cfg_font');
 var cmn_style = require('../../../modules/reports/models/cfg_style');
+var mongoose = require('mongoose');
 
 test('test get report pages function: ', function (t) {
     /*/
@@ -43,4 +44,10 @@ test('test get report pages function: ', function (t) {
         return true;
     });
     //*/
+});
+
+test('finish', function (t) {
+    mongoose.disconnect();
+    t.pass('closing db connection');
+    t.end();
 });