| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- // Copyright 2015 The MemDB Authors.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- // implied. See the License for the specific language governing
- // permissions and limitations under the License. See the AUTHORS file
- // for names of contributors.
- 'use strict';
- var P = require('bluebird');
- var util = require('util');
- var should = require('should');
- var env = require('../env');
- var memdb = require('../../lib');
- var logger = require('memdb-logger').getLogger('test', __filename);
- describe('mdbgoose test', function(){
- beforeEach(env.flushdb);
- it('mdbgoose', function(cb){
- var mdbgoose = memdb.goose;
- delete mdbgoose.connection.models.player;
- var Player = mdbgoose.model('player', new mdbgoose.Schema({
- _id : String,
- areaId : String,
- name : String,
- fullname : {first: String, second: String},
- items : [mdbgoose.SchemaTypes.Mixed],
- extra : mdbgoose.SchemaTypes.Mixed,
- }, {collection : 'player', versionKey: false}));
- return P.try(function(){
- return env.startCluster('s1', function(config){
- config.persistentDelay = 0;
- });
- })
- .then(function(ret){
- return mdbgoose.connectAsync(env.config);
- })
- .then(function(){
- return mdbgoose.transaction(function(){
- var player1 = new Player({
- _id : 'p1',
- areaId: 'a2',
- name: 'rain',
- fullname: {first: 'first', second: 'second'},
- items : [{name : 'item1', type : 1}],
- extra: {xx: 'extra val'},
- });
- var player2 = new Player({
- _id : 'p2',
- areaId: 'a1',
- name: 'snow',
- fullname: {first: 'first', second: 'second'},
- items : [{name : 'item1', type : 1}],
- extra: {},
- });
- return P.try(function(){
- return player1.saveAsync();
- })
- .then(function(){
- return Player.findAsync({_id : 'p1'})
- .then(function(players){
- players.length.should.eql(1);
- players[0]._id.should.eql('p1');
- });
- })
- .then(function(){
- return Player.find({_id : 'p1'})
- .select('name')
- .execAsync()
- .then(function(players){
- players[0].name.should.eql('rain');
- });
- })
- .then(function(){
- return Player.findOneAsync({_id : 'p1'})
- .then(function(player){
- player._id.should.eql('p1');
- });
- })
- .then(function(){
- return Player.findByIdAsync('p1')
- .then(function(player){
- player._id.should.eql('p1');
- });
- })
- .then(function(){
- return Player.findReadOnly({_id : 'p1'})
- .then(function(players){
- players.length.should.eql(1);
- players[0]._id.should.eql('p1');
- });
- })
- .then(function(){
- return Player.findOneReadOnlyAsync({_id : 'p1'})
- .then(function(player){
- player._id.should.eql('p1');
- });
- })
- .then(function(){
- return Player.findByIdReadOnlyAsync('p1')
- .then(function(player){
- player._id.should.eql('p1');
- });
- })
- .then(function(){
- return Player.findByIdAsync('p1');
- })
- .then(function(player){
- player.name = 'snow';
- player.areaId = 'a1';
- player.fullname.first = 'changed first';
- player.fullname.second = 'changed second';
- player.items.push({name : 'item2', type : 2});
- player.extra = {xx1 : 'changed extra'};
- return player.saveAsync();
- })
- .then(function(){
- return P.try(function(){
- return Player.findByIdAsync('p1');
- })
- .then(function(player){
- logger.debug('%j', player);
- player.name.should.eql('snow');
- player.items.length.should.eql(2);
- });
- })
- .then(function(){
- return player2.saveAsync();
- })
- .then(function(){
- return P.try(function(){
- return Player.findAsync({areaId : 'a1'});
- })
- .then(function(players){
- logger.debug('%j', players);
- players.length.should.eql(2);
- players.forEach(function(player){
- player.areaId.should.eql('a1');
- });
- });
- })
- .then(function(){
- return P.try(function(){
- return Player.countAsync({areaId : 'a1'});
- })
- .then(function(count){
- count.should.eql(2);
- });
- })
- .then(function(){
- return P.try(function(){
- return Player.findAsync({areaId : 'a1'}, null, {limit : 1});
- })
- .then(function(players){
- logger.debug('%j', players);
- players.length.should.eql(1);
- });
- })
- .then(function(){
- return Player.findByIdReadOnlyAsync('p1')
- .then(function(ret){
- logger.debug('%j', ret);
- });
- });
- }, 's1');
- })
- .delay(1000)
- .then(function(){
- // Call mongodb directly
- return Player.findMongoAsync();
- })
- .then(function(players){
- logger.debug('%j', players);
- players.length.should.eql(2);
- // players[0].name = 'changed';
- // return players[0].saveAsync()
- // .catch(function(e){
- // logger.error(e.stack); // should throw error
- // });
- })
- .then(function(){
- return Player.countMongoAsync({}); //TODO: callback not called when not pass any arguments, bluebird's bug?
- })
- .then(function(ret){
- ret.should.eql(2);
- })
- .then(function(){
- return mdbgoose.disconnectAsync();
- })
- .finally(function(){
- return env.stopCluster();
- })
- .nodeify(cb);
- });
- it('gen collection config', function(cb){
- var mdbgoose = memdb.goose;
- var Schema = mdbgoose.Schema;
- var types = mdbgoose.SchemaTypes;
- delete mdbgoose.connection.models.player;
- var DummySchema = new Schema({
- _id : String,
- name : String,
- first : {type : String, indexIgnore : ['']},
- last : {type : String, indexIgnore : ['']},
- groupId : {type : String, index : true, indexIgnore : [-1]},
- uniqKey : {type : String, unique : true},
- uniqKey2 : {type : String, index : {unique : true}},
- }, {collection : 'dummy'});
- DummySchema.index({first : 1, last : 1}, {unique : true});
- mdbgoose.model('Dummy', DummySchema);
- var config = mdbgoose.genCollectionConfig();
- var expected = {
- dummy: {
- indexes: [
- {
- keys: ['groupId'],
- valueIgnore: {
- groupId: [-1]
- }
- },
- {
- keys: ['uniqKey'],
- unique: true,
- valueIgnore: {
- uniqKey: []
- }
- },
- {
- keys: ['uniqKey2'],
- unique: true,
- valueIgnore: {
- uniqKey2: []
- }
- }, {
- keys: ['first', 'last'],
- valueIgnore: {
- first: [''],
- last: ['']
- },
- unique: true
- }]
- }
- };
- config.dummy.should.eql(expected.dummy);
- cb();
- });
- });
|