Example #1
0
 it.should("retrive models by database", function () {
     // check for both methods of retrieving the model
     assert.strictEqual(patio.getModel(ds1), Employee);
     assert.strictEqual(patio.getModel(ds2), Employee2);
     assert.strictEqual(patio.getModel("employee",DB1), Employee);
     assert.strictEqual(patio.getModel("employee",DB2), Employee2);
 });
helper.loadModels().then(function() {
    var Employee = patio.getModel("employee");
    var suite = vows.describe("TimeStampPlugin default");
    suite.addBatch({

        "when creating an employee" : {
            topic : function() {
                Employee.save({
                    firstname : "doug",
                    lastname : "martin",
                    midinitial : null,
                    gender : "M",
                    street : "1 nowhere st.",
                    city : "NOWHERE"
                }).then(hitch(this, function(e) {
                    //force reload
                    e.reload().then(hitch(this, "callback", null), hitch(this, "callback"));
                }), hitch(this, "callback"));
            },

            "the updatedAt time stamp should not be set" : function(topic) {
                assert.isNull(topic.updated);
            },

            "the createdAt time stamp should be set" : function(topic) {
                assert.isNotNull(topic.created);
                assert.instanceOf(topic.created, patio.SQL.DateTime);
            },

            "when updating an employee" : {
                topic : function(e) {
                    //setTimeout to ensure new timeout
                    setTimeout(hitch(this, function() {
                        e.firstname = "dave";
                        e.save().then(hitch(this, function(e) {
                            //force reload
                            e.reload().then(hitch(this, "callback", null), hitch(this, "callback"));
                        }), hitch(this, "callback"));
                    }), 1000);
                },

                "the updated time stamp should be set" : function(topic) {
                    assert.isNotNull(topic.updated);
                    assert.instanceOf(topic.updated, patio.SQL.DateTime);
                    assert.notDeepEqual(topic.updated, topic.created);
                }
            }
        }
    });

    suite.run({reporter : require("vows").reporter.spec}, function() {
        helper.dropModels().then(comb.hitch(ret, "callback"), comb.hitch(ret, "errback"));
    });
});
Example #3
0
helper.loadModels().then(function () {
    var Works = patio.getModel("works"), Employee = patio.getModel("employee");
    var suite = vows.describe("One to One lazy association ");

    suite.addBatch({
        "A model":{
            topic:function () {
                return Employee
            },

            "should have associations":function () {
                assert.deepEqual(Employee.associations, ["works"]);
                assert.deepEqual(Works.associations, ["employee"]);
                var emp = new Employee();
                var work = new Works();
                assert.deepEqual(emp.associations, ["works"]);
                assert.deepEqual(work.associations, ["employee"]);
            }
        }
    });

    suite.addBatch({

        "When creating a employee ":{
            topic:function () {
                var e1 = new Employee({
                    lastname:"last" + 1,
                    firstname:"first" + 1,
                    midinitial:"m",
                    gender:gender[1 % 2],
                    street:"Street " + 1,
                    city:"City " + 1,
                    works:{
                        companyName:"Google",
                        salary:100000
                    }
                }).save().then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " the employee should work at google ":function (employee) {
                var works = employee.works.then(function (works) {
                    assert.equal(works.companyName, "Google");
                    assert.equal(works.salary, 100000);
                });

            }
        }

    });

    suite.addBatch({

        "When finding an employee":{
            topic:function () {
                Employee.one().then(hitch(this, "callback", null), hitch(this, "callback"));
            },


            " the employees work should not be loaded yet":{
                topic:function (employee) {
                    var works = employee.works;
                    assert.instanceOf(works, Promise);
                    works.then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                " but now it should":function (works) {
                    assert.equal(works.companyName, "Google");
                    assert.equal(works.salary, 100000);
                }
            }
        }

    });


    suite.addBatch({

        "When finding workers":{
            topic:function () {
                Works.one().then(hitch(this, function (worker) {
                    worker.employee.then(hitch(this, "callback", null), hitch(this, "callback"));
                }));
            },

            " the worker should work at google and have an associated employee":function (emp) {
                assert.equal(emp.lastname, "last" + 1);
                assert.equal(emp.firstname, "first" + 1);
                assert.equal(emp.midinitial, "m");
                assert.equal(emp.gender, gender[1 % 2]);
                assert.equal(emp.street, "Street " + 1);
                assert.equal(emp.city, "City " + 1);
                return emp;
            }
        }

    });

    suite.addBatch({

        "When deleting an employee":{
            topic:function () {
                Employee.one().chain(
                    function (e) {
                        return e.remove();
                    }).chain(hitch(Works, "count"), hitch(this, "callback")).then(hitch(this, "callback", null), hitch(this, "callback"));
            },


            " the the works count should be 1 ":function (count) {
                assert.equal(count, 1);
            }
        }

    });

    suite.addBatch({

        "When creating a employee ":{
            topic:function () {
                var e1 = new Employee({
                    lastname:"last" + 1,
                    firstname:"first" + 1,
                    midinitial:"m",
                    gender:gender[1 % 2],
                    street:"Street " + 1,
                    city:"City " + 1,
                    works:{
                        companyName:"Google",
                        salary:100000
                    }
                }).save().then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " the employee should work at google ":{
                topic:function (employee) {
                    var works = employee.works.then(function (works) {
                        assert.equal(works.companyName, "Google");
                        assert.equal(works.salary, 100000);
                    });
                    employee.works = null;
                    Employee.findById(employee.id).then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                "and when setting works to null and not saving":{
                    topic:function (employee) {
                        employee.works.then(hitch(this, "callback", null, employee), hitch(this, "callback"));
                    },

                    "the employee should still work at google":{
                        topic:function (employee, works) {
                            assert.instanceOf(employee, Employee);
                            assert.instanceOf(works, Works);
                            assert.equal(works.companyName, "Google");
                            assert.equal(works.salary, 100000);
                            comb.executeInOrder(employee, Employee,
                                function (employee, Employee) {
                                    employee.works = null;
                                    employee.save();
                                    return Employee.findById(employee.id);
                                }).then(hitch(this, "callback", null), hitch(this, "callback"));
                        },
                        "but when setting works to null and saving":{
                            topic:function (employee) {
                                employee.works.then(hitch(this, "callback", null), hitch(this, "callback"));
                            },

                            "works should be null":function (works) {
                                assert.isNull(works);
                            }
                        }
                    }
                }

            }
        }
    });

    suite.addBatch({

        "When creating a employee ":{
            topic:function () {
                var e1 = new Employee({
                    lastname:"last" + 1,
                    firstname:"first" + 1,
                    midinitial:"m",
                    gender:gender[1 % 2],
                    street:"Street " + 1,
                    city:"City " + 1,
                    works:{
                        companyName:"Google",
                        salary:100000
                    }
                }).save().then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " the employee should work at google ":{
                topic:function (employee) {
                    var works = employee.works.then(hitch(this, function (works) {
                        assert.equal(works.companyName, "Google");
                        assert.equal(works.salary, 100000);
                        works.employee = null;
                        Works.findById(works.id).then(hitch(this, "callback", null), hitch(this, "callback"));
                    }));
                },

                "and when setting employee to null on works and not saving":{
                    topic:function (works) {
                        works.employee.then(hitch(this, "callback", null, works), hitch(this, "callback"));
                    },

                    "works should still have an employee":{
                        topic:function (works, employee) {
                            assert.instanceOf(employee, Employee);
                            assert.instanceOf(works, Works);
                            assert.equal(works.companyName, "Google");
                            assert.equal(works.salary, 100000);
                            works.employee = null;
                            works.save().chain(hitch(Works, "findById", works.id), hitch(this, "callback")).then(hitch(this, "callback", null, employee), hitch(this, "callback"));
                        },
                        "but when setting employee to null and saving":{
                            topic:function (employee, works) {
                                comb.executeInOrder(employee, works, Employee,
                                    function (employee, works, Employee) {
                                        var emp = works.employee;
                                        var newEmp = Employee.findById(employee.id);
                                        var nullWorks = newEmp.works;
                                        return {emp:emp, employee:newEmp, nullWorks:nullWorks };
                                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
                                ;
                            },

                            "employee should be null but employee should still exists but not work anywhere":function (res) {
                                assert.isNull(res.emp);
                                assert.instanceOf(res.employee, Employee);
                                assert.isNull(res.nullWorks);
                            }
                        }
                    }
                }

            }
        }

    });


    suite.run({reporter:require("vows").reporter.spec}, function () {
        helper.dropModels().then(comb.hitch(ret, "callback"), comb.hitch(ret, "errback"));
    });

}, function (err) {
Example #4
0
helper.loadModels().then(function() {
    var Employee = patio.getModel("employee");
    var DB = patio.defaultDatabase;
    suite.addBatch({
        "should save an employee" : {
            topic : function() {
                var emp = new Employee({
                    firstname : "doug",
                    lastname : "martin",
                    position : 1,
                    midinitial : null,
                    gender : "M",
                    street : "1 nowhere st.",
                    city : "NOWHERE"}).save().then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " and get a list of one employees" : function(emp) {
                assert.instanceOf(emp, Employee);
                assert.equal("doug", emp.firstname);
                assert.equal("martin", emp.lastname);
                assert.isNull(emp.midinitial);
                assert.equal("M", emp.gender);
                assert.equal("1 nowhere st.", emp.street);
                assert.equal("NOWHERE", emp.city);
            }


        }
    });

    suite.addBatch({
        "should save a batch of employees" : {
            topic : function() {
                var emps = [];
                for (var i = 0; i < 20; i++) {
                    emps.push({
                        lastname : "last" + i,
                        position : i,
                        firstname : "first" + i,
                        midinitial : "m",
                        gender : gender[i % 2],
                        street : "Street " + i,
                        city : "City " + i
                    });
                }
                comb.executeInOrder(Employee,
                    function(emp) {
                        emp.truncate();
                        var ret = {};
                        ret.employees = emp.save(emps);
                        ret.count = emp.count();
                        return ret;
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " and get a get a count of 20 and 20 different emplyees" : function(ret) {
                assert.equal(ret.count, 20);
                assert.lengthOf(ret.employees, 20);
                ret.employees.forEach(function(emp, i) {
                    assert.equal(emp.lastname, "last" + i);
                    assert.equal(emp.firstname, "first" + i);
                    assert.equal(emp.midinitial, "m");
                    assert.equal(emp.gender, gender[i % 2]);
                    assert.equal(emp.street, "Street " + i);
                    assert.equal(emp.city, "City " + i);
                });
            }


        }
    });

    suite.addBatch({
        "A Model " : {

            topic : function() {
                var emps = [];
                for (var i = 0; i < 20; i++) {
                    emps.push({
                        lastname : "last" + i,
                        firstname : "first" + i,
                        position : i,
                        midinitial : "m",
                        gender : gender[i % 2],
                        street : "Street " + i,
                        city : "City " + i
                    });
                }
                comb.executeInOrder(Employee,
                    function(emp) {
                        emp.truncate();
                        emp.save(emps);
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            "Should reload"  : {
                topic : function() {
                    comb.executeInOrder(Employee, function(Employee){
                         var emp = Employee.findById(1);
                        emp.lastname = "martin";
                        return emp.reload();
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                "and return employees" : function(topic) {
                    assert.instanceOf(topic, Employee);
                    assert.equal(topic.id, 1);
                    assert.equal(topic.lastname, "last0");
                }
            },

            "Should findById"  : {
                topic : function() {
                    Employee.findById(1).then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                "and return employees" : function(topic) {
                    assert.instanceOf(topic, Employee);
                    assert.equal(topic.id, 1);
                }
            },

            "Should filter"  : {
                topic : function() {
                    var id = sql.identifier("id");
                    comb.executeInOrder(Employee,
                        function(Employee) {
                            var ret = {};
                            ret.query1 = Employee.filter({id : [1,2,3,4,5,6]}).all()
                            ret.query2 = Employee.filter(id.gt(5), id.lt(11)).order("id").last();
                            ret.query3 = Employee.filter(
                                function() {
                                    return this.firstname.like(/first1[1|2]*$/);
                                }).order("firstname").all();
                            ret.query4 = Employee.filter({id : {between : [1,5]}}).order("id").all();
                            ret.query5 = [];
                            Employee.filter(
                                function() {
                                    return this.id.gt(15);
                                }).forEach(
                                function(emp) {
                                    ret.query5.push(emp)
                                });
                            return ret;

                        }).then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                "and return employees" : function(topic) {
                    var i = 1;
                    var query1 = topic.query1, query2 = topic.query2, query3 = topic.query3, query4 = topic.query4, query5 = topic.query5, query6 = topic.query6;
                    assert.lengthOf(query1, 6);
                    query1.forEach(function(t) {
                        assert.instanceOf(t, Employee);
                        assert.equal(i++, t.id);
                    });
                    assert.equal(query2.id, 10);
                    assert.lengthOf(query3, 3);
                    assert.instanceOf(query3[0], Employee);
                    assert.equal(query3[0].firstname, "first1");
                    assert.instanceOf(query3[1], Employee);
                    assert.equal(query3[1].firstname, "first11");
                    assert.instanceOf(query3[2], Employee);
                    assert.equal(query3[2].firstname, "first12");
                    assert.deepEqual(query4.map(function(e) {
                        assert.instanceOf(e, Employee);
                        return e.id;
                    }), [1,2,3,4,5]);
                    assert.deepEqual(query5.map(function(e) {
                        assert.instanceOf(e, Employee);
                        return e.id;
                    }), [16,17,18,19,20]);


                }
            },

            "Should find by gender"  : {
                topic : function() {
                    var self = this;
                    Employee.findByGender("F").then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                "and return female employees" : function(topic) {
                    topic.forEach(function(emp) {
                        assert.instanceOf(emp, Employee);
                        assert.equal("F", emp.gender);
                    });
                }
            },

            "Should count employees"  : {
                topic : function() {
                    Employee.count().then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                "and return 20" : function(topic) {
                    assert.equal(20, topic);
                }
            },

            "Should find all employees"  : {
                topic : function() {
                    Employee.all().then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                "and return 20 employees" : function(topic) {
                    assert.lengthOf(topic, 20);
                    topic.forEach(function(e) {
                        assert.instanceOf(e, Employee);
                    });
                }
            },

            "Should map all employees"  : {
                topic : function() {
                    comb.executeInOrder(Employee,
                        function(Employee) {
                            var ret = {};
                            ret.query1 = Employee.map("id");
                            ret.query2 = Employee.order("position").map(function(e) {
                                return e.firstname + " " + e.lastname;
                            });
                            return ret;
                        }).then(hitch(this, "callback", null), hitch(this, "callback"));

                },

                "and return 20 ids" : function(topic) {
                    assert.lengthOf(topic.query1, 20);
                    topic.query1.forEach(function(id, i) {
                        assert.equal(id, i + 1);
                    });
                    assert.lengthOf(topic.query2, 20);
                    topic.query2.forEach(function(name, i) {
                        assert.equal(name, "first" + i + " last" + i);
                    })

                }
            },


            "Should loop through all employees"  : {
                topic : function() {
                    var ret = [];
                    var d = Employee.forEach(
                        function(emp) {
                            ret.push(emp);
                        }).then(hitch(this, "callback", null, ret), hitch(this, "callback"));
                },

                "and return 20 employees" : function(topic) {
                    assert.lengthOf(topic, 20);
                    topic.forEach(function(e) {
                        assert.instanceOf(e, Employee);
                    });
                }
            },

            "Should find first employee"  : {
                topic : function() {
                    var d = Employee.one().then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                "and return employee with id of 1" : function(topic) {
                    assert.instanceOf(topic, Employee);
                    assert.equal(1, topic.id);
                }
            },

            "Should find first employee with query"  : {
                topic : function() {
                    var self = this;
                    this.count = 1;
                    var id = sql.identifier("id");
                    var d = Employee.first(id.gt(5), id.lt(11)).then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                "and return employee with id of 1" : function(topic) {
                    assert.instanceOf(topic, Employee);
                    assert.equal(topic.id, 6);
                }
            },

            "Should find last employee"  : {
                topic : function() {
                    var self = this;
                    var d = Employee.order("firstname").last().then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                "and return employee a first name of first9" : function(topic) {
                    assert.throws(hitch(Employee, "last"));
                    assert.instanceOf(topic, Employee);
                    assert.equal(topic.firstname, "first9");
                }
            }
        }
    });
    suite.addBatch({
        "Should save an employee"  : {
            topic : function() {
                Employee.save({
                    firstname : "doug",
                    lastname : "martin",
                    position : 21,
                    midinitial : null,
                    gender : "M",
                    street : "1 nowhere st.",
                    city : "NOWHERE"
                }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            "and the employee should have an id" : function(emp) {
                assert.instanceOf(emp, Employee);
                assert.isNumber(emp.id);
            },

            "Should be able to update the employee" : {
                topic : function(emp) {
                    emp.firstname = "douglas";
                    emp.update().then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                "and when querying the employee it should be updated" : {

                    topic : function(e, emp) {
                        assert.instanceOf(e, Employee);
                        assert.equal(e.firstname, "douglas");
                        Employee.one({id : emp.id}).then(hitch(this, "callback", null), hitch(this, "callback"));
                    },

                    " with the new name" : function(emp) {
                        assert.instanceOf(emp, Employee);
                        assert.isNumber(emp.id);
                        assert.equal(emp.firstname, "douglas");
                    },

                    "Should be able to delete the employee" :  {
                        topic : function(e, emp) {
                            emp.remove().then(hitch(this, "callback", null, emp), hitch(this, "callback"));
                        },

                        "and when when querying the deleted employee" : {

                            topic : function(emp) {
                                var self = this;
                                Employee.filter({id : emp.id}).one().then(hitch(this, "callback", null), hitch(this, "callback"));
                            },

                            "it should be null" : function(topic) {
                                assert.isNull(topic);
                            }
                        }
                    }
                }

            }
        }
    });

    suite.addBatch({
        "Should do a batch update" : {
            topic : function() {
                comb.executeInOrder(Employee,
                    function(Employee) {
                        Employee.update({firstname : "doug"});
                        return Employee.all();
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " all records should be updated" : function(records) {
                assert.lengthOf(records, 20);
                records.forEach(function(r) {
                    assert.equal(r.firstname, "doug");
                });
            }
        }
    });

    suite.addBatch({
        "Should do an update on a single record" : {
            topic : function() {
                comb.executeInOrder(Employee,
                    function(Employee) {
                        Employee.update({firstname : "dougie"}, {id: 2});
                        return Employee.filter({id:2}).one();
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " all records should be updated" : function(emp) {
                assert.instanceOf(emp, Employee);
                assert.equal(emp.firstname, "dougie");
            }
        }
    });

    suite.run({reporter : require("vows").reporter.spec}, function() {
        helper.dropModels().then(comb.hitch(ret, "callback"), comb.hitch(ret, "errback"));
    });
}, function(err) {
Example #5
0
helper.loadModels().then(function () {
    var Company = patio.getModel("company"), Employee = patio.getModel("employee");

    var suite = vows.describe("Many to many eager association ");

    suite.addBatch({
        "A model":{
            topic:function () {
                return Employee
            },

            "should have associations":function () {
                assert.deepEqual(Employee.associations, ["companies"]);
                assert.deepEqual(Company.associations, ["employees"]);
                var emp = new Employee();
                var company = new Company();
                assert.deepEqual(emp.associations, ["companies"]);
                assert.deepEqual(company.associations, ["employees"]);
            }
        }
    });

    suite.addBatch({

        "When creating a company with employees":{
            topic:function () {
                var c1 = new Company({
                    companyName:"Google",
                    employees:[
                        {
                            lastname:"last" + 1,
                            firstname:"first" + 1,
                            midinitial:"m",
                            gender:gender[1 % 2],
                            street:"Street " + 1,
                            city:"City " + 1
                        },
                        {
                            lastname:"last" + 2,
                            firstname:"first" + 2,
                            midinitial:"m",
                            gender:gender[2 % 2],
                            street:"Street " + 2,
                            city:"City " + 2
                        }
                    ]
                });
                c1.save().then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " the company should have employees ":{
                topic:function (company) {
                    var emps = company.employees;
                    assert.lengthOf(emps, 2);
                    emps.forEach(function (emp, i) {
                        assert.equal(emp.id, i + 1);
                    }, this);
                    return emps;
                },

                " when querying the employees ":{
                    topic:function (emps, company) {
                        Employee.all().then(hitch(this, "callback", null), hitch(this, "callback"));
                    },

                    "the employees company should be loaded":function (emps) {
                        assert.lengthOf(emps, 2);
                        assert.isTrue(emps[0].companies.every(function (c) {
                            return c.companyName == "Google"
                        }));
                        assert.isTrue(emps[1].companies.every(function (c) {
                            return c.companyName == "Google"
                        }));
                    }
                }
            }
        }

    });

    suite.addBatch({

        "When finding a company":{
            topic:function () {
                Company.one().then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " the companys employees should be loaded ":function (company) {
                var emps = company.employees;
                assert.lengthOf(emps, 2);
                var ids = [1, 2];
                emps.forEach(function (emp, i) {
                    assert.equal(ids[i], emp.id);
                });

            }
        }

    });

    suite.addBatch({
        "When finding a company and adding an employee":{
            topic:function () {
                var emp = new Employee({
                    lastname:"last" + 3,
                    firstname:"first" + 3,
                    midinitial:"m",
                    gender:gender[1 % 3],
                    street:"Street " + 3,
                    city:"City " + 3
                });
                comb.executeInOrder(Company,
                    function (Company) {
                        var company = Company.one();
                        company.addEmployee(emp);
                        return company;
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));

            },

            "the company should have three employees ":function (company) {
                var emps = company.employees;
                assert.lengthOf(emps, 3);
                var ids = [1, 2, 3];
                emps.forEach(function (emp, i) {
                    assert.equal(emp.id, ids[i]);
                });
            }
        }
    });

    suite.addBatch({

        "When finding a company and removing an employee and deleting the employee":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        var emps = company.employees;
                        company.removeEmployee(emps[0], true);
                        return {company:company, empCount:Employee.count()};
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            "the company should have two employees but still three in the db":function (ret) {
                var emps = ret.company.employees;
                assert.lengthOf(emps, 2);
                var ids = [2, 3];
                emps.forEach(function (emp, i) {
                    assert.equal(ids[i], emp.id);
                });
                assert.equal(ret.empCount, 2);
            }
        }

    });

    suite.addBatch({

        "When finding a company and removing multiple employees and deleting the employees":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        var emps = company.employees;
                        company.removeEmployees(emps, true);
                        return {company:company, empCount:Employee.count()};
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },


            "the company should have no employees ":function (ret) {
                assert.lengthOf(ret.company.employees, 0);
                assert.equal(ret.empCount, 0);
            }
        }

    });

    suite.addBatch({

        "When finding a company and adding employees":{
            topic:function () {
                var employees = [];
                for (var i = 0; i < 3; i++) {
                    employees.push({
                        lastname:"last" + i,
                        firstname:"first" + i,
                        midinitial:"m",
                        gender:gender[i % 2],
                        street:"Street " + i,
                        city:"City " + i
                    });
                }
                comb.executeInOrder(Company,
                    function (Company) {
                        var company = Company.one();
                        company.addEmployees(employees);
                        return company;
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " the company should have three employees":function (company) {
                var emps = company.employees;
                assert.lengthOf(emps, 3);
                emps.forEach(function (emp) {
                    assert.instanceOf(emp, Employee);
                });
            }


        }

    });

    suite.addBatch({

        "When finding a company and removing an employee and not deleting the employee":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        var emps = company.employees;
                        company.removeEmployee(emps[0]);
                        return {company:company, empCount:Employee.count()};
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));

            },

            "the company should have two employees ":function (ret) {
                var emps = ret.company.employees;
                assert.lengthOf(emps, 2);
                assert.equal(ret.empCount, 3);
            }

        }

    });

    suite.addBatch({

        "When finding a company and removing multiple employees and deloting the employees":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        var emps = company.employees;
                        company.removeEmployees(emps);
                        return {company:company, empCount:Employee.count()};
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            "the company should have no employees ":function (ret) {
                assert.lengthOf(ret.company.employees, 0);
                assert.equal(ret.empCount, 3);
            }

        }

    });

    suite.addBatch({

        "When finding setting the employees on a company":{
            topic:function () {
                comb.executeInOrder(Company, Employee, console,
                    function (Company, Employee, console) {
                        var company, emps = Employee.all();
                        (company = Company.one()).employees = emps;
                        company.save();
                        return company;
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            "the company should have all of the employees ":function (company) {
                assert.lengthOf(company.employees, 3);
            }

        }

    });


    suite.addBatch({
        "When deleting a company":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        company.remove();
                        return Employee.count();
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " the employees should still exist ":function (count) {
                assert.equal(count, 3);
            }
        }
    });


    suite.run({reporter:require("vows").reporter.spec}, function () {
        helper.dropModels().then(comb.hitch(ret, "callback"), comb.hitch(ret, "errback"));
    });

});
helper.loadModels().then(function () {
    var Company = patio.getModel("company"), Employee = patio.getModel("employee");

    var suite = vows.describe("Many to one Many association with a customFilter ");

    suite.addBatch({
        "A model":{
            topic:function () {
                return Employee
            },

            "should have associations":function () {
                assert.deepEqual(Employee.associations, ["companies"]);
                assert.deepEqual(Company.associations, ["employees", "omahaEmployees", "lincolnEmployees"]);
                var emp = new Employee();
                var company = new Company();
                assert.deepEqual(emp.associations, ["companies"]);
                assert.deepEqual(company.associations, ["employees", "omahaEmployees", "lincolnEmployees"]);
            }
        }
    });

    suite.addBatch({

        "When creating a company with employees":{
            topic:function () {

                var employees = [];
                for (var i = 0; i < 3; i++) {
                    employees.push({
                        lastname:"last" + i,
                        firstname:"first" + i,
                        midinitial:"m",
                        gender:gender[i % 2],
                        street:"Street " + i,
                        city:cities[i % 3]
                    });
                }
                var c1 = new Company({
                    companyName:"Google",
                    employees:employees
                });
                c1.save().then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " the company should have employees ":{
                topic:function (company) {
                    comb.executeInOrder(company,
                        function (company) {
                            return {
                                omahaEmployees:company.omahaEmployees,
                                lincolnEmployees:company.lincolnEmployees,
                                employees:company.employees
                            }
                        }).then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                " when querying the employees there should be employees, omahaEmployees, and lincolnEmployees ":{
                    topic:function (ret, company) {
                        var employees = ret.employees,
                            omahaEmployees = ret.omahaEmployees,
                            lincolnEmployees = ret.lincolnEmployees;
                        assert.lengthOf(employees, 3);
                        assert.lengthOf(lincolnEmployees, 1);
                        assert.equal(lincolnEmployees[0].city, "Lincoln")
                        assert.lengthOf(omahaEmployees, 1);
                        assert.equal(omahaEmployees[0].city, "Omaha")
                        comb.executeInOrder(assert, Employee,
                            function (assert, Employee) {
                                var emps = Employee.all();
                                assert.lengthOf(emps, 3);
                                return {companies1:emps[0].companies, companies2:emps[1].companies, companies3:emps[2].companies};
                            }).then(hitch(this, "callback", null), hitch(this, "callback"));

                    },

                    "the employees company should be loaded":function (ret) {
                        assert.isTrue(ret.companies1.every(function (c) {
                            return c.companyName == "Google";
                        }));
                        assert.isTrue(ret.companies2.every(function (c) {
                            return c.companyName == "Google";
                        }));
                        assert.isTrue(ret.companies3.every(function (c) {
                            return c.companyName == "Google";
                        }));
                    }
                }
            }
        }

    });

    suite.addBatch({

        "When finding a company":{
            topic:function () {
                Company.one().then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " the company's employees should not be loaded ":{
                topic:function (company) {
                    company.employees.then(hitch(this, "callback", null), hitch(this, "callback"));
                },

                " but after fetching them there should be three":function (emps) {
                    assert.lengthOf(emps, 3);
                },

                " and adding an employee":{
                    topic:function (i, company) {
                        var emp = new Employee({
                            lastname:"last" + 3,
                            firstname:"first" + 3,
                            midinitial:"m",
                            gender:gender[1 % 3],
                            street:"Street " + 3,
                            city:"omaha"
                        });
                        comb.executeInOrder(company,
                            function (company) {
                                company.addEmployee(emp);
                                return {
                                    employees:company.employees,
                                    omahaEmployees:company.omahaEmployees,
                                    lincolnEmployees:company.lincolnEmployees
                                };
                            }).then(hitch(this, "callback", null), hitch(this, "callback"));
                    },

                    "the company should have four employees two omahaEmployees, and 1 lincolEmployee ":function (ret) {
                        var emps = ret.employees;
                        assert.lengthOf(ret.employees, 4);
                        assert.lengthOf(ret.omahaEmployees, 2);
                        assert.isTrue(ret.omahaEmployees.every(function (emp) {
                            return emp.city.match(/omaha/i) != null;
                        }));
                        assert.lengthOf(ret.lincolnEmployees, 1);
                        assert.isTrue(ret.lincolnEmployees.every(function (emp) {
                            return emp.city.match(/lincoln/i) != null;
                        }));
                    }
                }
            }



        }
    });

    suite.addBatch({

        "When finding a company and removing an omaha employee and deleting the employee":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        var emps = company.omahaEmployees;
                        company.removeOmahaEmployee(emps[0], true);
                        return {employees:company.employees, empCount:Employee.count()};
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            "the company should have three employees ":function (ret) {
                var emps = ret.employees;
                assert.lengthOf(emps, 3);
                assert.equal(ret.empCount, 3);
            }
        }

    });

    suite.addBatch({

        "When finding a company and removing a lincoln employee and deleting the employee":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        var emps = company.lincolnEmployees;
                        company.removeLincolnEmployee(emps[0], true);
                        return {employees:company.employees, empCount:Employee.count()};
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            "the company should have two employees ":function (ret) {
                var emps = ret.employees;
                assert.lengthOf(emps, 2);
                assert.equal(ret.empCount, 2);
            }
        }

    });

    suite.addBatch({

        "When finding a company and removing multiple employees and deleting the employees":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        var emps = company.employees;
                        company.removeEmployees(emps, true);
                        return {employees:company.employees, empCount:Employee.count()};
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            "the company should have no employees ":function (ret) {
                assert.lengthOf(ret.employees, 0);
                assert.equal(ret.empCount, 0);
            }
        }

    });

    suite.addBatch({

        "When finding a company and adding employees":{
            topic:function () {
                var employees = [];
                for (var i = 0; i < 3; i++) {
                    employees.push({
                        lastname:"last" + i,
                        firstname:"first" + i,
                        midinitial:"m",
                        gender:gender[i % 2],
                        street:"Street " + i,
                        city:cities[i % 3]
                    });
                }
                comb.executeInOrder(Company,
                    function (Company) {
                        var company = Company.one();
                        company.addEmployees(employees);
                        return {
                            employees:company.employees,
                            omahaEmployees:company.omahaEmployees,
                            lincolnEmployees:company.lincolnEmployees
                        };
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            "the company should have three employees ":function (ret) {
                var emps = ret.employees;
                assert.lengthOf(ret.employees, 3);
                assert.lengthOf(ret.omahaEmployees, 1);
                assert.isTrue(ret.omahaEmployees.every(function (emp) {
                    return emp.city.match(/omaha/i) != null;
                }));
                assert.lengthOf(ret.lincolnEmployees, 1);
                assert.isTrue(ret.lincolnEmployees.every(function (emp) {
                    return emp.city.match(/lincoln/i) != null;
                }));
            }
        }

    });

    suite.addBatch({

        "When finding a company and removing an omaha employee and not deleting the employee":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        var emps = company.omahaEmployees;
                        company.removeOmahaEmployee(emps[0]);
                        return {employees:company.employees, omahaEmployees:company.omahaEmployees, lincolnEmployees:company.lincolnEmployees, empCount:Employee.count()};
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },


            "the company should have two employees and no omaha employees":function (ret) {
                var emps = ret.employees;
                assert.lengthOf(emps, 2);
                assert.lengthOf(ret.omahaEmployees, 0);
                assert.lengthOf(ret.lincolnEmployees, 1);
                assert.equal(ret.empCount, 3);
            }
        }

    });

    suite.addBatch({

        "When finding a company and removing a lincoln employee and not deleting the employee":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        var emps = company.lincolnEmployees;
                        company.removeLincolnEmployee(emps[0]);
                        return {employees:company.employees, omahaEmployees:company.omahaEmployees, lincolnEmployees:company.lincolnEmployees, empCount:Employee.count()};
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },


            "the company should have one employees and no omaha or lincoln employees":function (ret) {
                var emps = ret.employees;
                assert.lengthOf(emps, 1);
                assert.lengthOf(ret.omahaEmployees, 0);
                assert.lengthOf(ret.lincolnEmployees, 0);
                assert.equal(ret.empCount, 3);
            }
        }

    });

    suite.addBatch({

        "When finding a company and removing multiple employees and not deleting the employees":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        var emps = company.employees;
                        company.removeEmployees(emps);
                        return {employees:company.employees, empCount:Employee.count()};
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            "the company should have no employees ":function (ret) {
                assert.lengthOf(ret.employees, 0);
                assert.equal(ret.empCount, 3);
            }
        }

    });


    suite.addBatch({
        "When deleting a company":{
            topic:function () {
                comb.executeInOrder(Company, Employee,
                    function (Company, Employee) {
                        var company = Company.one();
                        company.remove();
                        return Employee.count();
                    }).then(hitch(this, "callback", null), hitch(this, "callback"));
            },

            " the company should no employees ":function (count) {
                assert.equal(count, 3);
            }
        }
    });


    suite.run({reporter:require("vows").reporter.spec}, function () {
        helper.dropModels().then(comb.hitch(ret, "callback"), comb.hitch(ret, "errback"));
    });

});