Permalink
Browse files

testing model destroy

  • Loading branch information...
1 parent cd45790 commit 565869354a31469c314d4e36eaa1d734cec05aec @pgte committed Jan 28, 2011
Showing with 69 additions and 2 deletions.
  1. +1 −1 Makefile
  2. +1 −1 lib/alfred/meta/model/model.js
  3. +67 −0 test/model/test_destroy.js
View
@@ -22,7 +22,7 @@ test: mkdirtmp
operators/test_find_stream operators/test_find_stream_chained \
recovery/collection_recovery_test \
replication/test_master replication/test_slave replication/test_slave_reconnect replication/test_master_temp_roll replication/test_master_seek \
- model/test_create model/test_properties model/test_inspect model/test/validations
+ model/test_create model/test_properties model/test_inspect model/test_validations model/test_destroy
benchmark: mkdirtmp mkdirresults
node tools/benchmarks.js benchmark_find benchmark_collection benchmark_collection_filter benchmark_key_map benchmark_key_map_each_with_pos benchmark_indexed_key_map \
@@ -83,7 +83,7 @@ Model.prototype.atomic = function(id, doc_callback, final_callback) {
Model.prototype.delete = function(id, callback) {
var self = this;
this.initiatedPromise(function() {
- self.collection.delete(id, function(err) {
+ self.collection.destroy(id, function(err) {
if (err) { self._error(err); return; }
callback();
});
View
@@ -0,0 +1,67 @@
+var assert = require('assert')
+ , fs = require('fs')
+ , util = require('util');
+
+var DB_PATH = __dirname + '/../../tmp/db';
+
+var USERS = [
+ {name: 'Pedro', age: 35, sex: 'm'}
+ , {name: 'John', age: 32, sex: 'm'}
+ , {name: 'Bruno', age: 28, sex: 'm'}
+ , {name: 'Sandra', age: 35, sex: 'f'}
+ , {name: 'Patricia', age: 42, sex: 'f'}
+ , {name: 'Joana', age: 29, sex: 'f'}
+ , {name: 'Susana', age: 30, sex: 'f'}
+];
+
+module.exports.setup = function(next) {
+ fs.readdirSync(DB_PATH).forEach(function(dir) {
+ fs.unlinkSync(DB_PATH + '/' + dir);
+ });
+ next();
+};
+
+module.exports.run = function(next) {
+ var alfred = require('../../lib/alfred');
+
+ var timeout = setTimeout(function() {
+ throw new Error('timeout');
+ }, 5000);
+
+ alfred.open(DB_PATH, function(err, db) {
+ if (err) { next(err); return; }
+
+ db.on('error', function(err) {
+ next(err);
+ });
+
+ var User = db.define('User');
+ User.property('name');
+ User.property('age', Number);
+ User.property('sex', 'string', {
+ required: true,
+ minimum: 1,
+ maximum: 1
+ });
+
+ var left = USERS.length;
+ var users = [];
+
+ USERS.forEach(function(USER) {
+ var user = User.new(USER);
+ user.save(function(errors) {
+ if (errors) { next(new Error('validation errors: ' + util.inspect(errors))); return; }
+ users.push(user);
+ if (-- left === 0) {
+ user.destroy(function() {
+ User.get(user.id, function(gotUser) {
+ assert.ok(gotUser === null);
+ db.close(next);
+ });
+ });
+ }
+ });
+ });
+
+ });
+};

0 comments on commit 5658693

Please sign in to comment.