aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets/sqlite.test.js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-05-16 17:18:50 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-05-16 17:18:50 -0700
commit0d12a1f9ee8b19393ae2dd95d8c4007e84a63ca9 (patch)
treeef4d145ac60fb9ccdd9a73a78739a37861684f5e /integration/bunjs-only-snippets/sqlite.test.js
parent036eb2a9ed67822e18330f0b8c23e0fe702257fe (diff)
downloadbun-0d12a1f9ee8b19393ae2dd95d8c4007e84a63ca9.tar.gz
bun-0d12a1f9ee8b19393ae2dd95d8c4007e84a63ca9.tar.zst
bun-0d12a1f9ee8b19393ae2dd95d8c4007e84a63ca9.zip
reset on stmt on error
Diffstat (limited to 'integration/bunjs-only-snippets/sqlite.test.js')
-rw-r--r--integration/bunjs-only-snippets/sqlite.test.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/integration/bunjs-only-snippets/sqlite.test.js b/integration/bunjs-only-snippets/sqlite.test.js
index 3f1e6c5db..2250f97f0 100644
--- a/integration/bunjs-only-snippets/sqlite.test.js
+++ b/integration/bunjs-only-snippets/sqlite.test.js
@@ -381,3 +381,50 @@ it("db.query()", () => {
db.close();
db.close();
});
+
+it("db.transaction()", () => {
+ const db = Database.open(":memory:");
+
+ db.exec(
+ "CREATE TABLE cats (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE, age INTEGER)"
+ );
+
+ const insert = db.prepare(
+ "INSERT INTO cats (name, age) VALUES (@name, @age)"
+ );
+
+ expect(db.inTransaction).toBe(false);
+ const insertMany = db.transaction((cats) => {
+ expect(db.inTransaction).toBe(true);
+ try {
+ for (const cat of cats) insert.run(cat);
+ } catch (exception) {
+ throw exception;
+ }
+ });
+
+ try {
+ insertMany([
+ { "@name": "Joey", "@age": 2 },
+ { "@name": "Sally", "@age": 4 },
+ { "@name": "Junior", "@age": 1 },
+ { "@name": "Sally", "@age": 4 },
+ ]);
+ throw new Error("Should have thrown");
+ } catch (exception) {
+ expect(exception.message).toBe("constraint failed");
+ }
+
+ expect(db.inTransaction).toBe(false);
+ expect(db.query("SELECT * FROM cats").all().length).toBe(0);
+
+ expect(db.inTransaction).toBe(false);
+ insertMany([
+ { "@name": "Joey", "@age": 2 },
+ { "@name": "Sally", "@age": 4 },
+ { "@name": "Junior", "@age": 1 },
+ ]);
+ expect(db.inTransaction).toBe(false);
+ expect(db.query("SELECT * FROM cats").all().length).toBe(3);
+ expect(db.inTransaction).toBe(false);
+});