Skip to content

Commit

Permalink
add test for using file in dirrectory
Browse files Browse the repository at this point in the history
  • Loading branch information
e1arikawa committed Nov 13, 2024
1 parent fc46200 commit a63b91d
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion packages/duckdb-wasm/test/opfs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function testOPFS(baseDir: string, bundle: () => duckdb.DuckDBBundle): vo
await writable.write(parquetBuffer);
await writable.close();
//2. handle is empty object, because worker gets a File Handle using the file name.
await db.registerFileHandle('test.parquet', {}, duckdb.DuckDBDataProtocol.BROWSER_FSACCESS, true);
await db.registerFileHandle('test.parquet', null, duckdb.DuckDBDataProtocol.BROWSER_FSACCESS, true);
await conn.send(`CREATE TABLE lineitem1 AS SELECT * FROM read_parquet('test.parquet')`);
await conn.send(`CHECKPOINT;`);

Expand All @@ -120,6 +120,33 @@ export function testOPFS(baseDir: string, bundle: () => duckdb.DuckDBBundle): vo
expect(table1.getChildAt(0)?.get(0)).toBeGreaterThan(60_000);
});

it('Load Parquet file that are already with opfs file handler in datadir', async () => {
//1. write to opfs
const parquetBuffer = await fetch(`${baseDir}/tpch/0_01/parquet/lineitem.parquet`).then(res =>
res.arrayBuffer(),
);
const opfsRoot = await navigator.storage.getDirectory();
const datadir = await opfsRoot.getDirectoryHandle("datadir", {create: true});
const fileHandle = await datadir.getFileHandle('test.parquet', {create: true});
const writable = await fileHandle.createWritable();
await writable.write(parquetBuffer);
await writable.close();
//2. handle is opfs file handler
await db.registerFileHandle('test.parquet', fileHandle, duckdb.DuckDBDataProtocol.BROWSER_FSACCESS, true);
await conn.send(`CREATE TABLE lineitem1 AS SELECT * FROM read_parquet('test.parquet')`);
await conn.send(`CHECKPOINT;`);

const result1 = await conn.send(`SELECT count(*)::INTEGER as cnt FROM lineitem1;`);
const batches1 = [];
for await (const batch of result1) {
batches1.push(batch);
}
const table1 = await new arrow.Table<{ cnt: arrow.Int }>(batches1);
expect(table1.getChildAt(0)?.get(0)).toBeGreaterThan(60_000);
await db.dropFile('test.parquet');
await opfsRoot.removeEntry("datadir");
});

it('Load Parquet file that are already', async () => {
const parquetBuffer = await fetch(`${baseDir}/tpch/0_01/parquet/lineitem.parquet`).then(res =>
res.arrayBuffer(),
Expand Down Expand Up @@ -268,5 +295,14 @@ export function testOPFS(baseDir: string, bundle: () => duckdb.DuckDBBundle): vo
});
await opfsRoot.removeEntry('test.parquet').catch(() => {
});
try {
let datadir = await opfsRoot.getDirectoryHandle('datadir');
datadir.removeEntry('test.parquet').catch(() => {
});
} catch (e) {
//
}
await opfsRoot.removeEntry('datadir').catch(() => {
});
}
}

0 comments on commit a63b91d

Please sign in to comment.