-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdf43f2
commit 9b872cf
Showing
4 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Pool } from "../mod.ts"; | ||
import { | ||
assert, | ||
assertEquals, | ||
assertThrowsAsync, | ||
} from "https://deno.land/std/testing/asserts.ts"; | ||
|
||
const decoder = new TextDecoder(); | ||
const { test } = Deno; | ||
|
||
const pool = new Pool({ | ||
password: "esri@123", | ||
}); | ||
|
||
test("transaction should ok", async () => { | ||
// prepare data | ||
const { rowCount, rows } = await pool.query( | ||
`INSERT INTO users(id, name) VALUES(1, 'zfx') RETURNING id`, | ||
); | ||
assert(rowCount === 1); | ||
assert(rows.length === 1); | ||
assert(rows[0].id === 1); | ||
|
||
const client = await pool.connect(); | ||
|
||
try { | ||
// rollback | ||
await client.query("BEGIN"); | ||
const del = await client.query("DELETE FROM users WHERE id=1"); | ||
assert(del.command === "DELETE"); | ||
assert(del.rowCount === 1); | ||
const { rows } = await client.query("SELECT * FROM users"); | ||
assert(rows.length === 0); | ||
await client.query("ROLLBACK"); | ||
const res = await client.query("SELECT * FROM users"); | ||
assert(res.rows.length === 1); | ||
assert(res.rows[0].id === 1); | ||
assert(res.rows[0].name === "zfx"); | ||
|
||
// commit | ||
try { | ||
await client.query("BEGIN"); | ||
const { rows } = await client.query( | ||
`INSERT INTO users(id, name) VALUES(2, 'zfx2') RETURNING id`, | ||
); | ||
assert(rows.length === 1); | ||
assert(rows[0].id === 2); | ||
await client.query("COMMIT"); | ||
const res = await client.query("SELECT * FROM users"); | ||
assert(res.rows.length === 2); | ||
} catch (e) { | ||
await client.query("ROLLBACK"); | ||
throw e; | ||
} | ||
} catch (error) { | ||
console.debug(error); | ||
assert(false); | ||
} finally { | ||
// clean | ||
client.release(); | ||
const res = await pool.query("DELETE FROM users WHERE id in(1, 2)"); | ||
assert(res.rowCount === 2); | ||
await pool.end(); | ||
} | ||
}); |