require("should");
var helper = require("../../helper");

var purchaseRequestDataUtil = require("../../data-util/garment-purchasing/purchase-request-data-util");
var purchaseOrderDataUtil = require("../../data-util/garment-purchasing/purchase-order-data-util");
var validatePO = require("dl-models").validator.garmentPurchasing.garmentPurchaseOrder;
var PurchaseOrderManager = require("../../../src/managers/garment-purchasing/purchase-order-manager");
var purchaseOrderManager = null;

before('#00. connect db', function (done) {
    helper.getDb()
        .then(db => {
            purchaseOrderManager = new PurchaseOrderManager(db, {
                username: 'dev'
            });
            done();
        })
        .catch(e => {
            done(e);
        });
});

var listPurchaseOrder = [];
it('#01. should success when get new data purchase-request ', function (done) {
    purchaseRequestDataUtil.getNewTestData()
        .then(purchaseRequest => {
            listPurchaseOrder.push(purchaseRequest);
            done();
        })
        .catch(e => {
            done(e);
        });
});
var createdId = {};
it('#02. should success when create new purchase-order', function (done) {
    purchaseOrderManager.createMultiple(listPurchaseOrder)
        .then(data => {
            data.should.be.instanceof(Array);
            createdId = data[0];
            done();
        })
        .catch(e => {
            done(e);
        });
});

var createdData;
var categoryId;
it(`#03. should success when get created data with id`, function (done) {
    purchaseOrderManager.getSingleById(createdId)
        .then((data) => {
            data.should.instanceof(Object);
            validatePO(data);
            createdData = data;
            categoryId = createdData.items[0].categoryId
            done();
        })
        .catch((e) => {
            done(e);
        });
});

it("#04. should success when read data", function (done) {
    purchaseOrderManager.read({
        filter: {
            _id: createdId
        }
    })
        .then((documents) => {
            //process documents
            documents.should.have.property("data");
            documents.data.should.be.instanceof(Array);
            documents.data.length.should.not.equal(0);
            done();
        })
        .catch((e) => {
            done(e);
        });
});

it('#05. should success when get data by keyword', function (done) {
    var shipmentDate = new Date();
    var moment = require('moment');
    purchaseOrderManager.getPurchaseOrderByTag(categoryId,"#buyer1",moment(shipmentDate).format("YYYY-MM-DD"))
        .then(data => {
            data.should.be.instanceof(Array);
            done();
        })
        .catch(e => {
            done(e);
        });
});

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


it(`#07. should _deleted=true`, function (done) {
    purchaseOrderManager.getSingleByQuery({
        _id: createdId
    })
        .then((data) => {
            validatePO(data);
            data._deleted.should.be.Boolean();
            data._deleted.should.equal(true);
            done();
        })
        .catch((e) => {
            done(e);
        });
});
@@ -3402,28 +3402,617 @@
     done(e);%0A        %7D);%0A%7D);
+%0A%0Ait(%22#08 should error when create with empty data%22, function (done) %7B%0A        purchaseOrderManager.create(%7B%7D)%0A            .then((id) =%3E %7B%0A                done(%22Should not be able to create with empty data%22);%0A            %7D)%0A            .catch((e) =%3E %7B%0A                try %7B%0A                    e.name.should.equal(%22ValidationError%22);%0A                    e.should.have.property(%22errors%22);%0A                    e.errors.should.instanceof(Object);%0A                    done();%0A                %7D%0A                catch (ex) %7B%0A                    done(e);%0A                %7D%0A            %7D);%0A    %7D);

add unit test