feat(cart): Migration file (#6156)

* feat(cart): Migration file

* fixup migration

* Add indexes on cart table for currency, region, and sales channel

* fix indexes

* address PR comments
This commit is contained in:
Oli Juhl authored and GitHub committed 2024-01-29 12:49:48 +01:00
1 parent 512b041929
commit c15438c744
10 files changed
+1530 -261

No files matched your search

@@ -1,128 +0,0 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { AddressService } from "../../../../src/services"
import { MikroOrmWrapper } from "../../../utils"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
jest.setTimeout(30000)
describe("Address Service", () => {
let service: AddressService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
testManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("addressService")
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("create", () => {
it("should create an address successfully", async () => {
const [createdAddress] = await service.create([
{
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
city: "Test city",
country_code: "US",
postal_code: "12345",
},
])
const [address] = await service.list({ id: [createdAddress.id] })
expect(address).toEqual(
expect.objectContaining({
id: createdAddress.id,
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
city: "Test city",
country_code: "US",
postal_code: "12345",
})
)
})
})
describe("update", () => {
it("should throw an error if address does not exist", async () => {
const error = await service
.update([
{
id: "none-existing",
},
])
.catch((e) => e)
expect(error.message).toContain(
'Address with id "none-existing" not found'
)
})
it("should update an address successfully", async () => {
const [createdAddress] = await service.create([
{
first_name: "Jane",
last_name: "Doe",
address_1: "Test street 1",
city: "Test city",
country_code: "US",
postal_code: "12345",
},
])
await service.update([
{
id: createdAddress.id,
address_1: "Test street 2",
city: "Test city 2",
},
])
const [address] = await service.list({ id: [createdAddress.id] })
expect(address).toEqual(
expect.objectContaining({
id: createdAddress.id,
first_name: "Jane",
last_name: "Doe",
address_1: "Test street 2",
city: "Test city 2",
country_code: "US",
postal_code: "12345",
})
)
})
})
describe("delete", () => {
it("should delete a cart successfully", async () => {
const [createdAddress] = await service.create([
{
first_name: "Jane",
last_name: "Doe",
},
])
await service.delete([createdAddress.id])
const carts = await service.list({ id: [createdAddress.id] })
expect(carts.length).toEqual(0)
})
})
})
@@ -1,9 +1,9 @@
import { Modules } from "@medusajs/modules-sdk"
import { ICartModuleService } from "@medusajs/types"
import { CheckConstraintViolationException } from "@mikro-orm/core"
import { MikroOrmWrapper } from "../../../utils"
import { Modules } from "@medusajs/modules-sdk"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
import { MikroOrmWrapper } from "../../../utils"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
jest.setTimeout(30000)
@@ -1,122 +0,0 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { CartService } from "../../../../src/services"
import { createCarts } from "../../../__fixtures__/cart"
import { MikroOrmWrapper } from "../../../utils"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
jest.setTimeout(30000)
describe("Cart Service", () => {
let service: CartService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
testManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("cartService")
await createCarts(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("create", () => {
it("should throw an error when required params are not passed", async () => {
const error = await service
.create([
{
email: "test@email.com",
} as any,
])
.catch((e) => e)
expect(error.message).toContain(
"Value for Cart.currency_code is required, 'undefined' found"
)
})
it("should create a cart successfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
const [cart] = await service.list({ id: [createdCart.id] })
expect(cart).toEqual(
expect.objectContaining({
id: createdCart.id,
currency_code: "eur",
})
)
})
})
describe("update", () => {
it("should throw an error if cart does not exist", async () => {
const error = await service
.update([
{
id: "none-existing",
},
])
.catch((e) => e)
expect(error.message).toContain('Cart with id "none-existing" not found')
})
it("should update a cart successfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
const [updatedCart] = await service.update([
{
id: createdCart.id,
email: "test@email.com",
},
])
const [cart] = await service.list({ id: [createdCart.id] })
expect(cart).toEqual(
expect.objectContaining({
id: createdCart.id,
currency_code: "eur",
email: updatedCart.email,
})
)
})
})
describe("delete", () => {
it("should delete a cart successfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
await service.delete([createdCart.id])
const carts = await service.list({ id: [createdCart.id] })
expect(carts.length).toEqual(0)
})
})
})