fix: package.json run test integration in packages (#7249)

This commit is contained in:
Adrien de Peretti
2024-05-06 19:25:37 +02:00
committed by GitHub
parent e7a3528367
commit a736e728b8
6 changed files with 42 additions and 16 deletions
+8
View File
@@ -0,0 +1,8 @@
---
"@medusajs/medusa": patch
"@medusajs/product": patch
"@medusajs/utils": patch
---
fix: Product categories repository and end points
+2 -2
View File
@@ -74,9 +74,9 @@
"lint:path": "eslint --ignore-path .eslintignore --ext .js,.ts,.tsx", "lint:path": "eslint --ignore-path .eslintignore --ext .js,.ts,.tsx",
"prettier": "prettier", "prettier": "prettier",
"jest": "jest", "jest": "jest",
"test": "turbo run test --concurrency=50% --no-daemon --no-cache --force", "test": "turbo run test --concurrency=50% --no-daemon --no-cache --force --filter='./packages/*' --filter='./packages/core/*' --filter='./packages/cli/*' --filter='./packages/modules/*' --filter='./packages/modules/providers/*'",
"test:chunk": "./scripts/run-workspace-unit-tests-in-chunks.sh", "test:chunk": "./scripts/run-workspace-unit-tests-in-chunks.sh",
"test:integration:packages": "turbo run test:integration --concurrency=50% --no-daemon --no-cache --force --filter='./packages/*'", "test:integration:packages": "turbo run test:integration --concurrency=50% --no-daemon --no-cache --force --filter='./packages/*' --filter='./packages/core/*' --filter='./packages/cli/*' --filter='./packages/modules/*' --filter='./packages/modules/providers/*'",
"test:integration:api": "turbo run test:integration:chunk --concurrency=50% --no-daemon --no-cache --force --filter=integration-tests-api", "test:integration:api": "turbo run test:integration:chunk --concurrency=50% --no-daemon --no-cache --force --filter=integration-tests-api",
"test:integration:plugins": "turbo run test:integration --concurrency=50% --no-daemon --no-cache --filter=integration-tests-plugins", "test:integration:plugins": "turbo run test:integration --concurrency=50% --no-daemon --no-cache --filter=integration-tests-plugins",
"test:integration:modules": "turbo run test:integration:chunk --concurrency=50% --no-daemon --no-cache --force --filter=integration-tests-modules", "test:integration:modules": "turbo run test:integration:chunk --concurrency=50% --no-daemon --no-cache --force --filter=integration-tests-modules",
@@ -9,7 +9,6 @@ import { MedusaError, upperCaseFirst } from "../../common"
export const dbErrorMapper = (err: Error) => { export const dbErrorMapper = (err: Error) => {
if (err instanceof NotFoundError) { if (err instanceof NotFoundError) {
console.log(err)
throw new MedusaError(MedusaError.Types.NOT_FOUND, err.message) throw new MedusaError(MedusaError.Types.NOT_FOUND, err.message)
} }
@@ -10,8 +10,8 @@ export const defaults = [
"created_at", "created_at",
"updated_at", "updated_at",
"metadata", "metadata",
"parent_category", "*parent_category",
"category_children", "*category_children",
] ]
export const allowed = [ export const allowed = [
@@ -132,7 +132,6 @@ moduleIntegrationTestRunner({
handle: "category-0", handle: "category-0",
mpath: "category-0.", mpath: "category-0.",
parent_category_id: null, parent_category_id: null,
parent_category: null,
category_children: [ category_children: [
expect.objectContaining({ expect.objectContaining({
id: "category-1", id: "category-1",
@@ -551,7 +550,6 @@ moduleIntegrationTestRunner({
handle: "category-0", handle: "category-0",
mpath: "category-0.", mpath: "category-0.",
parent_category_id: null, parent_category_id: null,
parent_category: null,
category_children: [ category_children: [
expect.objectContaining({ expect.objectContaining({
id: "category-1", id: "category-1",
@@ -803,7 +801,6 @@ moduleIntegrationTestRunner({
handle: "category-0", handle: "category-0",
mpath: "category-0.", mpath: "category-0.",
parent_category_id: null, parent_category_id: null,
parent_category: null,
category_children: [ category_children: [
expect.objectContaining({ expect.objectContaining({
id: "category-1", id: "category-1",
@@ -865,7 +862,6 @@ moduleIntegrationTestRunner({
handle: "category-0", handle: "category-0",
mpath: "category-0.", mpath: "category-0.",
parent_category_id: null, parent_category_id: null,
parent_category: null,
category_children: [ category_children: [
expect.objectContaining({ expect.objectContaining({
id: "category-1", id: "category-1",
@@ -4,11 +4,11 @@ import {
ProductCategoryTransformOptions, ProductCategoryTransformOptions,
ProductTypes, ProductTypes,
} from "@medusajs/types" } from "@medusajs/types"
import { DALUtils, MedusaError, isDefined } from "@medusajs/utils" import { DALUtils, isDefined, MedusaError } from "@medusajs/utils"
import { import {
LoadStrategy,
FilterQuery as MikroFilterQuery, FilterQuery as MikroFilterQuery,
FindOptions as MikroOptions, FindOptions as MikroOptions,
LoadStrategy,
} from "@mikro-orm/core" } from "@mikro-orm/core"
import { SqlEntityManager } from "@mikro-orm/postgresql" import { SqlEntityManager } from "@mikro-orm/postgresql"
import { ProductCategory } from "@models" import { ProductCategory } from "@models"
@@ -51,7 +51,9 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
} }
const shouldExpandParent = const shouldExpandParent =
familyOptions.includeAncestorsTree || populate.includes("parent_category") || fields.some(field => field.startsWith('parent_category')) familyOptions.includeAncestorsTree ||
populate.includes("parent_category") ||
fields.some((field) => field.startsWith("parent_category."))
if (shouldExpandParent) { if (shouldExpandParent) {
populate.indexOf("parent_category") === -1 && populate.indexOf("parent_category") === -1 &&
@@ -59,7 +61,9 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
} }
const shouldExpandChildren = const shouldExpandChildren =
familyOptions.includeDescendantsTree || populate.includes("category_children") || fields.some(field => field.startsWith('category_children')) familyOptions.includeDescendantsTree ||
populate.includes("category_children") ||
fields.some((field) => field.startsWith("category_children."))
if (shouldExpandChildren) { if (shouldExpandChildren) {
populate.indexOf("category_children") === -1 && populate.indexOf("category_children") === -1 &&
@@ -85,7 +89,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
const productCategories = await manager.find( const productCategories = await manager.find(
ProductCategory, ProductCategory,
findOptions_.where as MikroFilterQuery<ProductCategory>, findOptions_.where as MikroFilterQuery<ProductCategory>,
findOptions_.options as MikroOptions<ProductCategory> { ...findOptions_.options } as MikroOptions<ProductCategory>
) )
if ( if (
@@ -116,6 +120,20 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
): Promise<ProductCategory[]> { ): Promise<ProductCategory[]> {
const manager = super.getActiveManager<SqlEntityManager>(context) const manager = super.getActiveManager<SqlEntityManager>(context)
// We dont want to get the relations as we will fetch all the categories and build the tree manually
let relationIndex =
findOptions.options?.populate?.indexOf("parent_category")
const shouldPopulateParent = relationIndex !== -1
if (shouldPopulateParent && include.ancestors) {
findOptions.options!.populate!.splice(relationIndex as number, 1)
}
relationIndex = findOptions.options?.populate?.indexOf("category_children")
const shouldPopulateChildren = relationIndex !== -1
if (shouldPopulateChildren && include.descendants) {
findOptions.options!.populate!.splice(relationIndex as number, 1)
}
const mpaths: any[] = [] const mpaths: any[] = []
const parentMpaths = new Set() const parentMpaths = new Set()
for (const cat of productCategories) { for (const cat of productCategories) {
@@ -161,7 +179,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
const categoriesById = new Map(allCategories.map((cat) => [cat.id, cat])) const categoriesById = new Map(allCategories.map((cat) => [cat.id, cat]))
allCategories.forEach((cat: any) => { allCategories.forEach((cat: any) => {
if (cat.parent_category_id) { if (cat.parent_category_id && include.ancestors) {
cat.parent_category = categoriesById.get(cat.parent_category_id) cat.parent_category = categoriesById.get(cat.parent_category_id)
} }
}) })
@@ -178,12 +196,17 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
} }
if (level === 0) { if (level === 0) {
if (!include.ancestors && !shouldPopulateParent) {
delete category.parent_category
}
return category return category
} }
if (include.ancestors) { if (include.ancestors) {
delete category.category_children delete category.category_children
} }
if (include.descendants) { if (include.descendants) {
delete category.parent_category delete category.parent_category
} }
@@ -205,7 +228,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
context: Context = {} context: Context = {}
): Promise<[ProductCategory[], number]> { ): Promise<[ProductCategory[], number]> {
const manager = super.getActiveManager<SqlEntityManager>(context) const manager = super.getActiveManager<SqlEntityManager>(context)
const findOptions_ = this.buildFindOptions(findOptions, transformOptions) const findOptions_ = this.buildFindOptions(findOptions, transformOptions)
const [productCategories, count] = await manager.findAndCount( const [productCategories, count] = await manager.findAndCount(