From 83a68ea55839e368f3673138850919377db15ece Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Fri, 6 Jan 2023 14:35:20 +0530 Subject: [PATCH 1/4] refactor: move repository specs into its own folder --- .../__tests__/product-categories/queries.ts} | 29 ++++++++++--------- .../repositories/factories/index.ts | 1 + .../simple-product-category-factory.ts | 12 ++++++++ integration-tests/repositories/jest.config.js | 21 ++++++++++++++ .../repositories/medusa-config.js | 15 ++++++++++ integration-tests/repositories/package.json | 22 ++++++++++++++ yarn.lock | 15 ++++++++++ 7 files changed, 102 insertions(+), 13 deletions(-) rename integration-tests/{api/__tests__/product-categories/materialized-path.ts => repositories/__tests__/product-categories/queries.ts} (61%) create mode 100644 integration-tests/repositories/factories/index.ts create mode 100644 integration-tests/repositories/factories/simple-product-category-factory.ts create mode 100644 integration-tests/repositories/jest.config.js create mode 100644 integration-tests/repositories/medusa-config.js create mode 100644 integration-tests/repositories/package.json diff --git a/integration-tests/api/__tests__/product-categories/materialized-path.ts b/integration-tests/repositories/__tests__/product-categories/queries.ts similarity index 61% rename from integration-tests/api/__tests__/product-categories/materialized-path.ts rename to integration-tests/repositories/__tests__/product-categories/queries.ts index ac41694dd331b..03c2bff651328 100644 --- a/integration-tests/api/__tests__/product-categories/materialized-path.ts +++ b/integration-tests/repositories/__tests__/product-categories/queries.ts @@ -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([ diff --git a/integration-tests/repositories/factories/index.ts b/integration-tests/repositories/factories/index.ts new file mode 100644 index 0000000000000..fee1d059947bb --- /dev/null +++ b/integration-tests/repositories/factories/index.ts @@ -0,0 +1 @@ +export * from "./simple-product-category-factory" diff --git a/integration-tests/repositories/factories/simple-product-category-factory.ts b/integration-tests/repositories/factories/simple-product-category-factory.ts new file mode 100644 index 0000000000000..30a10bbf71a92 --- /dev/null +++ b/integration-tests/repositories/factories/simple-product-category-factory.ts @@ -0,0 +1,12 @@ +import { Connection } from "typeorm" +import { ProductCategory } from "@medusajs/medusa" + +export const simpleProductCategoryFactory = async ( + connection: Connection, + data: Partial = {} +): Promise => { + const manager = connection.manager + const address = manager.create(ProductCategory, data) + + return await manager.save(address) +} diff --git a/integration-tests/repositories/jest.config.js b/integration-tests/repositories/jest.config.js new file mode 100644 index 0000000000000..60751e306ade4 --- /dev/null +++ b/integration-tests/repositories/jest.config.js @@ -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", +} diff --git a/integration-tests/repositories/medusa-config.js b/integration-tests/repositories/medusa-config.js new file mode 100644 index 0000000000000..ef42b431531cf --- /dev/null +++ b/integration-tests/repositories/medusa-config.js @@ -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", + }, +} diff --git a/integration-tests/repositories/package.json b/integration-tests/repositories/package.json new file mode 100644 index 0000000000000..8842fbcebed75 --- /dev/null +++ b/integration-tests/repositories/package.json @@ -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" + } +} diff --git a/yarn.lock b/yarn.lock index 1107514d3de8b..29806309cbdc8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -19517,6 +19517,21 @@ __metadata: languageName: unknown linkType: soft +"integration-tests-repositories@workspace:integration-tests/repositories": + version: 0.0.0-use.local + resolution: "integration-tests-repositories@workspace:integration-tests/repositories" + dependencies: + "@babel/cli": ^7.12.10 + "@babel/core": ^7.12.10 + "@babel/node": ^7.12.10 + "@medusajs/medusa": "*" + babel-preset-medusa-package: "*" + jest: ^26.6.3 + medusa-interfaces: "*" + typeorm: ^0.2.31 + languageName: unknown + linkType: soft + "internal-slot@npm:^1.0.3": version: 1.0.3 resolution: "internal-slot@npm:1.0.3" From 7ac1c1ff7ad24bee944b30f6e7079c50d57d0dab Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Fri, 6 Jan 2023 14:40:11 +0530 Subject: [PATCH 2/4] chore: add repository specs to root package --- integration-tests/jest.config.js | 1 + package.json | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/integration-tests/jest.config.js b/integration-tests/jest.config.js index f9e2ed225d171..f56607473574b 100644 --- a/integration-tests/jest.config.js +++ b/integration-tests/jest.config.js @@ -14,6 +14,7 @@ module.exports = { projects: [ "/integration-tests/api/jest.config.js", "/integration-tests/plugins/jest.config.js", + "/integration-tests/repositories/jest.config.js", ], testPathIgnorePatterns: [ `/examples/`, diff --git a/package.json b/package.json index fcdf8d041f687..40eee59e3420b 100644 --- a/package.json +++ b/package.json @@ -66,10 +66,11 @@ "lint:docs": "eslint -c docs/.eslintrc.js --ignore-path docs/.eslintignore docs/content", "prettier": "prettier", "jest": "jest", - "test": "turbo run test --no-daemon --filter=!integration-tests-api --filter=!integration-tests-plugins", - "test:integration": "turbo run test --no-daemon --filter=integration-tests-api --filter=integration-tests-plugins", + "test": "turbo run test --no-daemon --filter=!integration-tests-api --filter=!integration-tests-plugins --filter=!integration-tests-repositories", + "test:integration": "turbo run test --no-daemon --filter=integration-tests-api --filter=integration-tests-plugins --filter=integration-tests-repositories", "test:integration:api": "turbo run test --no-daemon --filter=integration-tests-api", "test:integration:plugins": "turbo run test --no-daemon --filter=integration-tests-plugins", + "test:integration:repositories": "turbo run test --no-daemon --filter=integration-tests-repositories", "openapi:generate": "node ./scripts/build-openapi.js", "generate:services": "typedoc --options typedoc.services.js", "generate:js-client": "typedoc --options typedoc.js-client.js", From 03e4cc8098acf16ee1340ccd7e15f49fe673bfa7 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Fri, 6 Jan 2023 14:54:21 +0530 Subject: [PATCH 3/4] chore: add github action for repository tests --- .github/workflows/action.yml | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 3cb81e984904d..55574cafb28a0 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -179,3 +179,52 @@ jobs: run: yarn test:integration:plugins env: DB_PASSWORD: postgres + + + integration-tests-repositories: + needs: setup + runs-on: ubuntu-latest + env: + TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }} + TURBO_TEAM: ${{ secrets.TURBO_TEAM }} + + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + POSTGRES_USER: postgres + options: >- + --health-cmd pg_isready + --health-interval 1s + --health-timeout 10s + --health-retries 10 + ports: + - 5432:5432 + + steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.9.1 + with: + access_token: ${{ github.token }} + + - name: Checkout + uses: actions/checkout@v2.3.5 + with: + fetch-depth: 0 + + - name: Setup Node.js environment + uses: actions/setup-node@v2.4.1 + with: + node-version: "14" + cache: "yarn" + + - name: Install dependencies + uses: ./.github/actions/cache-deps + with: + extension: pipeline + + - name: Run repository integration tests + run: yarn test:integration:repositories + env: + DB_PASSWORD: postgres From 8607302021809c803cdad42bddef58edef0ad556 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Fri, 6 Jan 2023 14:58:17 +0530 Subject: [PATCH 4/4] chore: add repositories integration to changeset ignore --- .changeset/config.json | 2 +- .github/workflows/action.yml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.changeset/config.json b/.changeset/config.json index 46d4c73d81baa..9a1fd5283063c 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -7,7 +7,7 @@ "access": "public", "baseBranch": "master", "updateInternalDependencies": "patch", - "ignore": ["integration-tests-api", "integration-tests-plugins"], + "ignore": ["integration-tests-api", "integration-tests-plugins", "integration-tests-repositories"], "snapshot": { "useCalculatedVersion": true } diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 55574cafb28a0..284401ddac789 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -180,7 +180,6 @@ jobs: env: DB_PASSWORD: postgres - integration-tests-repositories: needs: setup runs-on: ubuntu-latest