-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add d1 tests and d1 specific error type
- Loading branch information
Showing
9 changed files
with
282 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.wrangler |
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,105 @@ | ||
use serde::Deserialize; | ||
use worker::*; | ||
|
||
use crate::SomeSharedData; | ||
|
||
#[derive(Deserialize)] | ||
struct Person { | ||
id: u32, | ||
name: String, | ||
age: u32, | ||
} | ||
|
||
pub async fn prepared_statement( | ||
_req: Request, | ||
ctx: RouteContext<SomeSharedData>, | ||
) -> Result<Response> { | ||
let db = ctx.env.d1("DB")?; | ||
let stmt = worker::query!(&db, "SELECT * FROM people WHERE name = ?", "Ryan Upton")?; | ||
|
||
// All rows | ||
let results = stmt.all().await?; | ||
let people = results.results::<Person>()?; | ||
|
||
assert!(results.success()); | ||
assert_eq!(results.error(), None); | ||
assert_eq!(people.len(), 1); | ||
assert_eq!(people[0].name, "Ryan Upton"); | ||
assert_eq!(people[0].age, 21); | ||
assert_eq!(people[0].id, 6); | ||
|
||
// All columns of the first rows | ||
let person = stmt.first::<Person>(None).await?.unwrap(); | ||
assert_eq!(person.name, "Ryan Upton"); | ||
assert_eq!(person.age, 21); | ||
|
||
// The name of the first row | ||
let name = stmt.first::<String>(Some("name")).await?.unwrap(); | ||
assert_eq!(name, "Ryan Upton"); | ||
|
||
// All of the rows as column arrays of raw JSON values. | ||
let rows = stmt.raw::<serde_json::Value>().await?; | ||
assert_eq!(rows.len(), 1); | ||
let columns = &rows[0]; | ||
|
||
assert_eq!(columns[0].as_u64(), Some(6)); | ||
assert_eq!(columns[1].as_str(), Some("Ryan Upton")); | ||
assert_eq!(columns[2].as_u64(), Some(21)); | ||
|
||
Response::ok("ok") | ||
} | ||
|
||
pub async fn batch(_req: Request, ctx: RouteContext<SomeSharedData>) -> Result<Response> { | ||
let db = ctx.env.d1("DB")?; | ||
let mut results = db | ||
.batch(vec![ | ||
worker::query!(&db, "SELECT * FROM people WHERE id < 4"), | ||
worker::query!(&db, "SELECT * FROM people WHERE id > 4"), | ||
]) | ||
.await? | ||
.into_iter(); | ||
|
||
let first_results = results.next().unwrap().results::<Person>()?; | ||
assert_eq!(first_results.len(), 3); | ||
assert_eq!(first_results[0].id, 1); | ||
assert_eq!(first_results[1].id, 2); | ||
assert_eq!(first_results[2].id, 3); | ||
|
||
let second_results = results.next().unwrap().results::<Person>()?; | ||
assert_eq!(second_results.len(), 2); | ||
assert_eq!(second_results[0].id, 5); | ||
assert_eq!(second_results[1].id, 6); | ||
|
||
Response::ok("ok") | ||
} | ||
|
||
pub async fn exec(mut req: Request, ctx: RouteContext<SomeSharedData>) -> Result<Response> { | ||
let db = ctx.env.d1("DB")?; | ||
let result = db | ||
.exec(req.text().await?.as_ref()) | ||
.await.expect("doesn't exist"); | ||
|
||
Response::ok(result.count().unwrap_or_default().to_string()) | ||
} | ||
|
||
pub async fn dump(_req: Request, ctx: RouteContext<SomeSharedData>) -> Result<Response> { | ||
let db = ctx.env.d1("DB")?; | ||
let bytes = db.dump().await?; | ||
Response::from_bytes(bytes) | ||
} | ||
|
||
pub async fn error(_req: Request, ctx: RouteContext<SomeSharedData>) -> Result<Response> { | ||
let db = ctx.env.d1("DB")?; | ||
let error = db | ||
.exec("THIS IS NOT VALID SQL") | ||
.await | ||
.expect_err("did not get error"); | ||
|
||
if let Error::D1(error) = error { | ||
assert_eq!(error.cause(), "Error in line 1: THIS IS NOT VALID SQL: ERROR 9009: SQL prepare error: near \"THIS\": syntax error in THIS IS NOT VALID SQL at offset 0") | ||
} else { | ||
panic!("expected D1 error"); | ||
} | ||
|
||
Response::ok("") | ||
} |
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 was deleted.
Oops, something went wrong.
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,70 @@ | ||
import { describe, test, expect, beforeAll } from "vitest"; | ||
|
||
const hasLocalDevServer = await fetch("http://localhost:8787/request") | ||
.then((resp) => resp.ok) | ||
.catch(() => false); | ||
|
||
async function exec(query: string): Promise<number> { | ||
const resp = await fetch("http://localhost:8787/d1/exec", { | ||
method: "POST", | ||
body: query.split("\n").join(""), | ||
}); | ||
|
||
const body = await resp.text(); | ||
expect(resp.status).toBe(200); | ||
return Number(body); | ||
} | ||
|
||
describe.skipIf(!hasLocalDevServer)("d1", () => { | ||
test("create table", async () => { | ||
const query = `CREATE TABLE IF NOT EXISTS uniqueTable ( | ||
id INTEGER PRIMARY KEY, | ||
name TEXT NOT NULL, | ||
age INTEGER NOT NULL | ||
);`; | ||
|
||
expect(await exec(query)).toBe(1); | ||
}); | ||
|
||
test("insert data", async () => { | ||
let query = `CREATE TABLE IF NOT EXISTS people ( | ||
id INTEGER PRIMARY KEY, | ||
name TEXT NOT NULL, | ||
age INTEGER NOT NULL | ||
);`; | ||
|
||
expect(await exec(query)).toBe(1); | ||
|
||
query = `INSERT OR IGNORE INTO people | ||
(id, name, age) | ||
VALUES | ||
(1, 'Freddie Pearce', 26), | ||
(2, 'Wynne Ogley', 67), | ||
(3, 'Dorian Fischer', 19), | ||
(4, 'John Smith', 92), | ||
(5, 'Magaret Willamson', 54), | ||
(6, 'Ryan Upton', 21);`; | ||
|
||
expect(await exec(query)).toBe(1); | ||
}); | ||
|
||
test("prepared statement", async () => { | ||
const resp = await fetch("http://localhost:8787/d1/prepared"); | ||
expect(resp.status).toBe(200); | ||
}); | ||
|
||
test("batch", async () => { | ||
const resp = await fetch("http://localhost:8787/d1/batch"); | ||
expect(resp.status).toBe(200); | ||
}); | ||
|
||
test("dump", async () => { | ||
const resp = await fetch("http://localhost:8787/d1/dump"); | ||
expect(resp.status).toBe(200); | ||
}); | ||
|
||
test("dump", async () => { | ||
const resp = await fetch("http://localhost:8787/d1/error"); | ||
expect(resp.status).toBe(200); | ||
}); | ||
}); |
Oops, something went wrong.