Linted code.
This commit is contained in:
1 parent
d12600348f
commit
eae420a5a4
8 files changed
+200
-214
No files matched your search
+104
-111
@@ -1,142 +1,135 @@
|
||||
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
|
||||
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts';
|
||||
|
||||
import {
|
||||
DenoKVBlobService,
|
||||
DenoKVFileService,
|
||||
FileMetaData,
|
||||
FileService,
|
||||
} from "../src/services/mod.ts";
|
||||
import { asyncIterableToArray } from "../src/utils.ts";
|
||||
import { generateLargeText } from "./utils.ts";
|
||||
import { StringReader } from "https://deno.land/std@0.191.0/io/mod.ts";
|
||||
import { DenoKVBlobService, DenoKVFileService, FileMetaData, FileService } from '../src/services/mod.ts';
|
||||
import { asyncIterableToArray } from '../src/utils.ts';
|
||||
import { generateLargeText } from './utils.ts';
|
||||
import { StringReader } from 'https://deno.land/std@0.191.0/io/mod.ts';
|
||||
|
||||
Deno.test('DenoKVBlobService happy path', async (t) => {
|
||||
localStorage.clear();
|
||||
const kv = await Deno.openKv(':memory:');
|
||||
|
||||
const bs = new DenoKVBlobService(kv, 50);
|
||||
|
||||
Deno.test("DenoKVBlobService happy path", async (t) => {
|
||||
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 () => {
|
||||
const size = await bs.persistBlob('0002', 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);
|
||||
});
|
||||
|
||||
function toUint8Array(s: string): Uint8Array {
|
||||
return new TextEncoder().encode(s);
|
||||
}
|
||||
await t.step('read write large text', async () => {
|
||||
const largeText = toUint8Array(generateLargeText(33, 100));
|
||||
const size = await bs.persistBlob('0001', largeText);
|
||||
const content = await bs.readAllBlob('0001');
|
||||
assertEquals(content, largeText);
|
||||
assertEquals(await bs.getNumberOfBlobs('0001'), 21);
|
||||
assertEquals(size, largeText.length);
|
||||
});
|
||||
|
||||
await t.step("read write small text", async () => {
|
||||
const size = await bs.persistBlob("0002", 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);
|
||||
});
|
||||
await t.step('read write large file', async () => {
|
||||
const fileName = 'tests/examples/teapot.obj';
|
||||
const f = await Deno.open(fileName);
|
||||
const size = await bs.persistReader('large-stream', f);
|
||||
|
||||
await t.step("read write large text", async () => {
|
||||
const largeText = toUint8Array(generateLargeText(33, 100));
|
||||
const size = await bs.persistBlob("0001", largeText);
|
||||
const content = await bs.readAllBlob("0001");
|
||||
assertEquals(content, largeText);
|
||||
assertEquals(await bs.getNumberOfBlobs("0001"), 21);
|
||||
assertEquals(size, largeText.length);
|
||||
});
|
||||
assertEquals(await bs.getNumberOfBlobs('large-stream'), 1030);
|
||||
assertEquals((await Deno.readFile(fileName)).length, size);
|
||||
f.close();
|
||||
});
|
||||
|
||||
await t.step("read write large file", async () => {
|
||||
const fileName = 'tests/examples/teapot.obj';
|
||||
const f = await Deno.open(fileName);
|
||||
const size = await bs.persistReader("large-stream", f);
|
||||
|
||||
assertEquals(await bs.getNumberOfBlobs("large-stream"), 1030);
|
||||
assertEquals((await Deno.readFile(fileName)).length, size);
|
||||
f.close();
|
||||
});
|
||||
|
||||
kv.close();
|
||||
kv.close();
|
||||
});
|
||||
|
||||
Deno.test("DenoKVFileService happy path xxx ", 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;
|
||||
Deno.test('DenoKVFileService happy path xxx ', 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 xyzabc", async () => {
|
||||
file1 = await fs.createFromFullContent(fileName1, "bob le chef");
|
||||
file2 = await fs.createFromFullContent(fileName2, "le geant vert ☀️");
|
||||
await t.step('Create 2 files xyzabc', async () => {
|
||||
file1 = await fs.createFromFullContent(fileName1, 'bob le chef');
|
||||
file2 = await fs.createFromFullContent(fileName2, 'le geant vert ☀️');
|
||||
|
||||
assertEquals(file1.path, fileName1);
|
||||
assertEquals(file2.contentSize, 47);
|
||||
});
|
||||
assertEquals(file1.path, fileName1);
|
||||
assertEquals(file2.contentSize, 47);
|
||||
});
|
||||
|
||||
await t.step("Get file metadata", async () => {
|
||||
const ret = await fs.getMetaDataByID(file1!.id);
|
||||
assertEquals(ret, file1);
|
||||
});
|
||||
await t.step('Get file metadata', async () => {
|
||||
const ret = await fs.getMetaDataByID(file1!.id);
|
||||
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 () => {
|
||||
const ret = await fs.getRawContentByPath(fileName1);
|
||||
assertEquals(new TextDecoder().decode(ret!), "bob le chef");
|
||||
});
|
||||
await t.step('Get File by path', async () => {
|
||||
const ret = await fs.getRawContentByPath(fileName1);
|
||||
assertEquals(new TextDecoder().decode(ret!), 'bob le chef');
|
||||
});
|
||||
|
||||
await t.step("Get File by Id", async () => {
|
||||
const ret = await fs.getRawContentByPath(fileName1);
|
||||
assertEquals(new TextDecoder().decode(ret!), "bob le chef");
|
||||
});
|
||||
await t.step('Get File by Id', async () => {
|
||||
const ret = await fs.getRawContentByPath(fileName1);
|
||||
assertEquals(new TextDecoder().decode(ret!), '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("Create File from reader", async () => {
|
||||
const newPath = "boris/streamed-file.txt";
|
||||
await t.step('Create File from reader', async () => {
|
||||
const newPath = 'boris/streamed-file.txt';
|
||||
|
||||
const ret = await fs.createFromReader(newPath, new StringReader("hello world 18"));
|
||||
assertEquals(ret.contentSize, 40);
|
||||
const ret = await fs.createFromReader(newPath, new StringReader('hello world 18'));
|
||||
assertEquals(ret.contentSize, 40);
|
||||
|
||||
const s = await fs.getRawContentByPath(newPath);
|
||||
assertEquals(new TextDecoder().decode(s!), "hello world 18")
|
||||
});
|
||||
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, newPath);
|
||||
assertEquals(ret?.path, newPath);
|
||||
await t.step('Rename file', async () => {
|
||||
const newPath = 'boris/file2.txt';
|
||||
const ret = await fs.renameById(file2!.id, newPath);
|
||||
assertEquals(ret?.path, newPath);
|
||||
|
||||
assertEquals(
|
||||
new TextDecoder().decode((await fs.getRawContentByPath(newPath))!),
|
||||
'le geant vert ☀️',
|
||||
);
|
||||
});
|
||||
assertEquals(
|
||||
new TextDecoder().decode((await fs.getRawContentByPath(newPath))!),
|
||||
'le geant vert ☀️',
|
||||
);
|
||||
});
|
||||
|
||||
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 inputContent = new Uint8Array([240]);
|
||||
const file = await fs.createFromFullContent("hello", inputContent);
|
||||
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 inputContent = new Uint8Array([240]);
|
||||
const file = await fs.createFromFullContent('hello', inputContent);
|
||||
|
||||
assertEquals(file.path, "hello");
|
||||
assertEquals(file.contentSize, 27);
|
||||
assertEquals(file.path, 'hello');
|
||||
assertEquals(file.contentSize, 27);
|
||||
|
||||
const persistedContent = await fs.getRawContentByPath("hello");
|
||||
assertEquals(persistedContent, inputContent);
|
||||
});
|
||||
const persistedContent = await fs.getRawContentByPath('hello');
|
||||
assertEquals(persistedContent, inputContent);
|
||||
});
|
||||
|
||||
kv.close();
|
||||
kv.close();
|
||||
});
|
||||
+42
-40
@@ -1,22 +1,25 @@
|
||||
|
||||
export function* randomNumbers(seed: number, max: number, maxResult: number|undefined = undefined) : Generator<number> {
|
||||
export function* randomNumbers(
|
||||
seed: number,
|
||||
max: number,
|
||||
maxResult: number | undefined = undefined,
|
||||
): Generator<number> {
|
||||
const a = 48271;
|
||||
const m = 2147483647;
|
||||
const m = 2147483647;
|
||||
max = Math.floor(max);
|
||||
let i = 0;
|
||||
while (true) {
|
||||
seed = (a * seed) % m;
|
||||
seed = (a * seed) % m;
|
||||
yield seed % max;
|
||||
i ++;
|
||||
if(maxResult !== undefined && i >= maxResult){
|
||||
return;
|
||||
i++;
|
||||
if (maxResult !== undefined && i >= maxResult) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function range(min: number, max: number): number[]{
|
||||
const ret = []
|
||||
for(let i = min; i < max; i ++){
|
||||
export function range(min: number, max: number): number[] {
|
||||
const ret = [];
|
||||
for (let i = min; i < max; i++) {
|
||||
ret.push(i);
|
||||
}
|
||||
return ret;
|
||||
@@ -24,37 +27,36 @@ export function range(min: number, max: number): number[]{
|
||||
|
||||
export 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",
|
||||
'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]);
|
||||
seed = (seed * 4931 + 2753) % words.length;
|
||||
ret.push(words[seed]);
|
||||
}
|
||||
return ret.join(" ");
|
||||
}
|
||||
|
||||
return ret.join(' ');
|
||||
}
|
||||
Reference in new issue
Block a user