import { expect, it } from "bun:test"; import { Database, constants } from "bun:sqlite"; var encode = (text) => Buffer.from(text); it("Database.open", () => { // in a folder which doesn't exist try { Database.open( "/this/database/does/not/exist.sqlite", constants.SQLITE_OPEN_READWRITE ); throw new Error("Expected an error to be thrown"); } catch (error) { expect(error.message).toBe("unable to open database file"); } // in a file which doesn't exist try { Database.open( `/tmp/database-${Math.random()}.sqlite`, constants.SQLITE_OPEN_READWRITE ); throw new Error("Expected an error to be thrown"); } catch (error) { expect(error.message).toBe("unable to open database file"); } // in a file which doesn't exist try { Database.open(`/tmp/database-${Math.random()}.sqlite`, { readonly: true }); throw new Error("Expected an error to be thrown"); } catch (error) { expect(error.message).toBe("unable to open database file"); } // in a file which doesn't exist try { Database.open(`/tmp/database-${Math.random()}.sqlite`, { readwrite: true }); throw new Error("Expected an error to be thrown"); } catch (error) { expect(error.message).toBe("unable to open database file"); } // create works { var db = Database.open(`/tmp/database-${Math.random()}.sqlite`, { create: true, }); db.close(); } // this should not throw // it creates an in-memory db new Database().close(); }); it("creates", () => { const db = Database.open(":memory:"); db.exec( "CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, created TEXT, deci FLOAT, blobby BLOB)" ); const stmt = db.prepare( "INSERT INTO test (name, value, deci, created, blobby) VALUES (?, ?, ?, ?, ?)" ); stmt.run([ "foo", 1, Math.fround(1.111), new Date(1995, 12, 19).toISOString(), encode("Hello World"), ]); stmt.run([ "bar", 2, Math.fround(2.222), new Date(1995, 12, 19).toISOString(), encode("Hello World"), ]); stmt.run([ "baz", 3, Math.fround(3.333), new Date(1995, 12, 19).toISOString(), encode("Hello World"), ]); stmt.finalize(); const stmt2 = db.prepare("SELECT * FROM test"); expect(JSON.stringify(stmt2.get())).toBe( JSON.stringify({ id: 1, name: "foo", value: 1, created: new Date(1995, 12, 19).toISOString(), deci: Math.fround(1.111), blobby: encode("Hello World"), }) ); expect(JSON.stringify(stmt2.all())).toBe( JSON.stringify([ { id: 1, name: "foo", value: 1, created: new Date(1995, 12, 19).toISOString(), deci: Math.fround(1.111), blobby: encode("Hello World"), }, { id: 2, name: "bar", value: 2, created: new Date(1995, 12, 19).toISOString(), deci: Math.fround(2.222), blobby: encode("Hello World"), }, { id: 3, name: "baz", value: 3, created: new Date(1995, 12, 19).toISOString(), deci: Math.fround(3.333), blobby: encode("Hello World"), }, ]) ); expect(stmt2.run()).toBe(undefined); // not necessary to run but it's a good practice stmt2.finalize(); }); it("typechecks", () => { const db = Database.open(":memory:"); db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)"); db.exec('INSERT INTO test (name) VALUES ("Hello")'); db.exec('INSERT INTO test (name) VALUES ("World")'); const q = db.prepare("SELECT * FROM test WHERE (name = ?)"); var expectfail = (val) => { try { q.run([val]); throw new Error("Expected error"); } catch (e) { expect(e.message !== "Expected error").toBe(true); expect(e.name).toBe("TypeError"); } try { q.all([val]); throw new Error("Expected error"); } catch (e) { expect(e.message !== "Expected error").toBe(true); expect(e.name).toBe("TypeError"); } try { q.get([val]); throw new Error("Expected error"); } catch (e) { expect(e.message !== "Expected error").toBe(true); expect(e.name).toBe("TypeError"); } }; expectfail(Symbol("oh hai")); expectfail(new Date()); expectfail(class Foo {}); expectfail(() => class Foo {}); expectfail(new RangeError("what")); expectfail(new Map()); expectfail(new Map([["foo", "bar"]])); expectfail(new Set()); expectfail(new Set([1, 2, 3])); }); it("db.query supports TypedArray", () => { const db = Database.open(":memory:"); db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, blobby BLOB)"); const stmt = db.prepare("INSERT INTO test (blobby) VALUES (?)"); stmt.run([encode("Hello World")]); stmt.finalize(); const stmt2 = db.prepare("SELECT * FROM test"); expect(JSON.stringify(stmt2.get())).toBe( JSON.stringify({ id: 1, blobby: encode("Hello World"), }) ); const stmt3 = db.prepare("SELECT * FROM test WHERE (blobby = ?)"); expect(JSON.stringify(stmt3.get([encode("Hello World")]))).toBe( JSON.stringify({ id: 1, blobby: encode("Hello World"), }) ); expect( JSON.stringify( db .query("SELECT * FROM test WHERE (blobby = ?)") .get([encode("Hello World")]) ) ).toBe( JSON.stringify({ id: 1, blobby: encode("Hello World"), }) ); expect(stmt3.get([encode("Hello World NOT")])).toBe(null); }); it("supports serialize/deserialize", () => { const db = Database.open(":memory:"); db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)"); db.exec('INSERT INTO test (name) VALUES ("Hello")'); db.exec('INSERT INTO test (name) VALUES ("World")'); const input = db.serialize(); const db2 = new Database(input); const stmt = db2.prepare("SELECT * FROM test"); expect(JSON.stringify(stmt.get())).toBe( JSON.stringify({ id: 1, name: "Hello", }) ); expect(JSON.stringify(stmt.all())).toBe( JSON.stringify([ { id: 1, name: "Hello", }, { id: 2, name: "World", }, ]) ); db2.exec("insert into test (name) values ('foo')"); expect(JSON.stringify(stmt.all())).toBe( JSON.stringify([ { id: 1, name: "Hello", }, { id: 2, name: "World", }, { id: 3, name: "foo", }, ]) ); const db3 = new Database(input, { readonly: true }); try { db3.exec("insert into test (name) values ('foo')"); throw new Error("Expected error"); } catch (e) { expect(e.message).toBe("attempt to write a readonly database"); } }); it("db.query()", () => { const db = Database.open(":memory:"); db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)"); expect(db[Symbol.for("Bun.Database.cache.count")]).toBe(0); var q = db.query("SELECT * FROM test WHERE name = ?"); expect(q.get("Hello") === null).toBe(true); db.exec('INSERT INTO test (name) VALUES ("Hello")'); db.exec('INSERT INTO test (name) VALUES ("World")'); var rows = db.query("SELECT * FROM test WHERE name = ?").all(["Hello"]); expect(JSON.stringify(rows)).toBe(JSON.stringify([{ id: 1, name: "Hello" }])); rows = db.query("SELECT * FROM test WHERE name = ?").all(["World"]); // if this fails, it means the query caching failed to update expect(JSON.stringify(rows)).toBe(JSON.stringify([{ id: 2, name: "World" }])); rows = db.query("SELECT * FROM test WHERE name = ?").all(["Hello"]); expect(JSON.stringify(rows)).toBe(JSON.stringify([{ id: 1, name: "Hello" }])); // check that the query is cached expect(db[Symbol.for("Bun.Database.cache.count")]).toBe(1); db.clearQueryCache(); // check clearing the cache decremented the counter expect(db[Symbol.for("Bun.Database.cache.count")]).toBe(0); q.finalize(); try { // check clearing the cache decremented the counter q.all(["Hello"]); throw new Error("Should have thrown"); } catch (e) { expect(e.message !== "Should have thrown").toBe(true); } // check that invalid queries are not cached // and invalid queries throw try { db.query("SELECT * FROM BACON", ["Hello"]).all(); throw new Error("Should have thrown"); } catch (e) { expect(e.message !== "Should have thrown").toBe(true); expect(db[Symbol.for("Bun.Database.cache.count")]).toBe(0); } // check that it supports multiple arguments expect( JSON.stringify( db .query("SELECT * FROM test where (name = ? OR name = ?)") .all(["Hello", "Fooooo"]) ) ).toBe(JSON.stringify([{ id: 1, name: "Hello" }])); expect( JSON.stringify( db .query("SELECT * FROM test where (name = ? OR name = ?)") .all("Hello", "Fooooo") ) ).toBe(JSON.stringify([{ id: 1, name: "Hello" }])); // throws if insufficeint arguments try { db.query("SELECT * FROM test where (name = ? OR name = ?)").all("Hello"); } catch (e) { expect(e.message).toBe("Expected 2 values, got 1"); } // named parameters expect( JSON.stringify( db .query("SELECT * FROM test where (name = $hello OR name = $goodbye)") .all({ $hello: "Hello", $goodbye: "Fooooo", }) ) ).toBe(JSON.stringify([{ id: 1, name: "Hello" }])); db.close(); // Check that a closed database doesn't crash // and does throw an error when trying to run a query try { db.query("SELECT * FROM test WHERE name = ?").all(["Hello"]); throw new Error("Should have thrown"); } catch (e) { expect(e.message !== "Should have thrown").toBe(true); } // check that we can call close multiple times // it should not throw so that your code doesn't break db.close(); 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); }); ='jarred/uws'>jarred/uws Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/src/sourcemap/CodeCoverage.zig (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2023-09-06fix(runtime): fix `events.once` not working (#4520)Gravatar dave caruso 3-5/+18
2023-09-06allocate task for `ThreadSafeFunction` (#4513)Gravatar Dylan Conway 3-4/+18
2023-09-05Update development.md (#4480)Gravatar Derrick Farris 1-1/+1
2023-09-05update root certs (#4499)Gravatar Ciro Spaciari 2-912/+1993
2023-09-05fix text decode trim (#4495)Gravatar Dylan Conway 3-5/+143
2023-09-05Align `process.nextTick` execution order with Node (#4409)Gravatar Jarred Sumner 17-87/+1481
2023-09-05feat(runtime): Implement `fs.watchFile` (#4467)Gravatar dave caruso 17-12/+1309
2023-09-05fix(node:net): emit close event on connection error (#4336)Gravatar dave caruso 3-11/+24
2023-09-05fix(fetch) always use readable stream if it is available (#4503)Gravatar Ciro Spaciari 2-27/+82
2023-09-05fix SSL proxy tunneling on fetch (#4510)Gravatar Ciro Spaciari 2-17/+76
2023-09-05fix(install): ensure all lockfile structs do not have undefined padding (#4401)Gravatar dave caruso 5-2/+113
2023-09-05fix checkout/build failure due to `src/deps/uws` (#4505)Gravatar Alex Lam S.L 5-15/+10
2023-09-05fix ipv6 localhost fetch (#4498)Gravatar Dylan Conway 2-2/+31
2023-09-05minor rebuild diffs (#4486)Gravatar Alex Lam S.L 4-4999/+4999
2023-09-05fix dup syscall on Windows (#4496)Gravatar Jason 2-3/+3
2023-09-04no need to chmod (#4490)Gravatar Dylan Conway 1-2/+0
2023-09-04fix zlib deflate on fetch (#4483)Gravatar Ciro Spaciari 3-8/+47
2023-09-04chore: fix typo (#4476)Gravatar Ai Hoshino 1-1/+1
2023-09-04fix(HTMLRewriter) buffer response before transform (#4418)Gravatar Ciro Spaciari 18-5941/+6655
2023-09-03initialize JSC for macros from cliGravatar Dylan Conway 1-0/+4
2023-09-03fix(syscall): fix handling syscall errno (#4461)Gravatar Ai Hoshino 2-1/+22
2023-09-02workaround a zig bug (#4440)Gravatar dave caruso 1-3/+4
2023-09-01docs: fix http simple example log statement (#4320)Gravatar Karl Böhlmark 1-1/+1
2023-09-01Fix typo (#4445)Gravatar Jorge Jiménez 1-1/+1
2023-09-01keep export star as (#4451)Gravatar Dylan Conway 1-14/+0
2023-09-01bun-vscode 0.0.8Gravatar Colin McDonnell 3-41/+39
2023-09-01Update commandsGravatar Colin McDonnell 3-4/+6
2023-09-01fix `Bun.serve` with tls and `Bun.file` (#4450)Gravatar Dylan Conway 3-14/+40
2023-09-01exclusive maxGravatar Dylan Conway 1-1/+1
2023-09-01Fix debug console from appears on startGravatar Ashcon Partovi 2-2/+5
2023-09-01Add configuration options to extensionGravatar Ashcon Partovi 5-5/+137
2023-09-01Fix run button starting cwd at /Gravatar Ashcon Partovi 1-0/+2
2023-09-01fix(runtime): fix dns_resolver crash (#4435)Gravatar dave caruso 3-17/+19
2023-09-01Fix background colorGravatar Ashcon Partovi 1-2/+3
2023-09-01Allow older versions of VSCodeGravatar Ashcon Partovi 2-6/+5
2023-09-01Fix README for extensionGravatar Ashcon Partovi 2-7/+12
2023-09-01Update VSCode extensionGravatar Ashcon Partovi 1-3/+4
2023-09-01Fix breakpoint on entry for extensionGravatar Ashcon Partovi 5-18/+15
2023-09-01Add Bun.canReload event to inspectorGravatar Ashcon Partovi 2-0/+17
2023-08-31JavaScript Debug Terminal == Bun TerminalGravatar Ashcon Partovi 1-0/+32
2023-08-31fix(runtime): `fs.cp` edge cases (#4439)Gravatar dave caruso 2-8/+44
2023-08-31only set initial debugger breakpoint once (#4441)Gravatar Dylan Conway 1-2/+11
2023-08-31Make breakpoints faster in VSCode extensionGravatar Ashcon Partovi 1-241/+327
2023-08-31`bun install` correctly join dependency URLs (#4421)Gravatar Julian 6-64/+243
2023-08-31get name if not provided in `FormData.append` (#4434)Gravatar Dylan Conway 4-5/+45
2023-08-31Fix vscode debug terminalGravatar Ashcon Partovi 1-21/+0