var helper = require("../../helper");
var validator = require('dl-models').validator.master;
var validatorPurchasing = require('dl-models').validator.purchasing;
var UnitPaymentOrderManager = require("../../../src/managers/purchasing/unit-payment-order-manager");
var unitPaymentOrderManager = null;
var unitPaymentOrder = require("../../data-util/purchasing/unit-payment-order-data-util");
var unitReceiptNote = require('../../data-util/purchasing/unit-receipt-note-data-util');

require("should");

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

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

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


it('#02. should success when update data (remove item)', function (done) {
    delete createdData.items[0];
    unitPaymentOrderManager.update({ createdData })
        .then((id) => {
            id.should.be.Object();
            done();
        })
        .catch(e => {
            try {
                done();
            }
            catch (ex) {
                done(ex);
            }
        })
});

it('#03. should success when update data (add item)', function (done) {
    unitReceiptNote.getNewData()
        .then((data) => {
            var _item = {
                unitReceiptNoteId: data._id,
                unitReceiptNote: data
            }
            createdData.items.push(_item);
            unitPaymentOrderManager.update({ createdData })
        })
        .then((id) => {
            id.should.be.Object();
            done();
        })
        .catch((e) => {
            done(e);
        });
});
@@ -1612,34 +1612,32 @@
 rManager.update(
-%7B 
 createdData %7D)%0A 
@@ -1623,34 +1623,32 @@
 date(createdData
- %7D
 )%0A        .then(
@@ -2226,10 +2226,8 @@
 ate(
-%7B 
 crea
@@ -2233,18 +2233,16 @@
 atedData
- %7D
 )%0A      
@@ -2343,35 +2343,33 @@
 %0A        .catch(
-(e)
+e
  =%3E %7B%0A          
@@ -2366,32 +2366,132 @@
             
-done(e);
+try %7B%0A                done();%0A            %7D%0A            catch (ex) %7B%0A                done(ex);%0A            %7D
 %0A        %7D);
@@ -2481,21 +2481,20 @@
     %7D%0A        %7D)
-;
 %0A%7D);

update unit test