Skip to content

Commit

Permalink
feat(driver-adapters): assertions in driver adapters (#4211)
Browse files Browse the repository at this point in the history
* chore(driver-adapters): remove outdated part of README

* test(driver-adapters): turn libquery tests into assertions

* chore: merge main, fix conflicts, unify test commands

* chore: bump prisma version

* fix: README

* chore: add pgbouncer=true comment in .envrc.example to trigger "DEALLOCATE ALL" in "@prisma/client" tests with Rust drivers

* fix: remove type ignores and update Prisma Client initialisation

* chore: address comments

* chore: remove unused var

* chore: Update query-engine/driver-adapters/js/smoke-test-js/src/client/client.ts

Co-authored-by: Alexey Orlenko <alex@aqrln.net>

* chore: apply nit

Co-authored-by: Alexey Orlenko <alex@aqrln.net>

* chore: remove unused code

---------

Co-authored-by: Alexey Orlenko <alex@aqrln.net>
  • Loading branch information
jkomyno and aqrln committed Sep 11, 2023
1 parent 4991945 commit 9e8c303
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 245 deletions.
24 changes: 12 additions & 12 deletions query-engine/driver-adapters/js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 24 additions & 6 deletions query-engine/driver-adapters/js/smoke-test-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,52 @@ It contains a subset of `@prisma/client`, plus some handy executable smoke tests

## How to setup

We assume Node.js `v20.5.1`+ is installed. If not, run `nvm use` in the current directory.
We assume a recent Node.js is installed (e.g., `v20.5.x`). If not, run `nvm use` in the current directory.
It's very important to double-check if you have multiple versions installed, as both PlanetScale and Neon requires either Node.js `v18`+ or a custom `fetch` function.

In the parent directory (`cd ..`):
- Build the driver adapters via `pnpm i && pnpm build`

In the current directoy:
- Create a `.envrc` starting from `.envrc.example`, and fill in the missing values following the given template
- Install Node.js dependencies via
```bash
pnpm i
```

Anywhere in the repository:
- Run `cargo build -p query-engine-node-api` to compile the `libquery` Query Engine

### PlanetScale

If you don't have a connection string yet:

- [Follow the Notion document](https://www.notion.so/How-to-get-a-PlanetScale-and-Neon-database-for-testing-93d978061f9c4ffc80ebfed36896af16) or create a new database on [PlanetScale](https://planetscale.com/)
- Create a new database on [PlanetScale](https://planetscale.com/)
- Go to `Settings` > `Passwords`, and create a new password for the `main` database branch. Select the `Prisma` template and copy the generated URL (comprising username, password, etc). Paste it in the `JS_PLANETSCALE_DATABASE_URL` environment variable in `.envrc`.

In the current directory:
- Run `pnpm prisma:planetscale` to push the Prisma schema and insert the test data.
- Run `pnpm planetscale` to run smoke tests using `libquery` against the PlanetScale database.
- Run `pnpm planetscale:client` to run smoke tests using `@prisma/client` against the PlanetScale database.

Note: you used to be able to run these Prisma commands without changing the provider name, but [#4074](https://github.com/prisma/prisma-engines/pull/4074) changed that (see https://github.com/prisma/prisma-engines/pull/4074#issuecomment-1649942475).
For more fine-grained control:
- Run `pnpm planetscale:libquery` to test using `libquery`
- Run `pnpm planetscale:client` to test using `@prisma/client`

### Neon

If you don't have a connection string yet:

- [Follow the Notion document](https://www.notion.so/How-to-get-a-PlanetScale-and-Neon-database-for-testing-93d978061f9c4ffc80ebfed36896af16) or create a new database with Neon CLI `npx neonctl projects create` or in [Neon Console](https://neon.tech).
- Create a new database with Neon CLI `npx neonctl projects create` or in [Neon Console](https://neon.tech).
- Paste the connection string to `JS_NEON_DATABASE_URL`.

In the current directory:
- Run `pnpm prisma:neon` to push the Prisma schema and insert the test data.
- Run `pnpm neon` to run smoke tests using `libquery` against the Neon database.
- Run `pnpm neon:client` to run smoke tests using `@prisma/client` against the Neon database.
- Run `pnpm neon:ws` to run smoke tests using `libquery` against the Neon database, using a WebSocket connection.
For more fine-grained control:
- Run `pnpm neon:ws:libquery` to test using `libquery`
- Run `pnpm neon:ws:client` to test using `@prisma/client`
- Run `pnpm neon:http` to run smoke tests using `libquery` against the Neon database, using an HTTP connection. In this case, transactions won't work, and tests are expected to fail.
For more fine-grained control:
- Run `pnpm neon:ws:http` to test using `libquery`
- Run `pnpm neon:ws:http` to test using `@prisma/client`
18 changes: 11 additions & 7 deletions query-engine/driver-adapters/js/smoke-test-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
"prisma:db:push:mysql": "prisma db push --schema ./prisma/mysql/schema.prisma --force-reset",
"prisma:db:execute:mysql": "prisma db execute --schema ./prisma/mysql/schema.prisma --file ./prisma/mysql/commands/type_test/insert.sql",
"prisma:neon": "cross-env-shell DATABASE_URL=\"${JS_NEON_DATABASE_URL}\" \"pnpm prisma:db:push:postgres && pnpm prisma:db:execute:postgres\"",
"neon:ws": "cross-env-shell DATABASE_URL=\"${JS_NEON_DATABASE_URL}\" \"tsx ./src/libquery/neon.ws.ts\"",
"neon:http": "cross-env-shell DATABASE_URL=\"${JS_NEON_DATABASE_URL}\" \"tsx ./src/libquery/neon.http.ts\"",
"neon:ws:libquery": "DATABASE_URL=\"${JS_NEON_DATABASE_URL}\" node --test --loader=tsx ./src/libquery/neon.ws.test.ts",
"neon:http:libquery": "DATABASE_URL=\"${JS_NEON_DATABASE_URL}\" node --test --loader=tsx ./src/libquery/neon.http.test.ts",
"neon:ws:client": "DATABASE_URL=\"${JS_NEON_DATABASE_URL}\" node --test --loader=tsx ./src/client/neon.ws.test.ts",
"neon:http:client": "DATABASE_URL=\"${JS_NEON_DATABASE_URL}\" node --test --loader=tsx ./src/client/neon.http.test.ts",
"neon:ws": "pnpm neon:ws:libquery && pnpm neon:ws:client",
"neon:http": "pnpm neon:http:libquery && pnpm neon:http:client",
"prisma:pg": "cross-env-shell DATABASE_URL=\"${JS_PG_DATABASE_URL}\" \"pnpm prisma:db:push:postgres && pnpm prisma:db:execute:postgres\"",
"pg": "cross-env-shell DATABASE_URL=\"${JS_PG_DATABASE_URL}\" \"tsx ./src/libquery/pg.ts\"",
"pg:libquery": "cross-env-shell DATABASE_URL=\"${JS_PG_DATABASE_URL}\" node --test --loader=tsx ./src/libquery/pg.test.ts",
"pg:client": "DATABASE_URL=\"${JS_PG_DATABASE_URL}\" node --test --loader=tsx ./src/client/pg.test.ts",
"pg": "pnpm pg:libquery && pnpm pg:client",
"prisma:planetscale": "cross-env-shell DATABASE_URL=\"${JS_PLANETSCALE_DATABASE_URL}\" \"pnpm prisma:db:push:mysql && pnpm prisma:db:execute:mysql\"",
"planetscale": "cross-env-shell DATABASE_URL=\"${JS_PLANETSCALE_DATABASE_URL}\" \"tsx ./src/libquery/planetscale.ts\"",
"planetscale:client": "DATABASE_URL=\"${JS_PLANETSCALE_DATABASE_URL}\" node --test --loader=tsx ./src/client/planetscale.test.ts"
"planetscale:libquery": "DATABASE_URL=\"${JS_PLANETSCALE_DATABASE_URL}\" node --test --loader=tsx ./src/libquery/planetscale.test.ts",
"planetscale:client": "DATABASE_URL=\"${JS_PLANETSCALE_DATABASE_URL}\" node --test --loader=tsx ./src/client/planetscale.test.ts",
"planetscale": "pnpm planetscale:libquery && pnpm planetscale:client"
},
"keywords": [],
"author": "Alberto Schiabel <schiabel@prisma.io>",
Expand All @@ -32,7 +36,7 @@
"@jkomyno/prisma-driver-adapter-utils": "workspace:*",
"@neondatabase/serverless": "^0.6.0",
"@planetscale/database": "^1.11.0",
"@prisma/client": "5.3.0-integration-feat-driver-adapters-in-client.1",
"@prisma/client": "5.3.0-integration-feat-driver-adapters-in-client.3",
"pg": "^8.11.3",
"superjson": "^1.13.1",
"undici": "^5.23.0"
Expand All @@ -41,7 +45,7 @@
"@types/node": "^20.5.1",
"@types/pg": "^8.10.2",
"cross-env": "^7.0.3",
"prisma": "5.3.0-integration-feat-driver-adapters-in-client.1",
"prisma": "5.3.0-integration-feat-driver-adapters-in-client.3",
"tsx": "^3.12.7"
}
}
26 changes: 8 additions & 18 deletions query-engine/driver-adapters/js/smoke-test-js/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export async function smokeTestClient(driverAdapter: DriverAdapter) {
describe(isUsingDriverAdapters ? `using Driver Adapters` : `using Rust drivers`, () => {
it('batch queries', async () => {
const prisma = new PrismaClient({
// @ts-ignore
jsConnector: adapter,
adapter,
log,
})

Expand Down Expand Up @@ -48,34 +47,25 @@ export async function smokeTestClient(driverAdapter: DriverAdapter) {
'-- Implicit "COMMIT" query via underlying driver',
]

const postgresExpectedQueries = [
'BEGIN',
'DEALLOCATE ALL',
'SELECT 1',
'SELECT 2',
'SELECT 3',
'COMMIT',
]

if (['mysql'].includes(provider)) {
if (isUsingDriverAdapters) {
assert.deepEqual(queries, driverAdapterExpectedQueries)
} else {
assert.deepEqual(queries, defaultExpectedQueries)
}
} else if (['postgres'].includes(provider)) {
if (isUsingDriverAdapters) {
assert.deepEqual(queries, defaultExpectedQueries)
} else {
assert.deepEqual(queries, postgresExpectedQueries)
}
// Note: the "DEALLOCATE ALL" query is only present after "BEGIN" when using Rust Postgres with pgbouncer.
assert.deepEqual(queries.at(0), defaultExpectedQueries.at(0))
assert.deepEqual(
queries.filter((q) => q !== 'DEALLOCATE ALL'),
defaultExpectedQueries
)
}
})

it('applies isolation level when using batch $transaction', async () => {
const prisma = new PrismaClient({
// @ts-ignore
jsConnector: adapter,
adapter,
log,
})

Expand Down

0 comments on commit 9e8c303

Please sign in to comment.