Refactored a bit the code; the original code was too experimental in style.

This commit is contained in:
didier amyot committed 2023-06-13 08:00:01 -04:00
1 parent f7a8d494f5
commit bbd705ee3b
3 files changed
+55 -60

No files matched your search

+21 -19
View File
@@ -24,8 +24,8 @@ Deno.test("DenoKVBlobService happy path", async (t) => {
await t.step("read write small text", async () => {
const size = await bs.persistBlob("0002", toUint8Array("hello world"));
const body = await bs.readAllBlob("0002");
assertEquals(body, toUint8Array("hello world"));
const content = await bs.readAllBlob("0002");
assertEquals(content, toUint8Array("hello world"));
assertEquals(await bs.getNumberOfBlobs("0002"), 1);
assertEquals(size, "hello world".length);
});
@@ -33,8 +33,8 @@ Deno.test("DenoKVBlobService happy path", async (t) => {
await t.step("read write large text", async () => {
const largeText = toUint8Array(generateLargeText(33, 100));
const size = await bs.persistBlob("0001", largeText);
const body = await bs.readAllBlob("0001");
assertEquals(body, largeText);
const content = await bs.readAllBlob("0001");
assertEquals(content, largeText);
assertEquals(await bs.getNumberOfBlobs("0001"), 21);
assertEquals(size, largeText.length);
});
@@ -61,15 +61,15 @@ Deno.test("DenoKVFileService happy path xxx ", async (t) => {
let file2: FileMetaData | undefined;
await t.step("Create 2 files xyzabc", async () => {
file1 = await fs.createFromFullBody(fileName1, "bob le chef");
file2 = await fs.createFromFullBody(fileName2, "le geant vert ☀️");
file1 = await fs.createFromFullContent(fileName1, "bob le chef");
file2 = await fs.createFromFullContent(fileName2, "le geant vert ☀️");
assertEquals(file1.path, fileName1);
assertEquals(file2.bodySize, 47);
assertEquals(file2.contentSize, 47);
});
await t.step("Get file metadata", async () => {
const ret = await fs.getMetaDataByID(file1?.id ?? "404");
const ret = await fs.getMetaDataByID(file1!.id);
assertEquals(ret, file1);
});
@@ -80,11 +80,13 @@ Deno.test("DenoKVFileService happy path xxx ", async (t) => {
});
await t.step("Get File by path", async () => {
assertEquals(await fs.getFullBodyByPath(fileName1), "bob le chef");
const ret = await fs.getRawContentByPath(fileName1);
assertEquals(new TextDecoder().decode(ret!), "bob le chef");
});
await t.step("Get File by Id", async () => {
assertEquals(await fs.getFullBodyByPath(fileName1), "bob le chef");
const ret = await fs.getRawContentByPath(fileName1);
assertEquals(new TextDecoder().decode(ret!), "bob le chef");
});
await t.step("Delete File", async () => {
@@ -102,19 +104,19 @@ Deno.test("DenoKVFileService happy path xxx ", async (t) => {
const newPath = "boris/streamed-file.txt";
const ret = await fs.createFromReader(newPath, new StringReader("hello world 18"));
assertEquals(ret.bodySize, 40);
assertEquals(ret.contentSize, 40);
const s = await fs.getRawBodyByPath(newPath);
const s = await fs.getRawContentByPath(newPath);
assertEquals(new TextDecoder().decode(s!), "hello world 18")
});
await t.step("Rename file", async () => {
const newPath = "boris/file2.txt";
const ret = await fs.renameById(file2?.id ?? "404", newPath);
const ret = await fs.renameById(file2!.id, newPath);
assertEquals(ret?.path, newPath);
assertEquals(
await fs.getFullBodyByPath(newPath),
new TextDecoder().decode((await fs.getRawContentByPath(newPath))!),
'le geant vert ☀️',
);
});
@@ -126,14 +128,14 @@ 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);
const inputContent = new Uint8Array([240]);
const file = await fs.createFromFullContent("hello", inputContent);
assertEquals(file.path, "hello");
assertEquals(file.bodySize, 27);
assertEquals(file.contentSize, 27);
const bodyString = await fs.getFullBodyByPath("hello");
assertEquals(bodyString, new TextDecoder().decode(inputBody));
const persistedContent = await fs.getRawContentByPath("hello");
assertEquals(persistedContent, inputContent);
});
kv.close();