|
| 1 | +var _ = require('lodash'); |
| 2 | +var should = require('should'); |
| 3 | +var helper = require('../support/spec_helper'); |
| 4 | +var async = require('async'); |
| 5 | +var ORM = require('../../'); |
| 6 | + |
| 7 | +describe("Model.exists()", function() { |
| 8 | + var db = null; |
| 9 | + var Person = null; |
| 10 | + |
| 11 | + var setup = function () { |
| 12 | + return function (done) { |
| 13 | + Person = db.define("person", { |
| 14 | + name : String |
| 15 | + }); |
| 16 | + |
| 17 | + return helper.dropSync(Person, function () { |
| 18 | + Person.create([{ |
| 19 | + id : 1, |
| 20 | + name: "Jeremy Doe" |
| 21 | + }, { |
| 22 | + id : 2, |
| 23 | + name: "John Doe" |
| 24 | + }, { |
| 25 | + id : 3, |
| 26 | + name: "Jane Doe" |
| 27 | + }], done); |
| 28 | + }); |
| 29 | + }; |
| 30 | + }; |
| 31 | + |
| 32 | + before(function (done) { |
| 33 | + helper.connect(function (connection) { |
| 34 | + db = connection; |
| 35 | + |
| 36 | + return done(); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + after(function () { |
| 41 | + return db.close(); |
| 42 | + }); |
| 43 | + |
| 44 | + describe("with an id", function () { |
| 45 | + before(setup()); |
| 46 | + |
| 47 | + it("should return true if found", function (done) { |
| 48 | + Person.exists(2, function (err, exists) { |
| 49 | + should.equal(err, null); |
| 50 | + |
| 51 | + exists.should.be.true; |
| 52 | + |
| 53 | + return done(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should return false if not found", function (done) { |
| 58 | + Person.exists(4, function (err, exists) { |
| 59 | + should.equal(err, null); |
| 60 | + |
| 61 | + exists.should.be.false; |
| 62 | + |
| 63 | + return done(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("with a list of ids", function () { |
| 69 | + before(setup()); |
| 70 | + |
| 71 | + it("should return true if found", function (done) { |
| 72 | + Person.exists([ 2 ], function (err, exists) { |
| 73 | + should.equal(err, null); |
| 74 | + |
| 75 | + exists.should.be.true; |
| 76 | + |
| 77 | + return done(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should return false if not found", function (done) { |
| 82 | + Person.exists([ 4 ], function (err, exists) { |
| 83 | + should.equal(err, null); |
| 84 | + |
| 85 | + exists.should.be.false; |
| 86 | + |
| 87 | + return done(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe("with a conditions object", function () { |
| 93 | + before(setup()); |
| 94 | + |
| 95 | + it("should return true if found", function (done) { |
| 96 | + Person.exists({ name: "John Doe" }, function (err, exists) { |
| 97 | + should.equal(err, null); |
| 98 | + |
| 99 | + exists.should.be.true; |
| 100 | + |
| 101 | + return done(); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should return false if not found", function (done) { |
| 106 | + Person.exists({ name: "Jack Doe" }, function (err, exists) { |
| 107 | + should.equal(err, null); |
| 108 | + |
| 109 | + exists.should.be.false; |
| 110 | + |
| 111 | + return done(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments