var helper = require("../helper");
var MachineManager = require("../../src/managers/master/machine-manager");
var instanceManager = null;
require("should");

function getData() {
    var Machine = require('dl-models').master.Machine;
    var machine = new Machine();

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

    machine.name = `name [${code}]`;
    machine.unit = `division [${code}]`;
    machine.process = `subdivision [${code}]`;
    machine.manufacture=`manufacture [${code}]`;
    machine.year = now.getFullYear();
    machine.machineCondition=`machine condition [${code}]`;

    return machine;
}

before('#00. connect db', function (done) {
    helper.getDb()
        .then(db => {
            instanceManager = new MachineManager(db, {
                username: 'unit-test'
            });
            done();
        })
        .catch(e => {
            done(e);
        })
});

it('#01. should success when read data', function (done) {
    instanceManager.read()
        .then(documents => {
            //process documents
            documents.data.should.be.instanceof(Array);
            done();
        })
        .catch(e => {
            done(e);
        })
});

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

var createdData;
it(`#03. should success when get created data with id`, function (done) {
    instanceManager.getSingleByQuery({ _id: createdId })
        .then(data => {
            // validate.product(data);
            data.should.instanceof(Object);
            createdData = data;
            done();
        })
        .catch(e => {
            done(e);
        })
});

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

    createdData.name += '[updated]';
    createdData.unit += '[updated]';
    createdData.process += '[updated]';
    createdData.manufacture += '[updated]';
    createdData.year += '[updated]';
    createdData.machineCondition += '[updated]';

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

it(`#05. should success when get updated data with id`, function (done) {
    instanceManager.getSingleByQuery({ _id: createdId })
        .then(data => {
            // validate.product(data);
            data.code.should.equal(createdData.code);
            data.name.should.equal(createdData.name);
            data.unit.should.equal(createdData.unit);
            data.process.should.equal(createdData.process);
            data.manufacture.should.equal(createdData.manufacture);
            data.year.should.equal(createdData.year);
            data.machineCondition.should.equal(createdData.machineCondition);
            done();
        })
        .catch(e => {
            done(e);
        })
});

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

it(`#07. should _deleted=true`, function (done) {
    instanceManager.getSingleByQuery({ _id: createdId })
        .then(data => {
            // validate.product(data);
            data._deleted.should.be.Boolean();
            data._deleted.should.equal(true);
            done();
        })
        .catch(e => {
            done(e);
        })
});
@@ -135,49 +135,194 @@
 ll;%0A
-require(%22should%22);%0A%0Afunction getData() %7B%0A
+var UnitUtil = require('../data-util/master/unit-data-util');%0Arequire(%22should%22);%0A%0Afunction getData() %7B%0A    return Promise.resolve(UnitUtil.getTestData())%0A        .then(unit =%3E %7B%0A        
     
@@ -372,16 +372,24 @@
 achine;%0A
+        
     var 
@@ -410,24 +410,32 @@
 Machine();%0A%0A
+        
     var now 
@@ -444,24 +444,32 @@
 new Date();%0A
+        
     var stam
@@ -488,16 +488,24 @@
 00 %7C 0;%0A
+        
     var 
@@ -528,24 +528,32 @@
 tring(36);%0A%0A
+        
     machine.
@@ -569,32 +569,40 @@
 ame %5B$%7Bcode%7D%5D%60;%0A
+        
     machine.unit
@@ -605,33 +605,64 @@
 unit
+Id
  = 
-%60division %5B$%7Bcode%7D%5D%60;%0A
+unit._id;%0A            machine.unit = unit;%0A        
     
@@ -696,32 +696,40 @@
 ion %5B$%7Bcode%7D%5D%60;%0A
+        
     machine.manu
@@ -757,24 +757,32 @@
 %5B$%7Bcode%7D%5D%60;%0A
+        
     machine.
@@ -815,24 +815,25 @@
     
-machine.machineC
+        machine.c
 ondi
@@ -838,24 +838,16 @@
 dition=%60
-machine 
 conditio
@@ -861,16 +861,24 @@
 de%7D%5D%60;%0A%0A
+        
     retu
@@ -889,16 +889,28 @@
 achine;%0A
+        %7D);%0A
 %7D%0A%0Abefor
@@ -2283,45 +2283,8 @@
 %5D';%0A
-    createdData.unit += '%5Bupdated%5D';%0A
     
@@ -2412,32 +2412,25 @@
 createdData.
-machineC
+c
 ondition += 

test model machine