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

describe('sqlite_crud', function () {
    before(function (done) {
        var scenario = this;
        var filepath = scenario.dbFilepath = testResource.resolveWorkFile('database_crud_scenario_test.db');
        fs.exists(filepath, function (exists) {
            if (exists) {
                fs.unlink(filepath, done);
            } else {
                done();
            }
        });
    });
    it('Define models', function (done) {
        var scenario = this;
        scenario.models = {
            User: defineModel({
                username: null,
                password: null
            })
        };
        done();
    });
    it('Connect a database.', function (done) {
        var scenario = this;
        var filepath = scenario.dbFilepath;
        scenario.connection = connectSqliteDb(filepath, scenario.models);
        done();
    });
    it('Insert models.', function (done) {
        var scenario = this;
        var 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;
        var 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);
            assert.equal(User.prototype._connection._skip, 0);
            assert.equal(User.prototype._connection._limit, Infinity);
            done();
        }).limit(10).skip(2);
    });

    it('Count models.', function (done) {
        var scenario = this;
        var 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;
        var 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('Destroys model', function (done) {
        var scenario = this;
        var 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;
        var User = scenario.models.User;
        User.countAll(function (err, count) {
            assert.ifError(err);
            assert.equal(count, 1);
            done();
        });
    });

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