Skip to content

Commit

Permalink
1713021592
Browse files Browse the repository at this point in the history
  • Loading branch information
12ball committed Apr 13, 2024
1 parent 1f8d957 commit d9ba11f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion examples/example_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"write": {
"write01": {
"sql_template": "insert into t values (?);",
"args": ["a"]
"args": ["c"]
}
}
},
Expand Down
18 changes: 10 additions & 8 deletions js_api/api_test.ts
Original file line number Diff line number Diff line change
@@ -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");

Expand All @@ -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");
}
});
13 changes: 7 additions & 6 deletions js_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ class Corolla {
) {
return async (
args: Args,
): Promise<Result[]> => {
): Promise<Result[] | null> => {
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 [];
}
Expand Down
25 changes: 12 additions & 13 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit d9ba11f

Please sign in to comment.