-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(MemoryStorage): correctly respect the desc option (#1666)
- Loading branch information
1 parent
49e270c
commit b5f37f6
Showing
2 changed files
with
59 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
55 changes: 55 additions & 0 deletions
55
packages/memory-storage/test/reverse-datataset-list.test.ts
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,55 @@ | ||
import { rm } from 'node:fs/promises'; | ||
import { resolve } from 'node:path'; | ||
import { MemoryStorage } from '@crawlee/memory-storage'; | ||
import type { DatasetClient } from '@crawlee/types'; | ||
|
||
const elements = Array.from({ length: 10 }, (_, i) => ({ number: i })); | ||
|
||
describe('Dataset#listItems respects the desc option', () => { | ||
const localDataDirectory = resolve(__dirname, './tmp/desc'); | ||
const storage = new MemoryStorage({ | ||
localDataDirectory, | ||
persistStorage: false, | ||
}); | ||
|
||
let dataset: DatasetClient; | ||
|
||
afterAll(async () => { | ||
await rm(localDataDirectory, { force: true, recursive: true }); | ||
}); | ||
|
||
beforeAll(async () => { | ||
const { id: falseDatasetId } = await storage.datasets().getOrCreate('false'); | ||
dataset = storage.dataset(falseDatasetId); | ||
|
||
await dataset.pushItems(elements); | ||
}); | ||
|
||
test('with desc: false', async () => { | ||
const result = await dataset.listItems({ desc: false, limit: 5 }); | ||
|
||
expect(result.items).toHaveLength(5); | ||
expect(result.items).toStrictEqual(elements.slice(0, 5)); | ||
}); | ||
|
||
test('with desc: true', async () => { | ||
const result = await dataset.listItems({ desc: true, limit: 5 }); | ||
|
||
expect(result.items).toHaveLength(5); | ||
expect(result.items).toStrictEqual(elements.slice().reverse().slice(0, 5)); | ||
}); | ||
|
||
test('with desc: false and offset: 2', async () => { | ||
const result = await dataset.listItems({ desc: false, limit: 5, offset: 2 }); | ||
|
||
expect(result.items).toHaveLength(5); | ||
expect(result.items).toStrictEqual(elements.slice(2, 7)); | ||
}); | ||
|
||
test('with desc: true and offset: 2', async () => { | ||
const result = await dataset.listItems({ desc: true, limit: 5, offset: 2 }); | ||
|
||
expect(result.items).toHaveLength(5); | ||
expect(result.items).toStrictEqual(elements.slice().reverse().slice(2, 7)); | ||
}); | ||
}); |