Skip to content

Commit

Permalink
Add basic roundtrip test for changes index
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Feb 14, 2024
1 parent 1be08c6 commit 7d1228d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/changes-index.test.js
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 added test/data/lake/index/app.nearcrowd.near.dat
Binary file not shown.
Binary file not shown.
Binary file added test/data/lake/index/changes.dat
Binary file not shown.

0 comments on commit 7d1228d

Please sign in to comment.