Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa): Improve product update performances #3417

Merged
merged 26 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions integration-tests/api/__tests__/batch-jobs/product/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe("Product import batch job", () => {
await batchJobSeeder(dbConnection)
await adminSeeder(dbConnection)
await userSeeder(dbConnection)
await simpleProductCollectionFactory(dbConnection, [
await simpleProductCollectionFactory(dbConnection, [
{
handle: collectionHandle1,
},
Expand Down Expand Up @@ -184,7 +184,7 @@ describe("Product import batch job", () => {
ean: null,
upc: null,
inventory_quantity: 10,
prices: [
prices: expect.arrayContaining([
expect.objectContaining({
currency_code: "eur",
amount: 100,
Expand All @@ -199,7 +199,7 @@ describe("Product import batch job", () => {
amount: 130,
region_id: "region-product-import-1",
}),
],
]),
options: expect.arrayContaining([
expect.objectContaining({
value: "option 1 value red",
Expand All @@ -211,24 +211,24 @@ describe("Product import batch job", () => {
}),
],
type: null,
images: [
images: expect.arrayContaining([
expect.objectContaining({
url: "test-image.png",
}),
],
options: [
]),
options: expect.arrayContaining([
expect.objectContaining({
title: "test-option-1",
}),
expect.objectContaining({
title: "test-option-2",
}),
],
tags: [
]),
tags: expect.arrayContaining([
expect.objectContaining({
value: "123_1",
}),
],
]),
collection: expect.objectContaining({
handle: collectionHandle1,
}),
Expand All @@ -250,7 +250,7 @@ describe("Product import batch job", () => {
ean: null,
upc: null,
inventory_quantity: 10,
prices: [
prices: expect.arrayContaining([
expect.objectContaining({
currency_code: "eur",
amount: 100,
Expand All @@ -265,7 +265,7 @@ describe("Product import batch job", () => {
amount: 130,
region_id: "region-product-import-1",
}),
],
]),
options: expect.arrayContaining([
expect.objectContaining({
value: "option 1 value red",
Expand All @@ -277,19 +277,19 @@ describe("Product import batch job", () => {
}),
],
type: null,
images: [
images: expect.arrayContaining([
expect.objectContaining({
url: "test-image.png",
}),
],
options: [
]),
options: expect.arrayContaining([
expect.objectContaining({
title: "test-option-1",
}),
expect.objectContaining({
title: "test-option-2",
}),
],
]),
tags: [],
collection: expect.objectContaining({
handle: collectionHandle1,
Expand Down
5 changes: 5 additions & 0 deletions packages/medusa-test-utils/src/mock-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export default {
},

getCustomRepository: function (repo) {
if (repo) {
repo["metadata"] = repo["metadata"] ?? {
columns: []
}
}
return repo;
},

Expand Down
107 changes: 56 additions & 51 deletions packages/medusa-test-utils/src/mock-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,30 @@ class MockRepo {
findAndCount,
del,
count,
insertBulk
insertBulk,
metadata
}) {
this.create_ = create;
this.update_ = update;
this.remove_ = remove;
this.delete_ = del;
this.softRemove_ = softRemove;
this.find_ = find;
this.findDescendantsTree_ = findDescendantsTree;
this.findOne_ = findOne;
this.findOneOrFail_ = findOneOrFail;
this.save_ = save;
this.findAndCount_ = findAndCount;
this.findOneWithRelations_ = findOneWithRelations;
this.insertBulk_ = insertBulk;
this.create_ = create
this.update_ = update
this.remove_ = remove
this.delete_ = del
this.softRemove_ = softRemove
this.find_ = find
this.findDescendantsTree_ = findDescendantsTree
this.findOne_ = findOne
this.findOneOrFail_ = findOneOrFail
this.save_ = save
this.findAndCount_ = findAndCount
this.findOneWithRelations_ = findOneWithRelations
this.insertBulk_ = insertBulk

this.metadata = metadata ?? {
columns: []
}
}

setFindOne(fn) {
this.findOne_ = fn;
this.findOne_ = fn
}

insertBulk = jest.fn().mockImplementation((...args) => {
Expand All @@ -42,83 +47,83 @@ class MockRepo {
})
create = jest.fn().mockImplementation((...args) => {
if (this.create_) {
return this.create_(...args);
return this.create_(...args)
}
return {};
});
return {}
})
softRemove = jest.fn().mockImplementation((...args) => {
if (this.softRemove_) {
return this.softRemove_(...args);
return this.softRemove_(...args)
}
return {};
});
return {}
})
remove = jest.fn().mockImplementation((...args) => {
if (this.remove_) {
return this.remove_(...args);
return this.remove_(...args)
}
return {};
});
return {}
})
update = jest.fn().mockImplementation((...args) => {
if (this.update_) {
return this.update_(...args);
return this.update_(...args)
}
});
})
findOneOrFail = jest.fn().mockImplementation((...args) => {
if (this.findOneOrFail_) {
return this.findOneOrFail_(...args);
return this.findOneOrFail_(...args)
}
});
})
findOneWithRelations = jest.fn().mockImplementation((...args) => {
if (this.findOneWithRelations_) {
return this.findOneWithRelations_(...args);
return this.findOneWithRelations_(...args)
}
});
})
findOne = jest.fn().mockImplementation((...args) => {
if (this.findOne_) {
return this.findOne_(...args);
return this.findOne_(...args)
}
});
})
findDescendantsTree = jest.fn().mockImplementation((...args) => {
if (this.findDescendantsTree_) {
return this.findDescendantsTree_(...args);
return this.findDescendantsTree_(...args)
}
});
})
findOneOrFail = jest.fn().mockImplementation((...args) => {
if (this.findOneOrFail_) {
return this.findOneOrFail_(...args);
return this.findOneOrFail_(...args)
}
});
})
find = jest.fn().mockImplementation((...args) => {
if (this.find_) {
return this.find_(...args);
return this.find_(...args)
}
});
})
softRemove = jest.fn().mockImplementation((...args) => {
if (this.softRemove_) {
return this.softRemove_(...args);
return this.softRemove_(...args)
}
});
})
save = jest.fn().mockImplementation((...args) => {
if (this.save_) {
return this.save_(...args);
return this.save_(...args)
}
return Promise.resolve(...args);
});
return Promise.resolve(...args)
})

