@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
967 lines • 233 kB
JavaScript
/**
* @module FileStorage
*/
import { vi, } from "vitest";
import {} from "../../../event-bus/contracts/_module.js";
import { FileSize } from "../../../file-size/implementations/_module.js";
import { FILE_EVENTS, KeyExistsFileError, KeyNotFoundFileError, } from "../../../file-storage/contracts/_module.js";
import {} from "../../../namespace/contracts/_module.js";
import {} from "../../../serde/contracts/_module.js";
import { isBytesArrayEqualityTester, resolveStream, } from "../../../test-utilities/_module.js";
import {} from "../../../time-span/contracts/_module.js";
import { TimeSpan } from "../../../time-span/implementations/_module.js";
import {} from "../../../utilities/_module.js";
/**
* The `fileStorageTestSuite` function simplifies the process of testing your custom implementation of {@link IFileStorage | `IFileStorage`} with `vitest`.
*
* IMPORT_PATH: `"@daiso-tech/core/file-storage/test-utilities"`
* @group TestUtilities
*/
export function fileStorageTestSuite(settings) {
const { expect, test, createFileStorage, describe, beforeEach: beforeEach_, excludeEventTests = false, excludeSerdeTests = false, eventDispatchWaitTime = TimeSpan.fromMilliseconds(10), } = settings;
let fileStorage;
let serde;
beforeEach_(async () => {
const { fileStorage: fileStorage_, serde: serde_ } = await createFileStorage();
fileStorage = fileStorage_;
serde = serde_;
});
const waitForSettings = {
interval: TimeSpan.fromTimeSpan(eventDispatchWaitTime).toMilliseconds(),
timeout: TimeSpan.fromTimeSpan(eventDispatchWaitTime)
.multiply(3)
.toMilliseconds(),
};
describe("IFileStorage tests:", () => {
expect.addEqualityTesters([isBytesArrayEqualityTester]);
describe("Api tests:", () => {
describe("method: getText", () => {
test("Should return null when key does not exists", async () => {
const noneExistingKey = "a";
const result = await fileStorage
.create(noneExistingKey)
.getText();
expect(result).toBeNull();
});
test("Should return text when key exists", async () => {
const key = "a";
const text = "CONTENT";
const data = new Uint8Array(Buffer.from(text, "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const result = await file.getText();
expect(result).toEqual(text);
});
});
describe("method: getTextOrFail", () => {
test("Should throw KeyNotFoundFileError when key does not exists", async () => {
const noneExistingKey = "a";
const result = fileStorage
.create(noneExistingKey)
.getTextOrFail();
await expect(result).rejects.toBeInstanceOf(KeyNotFoundFileError);
});
test("Should return text when key exists", async () => {
const key = "a";
const text = "CONTENT";
const data = new Uint8Array(Buffer.from(text, "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const result = await file.getTextOrFail();
expect(result).toEqual(text);
});
});
describe("method: getBytes", () => {
test("Should return null when key does not exists", async () => {
const noneExistingKey = "a";
const result = await fileStorage
.create(noneExistingKey)
.getBytes();
expect(result).toBeNull();
});
test("Should return Uint8Array when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const result = await file.getBytes();
expect(result).toEqual(data);
});
});
describe("method: getBytesOrFail", () => {
test("Should throw KeyNotFoundFileError when key does not exists", async () => {
const noneExistingKey = "a";
const result = fileStorage
.create(noneExistingKey)
.getBytesOrFail();
await expect(result).rejects.toBeInstanceOf(KeyNotFoundFileError);
});
test("Should return Uint8Array when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const result = await file.getBytesOrFail();
expect(result).toEqual(data);
});
});
describe("method: getBuffer", () => {
test("Should return null when key does not exists", async () => {
const noneExistingKey = "a";
const result = await fileStorage
.create(noneExistingKey)
.getBuffer();
expect(result).toBeNull();
});
test("Should return Uint8Array when key exists", async () => {
const key = "a";
const buffer = Buffer.from("CONTENT", "utf8");
const data = new Uint8Array(buffer);
const file = fileStorage.create(key);
await file.add({ data });
const result = await file.getBuffer();
expect(result).toEqual(buffer);
});
});
describe("method: getBufferOrFail", () => {
test("Should throw KeyNotFoundFileError when key does not exists", async () => {
const noneExistingKey = "a";
const result = fileStorage
.create(noneExistingKey)
.getBufferOrFail();
await expect(result).rejects.toBeInstanceOf(KeyNotFoundFileError);
});
test("Should return Uint8Array when key exists", async () => {
const key = "a";
const buffer = Buffer.from("CONTENT", "utf8");
const data = new Uint8Array(buffer);
const file = fileStorage.create(key);
await file.add({ data });
const result = await file.getBufferOrFail();
expect(result).toEqual(data);
});
});
describe("method: getArrayBuffer", () => {
test("Should return null when key does not exists", async () => {
const noneExistingKey = "a";
const result = await fileStorage
.create(noneExistingKey)
.getArrayBuffer();
expect(result).toBeNull();
});
test("Should return ArrayBuffer when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const result = await file.getArrayBuffer();
expect(result).toEqual(data);
});
});
describe("method: getArrayBufferOrFail", () => {
test("Should throw KeyNotFoundFileError when key does not exists", async () => {
const noneExistingKey = "a";
const result = fileStorage
.create(noneExistingKey)
.getArrayBufferOrFail();
await expect(result).rejects.toBeInstanceOf(KeyNotFoundFileError);
});
test("Should return ArrayBuffer when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const result = await file.getArrayBufferOrFail();
expect(result).toEqual(data);
});
});
describe("method: getReadable", () => {
test("Should return null when key does not exists", async () => {
const noneExistingKey = "a";
const result = await fileStorage
.create(noneExistingKey)
.getReadable();
expect(result).toBeNull();
});
test("Should return Readable when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const result = await resolveStream(await file.getReadable());
expect(result).toEqual(data);
});
});
describe("method: getReadableOrFail", () => {
test("Should throw KeyNotFoundFileError when key does not exists", async () => {
const noneExistingKey = "a";
const result = fileStorage
.create(noneExistingKey)
.getReadableOrFail();
await expect(result).rejects.toBeInstanceOf(KeyNotFoundFileError);
});
test("Should return Readable when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const result = await resolveStream(await file.getReadableOrFail());
expect(result).toEqual(data);
});
});
describe("method: getReadableStream", () => {
test("Should return null when key does not exists", async () => {
const noneExistingKey = "a";
const result = await fileStorage
.create(noneExistingKey)
.getReadableStream();
expect(result).toBeNull();
});
test("Should return Readable when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const result = await resolveStream(await file.getReadableStream());
expect(result).toEqual(data);
});
});
describe("method: getReadableStreamOrFail", () => {
test("Should throw KeyNotFoundFileError when key does not exists", async () => {
const noneExistingKey = "a";
const result = fileStorage
.create(noneExistingKey)
.getReadableStreamOrFail();
await expect(result).rejects.toBeInstanceOf(KeyNotFoundFileError);
});
test("Should return Readable when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const result = await resolveStream(await file.getReadableStreamOrFail());
expect(result).toEqual(data);
});
});
describe("method: getMetadata", () => {
test("Should return null when key does not exists", async () => {
const result = await fileStorage.create("a").getMetadata();
expect(result).toBeNull();
});
test("Should return initial metadata with a null updatedAt after add method", async () => {
const file = fileStorage.create("a.json");
const data = new Uint8Array(Buffer.from(JSON.stringify({ content: "CONTENT" }), "utf8"));
await file.add({ data });
const result = await file.getMetadata();
expect(result).toEqual({
etag: expect.any(String),
contentType: "application/json",
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return initial metadata with a null updatedAt after put method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.put({ data });
const result = await file.getMetadata();
expect(result).toEqual({
etag: expect.any(String),
contentType: "text/plain",
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after add method followed by update method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.add({ data });
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.update({
data: newData,
contentType: newContentType,
});
const result = await file.getMetadata();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after put method followed by update method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.put({ data });
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.update({
data: newData,
contentType: newContentType,
});
const result = await file.getMetadata();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after put overwrites an existing file", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.put({ data });
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.put({
data: newData,
contentType: newContentType,
});
const result = await file.getMetadata();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return initial metadata with a null updatedAt after addStream method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.addStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const result = await file.getMetadata();
expect(result).toEqual({
etag: expect.any(String),
contentType: "text/plain",
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return initial metadata with a null updatedAt after putStream method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.putStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const result = await file.getMetadata();
expect(result).toEqual({
etag: expect.any(String),
contentType: "text/plain",
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after addStream method followed by updateStream method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.addStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.updateStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(newData);
},
},
contentType: newContentType,
});
const result = await file.getMetadata();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after putStream method followed by updateStream method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.putStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.updateStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(newData);
},
},
contentType: newContentType,
});
const result = await file.getMetadata();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after putStream overwrites an existing file", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.putStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.putStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(newData);
},
},
contentType: newContentType,
});
const result = await file.getMetadata();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
});
describe("method: getMetadataOrFail", () => {
test("Should throw KeyNotFoundFileError when key does not exists", async () => {
const result = fileStorage.create("a").getMetadataOrFail();
await expect(result).rejects.toBeInstanceOf(KeyNotFoundFileError);
});
test("Should return initial metadata with a null updatedAt after add method", async () => {
const file = fileStorage.create("a.json");
const data = new Uint8Array(Buffer.from(JSON.stringify({ content: "CONTENT" }), "utf8"));
await file.add({ data });
const result = await file.getMetadataOrFail();
expect(result).toEqual({
etag: expect.any(String),
contentType: "application/json",
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return initial metadata with a null updatedAt after put method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.put({ data });
const result = await file.getMetadataOrFail();
expect(result).toEqual({
etag: expect.any(String),
contentType: "text/plain",
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after add method followed by update method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.add({ data });
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.update({
data: newData,
contentType: newContentType,
});
const result = await file.getMetadataOrFail();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after put method followed by update method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.put({ data });
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.update({
data: newData,
contentType: newContentType,
});
const result = await file.getMetadataOrFail();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after put overwrites an existing file", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.put({ data });
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.put({
data: newData,
contentType: newContentType,
});
const result = await file.getMetadataOrFail();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return initial metadata with a null updatedAt after addStream method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.addStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const result = await file.getMetadataOrFail();
expect(result).toEqual({
etag: expect.any(String),
contentType: "text/plain",
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return initial metadata with a null updatedAt after putStream method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.putStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const result = await file.getMetadataOrFail();
expect(result).toEqual({
etag: expect.any(String),
contentType: "text/plain",
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after addStream method followed by updateStream method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.addStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.updateStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(newData);
},
},
contentType: newContentType,
});
const result = await file.getMetadataOrFail();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after putStream method followed by updateStream method", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.putStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.updateStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(newData);
},
},
contentType: newContentType,
});
const result = await file.getMetadataOrFail();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
test("Should return metadata with a valid updatedAt after putStream overwrites an existing file", async () => {
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create("a.txt");
await file.putStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const newContentType = "application/octet-stream";
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.putStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(newData);
},
},
contentType: newContentType,
});
const result = await file.getMetadataOrFail();
expect(result).toEqual({
etag: expect.any(String),
contentType: newContentType,
fileSize: FileSize.fromBytes(data.byteLength),
updatedAt: expect.any(Date),
});
});
});
describe("method: exists", () => {
test("Should return false when key does not exists", async () => {
const noneExistingKey = "a";
const result = await fileStorage
.create(noneExistingKey)
.exists();
expect(result).toBe(false);
});
test("Should return true when key exists", async () => {
const key = "a";
const buffer = Buffer.from("CONTENT", "utf8");
const data = new Uint8Array(buffer);
const file = fileStorage.create(key);
await file.add({ data });
const result = await file.exists();
expect(result).toBe(true);
});
});
describe("method: missing", () => {
test("Should return true when key does not exists", async () => {
const noneExistingKey = "a";
const result = await fileStorage
.create(noneExistingKey)
.missing();
expect(result).toBe(true);
});
test("Should return false when key exists", async () => {
const key = "a";
const buffer = Buffer.from("CONTENT", "utf8");
const data = new Uint8Array(buffer);
const file = fileStorage.create(key);
await file.add({ data });
const result = await file.missing();
expect(result).toBe(false);
});
});
describe("method: add", () => {
test("Should return true when key does not exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const result = await fileStorage.create(key).add({ data });
expect(result).toBe(true);
});
test("Should return false when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8"));
const result = await file.add({ data: newData });
expect(result).toBe(false);
});
test("Should persist data when key does not exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const result = await file.getBytes();
expect(result).toEqual(data);
});
test("Should not persist data when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8"));
await file.add({ data: newData });
const result = await file.getBytes();
expect(result).toEqual(data);
});
});
describe("method: addOrFail", () => {
test("Should not throw error when key does not exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const result = fileStorage.create(key).addOrFail({ data });
await expect(result).resolves.toBeUndefined();
});
test("Should throw KeyExistsFileError when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.addOrFail({ data });
const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8"));
const result = file.addOrFail({ data: newData });
await expect(result).rejects.toBeInstanceOf(KeyExistsFileError);
});
test("Should persist data when key does not exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.addOrFail({ data });
const result = await file.getBytes();
expect(result).toEqual(data);
});
test("Should not persist data when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.addOrFail({ data });
const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8"));
try {
await file.addOrFail({ data: newData });
}
catch (error) {
if (!(error instanceof KeyExistsFileError)) {
throw error;
}
}
const result = await file.getBytes();
expect(result).toEqual(data);
});
});
describe("method: addStream", () => {
test("Should return true when key does not exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const result = await fileStorage.create(key).addStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
expect(result).toBe(true);
});
test("Should return false when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.addStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8"));
const result = await file.addStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(newData);
},
},
});
expect(result).toBe(false);
});
test("Should persist data when key does not exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.addStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const result = await file.getBytes();
expect(result).toEqual(data);
});
test("Should not persist data when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.addStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8"));
await file.addStream({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(newData);
},
},
});
const result = await file.getBytes();
expect(result).toEqual(data);
});
});
describe("method: addStreamOrFail", () => {
test("Should not throw error when key does not exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const result = fileStorage.create(key).addStreamOrFail({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
await expect(result).resolves.toBeUndefined();
});
test("Should throw KeyExistsFileError when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.addStreamOrFail({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8"));
const result = file.addStreamOrFail({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(newData);
},
},
});
await expect(result).rejects.toBeInstanceOf(KeyExistsFileError);
});
test("Should persist data when key does not exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.addStreamOrFail({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const result = await file.getBytes();
expect(result).toEqual(data);
});
test("Should not persist data when key exists", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.addStreamOrFail({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(data);
},
},
});
const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8"));
try {
await file.addStreamOrFail({
data: {
async *[Symbol.asyncIterator]() {
yield Promise.resolve(newData);
},
},
});
}
catch (error) {
if (!(error instanceof KeyExistsFileError)) {
throw error;
}
}
const result = await file.getBytes();
expect(result).toEqual(data);
});
});
describe("method: update", () => {
test("Should return false when key doesnt exist", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const result = await fileStorage.create(key).update({
data,
});
expect(result).toBe(false);
});
test("Should not persist data when key doesnt exist", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.update({ data });
const result = await file.getBytes();
expect(result).toBeNull();
});
test("Should return true when key exist", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const result = await file.update({ data: newData });
expect(result).toBe(true);
});
test("Should persist data when key exist", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
await file.add({ data });
const newData = new Uint8Array(Buffer.from("CONTENT", "utf8"));
await file.update({ data: newData });
const result = await file.getBytes();
expect(result).toEqual(newData);
});
});
describe("method: updateOrFail", () => {
test("Should throw KeyNotFoundFileError when key doesnt exist", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const result = fileStorage.create(key).updateOrFail({
data,
});
await expect(result).rejects.toBeInstanceOf(KeyNotFoundFileError);
});
test("Should not persist data when key doesnt exist", async () => {
const key = "a";
const data = new Uint8Array(Buffer.from("CONTENT", "utf8"));
const file = fileStorage.create(key);
try {
await fi