refactor(medusa): move repository specs into its own folder (#2952)

**What:**

Introduces a new folder under which repository specs will be placed. 

Why:

We don't currently have a good place to test ORM logic or custom queries against the database. The repository folder tests are a place for just exactly that. 

How:

Creates an internal package similar to other integration tests - api and plugins. 

CORE-965
This commit is contained in:
Riqwan Thamir
2023-01-09 09:19:01 +01:00
committed by GitHub
parent baeacd1cc5
commit b280e53bd3
11 changed files with 155 additions and 16 deletions

View File

@@ -14,6 +14,7 @@ module.exports = {
projects: [
"<rootDir>/integration-tests/api/jest.config.js",
"<rootDir>/integration-tests/plugins/jest.config.js",
"<rootDir>/integration-tests/repositories/jest.config.js",
],
testPathIgnorePatterns: [
`/examples/`,

View File

@@ -1,6 +1,7 @@
import path from "path"
import { ProductCategory } from "@medusajs/medusa"
import { initDb, useDb } from "../../../helpers/use-db"
import { simpleProductCategoryFactory } from '../../factories'
describe("Product Categories", () => {
let dbConnection
@@ -21,21 +22,18 @@ describe("Product Categories", () => {
})
describe("Tree Queries (Materialized Paths)", () => {
it("can fetch ancestors, descendents and root product categories", async () => {
const productCategoryRepository = dbConnection.getTreeRepository(ProductCategory)
let a1, a11, a111, a12
let productCategoryRepository
const a1 = productCategoryRepository.create({ name: 'a1', handle: 'a1' })
await productCategoryRepository.save(a1)
const a11 = productCategoryRepository.create({ name: 'a11', handle: 'a11', parent_category: a1 })
await productCategoryRepository.save(a11)
const a111 = productCategoryRepository.create({ name: 'a111', handle: 'a111', parent_category: a11 })
await productCategoryRepository.save(a111)
const a12 = productCategoryRepository.create({ name: 'a12', handle: 'a12', parent_category: a1 })
await productCategoryRepository.save(a12)
beforeEach(async () => {
a1 = await simpleProductCategoryFactory(dbConnection, { name: 'a1', handle: 'a1' })
a11 = await simpleProductCategoryFactory(dbConnection, { name: 'a11', handle: 'a11', parent_category: a1 })
a111 = await simpleProductCategoryFactory(dbConnection, { name: 'a111', handle: 'a111', parent_category: a11 })
a12 = await simpleProductCategoryFactory(dbConnection, { name: 'a12', handle: 'a12', parent_category: a1 })
productCategoryRepository = dbConnection.getTreeRepository(ProductCategory)
})
it("can fetch all root categories", async () => {
const rootCategories = await productCategoryRepository.findRoots()
expect(rootCategories).toEqual([
@@ -43,7 +41,9 @@ describe("Product Categories", () => {
name: "a1",
})
])
})
it("can fetch all ancestors of a category", async () => {
const a11Parent = await productCategoryRepository.findAncestors(a11)
expect(a11Parent).toEqual([
@@ -54,7 +54,10 @@ describe("Product Categories", () => {
name: "a11",
}),
])
})
it("can fetch all root descendants of a category", async () => {
const a1Children = await productCategoryRepository.findDescendants(a1)
expect(a1Children).toEqual([

View File

@@ -0,0 +1 @@
export * from "./simple-product-category-factory"

View File

@@ -0,0 +1,12 @@
import { Connection } from "typeorm"
import { ProductCategory } from "@medusajs/medusa"
export const simpleProductCategoryFactory = async (
connection: Connection,
data: Partial<ProductCategory> = {}
): Promise<ProductCategory> => {
const manager = connection.manager
const address = manager.create(ProductCategory, data)
return await manager.save(address)
}

View File

@@ -0,0 +1,21 @@
module.exports = {
name: "repositories",
testEnvironment: `node`,
rootDir: "./",
testTimeout: 10000,
testPathIgnorePatterns: [
`/examples/`,
`/www/`,
`/dist/`,
`/node_modules/`,
`__tests__/fixtures`,
`__testfixtures__`,
`.cache`,
],
transformIgnorePatterns: [`/dist`],
transform: { "^.+\\.[jt]s$": `../../jest-transformer.js` },
setupFiles: ["../setup-env.js"],
setupFilesAfterEnv: ["../setup.js"],
globalSetup: "../globalSetup.js",
globalTeardown: "../globalTeardown.js",
}

View File

@@ -0,0 +1,15 @@
const DB_HOST = process.env.DB_HOST
const DB_USERNAME = process.env.DB_USERNAME
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_NAME = process.env.DB_TEMP_NAME
module.exports = {
plugins: [],
projectConfig: {
redis_url: process.env.REDIS_URL,
database_url: `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`,
database_type: "postgres",
jwt_secret: "test",
cookie_secret: "test",
},
}

View File

@@ -0,0 +1,22 @@
{
"name": "integration-tests-repositories",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"private": true,
"scripts": {
"test": "jest --silent=false --runInBand --bail --detectOpenHandles --forceExit"
},
"dependencies": {
"@medusajs/medusa": "*",
"medusa-interfaces": "*",
"typeorm": "^0.2.31"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/node": "^7.12.10",
"babel-preset-medusa-package": "*",
"jest": "^26.6.3"
}
}