Created
August 23, 2012 00:39
-
-
Save aheckmann/3430891 to your computer and use it in GitHub Desktop.
no luck reproducing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var assert = require('assert') | |
console.log('\n==========='); | |
console.log(' mongoose version: %s', mongoose.version); | |
console.log('========\n\n'); | |
mongoose.connect('localhost', 'testing_nestedpushbug'); | |
mongoose.connection.on('error', function () { | |
console.error('connection error', arguments); | |
}); | |
var ActionSchema = new Schema({ | |
event: String, | |
properties: {}, | |
time: Number | |
}); | |
var TurnSchema = new Schema({ | |
player: String, | |
actions: [ActionSchema], | |
tile: [] // ADDED BY aheckmann! | |
}); | |
var PlayerSchema = new Schema({ | |
name: String | |
}); | |
var GameSchema = new Schema({ | |
players: [PlayerSchema], | |
tilesets: [String], | |
tiles: [{ name: String }], | |
total: Number, | |
turns: [TurnSchema], | |
status: { type: String, default: 'pending' }, | |
created: Number | |
}); | |
//var User = mongoose.model('User', UserSchema), | |
var Game = mongoose.model('Game', GameSchema), | |
Player = mongoose.model('Player', PlayerSchema), | |
//Tile = mongoose.model('Tile', TileSchema), | |
Action = mongoose.model('Action', ActionSchema), | |
Turn = mongoose.model('Turn', TurnSchema); | |
mongoose.set('debug', true); | |
mongoose.connection.on('open', function () { | |
Game.create({ turns: [{ player: 'me' }], tiles: [{ name:'a tile'}] }, function (err, game) { | |
if (err) return done(err); | |
Game.findOne({ _id: game.id }, function (err, doc) { | |
if (err) return done(err); | |
console.log('got', doc); | |
var action = new Action({ | |
event: 'draw tile' | |
, properties: { x: 1 } | |
, time: Math.round(+new Date() / 1000) | |
}); | |
doc.turns[doc.turns.length - 1].actions.push(action); // action won't save... | |
if (action.event == 'draw tile') { | |
doc.turns[doc.turns.length - 1].tile.push(doc.tiles.shift()); | |
} else if (action.event == 'end turn') { | |
var turn = new Turn({ | |
player: 'me2', | |
tile: [], | |
actions: [] | |
}); | |
doc.turns.push(turn); // ...if adding a new turn | |
} | |
console.log('before save', doc); | |
doc.save(function (err) { | |
if (err) return done(err); | |
Game.findById(doc, function (err, found) { | |
if (err) return done(err); | |
console.log('after save', found); | |
assert.equal(1, doc.turns[0].actions.length) | |
assert.equal('draw tile', doc.turns[0].actions[0].event); | |
assert.equal(0, doc.tiles.length) | |
done(); | |
}); | |
}); | |
}); | |
}); | |
}); | |
function done (err) { | |
if (err) console.error(err.stack); | |
mongoose.connection.db.dropDatabase(function () { | |
mongoose.connection.close(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment