-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic roundtrip test for changes index
- Loading branch information
Showing
4 changed files
with
37 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const test = require('tape'); | ||
const fs = require('fs/promises'); | ||
|
||
const { writeChangesFile, readChangesFile, mergeChangesFiles } = require('../storage/lake/changes-index'); | ||
|
||
test('roundtrip', async t => { | ||
await roundtrip(t, 'app.nearcrowd.near.dat'); | ||
await roundtrip(t, 'asset-manager.orderly-network.near.dat'); | ||
await roundtrip(t, 'changes.dat'); | ||
}); | ||
|
||
async function roundtrip(t, fileName) { | ||
const indexFileName = `test/data/lake/index/${fileName}`; | ||
const changes = await readStream(await readChangesFile(indexFileName)); | ||
const tempFileName = `${indexFileName}.tmp`; | ||
await writeChangesFile(tempFileName, convertChanges(changes)); | ||
const tempChanges = await readStream(await readChangesFile(tempFileName)); | ||
t.deepEqual(tempChanges, changes); | ||
t.ok((await fs.readFile(tempFileName)).equals(await fs.readFile(indexFileName))); | ||
} | ||
|
||
async function readStream(stream) { | ||
const chunks = []; | ||
for await (const chunk of stream) { | ||
chunks.push(chunk); | ||
} | ||
return chunks; | ||
} | ||
|
||
function convertChanges(allChanges) { | ||
const result = {}; | ||
for (const { accountId, key, changes } of allChanges) { | ||
result[accountId] = result[accountId] || []; | ||
result[accountId].push({ key, changes }); | ||
} | ||
return result; | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.