diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 306f4b6..9b01156 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -126,50 +126,50 @@ jobs: # run: "cd smoke_test/workers && python ../../hrana-test-server/server_v3.py node test.js" # env: {"LOCAL": "0", "URL": "http://localhost:8080"} - "vercel-test": - name: "Build and test with Vercel Edge Functions" - runs-on: ubuntu-latest - env: - VERCEL_TOKEN: "${{ secrets.VERCEL_TOKEN }}" - VERCEL_PROJECT_NAME: "smoke-test" - steps: - - name: "Checkout this repo" - uses: actions/checkout@v3 - - name: "Setup Node.js" - uses: actions/setup-node@v3 - with: - node-version: "lts/Hydrogen" - cache: "npm" - - name: "Install npm dependencies" - run: "npm ci" +# "vercel-test": +# name: "Build and test with Vercel Edge Functions" +# runs-on: ubuntu-latest +# env: +# VERCEL_TOKEN: "${{ secrets.VERCEL_TOKEN }}" +# VERCEL_PROJECT_NAME: "smoke-test" +# steps: +# - name: "Checkout this repo" +# uses: actions/checkout@v3 +# - name: "Setup Node.js" +# uses: actions/setup-node@v3 +# with: +# node-version: "lts/Hydrogen" +# cache: "npm" +# - name: "Install npm dependencies" +# run: "npm ci" - - name: "Checkout hrana-test-server" - uses: actions/checkout@v3 - with: - repository: "libsql/hrana-test-server" - path: "hrana-test-server" - - name: "Setup Python" - uses: actions/setup-python@v4 - with: - python-version: "3.10" - cache: "pip" - - name: "Install pip dependencies" - run: "pip install -r hrana-test-server/requirements.txt" +# - name: "Checkout hrana-test-server" +# uses: actions/checkout@v3 +# with: +# repository: "libsql/hrana-test-server" +# path: "hrana-test-server" +# - name: "Setup Python" +# uses: actions/setup-python@v4 +# with: +# python-version: "3.10" +# cache: "pip" +# - name: "Install pip dependencies" +# run: "pip install -r hrana-test-server/requirements.txt" - - name: "Build" - run: "npm run build" - - name: "Install npm dependencies of the Vercel test" - run: "cd smoke_test/vercel && npm install" +# - name: "Build" +# run: "npm run build" +# - name: "Install npm dependencies of the Vercel test" +# run: "cd smoke_test/vercel && npm install" - - name: "Test with Hrana 1 over WebSocket" - run: "cd smoke_test/vercel && python ../../hrana-test-server/server_v1.py node test.js" - env: {"URL": "ws://localhost:8080"} - - name: "Test with Hrana 2 over WebSocket" - run: "cd smoke_test/vercel && python ../../hrana-test-server/server_v2.py node test.js" - env: {"URL": "ws://localhost:8080"} - - name: "Test with Hrana 2 over HTTP" - run: "cd smoke_test/vercel && python ../../hrana-test-server/server_v2.py node test.js" - env: {"URL": "http://localhost:8080"} +# - name: "Test with Hrana 1 over WebSocket" +# run: "cd smoke_test/vercel && python ../../hrana-test-server/server_v1.py node test.js" +# env: {"URL": "ws://localhost:8080"} +# - name: "Test with Hrana 2 over WebSocket" +# run: "cd smoke_test/vercel && python ../../hrana-test-server/server_v2.py node test.js" +# env: {"URL": "ws://localhost:8080"} +# - name: "Test with Hrana 2 over HTTP" +# run: "cd smoke_test/vercel && python ../../hrana-test-server/server_v2.py node test.js" +# env: {"URL": "http://localhost:8080"} # - name: "Test with Hrana 3 over WebSocket" # run: "cd smoke_test/vercel && python ../../hrana-test-server/server_v3.py node test.js" # env: {"URL": "ws://localhost:8080"} diff --git a/src/__tests__/client.test.ts b/src/__tests__/client.test.ts index 0add4b1..a9334c9 100644 --- a/src/__tests__/client.test.ts +++ b/src/__tests__/client.test.ts @@ -45,6 +45,19 @@ function withClient( }; } +function withInMemoryClient( + f: (c: libsql.Client) => Promise, +): () => Promise { + return async () => { + const c = createClient({ url: ":memory:" }); + try { + await f(c); + } finally { + c.close(); + } + }; +} + describe("createClient()", () => { test("URL scheme not supported", () => { expect(() => createClient({url: "ftp://localhost"})) @@ -88,6 +101,10 @@ describe("createClient()", () => { // @ts-expect-error expect(() => createClient({...config, intMode: "foo"})).toThrow(/"foo"/); }); + + test("supports in-memory database", () => { + expect(() => createClient({url: ":memory:"})).not.toThrow(); + }); }); describe("execute()", () => { @@ -187,6 +204,21 @@ describe("execute()", () => { `); expect(rs.rowsAffected).toStrictEqual(3); })); + + test("query a single value using an in memory database", withInMemoryClient(async (c) => { + await c.batch([ + "DROP TABLE IF EXISTS t", + "CREATE TABLE t (a)", + "INSERT INTO t VALUES ('one'), ('two')", + ], "write"); + const insertRs = await c.execute("INSERT INTO t VALUES ('three')"); + expect(insertRs.lastInsertRowid).not.toBeUndefined(); + const selectRs = await c.execute({ + sql: "SELECT a FROM t WHERE ROWID = ?", + args: [insertRs.lastInsertRowid!], + }); + expect(Array.from(selectRs.rows[0])).toStrictEqual(["three"]); + })); }); describe("values", () => { diff --git a/src/config.ts b/src/config.ts index 32a881d..994c431 100644 --- a/src/config.ts +++ b/src/config.ts @@ -24,10 +24,32 @@ export function expandConfig(config: Config, preferHttp: boolean): ExpandedConfi throw new TypeError(`Expected client configuration as object, got ${typeof config}`); } - const uri = parseUri(config.url); - let tls: boolean | undefined = config.tls; let authToken = config.authToken; + let syncUrl = config.syncUrl; + const intMode = ""+(config.intMode ?? "number"); + if (intMode !== "number" && intMode !== "bigint" && intMode !== "string") { + throw new TypeError( + `Invalid value for intMode, expected "number", "bigint" or "string", \ + got ${JSON.stringify(intMode)}` + ); + } + + + if(config.url === ':memory:') { + return { + path: ':memory:', + scheme: 'file', + syncUrl, + intMode, + fetch: config.fetch, + tls: false, + authToken: undefined, + authority: undefined, + }; + } + + const uri = parseUri(config.url); for (const {key, value} of uri.query?.pairs ?? []) { if (key === "authToken") { authToken = value ? value : undefined; @@ -50,7 +72,6 @@ export function expandConfig(config: Config, preferHttp: boolean): ExpandedConfi ); } } - let syncUrl = config.syncUrl; const uriScheme = uri.scheme.toLowerCase(); let scheme: ExpandedScheme; @@ -87,14 +108,6 @@ export function expandConfig(config: Config, preferHttp: boolean): ExpandedConfi ); } - const intMode = ""+(config.intMode ?? "number"); - if (intMode !== "number" && intMode !== "bigint" && intMode !== "string") { - throw new TypeError( - `Invalid value for intMode, expected "number", "bigint" or "string", \ - got ${JSON.stringify(intMode)}` - ); - } - return { scheme, tls: tls ?? true,