Your IP : 216.73.217.95


Current Path : /home/deltalab/PMS/ims-connector/test/
Upload File :
Current File : //home/deltalab/PMS/ims-connector/test/product.test.js

/**
 * Product connection tests
 * 
 * Product creation, update and removal
 */

// DEPENDENCIES ====================================
const { expect, assert } = require('chai');
const { dotenv } = require('dotenv').config();
const mongoose = require('mongoose');
const fs = require('fs');
const path = require('path');

// RESOURCES =======================================
const productAdapter = require('../logic/shopify/product');
const { productModel } = require("../models/mongoose/product");
const { productInventoryModel } = require("../models/mongoose/product");

// TEST OBJECTS ====================================
// Canon LEGRIA HF M52
const imsgid = "gid://shopify/Product/6972292989104";

const myproduct = new productModel();
myproduct.title = "Mocha Test Product";
myproduct.description = "mocha test description";
myproduct.customDescription = "mocha test custom description";
myproduct.listingIds = [];
myproduct.partnerId = "6125199597b1612d8071eafd";
myproduct.media = [];
myproduct.price = 12.34;
myproduct.imsgid = "";
myproduct.sku = "TEST123";
myproduct.barcode = "123123123123";
myproduct.weight = 1;
myproduct.size = 1;
myproduct.requiresShipping = true;
myproduct.inventoryLevels = [];

const deltaInventory = new productInventoryModel();
deltaInventory.warehouseId = "615369c11200097db30d2004";
deltaInventory.amount = 12;

myproduct.inventoryLevels.push(deltaInventory);

// TEST CASES =======================================
describe('products', function () {
  this.timeout(10000);  // increased timeout because shopify is slow as fuk

  // Initialization --------------------------------
  before((done) => {
    done();
  });

  it('get product', async () => {
    const product = await productAdapter.getProduct(imsgid);
    expect(product).to.exist;
    console.log(product);
  });

  it('create product', async () => {
    // create 
    const productId = await productAdapter.createProduct(myproduct);
    expect(productId).to.exist;
    console.log(`product created: ${productId}`);
    // get
    const product = await productAdapter.getProduct(productId);
    expect(product).to.exist;
    console.log(product);
    // check
    expect(product.title).to.equals(myproduct.title);
    expect(product.description).to.equals(myproduct.description);
  });

  it('update product', async () => {
    // create 
    const productId = await productAdapter.createProduct(myproduct);
    expect(productId).to.exist;
    console.log(`product created: ${productId}`);
    // change data
    myproduct.imsgid = productId;
    myproduct.title = "Mocha Test Product Updated";
    myproduct.description = "mocha test description updated";
    const partnerInventory = new productInventoryModel();
    partnerInventory.warehouseId = "618cde8f6021cf2b0c0f2b92";
    partnerInventory.amount = 8;
    myproduct.inventoryLevels.push(partnerInventory);

    // update
    const updatedProductId = await productAdapter.updateProduct(myproduct);
    expect(updatedProductId).to.exist;
    expect(updatedProductId).to.equals(productId);
    // get
    const product = await productAdapter.getProduct(productId);
    expect(product).to.exist;
    console.log(product);
    // check
    expect(product.title).to.equals(myproduct.title);
    expect(product.description).to.equals(myproduct.description);
    //expect(product.quantity).to.equals(myproduct.quantity);
  });

  it('delete product', async () => {
    // create 
    const productId = await productAdapter.createProduct(myproduct);
    expect(productId).to.exist;
    console.log(`product created: ${productId}`);
    // delete
    const deletedProductId = await productAdapter.deleteProduct(productId);
    expect(deletedProductId).to.exist;
    expect(deletedProductId).to.equals(productId);
    // get
    const product = await productAdapter.getProduct(productId);
    expect(product).to.not.exist;
  });

  it('base64 to bytes', async () => {
    const filePath = "/Users/patam/Documents/Delta/INDACO/workspace/partner-manager-backend/test/testpicture.jpg";
    const fileStats = fs.statSync(filePath);
    const filename = path.basename(filePath);
    const pictureSize = new String(fileStats.size);
    // sanity check
    if (!fileStats.isFile()) throw new Error(`invalid picture path ${filePath}: not a file`);
    console.log('reading');
    // read the file as base64
    const fileData = fs.readFileSync(filePath, { encoding: 'base64' });
    // from base64 to bytes
    const buf = Buffer.from(fileData, 'base64');
    console.log('finished');
  })

  it('upload product picture', async () => {
    const productId = "gid://shopify/Product/6734676787376";
    const picturePath = "/Users/patam/Documents/Delta/INDACO/workspace/ims-connector/test/testpicture.jpg";
    try {
      // get picture stats
      const stats = fs.statSync(picturePath);
      const filename = path.basename(picturePath);
      const pictureSize = new String(stats.size);
      const fileType = "image/jpeg";
      // sanity check
      if (!stats.isFile()) throw new Error(`invalid picture path ${picturePath}: not a file`);
      console.log(`loading file ${picturePath}`);
      const pictureData = fs.readFileSync(picturePath, { encoding: 'base64' });
      console.log(`loading file ${pictureData.length}`);
      const pictureId = await productAdapter.uploadProductMedia(productId, filename, pictureData, fileType);
      console.log(`media imsgid is ${pictureId}`);
      await productAdapter.getProduct(productId);
      await new Promise(resolve => setTimeout(resolve, 5000));
      await productAdapter.getProduct(productId);
    } catch (err) {
      console.error(err);
    }
  });

  it('get media', async function () {
    const productId = "gid://shopify/Product/6734676787376";
    const product = await productAdapter.getProduct(productId);
    console.log(product.media); // no media expected here, because the query would be to big
    const media = await productAdapter.getProductMedia(productId);
    console.log(media); // all the media expected here
  });

  it('delete media', async function () {
    const productId = "gid://shopify/Product/6734676787376";
    const mediaIds = [];
    const deletedIds = await productAdapter.deleteProductMedia(productId, mediaIds);
    console.log(deletedIds);
  });

  it('adjust quantity', async function () {
    const productId = "gid://shopify/Product/6734676787376";
    const quantity = 0;
    let product = await productAdapter.getProductRawData(productId);
    console.log(product);
    const variant = product.variants.edges[0].node;
    const inventoryLevel = variant.inventoryItem.inventoryLevels.edges[0].node;
    const level = await productAdapter.inventoryAdjustQuantity(inventoryLevel.id, -10770);
    product = await productAdapter.getProductRawData(productId);
    console.log(product);

  });
});