UNPKG

opencart-manager

Version:

Node.js package helping to manage your opencart store data easily.

285 lines (284 loc) 15.5 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = __importDefault(require("../../index")); const config_1 = require("../config"); const deleting = false; let opencart; const id = {}; // const entity: { [K: string]: IEntity } = {}; test("Connecting to database", () => __awaiter(void 0, void 0, void 0, function* () { opencart = yield index_1.default(config_1.testDbCredentials, { tablePrefix: "oc_", }); })); test("Create and insert a new product", () => __awaiter(void 0, void 0, void 0, function* () { const product = opencart.product.create(); expect(product.id).toBeUndefined(); product.setData({ price: 1212, image: "catalog/demo/iphone_5.jpg" }); product.setDescription({ languageId: 1, name: "тестовый продукт" }); product.setDescription({ languageId: 2, name: "test product" }); yield product.insert(); expect(product.id).toBeInteger(); expect(product.data.price).toBe(1212); expect(product.description[1].name).toBe("тестовый продукт"); expect(product.description[2].name).toBe("test product"); id.product = product.id; })); test("Create and insert a new product special", () => __awaiter(void 0, void 0, void 0, function* () { const product = yield opencart.product.extract({ productId: id.product }); expect(product === null || product === void 0 ? void 0 : product.id).toBe(id.product); const productSpecial = opencart.product.special.create(product); productSpecial.setData({ price: 999 }); yield productSpecial.insert(); expect(productSpecial.id).toBeInteger(); expect(productSpecial.data.price).toBe(999); id.productSpecial = productSpecial.id; })); test("Create and insert a new product image", () => __awaiter(void 0, void 0, void 0, function* () { const product = yield opencart.product.extract({ productId: id.product }); expect(product === null || product === void 0 ? void 0 : product.id).toBe(id.product); const productImage = opencart.product.image.create(product); productImage.setData({ image: "catalog/demo/iphone_6.jpg" }); yield productImage.insert(); expect(productImage.id).toBeInteger(); expect(productImage.data.image).toBe("catalog/demo/iphone_6.jpg"); id.productImage = productImage.id; })); test("Create and insert a new category", () => __awaiter(void 0, void 0, void 0, function* () { const category = opencart.category.create(); expect(category.id).toBeUndefined(); category.setData({ image: "test/image.png" }); category.setDescription({ languageId: 1, name: "тестовая категория" }); category.setDescription({ languageId: 2, name: "test category" }); yield category.insert(); expect(category.id).toBeInteger(); expect(category.data.image).toBe("test/image.png"); expect(category.description[1].name).toBe("тестовая категория"); expect(category.description[2].name).toBe("test category"); id.category = category.id; })); test("Create and insert a new option", () => __awaiter(void 0, void 0, void 0, function* () { const option = opencart.option.create(); expect(option.id).toBeUndefined(); option.setData({ type: "select" }); option.setDescription({ languageId: 1, name: "тестовая опция" }); option.setDescription({ languageId: 2, name: "test option" }); yield option.insert(); expect(option.id).toBeInteger(); expect(option.data.type).toBe("select"); expect(option.description[1].name).toBe("тестовая опция"); expect(option.description[2].name).toBe("test option"); id.option = option.id; })); test("Create and insert a new option value", () => __awaiter(void 0, void 0, void 0, function* () { const option = yield opencart.option.extract({ optionId: id.option }); expect(option === null || option === void 0 ? void 0 : option.id).toBeInteger(); const optionValue = opencart.option.value.create(option); expect(optionValue.id).toBeUndefined(); optionValue.setData({ image: "test/image.png" }); optionValue.setDescription({ languageId: 1, name: "значение тест опции" }); optionValue.setDescription({ languageId: 2, name: "test option value" }); yield optionValue.insert(); expect(optionValue.id).toBeInteger(); expect(optionValue.data.image).toBe("test/image.png"); expect(optionValue.description[1].name).toBe("значение тест опции"); expect(optionValue.description[2].name).toBe("test option value"); id.optionValue = optionValue.id; })); test("Create and insert a new attribute group", () => __awaiter(void 0, void 0, void 0, function* () { const attributeGroup = opencart.attribute.group.create(); expect(attributeGroup.id).toBeUndefined(); attributeGroup.setData({ sortOrder: 12 }); attributeGroup.setDescription({ languageId: 1, name: "группа аттрибутов" }); attributeGroup.setDescription({ languageId: 2, name: "attribute group" }); yield attributeGroup.insert(); expect(attributeGroup.id).toBeInteger(); expect(attributeGroup.data.sortOrder).toBe(12); expect(attributeGroup.description[1].name).toBe("группа аттрибутов"); expect(attributeGroup.description[2].name).toBe("attribute group"); id.attributeGroup = attributeGroup.id; })); test("Create and insert a new attribute", () => __awaiter(void 0, void 0, void 0, function* () { const attributeGroup = yield opencart.attribute.group.extract({ attributeGroupId: id.attributeGroup, }); expect(attributeGroup === null || attributeGroup === void 0 ? void 0 : attributeGroup.id).toBe(id.attributeGroup); const attribute = opencart.attribute.create(attributeGroup); expect(attribute.id).toBeUndefined(); attribute.setData({ sortOrder: 14 }); attribute.setDescription({ languageId: 1, name: "тестовый аттрибут" }); attribute.setDescription({ languageId: 2, name: "test attribute" }); yield attribute.insert(); expect(attribute.id).toBeInteger(); expect(attribute.data.sortOrder).toBe(14); expect(attribute.description[1].name).toBe("тестовый аттрибут"); expect(attribute.description[2].name).toBe("test attribute"); id.attribute = attribute.id; })); test("Create and insert a new manufacturer", () => __awaiter(void 0, void 0, void 0, function* () { const manufacturer = opencart.manufacturer.create(); expect(manufacturer.id).toBeUndefined(); manufacturer.setData({ name: "Производитель тест" }); manufacturer.setDescription({ languageId: 1, name: "Производитель тест" }); manufacturer.setDescription({ languageId: 2, name: "Test manufacturer" }); yield manufacturer.insert(); expect(manufacturer.id).toBeInteger(); expect(manufacturer.data.name).toBe("Производитель тест"); expect(manufacturer.description[1].name).toBe("Производитель тест"); expect(manufacturer.description[2].name).toBe("Test manufacturer"); id.manufacturer = manufacturer.id; })); test("Attach category to product", () => __awaiter(void 0, void 0, void 0, function* () { const category = yield opencart.category.extract({ categoryId: id.category, }); const product = yield opencart.product.extract({ productId: id.product }); expect(category === null || category === void 0 ? void 0 : category.id).toBe(id.category); expect(product === null || product === void 0 ? void 0 : product.id).toBe(id.product); yield product.toCategory(category); expect(product.id).toBe(id.product); expect(category.id).toBe(id.category); })); test("Attach attribute to product", () => __awaiter(void 0, void 0, void 0, function* () { const attribute = yield opencart.attribute.extract({ attributeId: id.attribute, }); const product = yield opencart.product.extract({ productId: id.product }); expect(attribute === null || attribute === void 0 ? void 0 : attribute.id).toBe(id.attribute); expect(product === null || product === void 0 ? void 0 : product.id).toBe(id.product); yield product.setAttribute(attribute, { languageId: 1, text: "100ГБ тест", }); yield product.setAttribute(attribute, { languageId: 2, text: "100Gb test", }); expect(product.id).toBe(id.product); })); test("Create and insert an option to product attachment", () => __awaiter(void 0, void 0, void 0, function* () { const product = yield opencart.product.extract({ productId: id.product }); const option = yield opencart.option.extract({ optionId: id.option }); expect(product === null || product === void 0 ? void 0 : product.id).toBe(id.product); expect(option === null || option === void 0 ? void 0 : option.id).toBe(id.option); const productOption = yield opencart.product.option .create(product, option) .insert(); productOption.setData({ required: 0 }); yield productOption.update(); expect(productOption.data.required).toBe(0); expect(productOption.id).toBeInteger(); id.productOption = productOption.id; })); test("Create and insert an option value to product option attachment", () => __awaiter(void 0, void 0, void 0, function* () { const productOption = yield opencart.product.option.extract({ productOptionId: id.productOption, }); const optionValue = yield opencart.option.value.extract({ optionValueId: id.optionValue, }); expect(productOption === null || productOption === void 0 ? void 0 : productOption.id).toBe(id.productOption); expect(optionValue === null || optionValue === void 0 ? void 0 : optionValue.id).toBe(id.optionValue); const productOptionValue = yield opencart.product.option.value.create(productOption, optionValue); productOptionValue.setData({ price: 222 }); yield productOptionValue.insert(); expect(productOptionValue.id).toBeInteger(); expect(productOptionValue.data.price).toBe(222); id.productOptionValue = productOptionValue.id; })); test("", () => { }); if (deleting) { test("Extract and delete an attribute", () => __awaiter(void 0, void 0, void 0, function* () { const attribute = yield opencart.attribute.extract({ attributeId: id.attribute, }); expect(attribute === null || attribute === void 0 ? void 0 : attribute.id).toBe(id.attribute); yield attribute.delete(); expect(attribute.id).toBeUndefined(); })); test("Extract and delete an attribute group", () => __awaiter(void 0, void 0, void 0, function* () { const attributeGroup = yield opencart.attribute.group.extract({ attributeGroupId: id.attributeGroup, }); expect(attributeGroup === null || attributeGroup === void 0 ? void 0 : attributeGroup.id).toBe(id.attributeGroup); yield attributeGroup.delete(); expect(attributeGroup.id).toBeUndefined(); })); test("Extract and delete a product", () => __awaiter(void 0, void 0, void 0, function* () { const product = yield opencart.product.extract({ productId: id.product, }); expect(product === null || product === void 0 ? void 0 : product.id).toBe(id.product); yield product.delete(); expect(product.id).toBeUndefined(); })); test("Extract and delete a category", () => __awaiter(void 0, void 0, void 0, function* () { const category = yield opencart.category.extract({ categoryId: id.category, }); expect(category === null || category === void 0 ? void 0 : category.id).toBe(id.category); yield category.delete(); expect(category.id).toBeUndefined(); })); test("Extract and delete an option value to product option attachment", () => __awaiter(void 0, void 0, void 0, function* () { const productOptionValue = yield opencart.product.option.value.extract({ productOptionValueId: id.productOptionValue, }); expect(productOptionValue === null || productOptionValue === void 0 ? void 0 : productOptionValue.id).toBe(id.productOptionValue); yield productOptionValue.delete(); expect(productOptionValue.id).toBeUndefined(); })); test("Extract and delete an option to product attachment", () => __awaiter(void 0, void 0, void 0, function* () { const productOption = yield opencart.product.option.extract({ productOptionId: id.productOption, }); expect(productOption === null || productOption === void 0 ? void 0 : productOption.id).toBe(id.productOption); yield productOption.delete(); expect(productOption.id).toBeUndefined(); })); test("Extract and delete an option value", () => __awaiter(void 0, void 0, void 0, function* () { const optionValue = yield opencart.option.value.extract({ optionValueId: id.optionValue, }); expect(optionValue === null || optionValue === void 0 ? void 0 : optionValue.id).toBe(id.optionValue); yield optionValue.delete(); expect(optionValue.id).toBeUndefined(); })); // test("Extract and delete a product special", async () => { // const productSpecial = await opencart.product.special.extract({ // productSpecialId: id.productSpecial, // }); // expect(productSpecial?.id).toBe(id.productSpecial); // await productSpecial!.delete(); // expect(productSpecial!.id).toBeUndefined(); // }); test("Extract and delete an option", () => __awaiter(void 0, void 0, void 0, function* () { const option = yield opencart.option.extract({ optionId: id.option }); expect(option.id).toBe(id.option); yield option.delete(); expect(option.id).toBeUndefined(); })); test("Extract and delete manufacturer", () => __awaiter(void 0, void 0, void 0, function* () { const manufacturer = yield opencart.manufacturer.extract({ manufacturerId: id.manufacturer, }); expect(manufacturer.id).toBe(id.manufacturer); yield manufacturer.delete(); expect(manufacturer.id).toBeUndefined(); })); } test("Close the connection", () => __awaiter(void 0, void 0, void 0, function* () { opencart.destroyConnection(); }));