'use strict';

const MONGODB = process.env.MONGODB || 'mongodb://localhost:27017/test';

const assert = require('assert');
const Mongolass = require('..');
const Db = Mongolass.Db;
const Schema = Mongolass.Schema;
const Model = Mongolass.Model;
const mongolass = new Mongolass(MONGODB);

describe('index.js', function () {
  before(function* () {
    yield mongolass.model('User').insertOne({ name: 'aaa', age: 2 });
    yield mongolass.model('User').insertOne({ name: 'bbb', age: 1 });
  });

  after(function* () {
    yield mongolass.model('User').remove();
    mongolass.disconnect();
  });

  it('connect', function* () {
    let error;
    let db = yield mongolass.connect();
    assert.ok(db instanceof Db);

    let db2 = yield mongolass.connect();
    assert.ok(db instanceof Db);
    assert.ok(db === db2);

    try {
      yield mongolass.connect('mongodb://localhost:27018/test');
    } catch (e) {
      error = e;
    }
    assert.deepEqual(error.message, 'Already connected to mongodb://localhost:27017/test, please create another connection.');
  });

  it('disconnect', function* () {
    let mongolass2 = new Mongolass();
    yield mongolass2.connect(MONGODB);
    mongolass2.disconnect();
    assert.deepEqual(mongolass2._db, null);
    assert.deepEqual(mongolass2._conn, null);

    let mongolass3 = new Mongolass(MONGODB);
    yield mongolass3.connect();
    mongolass3.disconnect();
    assert.deepEqual(mongolass3._db, null);
    assert.deepEqual(mongolass3._conn, null);

    let mongolass4 = new Mongolass(MONGODB);
    mongolass4.disconnect();
  });

  it('schema', function* () {
    let UserSchema;
    let error;
    try {
      UserSchema = mongolass.schema('User', 'aaa');
    } catch(e) {
      error = e;
    }
    assert.deepEqual(error.message, 'Wrong schemaJSON for schema: User');

    UserSchema = mongolass.schema('User', {
      name: { type: 'string' },
      age: { type: 'number', range: [0, 100] }
    });
    assert.ok(UserSchema instanceof Schema);
    assert.ok(UserSchema === mongolass.schema('User'));
    try {
      UserSchema = mongolass.schema('User2');
    } catch(e) { error = e; }
    assert.deepEqual(error.message, 'No schema: User2');
  });

  it('model', function* () {
    let User;
    let User2;
    let error;
    let UserSchema = mongolass.schema('User', {
      name: { type: 'string' },
      age: { type: 'number', range: [0, 100] }
    });
    try {
      User = mongolass.model('User', 'aaa');
    } catch(e) {
      error = e;
    }
    assert.deepEqual(error.message, 'Wrong schemaJSON for schema: UserSchema');

    User = mongolass.model('User', UserSchema);
    assert.ok(User instanceof Model);
    assert.ok(User === mongolass.model('User'));
    assert.ok(User._schema._name === 'User');

    User2 = mongolass.model('User', {
      name: { type: 'string' },
      age: { type: 'number', range: [0, 100] }
    });
    assert.ok(User !== User2);
    assert.ok(User2 instanceof Model);
    assert.ok(User2 === mongolass.model('User'));
    assert.ok(User2._schema._name === 'UserSchema');
  });

  it('plugin', function* () {
    let error;
    let User = mongolass.model('User');
    try {
      mongolass.plugin('filter', function (result, key) {
        return result.map(function (item) {
          return item[key];
        });
      });
    } catch(e) {
      error = e;
    }
    assert.deepEqual(error.message, 'Wrong plugin name or hooks');

    mongolass.plugin('filter', {
      afterFind: function (result, key) {
        return result.map(function (item) {
          return item[key];
        });
      }
    });
    mongolass.plugin('idToString', {
      afterFind: function (ids) {
        return ids.map(function (id) {
          return id.toString();
        });
      }
    });
    let usernames = yield User.find().filter('_id').idToString();
    assert.deepEqual(usernames[0].length, 24);
    assert.deepEqual(usernames[1].length, 24);
    assert.deepEqual(typeof usernames[0], 'string');
    assert.deepEqual(typeof usernames[1], 'string');
  });
});

@@ -989,38 +989,23 @@
  to 
-mongodb://localhost:27017/test
+' + MONGODB + '
 , pl

correct connect test case assertion