diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 79e303b..c8d5e68 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -20,6 +20,6 @@ jobs: run: timeout 300 cargo test --verbose - name: Run background corolla server run: | - cargo run -- -s examples/example_spec.json -d tmp/$(date +%s).sqlite3 -r /test & + timeout 300 cargo run -- -s examples/example_spec.json -d tmp/$(date +%s).sqlite3 -r /test & sleep 5 bun test diff --git a/examples/example_spec.json b/examples/example_spec.json index ce56b32..2ec1533 100644 --- a/examples/example_spec.json +++ b/examples/example_spec.json @@ -13,7 +13,7 @@ "write": { "write01": { "sql_template": "insert into t values (?);", - "args": ["a"] + "args": ["c"] } } }, diff --git a/js_api/api_test.ts b/js_api/api_test.ts index abed75a..a63b6c1 100644 --- a/js_api/api_test.ts +++ b/js_api/api_test.ts @@ -1,5 +1,5 @@ import { Corolla } from "../js_api/index"; -import { afterAll, beforeAll, expect, test } from "bun:test"; +import { expect, test } from "bun:test"; const corolla = new Corolla("http://127.0.0.1:50000", "/test"); @@ -14,17 +14,19 @@ const read01 = corolla.make_read_query< test("write query", async () => { let res = await write01({ c: "carrot" }); - expect(res.status).toBe(200); + expect(res.ok).toBeTrue; res = await write01({ c: "tomato" }); - expect(res.status).toBe(200); + expect(res.ok).toBeTrue; res = await write01({ c: "squash" }); - expect(res.status).toBe(200); + expect(res.ok).toBeTrue; }); test("read query", async () => { const rows = await read01({}); - expect( - JSON.stringify(rows) === - JSON.stringify([{ c: "carrot" }, { c: "tomato" }, { c: "squash" }]), - ); + expect(rows !== null); + if (rows !== null) { + expect(rows[0].c).toBe("carrot"); + expect(rows[1].c).toBe("tomato"); + expect(rows[2].c).toBe("squash"); + } }); diff --git a/js_api/index.ts b/js_api/index.ts index dbe5472..16755d1 100644 --- a/js_api/index.ts +++ b/js_api/index.ts @@ -26,16 +26,17 @@ class Corolla { ) { return async ( args: Args, - ): Promise => { + ): Promise => { const url_query_args = args === undefined ? "" : "?" + new URLSearchParams(args); - const res: string[][] = await fetch( + const http_res = await fetch( `${this.server}${this.url_base}/read/${query}${url_query_args}`, - ) - .then(( - r, - ) => r.json()); + ); + if (!http_res.ok) { + return null; + } + const res: string[][] = await http_res.json(); if (res.length === 0) { return []; } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index d28ef01..c05485a 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -24,19 +24,18 @@ pub fn get_root_dir() -> PathBuf { pub async fn cleanup(delete_db: bool, proc: Option<&mut Child>) { if delete_db { let path = get_root_dir(); - for file in [ - "corolla-test.sqlite3", - "corolla-test.sqlite3-shm", - "corolla-test.sqlite3-wal", - ] - .iter() - { - Command::new("rm") - .arg(Path::new(&path).join("tmp").join(file).to_str().unwrap()) - .output() - .await - .expect("could not execute cleanup step"); - } + Command::new("rm") + .arg("-rf") + .arg(Path::new(&path).join("tmp").to_str().unwrap()) + .output() + .await + .expect("could not execute rm"); + Command::new("mkdir") + .arg("-p") + .arg(Path::new(&path).join("tmp").to_str().unwrap()) + .output() + .await + .expect("could not execute mkdir"); } match proc { Some(proc) => {