var testResource = require('../../test_resource'),
    fs = require('fs'),
    db = require('../../../lib/db'),
    connectMongoDb = db.connectMongoDb,
    defineModel = db.defineModel,
    assert = require('assert');

describe('mongo_crud', function () {
    before(function (done) {
        var scenario = this;
        scenario.dbUrl = 'localhost:27017/restful_mongo_crud_db_test';
        done();
    });
    it('Define models', function (done) {
        var scenario = this;
        scenario.models = {
            User: defineModel({})
        };
        done();
    });
    it('Connect a database.', function (done) {
        var scenario = this;
        var connection = scenario.connection = connectMongoDb(scenario.dbUrl, scenario.models);
        connection.dropDatabase(function (err) {
            assert.ifError(err);
            done();
        });
    });
    it('Insert models.', function (done) {
        var scenario = this,
            User = scenario.models.User;
        var user01 = new User({
            username: 'John'
        }), user02 = new User({
            username: 'Mary'
        });
        user01.insert(function (err, user01) {
            assert.ifError(err);
            assert.ok(user01._id);
            assert.equal(user01._vr, 0);
            assert.equal(user01.username, 'John');
            user02.insert(function (err, user02) {
                assert.ifError(err);
                assert.equal(user02.username, 'Mary');
                done();
            });
        });
    });
    it('Find models.', function (done) {
        var scenario = this,
            User = scenario.models.User;
        User.findAll(function (err, users) {
            assert.ifError(err);
            assert.equal(users.length, 2);
            User.findByCondition({
                username: 'Mary'
            }, function (err, users) {
                assert.ifError(err);
                assert.equal(users.length, 1);
                assert.equal(users[0].username, 'Mary');
                done();
            });
        });
    });
    it('Find with skip limit.', function (done) {
        var scenario = this,
            User = scenario.models.User;
        User.findByCondition({
        },function (err) {
            assert.ifError(err);
            done();
        }).limit(10).skip(2);
    });

    it('Count models.', function (done) {
        var scenario = this,
            User = scenario.models.User;
        User.countAll(function (err, count) {
            assert.ifError(err);
            assert.equal(count, 2);
            done();
        });
    });


    it('Update models.', function (done) {
        var scenario = this,
            User = scenario.models.User;
        User.findAll(function (err, users) {
            assert.ifError(err);
            var user01 = users[0];
            user01.fast = true;
            user01.update(function (err, data) {
                assert.ifError(err);
                assert.equal(data.fast, true);
                done();
            });
        });
    });
    it('Destroy model', function (done) {
        var scenario = this,
            User = scenario.models.User;
        User.findAll(function (err, users) {
            assert.ifError(err);
            var user01 = users[0];
            user01.destroy(function () {
                done();
            });
        });
    });

    it('Count models', function (done) {
        var scenario = this,
            User = scenario.models.User;
        User.countAll(function (err, count) {
            assert.ifError(err);
            assert.equal(count, 1);
            done();
        });
    });

    it('Ensure index', function (done) {
        var scenario = this,
            User = scenario.models.User;
        User.ensureIndex({
            'username': 1
        }, function (err) {
            assert.ifError(err);
            User.getIndexes(function (err, indeces) {
                assert.ifError(err);
                assert.equal(indeces.length, 2);
                done();
            });
        });
    });

    after(function (done) {
        var scenario = this;
        scenario.connection.close();
        done();
    });
});
