Lint code.
Added a deno.json and set linting rules. Obviously they are the perfect rules and no one should argue with them in any fashion ever.
This commit is contained in:
1 parent
ce7b6ae523
commit
8ce2dadc47
4 files changed
+225
-165
No files matched your search
+98
-96
@@ -1,6 +1,11 @@
|
||||
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
|
||||
|
||||
import {DenoKVFileService, FileMetaData, DenoKVBlobService, FileService} from "../src/fileservice.ts";
|
||||
import {
|
||||
DenoKVBlobService,
|
||||
DenoKVFileService,
|
||||
FileMetaData,
|
||||
FileService,
|
||||
} from "../src/fileservice.ts";
|
||||
|
||||
function generateLargeText(seed: number, size: number): string {
|
||||
const words = [
|
||||
@@ -28,128 +33,125 @@ function generateLargeText(seed: number, size: number): string {
|
||||
"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(' ');
|
||||
"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;
|
||||
|
||||
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);
|
||||
localStorage.clear();
|
||||
const kv = await Deno.openKv(":memory:");
|
||||
|
||||
function toUint8Array(s: string): Uint8Array{
|
||||
return new TextEncoder().encode(s);
|
||||
}
|
||||
const bs = new DenoKVBlobService(kv, 50);
|
||||
|
||||
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);
|
||||
});
|
||||
function toUint8Array(s: string): Uint8Array {
|
||||
return new TextEncoder().encode(s);
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
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);
|
||||
});
|
||||
|
||||
kv.close();
|
||||
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;
|
||||
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 ☀️");
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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("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("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 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("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("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("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);
|
||||
|
||||
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,
|
||||
);
|
||||
});
|
||||
|
||||
assertEquals((await fs.getFullBodyByPath(newPath))?.length, file2?.bodySize?? -1);
|
||||
});
|
||||
|
||||
kv.close();
|
||||
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);
|
||||
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);
|
||||
assertEquals(file.path, "hello");
|
||||
assertEquals(file.bodySize, inputBody.length);
|
||||
|
||||
const bodyString = await fs.getFullBodyByPath('hello');
|
||||
assertEquals(bodyString, new TextDecoder().decode(inputBody));
|
||||
});
|
||||
const bodyString = await fs.getFullBodyByPath("hello");
|
||||
assertEquals(bodyString, new TextDecoder().decode(inputBody));
|
||||
});
|
||||
|
||||
|
||||
kv.close();
|
||||
|
||||
});
|
||||
kv.close();
|
||||
});
|
||||
Reference in new issue
Block a user