let assert = require('assert');
import Redis from '../redis';
import Utils from '../utils';
import mockObjects from "../mockObjects";
import MysqlMock from "../mocks/mysqlMock";
import * as Bluebird from "bluebird"

import * as IORedis from "ioredis";
import MockObjects from '../mockObjects';

let config = {
    statusCheckInterval: 1,
    keys: {
        rediskey_connections: "test:"
    }
};

describe('test mysql', function () {

    it('create destroy', function (done) {
        class SpecialCase extends MysqlMock{
            public destroy(){
                done();
                return super.destroy();
            }
        }

        let redis = MockObjects.makeMysql(config, SpecialCase);
        redis.destroy();
    });

    it('status change', function (done) {
        let getStatus = function(){
            return "default";
        }
        let destroy = function(){}
        class SpecialCase extends MysqlMock{
            private i = 0;

            public destroy(): void {
                done();
                return super.destroy();
            }

            public ping(options?: any, callback?: (error: any) => void){
                let status = getStatus();

                this.i += 1;
                if(status == "default") this.i = 1;//Start to increment just after change getStatus function
                switch(this.i){
                    case 1:
                        //We cant know if is first or secand call
                        if(typeof callback === "function") callback(null);
                        break;
                    case 2:
                        assert.equal(getStatus(), "Connected")
                        assert.equal(mysql.isFunctional(), true)
                        if(typeof callback === "function") callback(new Error());
                        break;
                    case 3:
                        assert.equal(getStatus(), "Error")  
                        assert.equal(mysql.isFunctional(), false)
                        destroy();                    
                        break;
                }
            }
        }

        let mysql = MockObjects.makeMysql(config, SpecialCase);        
        getStatus = function() { return mysql.getStatus(); };
        destroy = function() { mysql.destroy(); };
    });
});        