Files
medusa-store/integration-tests/modules/__tests__/link-modules/index.ts
Carlos R. L. Rodrigues cc73802ab3 chore(order): dml (#10292)
* ../../core/types/src/dml/index.ts

* ../../core/types/src/dml/index.ts

* fix: relationships mapping

* handle nullable foreign keys types

* handle nullable foreign keys types

* handle nullable foreign keys types

* continue to update product category repository

* fix all product category repositories issues

* fix product category service types

* fix product module service types

* fix product module service types

* fix repository template type

* refactor: use a singleton DMLToMikroORM factory instance

Since the MikroORM MetadataStorage is global, we will also have to turn DML
to MikroORM entities conversion use a global bucket as well

* refactor: update product module to use DML in tests

* wip: tests

* WIP product linkable fixes

* continue type fixing and start test fixing

* test: fix more tests

* fix repository

* fix pivot table computaion + fix mikro orm repository

* fix many to many management and configuration

* fix many to many management and configuration

* fix many to many management and configuration

* update product tag relation configuration

* Introduce experimental dml hooks to fix some issues with categories

* more fixes

* fix product tests

* add missing id prefixes

* fix product category handle management

* test: fix more failing tests

* test: make it all green

* test: fix breaking tests

* fix: build issues

* fix: build issues

* fix: more breaking tests

* refactor: fix issues after merge

* refactor: fix issues after merge

* refactor: surpress types error

* test: fix DML failing tests

* improve many to many inference + tests

* Wip fix columns from product entity

* remove product model before create hook and manage handle validation and transformation at the service level

* test: fix breaking unit tests

* fix: product module service to not update handle on product update

* fix define link and joiner config

* test: fix joiner config test

* test: fix joiner config test

* fix joiner config primary keys

* Fix joiner config builder

* Fix joiner config builder

* test: remove only modifier from test

* refactor: remove hooks usage from product collection

* refactor: remove hooks usage from product-option

* refactor: remove hooks usage for computing category handle

* refactor: remove hooks usage from productCategory model

* refactor: remove hooks from DML

* refactor: remove cruft

* order dml

* cleanup

* re add foerign key indexes

* wip

* chore: remove unused types

* wip

* changes

* rm raw

* autoincrement

* wip

* rel

* refactor: cleanup

* migration and models configuration adjustments

* cleanup

* number searchable

* fix random ordering

* fix

* test: fix product-category tests

* test: update breaking DML tests

* test: array assertion to not care about ordering

* fix: temporarily apply id ordering for products

* types

* wip

* WIP type improvements

* update order models

* partially fix types temporarely

* rel

* fix: recursive type issue

* improve type inference breaks

* improve type inference breaks

* update models

* rm nullable

* default value

* repository

* update default value handling

* fix unit tests

* WIP

* toMikroORM

* fix relations

* cascades

* fix

* experimental dml hooks

* rm migration

* serial

* nullable autoincrement

* fix model

* model changes

* fix one to one DML

* order test

* fix addresses

* fix unit tests

* Re align dml entity name inference

* update model table name config

* update model table name config

* revert

* update return relation

* WIP

* hasOne

* models

* fix

* model

* initial commit

* cart service

* order module

* utils unit test

* index engine

* changeset

* merge

* fix hasOne with fk

* update

* free text filter per entity

* tests

* prod category

* property string many to many

* fix big number

* link modules migration set names

* merge

* shipping option rules

* serializer

* unit test

* fix test mikro orm init

* fix test mikro orm init

* Maintain merge object properties

* fix test mikro orm init

* prevent unit test from connecting to db

* wip

* fix test

* fix test

* link test

* schema

* models

* auto increment

* hook

* model hooks

* order

* wip

* orm version

* request return field

* fix return configuration on order model

* workflows

* core flows

* unit test

* test

* base repo

* test

* base repo

* test fix

* inventory move

* locking inventory

* test

* free text fix

* rm timeout mock

* migrate fulfillment values

* v6.4.3

* cleanup

* link-modules update sql

* revert test

* remove fake timers

---------

Co-authored-by: adrien2p <adrien.deperetti@gmail.com>
Co-authored-by: Harminder Virk <virk.officials@gmail.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
2025-01-21 08:04:47 -05:00

273 lines
7.3 KiB
TypeScript

import { getMigrationPlanner, initialize } from "@medusajs/link-modules"
import { MedusaModule } from "@medusajs/modules-sdk"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { ModuleJoinerConfig } from "@medusajs/types"
jest.setTimeout(5000000)
medusaIntegrationTestRunner({
testSuite: ({ dbConfig: { clientUrl } }) => {
let DB_URL
let links
beforeAll(async () => {
DB_URL = clientUrl
const linkDefinition: ModuleJoinerConfig[] = [
{
serviceName: "linkServiceName",
isLink: true,
databaseConfig: {
tableName: "linkTableName",
idPrefix: "prefix",
extraFields: {
extra_field: {
type: "integer",
defaultValue: "-1",
},
another_field: {
type: "string",
nullable: true,
},
},
},
relationships: [
{
serviceName: "moduleA",
primaryKey: "id",
foreignKey: "product_id",
alias: "product",
},
{
serviceName: "moduleB",
primaryKey: "id",
foreignKey: "inventory_item_id",
alias: "inventory",
},
],
},
]
const dbConfig = {
database: {
clientUrl: DB_URL,
},
}
jest.spyOn(MedusaModule, "getLoadedModules").mockImplementation((() => {
return [
{
moduleA: {
__definition: {
key: "moduleA",
},
},
},
{
moduleB: {
__definition: {
key: "moduleB",
},
},
},
]
}) as any)
const planner = getMigrationPlanner(dbConfig, linkDefinition)
await planner.executePlan(await planner.createPlan())
links = await initialize(dbConfig, linkDefinition)
})
afterAll(async () => {
jest.clearAllMocks()
})
describe("Link Modules", () => {
it("Should insert values in a declared link", async function () {
// simple
await links.linkServiceName.create("modA_id", "modB_id")
// extra fields
await links.linkServiceName.create("123", "abc", {
extra_field: 333,
another_field: "value**",
})
// bulk
await links.linkServiceName.create([
["111", "aaa", { another_field: "test" }],
["222", "bbb"],
["333", "ccc", { extra_field: 2 }],
["444", "bbb"],
])
const values = await links.linkServiceName.list()
expect(values).toEqual([
{
product_id: "modA_id",
inventory_item_id: "modB_id",
id: expect.stringMatching("prefix_.+"),
extra_field: -1,
another_field: null,
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
},
expect.objectContaining({
product_id: "123",
inventory_item_id: "abc",
id: expect.stringMatching("prefix_.+"),
extra_field: 333,
another_field: "value**",
}),
expect.objectContaining({
product_id: "111",
inventory_item_id: "aaa",
extra_field: -1,
another_field: "test",
}),
expect.objectContaining({
product_id: "222",
inventory_item_id: "bbb",
extra_field: -1,
another_field: null,
}),
expect.objectContaining({
product_id: "333",
inventory_item_id: "ccc",
id: expect.stringMatching("prefix_.+"),
extra_field: 2,
}),
expect.objectContaining({
product_id: "444",
inventory_item_id: "bbb",
}),
])
})
it("Should dismiss the link of a given pair of keys", async function () {
// simple
await links.linkServiceName.create("modA_id", "modB_id")
// extra fields
await links.linkServiceName.create("123", "abc", {
extra_field: 333,
another_field: "value**",
})
// bulk
await links.linkServiceName.create([
["111", "aaa", { another_field: "test" }],
["222", "bbb"],
["333", "ccc", { extra_field: 2 }],
["444", "bbb"],
])
// simple
const dismissSingle = await links.linkServiceName.dismiss(
"modA_id",
"modB_id"
)
// bulk
const dismissMulti = await links.linkServiceName.dismiss([
["111", "aaa"],
["333", "ccc"],
])
expect(dismissSingle).toEqual([
expect.objectContaining({
product_id: "modA_id",
inventory_item_id: "modB_id",
deleted_at: expect.any(Date),
}),
])
expect(dismissMulti).toEqual([
expect.objectContaining({
product_id: "111",
inventory_item_id: "aaa",
deleted_at: expect.any(Date),
}),
expect.objectContaining({
product_id: "333",
inventory_item_id: "ccc",
deleted_at: expect.any(Date),
}),
])
})
it("Should delete all the links related to a given key", async function () {
// simple
await links.linkServiceName.create("modA_id", "modB_id")
// extra fields
await links.linkServiceName.create("123", "abc", {
extra_field: 333,
another_field: "value**",
})
// bulk
await links.linkServiceName.create([
["111", "aaa", { another_field: "test" }],
["222", "bbb"],
["333", "ccc", { extra_field: 2 }],
["444", "bbb"],
])
await links.linkServiceName.softDelete({
inventory_item_id: "bbb",
})
const values = await links.linkServiceName.list(
{ inventory_item_id: "bbb" },
{ withDeleted: true }
)
expect(values).toEqual([
expect.objectContaining({
product_id: "222",
inventory_item_id: "bbb",
deleted_at: expect.any(Date),
}),
expect.objectContaining({
product_id: "444",
inventory_item_id: "bbb",
deleted_at: expect.any(Date),
}),
])
})
it("Should dismiss the link of a given pair of keys and recreate it", async function () {
await links.linkServiceName.create("123", "abc", {
extra_field: 333,
another_field: "value**",
})
await links.linkServiceName.dismiss("123", "abc")
const values = await links.linkServiceName.list()
expect(values).toHaveLength(0)
await links.linkServiceName.create("123", "abc", {
another_field: "changed",
})
const values2 = await links.linkServiceName.list()
expect(values2).toEqual([
expect.objectContaining({
product_id: "123",
inventory_item_id: "abc",
extra_field: 333,
another_field: "changed",
deleted_at: null,
}),
])
})
})
},
})