155 lines
4.4 KiB
TypeScript
155 lines
4.4 KiB
TypeScript
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
|
|
|
|
import {DenoKVFileService, FileMetaData, DenoKVBlobService, FileService} from "../src/fileservice.ts";
|
|
|
|
function generateLargeText(seed: number, size: number): string {
|
|
const words = [
|
|
"Norway",
|
|
"Germany",
|
|
"Ecuador",
|
|
"Chile",
|
|
"Colombia",
|
|
"Sri Lanka",
|
|
"Guinea",
|
|
"United Kingdom",
|
|
"Egypt",
|
|
"Palau",
|
|
"Latvia",
|
|
"Congo, Democratic Republic of the",
|
|
"Malawi",
|
|
"Senegal",
|
|
"Trinidad and Tobago",
|
|
"Guatemala",
|
|
"Chad",
|
|
"New Zealand",
|
|
"Malaysia",
|
|
"Lithuania",
|
|
"Taiwan",
|
|
"Solomon Islands",
|
|
"Grenada",
|
|
"Hungary",
|
|
"Korea, South"
|
|
]
|
|
let ret: string[] = [];
|
|
for(let i = 0; i < size; i ++){
|
|
seed = (seed * 4931 + 2753) % words.length;
|
|
ret.push(words[seed]);
|
|
}
|
|
return ret.join(' ');
|
|
}
|
|
|
|
async function asyncIterableToArray<T>(ite: AsyncIterable<T>): Promise<T[]>{
|
|
const ret: T[] = [];
|
|
for await(const e of ite){
|
|
ret.push(e);
|
|
}
|
|
return ret;
|
|
|
|
}
|
|
|
|
Deno.test("DenoKVBlobService happy path", async (t) => {
|
|
localStorage.clear();
|
|
const kv = await Deno.openKv(":memory:");
|
|
|
|
const bs = new DenoKVBlobService(kv, 50);
|
|
|
|
function toUint8Array(s: string): Uint8Array{
|
|
return new TextEncoder().encode(s);
|
|
}
|
|
|
|
await t.step("read write small text", async () => {
|
|
await bs.persistBlob('0002', toUint8Array('hello world'));
|
|
const body = await bs.getBlob('0002');
|
|
assertEquals(body, toUint8Array('hello world'));
|
|
assertEquals(await bs.getNumberOfBlobs('0002'), 1);
|
|
});
|
|
|
|
await t.step("read write large text", async () => {
|
|
const largeText = toUint8Array(generateLargeText(33, 100));
|
|
await bs.persistBlob('0001', largeText);
|
|
const body = await bs.getBlob('0001');
|
|
assertEquals(body, largeText);
|
|
assertEquals(await bs.getNumberOfBlobs('0001'), 21);
|
|
});
|
|
|
|
kv.close();
|
|
|
|
});
|
|
|
|
Deno.test("DenoKVFileService happy path", async (t) => {
|
|
const kv = await Deno.openKv(":memory:");
|
|
const fs: FileService = new DenoKVFileService(kv);
|
|
const fileName1 = "didier/file1.txt";
|
|
const fileName2 = "didier/file2.txt";
|
|
let file1 : FileMetaData|undefined;
|
|
let file2: FileMetaData|undefined;
|
|
|
|
|
|
await t.step("Create 2 files", async () => {
|
|
file1 = await fs.createFromFullBody(fileName1, "bob le chef");
|
|
file2 = await fs.createFromFullBody(fileName2, "le geant vert ☀️");
|
|
|
|
assertEquals(file1.path, fileName1);
|
|
assertEquals(file2.bodySize, "le geant vert ".length + 2);
|
|
});
|
|
|
|
await t.step("Get file metadata", async () => {
|
|
const ret = await fs.getMetaDataByID(file1?.id ?? '404');
|
|
assertEquals(ret, file1)
|
|
});
|
|
|
|
await t.step("List files", async () => {
|
|
const expected = [file1, file2];
|
|
const ret = await fs.list();
|
|
assertEquals(await asyncIterableToArray(ret), expected);
|
|
});
|
|
|
|
await t.step("Get File by path", async () => {
|
|
assertEquals(await fs.getFullBodyByPath(fileName1), "bob le chef");
|
|
});
|
|
|
|
await t.step("Get File by Id", async () => {
|
|
assertEquals(await fs.getFullBodyByPath(fileName1), "bob le chef");
|
|
});
|
|
|
|
await t.step("Delete File", async () => {
|
|
await fs.deleteByPath(fileName1);
|
|
await fs.deleteByPath("404 not found");
|
|
});
|
|
|
|
await t.step("List after delete", async () => {
|
|
const expected = [file2];
|
|
const ret = await fs.list();
|
|
assertEquals(await asyncIterableToArray(ret), expected);
|
|
});
|
|
|
|
await t.step("Rename file", async () => {
|
|
const newPath = "boris/file2.txt";
|
|
const ret = await fs.renameById(file2?.id ?? '404', newPath);
|
|
assertEquals(ret?.path, newPath);
|
|
|
|
assertEquals((await fs.getFullBodyByPath(newPath))?.length, file2?.bodySize?? -1);
|
|
});
|
|
|
|
kv.close();
|
|
});
|
|
|
|
|
|
Deno.test("DenoKVFileService happy path with byte array", async (t) => {
|
|
const kv = await Deno.openKv(":memory:");
|
|
const fs: FileService = new DenoKVFileService(kv);
|
|
await t.step("Create and read 1 binary file", async () => {
|
|
const inputBody = new Uint8Array([240]);
|
|
const file = await fs.createFromFullBody('hello', inputBody);
|
|
|
|
assertEquals(file.path, 'hello');
|
|
assertEquals(file.bodySize, inputBody.length);
|
|
|
|
const bodyString = await fs.getFullBodyByPath('hello');
|
|
assertEquals(bodyString, new TextDecoder().decode(inputBody));
|
|
});
|
|
|
|
|
|
kv.close();
|
|
|
|
}); |