Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: integration test

on:
pull_request:
workflow_dispatch:

jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: pnpm/action-setup@v4
with:
version: 9

- uses: actions/setup-node@v3
with:
node-version: '18'
cache: pnpm

- name: install dependencies
run: pnpm install --frozen-lockfile
- name: build
run: pnpm build
- name: test
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
run: |
cd integration-test
npx prisma db push
npx prisma generate
npx jest integration-test

9 changes: 8 additions & 1 deletion integration-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
1. cd to the `integration-test` directory
2. Run `npx prisma db push` to push the schema to the database
3. Run `npx prisma generate` to generate the Prisma client
4. Run `node test.js` to run the integration test
4. Run `npx jtest integration-test` to run the integration test

### Manual test

1. cd to the `integration-test` directory
2. Run `npx prisma db push` to push the schema to the database
3. Run `npx prisma generate` to generate the Prisma client
4. Run `node manually-test.js` to run the integration test

### Clean the environment

Expand Down
102 changes: 102 additions & 0 deletions integration-test/general.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { PrismaTiDBCloud } from "../dist/index.mjs";
import { PrismaClient } from "@prisma/client";
import * as dotenv from "dotenv";

let prisma: PrismaClient;

beforeAll(async () => {
dotenv.config();
const connectionString = `${process.env.DATABASE_URL}`;
// Initialize Prisma Client
const adapter = new PrismaTiDBCloud({ url: connectionString });
prisma = new PrismaClient({ adapter });
await prisma.user.deleteMany();
}, 20000);

describe("crud test", () => {
test("create user and query", async () => {
const user = await prisma.user.create({
data: {
email: "user1@pingcap.com",
name: "user1",
},
});
const queryUser = await prisma.user.findFirst({
where: { id: user.id },
});
expect(queryUser).not.toBeNull();
});

test("update user and query", async () => {
const user = await prisma.user.create({
data: {
email: "user2@pingcap.com",
name: "user2",
},
});
await prisma.user.update({
where: { id: user.id },
data: { name: "user2-update" },
});
const queryUser = await prisma.user.findFirst({
where: { name: "user2-update" },
});

expect(queryUser).not.toBeNull();
});
});

describe("tx test", () => {
test("test tx success", async () => {
const createUser1 = prisma.user.create({
data: {
email: "tx_user1@pingcap.com",
name: "tx_user1",
},
});
const createUser2 = prisma.user.create({
data: {
email: "tx_user2@pingcap.com",
name: "tx_user2",
},
});

await expect(
prisma.$transaction([createUser1, createUser2])
).resolves.toHaveLength(2);
const queryUser1 = await prisma.user.findFirst({
where: { name: "tx_user1" },
});
console.log("queryUser1", queryUser1);
const queryUser2 = await prisma.user.findFirst({
where: { name: "tx_user2" },
});
expect(queryUser1).not.toBeNull();
expect(queryUser2).not.toBeNull();
});

test("test tx rollback", async () => {
const createUser1 = prisma.user.create({
data: {
email: "tx_rollback1@pingcap.com",
name: "tx_user1",
},
});
const createUser2 = prisma.user.create({
data: {
email: "tx_rollback1@pingcap.com",
name: "tx_user2",
},
});

// excepet operations fail because the email address is duplicated
await expect(
prisma.$transaction([createUser1, createUser2])
).rejects.toThrow();
// except the first operation fails because of transaction aborts
const quertUser1 = await prisma.user.findFirst({
where: { name: "tx_rollback1" },
});
expect(quertUser1).toBeNull();
});
});
File renamed without changes.
10 changes: 10 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { JestConfigWithTsJest } from 'ts-jest'

const jestConfig: JestConfigWithTsJest = {
preset: 'ts-jest/presets/js-with-ts-esm',
clearMocks: true,
testMatch: ['**/*.test.ts'],
verbose: true
}

export default jestConfig
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@
},
"devDependencies": {
"@tidbcloud/serverless": "^0.1.0",
"@types/jest": "^30.0.0",
"@types/node": "^20.5.1",
"dotenv": "^16.5.0",
"jest": "^29.6.2",
"prettier": "^2.7.1",
"prisma": "6.12.0",
"ts-jest": "^29.1.1",
"tsup": "^7.2.0",
"tsx": "^3.12.7",
"typescript": "^5.1.6"
Expand Down
Loading