generated from well-known-components/base-ts-project
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implementation of allFileIds in MockedStorage (#51)
- Loading branch information
1 parent
343bfa4
commit 6e7defd
Showing
2 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { createReadStream, readFileSync } from 'fs' | ||
import { IContentStorageComponent } from '../src' | ||
import { bufferToStream, streamToBuffer } from '../src/content-item' | ||
import { MockedStorage } from '../src/MockedStorage' | ||
|
||
describe('MockedStorage', () => { | ||
let storage: IContentStorageComponent | ||
let id: string | ||
let content: Buffer | ||
let id2: string | ||
let content2: Buffer | ||
|
||
beforeEach(async () => { | ||
storage = new MockedStorage() | ||
|
||
id = 'some-id' | ||
content = Buffer.from('123') | ||
id2 = 'another-id' | ||
content2 = Buffer.from('456') | ||
}) | ||
|
||
describe('Buffer utils', () => { | ||
it('unit test small', async () => { | ||
const b = Buffer.from('123') | ||
const s = bufferToStream(b) | ||
expect(await streamToBuffer(s)).toEqual(b) | ||
}) | ||
it('unit test small uses buffer', async () => { | ||
const b = Buffer.from('123') | ||
for await (const chunk of bufferToStream(b)) { | ||
expect(Buffer.isBuffer(chunk)).toBe(true) | ||
} | ||
}) | ||
it('streamToBuffer package.json', async () => { | ||
const stream = createReadStream(__filename) | ||
const raw = readFileSync(__filename) | ||
expect(await streamToBuffer(stream)).toEqual(raw) | ||
}) | ||
it('streamToBuffer package.json uses buffer', async () => { | ||
for await (const chunk of createReadStream(__filename)) { | ||
expect(Buffer.isBuffer(chunk)).toBe(true) | ||
} | ||
}) | ||
it('unit test big', async () => { | ||
const b = Buffer.from(new Uint8Array(1000000).fill(0)) | ||
const s = bufferToStream(b) | ||
expect(await streamToBuffer(s)).toEqual(b) | ||
}) | ||
}) | ||
|
||
it(`When content is stored, then it can be retrieved`, async () => { | ||
await storage.storeStream(id, bufferToStream(content)) | ||
|
||
await retrieveAndExpectStoredContentToBe(id, content) | ||
}) | ||
|
||
it(`When content is stored, then we can check if it exists`, async function () { | ||
await storage.storeStream(id, bufferToStream(content)) | ||
|
||
const exists = await storage.existMultiple([id]) | ||
|
||
expect(exists.get(id)).toEqual(true) | ||
expect(await storage.exist(id)).toBe(true) | ||
}) | ||
|
||
it(`When content is stored on already existing id, then it overwrites the previous content`, async function () { | ||
const newContent = Buffer.from('456') | ||
|
||
await storage.storeStream(id, bufferToStream(content)) | ||
await storage.storeStream(id, bufferToStream(newContent)) | ||
|
||
await retrieveAndExpectStoredContentToBe(id, newContent) | ||
}) | ||
|
||
it(`When content is deleted, then it is no longer available`, async function () { | ||
await storage.storeStream(id, bufferToStream(content)) | ||
|
||
let exists = await storage.existMultiple([id]) | ||
expect(exists.get(id)).toBe(true) | ||
expect(await storage.exist(id)).toBe(true) | ||
|
||
await storage.delete([id]) | ||
|
||
exists = await storage.existMultiple([id]) | ||
expect(await storage.exist(id)).toBe(false) | ||
expect(exists.get(id)).toBe(false) | ||
}) | ||
|
||
it(`When multiple content is stored, then multiple content exist`, async () => { | ||
await storage.storeStream(id, bufferToStream(content)) | ||
await storage.storeStream(id2, bufferToStream(content2)) | ||
expect(Array.from((await storage.existMultiple([id, id2, 'notStored'])).entries())).toEqual([ | ||
[id, true], | ||
[id2, true], | ||
['notStored', false] | ||
]) | ||
}) | ||
|
||
it(`When multiple content is stored, then multiple content is correct`, async () => { | ||
await storage.storeStream(id, bufferToStream(content)) | ||
await storage.storeStream(id2, bufferToStream(content2)) | ||
|
||
await retrieveAndExpectStoredContentToBe(id, content) | ||
await retrieveAndExpectStoredContentToBe(id2, content2) | ||
}) | ||
|
||
it(`When a content with bad compression ratio is stored and compressed, then it is not stored compressed`, async () => { | ||
await storage.storeStreamAndCompress(id, bufferToStream(content)) | ||
const retrievedContent = await storage.retrieve(id) | ||
expect(retrievedContent?.encoding).toBeNull() | ||
expect(await streamToBuffer(await retrievedContent!.asStream())).toEqual(content) | ||
}) | ||
|
||
it(`When attempting to retrieve content by nonexistent key, then it is returns undefined`, async () => { | ||
await storage.storeStreamAndCompress(id, bufferToStream(content)) | ||
const retrievedContent = await storage.retrieve('saraza') | ||
expect(retrievedContent?.encoding).toBeUndefined() | ||
}) | ||
|
||
async function retrieveAndExpectStoredContentToBe(idToRetrieve: string, expectedContent: Buffer) { | ||
const retrievedContent = await storage.retrieve(idToRetrieve) | ||
expect(await streamToBuffer(await retrievedContent!.asStream())).toEqual(expectedContent) | ||
} | ||
|
||
it(`When content exists, then it is possible to iterate over all keys in storage`, async () => { | ||
await storage.storeStream(id, bufferToStream(content)) | ||
await storage.storeStream(id2, bufferToStream(content2)) | ||
|
||
async function check(prefix: string, expected: string[]) { | ||
const filtered = [] | ||
for await (const key of await storage.allFileIds(prefix)) { | ||
filtered.push(key) | ||
} | ||
for (const filteredKey of expected) { | ||
expect(filtered).toContain(filteredKey) | ||
} | ||
return filtered | ||
} | ||
|
||
await check('an', ['another-id']) | ||
await check('so', ['some-id']) | ||
await check(undefined, ['another-id', 'some-id']) | ||
}) | ||
}) |