var should = require('should');
var helper = require('../helper');
var validate = require('bateeq-models').validator.article;
var manager;

function getData() {
    var ArticleMotif = require('bateeq-models').article.ArticleMotif;
    var articleMotif = new ArticleMotif();

    var now = new Date();
    var stamp = now / 1000 | 0;
    var code = stamp.toString(36);

    articleMotif.code = code;
    articleMotif.name = `name[${code}]`;
    articleMotif.description = `description for ${code}`;

    return articleMotif;
}

before('#00. connect db', function(done) {
    helper.getDb()
        .then(db => {
            var ArticleMotifManager = require('../../src/managers/article/article-motif-manager');
            manager = new ArticleMotifManager(db, {
                username: 'unit-test'
            });
            done();
        })
        .catch(e => {
            done(e);
        })
});

var createdId;
it('#01. should success when create new data', function(done) {
    var data = getData();
    manager.create(data)
        .then(id => {
            id.should.be.Object();
            createdId = id;
            done();
        })
        .catch(e => {
            done(e);
        })
});

var createdData;
it(`#02. should success when get created data with id`, function(done) {
    manager.getSingleByQuery({_id:createdId})
        .then(data => {
            validate.articleMotif(data);
            createdData = data;
            done();
        })
        .catch(e => {
            done(e);
        })
});

it(`#03. should success when update created data`, function(done) {

    createdData.code += '[updated]';
    createdData.name += '[updated]';
    createdData.description += '[updated]';

    manager.update(createdData)
        .then(id => {
            createdId.toString().should.equal(id.toString());
            done();
        })
        .catch(e => {
            done(e);
        });
});

it(`#04. should success when get updated data with id`, function(done) {
    manager.getSingleByQuery({_id:createdId})
        .then(data => {
            validate.articleMotif(data);
            data.code.should.equal(createdData.code);
            data.name.should.equal(createdData.name);
            data.description.should.equal(createdData.description);
            done();
        })
        .catch(e => {
            done(e);
        })
});

it(`#05. should success when delete data`, function(done) { 
    manager.delete(createdData)
        .then(id => {
            createdId.toString().should.equal(id.toString());
            done();
        })
        .catch(e => {
            done(e);
        });
});

it(`#06. should _deleted=true`, function(done) {
    manager.getSingleByQuery({_id:createdId})
        .then(data => {
            validate.articleMotif(data);
            data._deleted.should.be.Boolean();
            data._deleted.should.equal(true);
            done();
        })
        .catch(e => {
            done(e);
        })
});

it('#07. should error when create new data with same code', function(done) {
    var data = Object.assign({}, createdData);
    delete data._id;
    manager.create(data)
        .then(id => {
            id.should.be.Object();
            createdId = id;
            done("Should not be able to create data with same code");
        })
        .catch(e => {
            done();
        })
});

it('#08. should error when create new data with code is empty', function(done) {
    var data = Object.assign({}, createdData);
    delete data._id;
    data.code='';
    manager.create(data)
        .then(id => {
            id.should.be.Object();
            createdId = id;
            done("Should not be able to create data with code is empty");
        })
        .catch(e => {
            done();
        })
});

it('#09. should error when create new data with name is empty ', function(done) {
    var data = Object.assign({}, createdData);
    delete data._id;
    data.name='';
    manager.create(data)
        .then(id => {
            id.should.be.Object();
            createdId = id;
            done("Should not be able to create data with same code");
        })
        .catch(e => {
            done();
        })
});
@@ -3333,32 +3333,86 @@
   .catch(e =%3E %7B%0A
+               e.errors.should.have.property('code');%0A
             done
@@ -3433,17 +3433,17 @@
 %0A%7D);
+ 
 %0A%0Ait('#
-08
+9
 . sh
@@ -3458,151 +3458,55 @@
 or w
-hen create new data with code is empty', function(done) %7B%0A    var data = Object.assign(%7B%7D, createdData);%0A    delete data._id;%0A    data.code='';
+ith property code and name ', function(done) %7B 
 %0A   
@@ -3513,36 +3513,34 @@
  manager.create(
-data
+%7B%7D
 )%0A        .then(
@@ -3550,71 +3550,9 @@
 =%3E %7B
-%0A            id.should.be.Object();%0A            createdId = id;
+ 
 %0A   
@@ -3589,41 +3589,41 @@
 ble 
-to create data with code is empty
+error with property code and name
 %22);%0A
@@ -3646,32 +3646,33 @@
    .catch(e =%3E %7B
+ 
 %0A            don
@@ -3663,32 +3663,27 @@
 %0A           
- done();
+try
 %0A        %7D)%0A
@@ -3683,398 +3683,196 @@
     
-%7D)%0A%7D);%0A%0Ait('#09. should error when create new data with name is empty ', function(done) %7B%0A    var data = Object.assign(%7B%7D, createdData);%0A    delete data._id;%0A    data.name='';%0A    manager.create(data)%0A        .then(id =%3E %7B%0A            id.should.be.Object();%0A            createdId = id;%0A            done(%22Should not be able to create data with same code%22);%0A        %7D)%0A        .catch(e =%3E %7B%0A 
+   %7B%0A               e.errors.should.have.property('code');%0A               e.errors.should.have.property('name');  %0A           %7Dcatch(ex)%0A           %7B%0A               done(ex);%0A           %7D%0A
     
@@ -3880,28 +3880,33 @@
       done();%0A        %7D)%0A%7D);
+%0A%0A%0A%0A%0A

check property error in unit test