findAndCount = jest.fn().mockImplementation((...args) => {
if (this.findAndCount_) {
return this.findAndCount_(...args);
return this.findAndCount_(...args)
}
return {};
});
return {}
})
delete = jest.fn().mockImplementation((...args) => {
if (this.delete_) {
return this.delete_(...args);
return this.delete_(...args)
}
return {};
});
return {}
})
}

export default (methods = {}) => {
return new MockRepo(methods);
};
return new MockRepo(methods)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
import { EventBusServiceMock } from "../../../../../services/__mocks__/event-bus"

describe("POST /admin/products/:id", () => {
describe("successfully updates a product", () => {
Expand All @@ -18,7 +17,7 @@ describe("POST /admin/products/:id", () => {
description: "Updated test description",
handle: "handle",
variants: [
{ id: IdMap.getId("variant_1"), title: "Green" },
{ id: IdMap.getId("testVariant"), title: "Green" },
{ title: "Blue" },
{ title: "Yellow" },
],
Expand Down Expand Up @@ -49,7 +48,6 @@ describe("POST /admin/products/:id", () => {
})

it("successfully updates variants and create new ones", async () => {
expect(ProductVariantServiceMock.delete).toHaveBeenCalledTimes(2)
expect(ProductVariantServiceMock.update).toHaveBeenCalledTimes(1)
expect(ProductVariantServiceMock.create).toHaveBeenCalledTimes(2)
})
Expand All @@ -74,7 +72,7 @@ describe("POST /admin/products/:id", () => {
)
expect(subject.status).toEqual(404)
expect(subject.error.text).toEqual(
`{"type":"not_found","message":"Variant with id: test_321 is not associated with this product"}`
`{"type":"not_found","message":"Variants with id: test_321 are not associated with this product"}`
)
})
})
Expand Down
Loading