Skip to content

Commit

Permalink
fix: implementation of allFileIds in MockedStorage (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
marianogoldman authored Dec 7, 2022
1 parent 343bfa4 commit 6e7defd
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/MockedStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class MockedStorage implements IContentStorageComponent {
}

async *allFileIds(prefix?: string): AsyncIterable<string> {
for (const key in this.storage.keys()) {
for (const key of this.storage.keys()) {
if (!prefix || key.startsWith(prefix)) {
yield key
}
Expand Down
144 changes: 144 additions & 0 deletions test/MockedStorage.spec.ts
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'])
})
})

0 comments on commit 6e7defd

Please sign in to comment.