Skip to content

Commit

Permalink
Make sure rest-api.test also can use .only in custom tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Feb 16, 2024
1 parent 458f062 commit be8cf7e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
11 changes: 1 addition & 10 deletions test/changes-index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const test = require('tape');
const fs = require('fs/promises');
const { customTest } = require('./utils/custom-test');

const { writeChangesFile, readChangesFile, mergeChangesFiles } = require('../storage/lake/changes-index');

Expand Down Expand Up @@ -119,16 +120,6 @@ function changesAreEqual(changes1, changes2) {
return true;
}

function customTest(fn) {
const result = function(...args) {
fn(test, ...args);
}
result.only = function(...args) {
fn(test.only, ...args);
}
return result;
}

async function readStream(stream) {
const chunks = [];
for await (const chunk of stream) {
Expand Down
29 changes: 16 additions & 13 deletions test/rest-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test.onFinish(async () => {
await redis.shutdown();
});

const { customTest } = require('./utils/custom-test');
const { dumpChangesToStorage: handleStreamerMessage } = require('../scripts/load-from-near-lake');
const storage = require('../storage');
const app = require('../app');
Expand Down Expand Up @@ -229,7 +230,7 @@ test('/healthz (synced)', async t => {
t.isEqual(response.status, 204);
});

function testRequestImpl(testName, url, expectedStatus, expectedOutput, input, initFn) {
function testRequestImpl(test, testName, url, expectedStatus, expectedOutput, input, initFn) {
test(testName, async t => {
t.teardown(() => storage.clearDatabase());

Expand Down Expand Up @@ -258,30 +259,32 @@ function testRequestImpl(testName, url, expectedStatus, expectedOutput, input, i
});
}

function testRequest(testName, url, expectedStatus, expectedOutput, input = null) {
testRequestImpl(testName, url, expectedStatus, expectedOutput, input, async () => {
const testRequest = customTest((test, testName, url, expectedStatus, expectedOutput, input = null) => {
testRequestImpl(test, testName, url, expectedStatus, expectedOutput, input, async () => {
await handleStreamerMessage(STREAMER_MESSAGE);
});
}
});

function testRequestAfterDeletion(testName, url, expectedStatus, expectedOutput, input = null) {
testRequestImpl(`after deletion: ${testName}`, url, expectedStatus, expectedOutput, input, async () => {
const testRequestAfterDeletion = customTest((test, testName, url, expectedStatus, expectedOutput, input = null) => {
testRequestImpl(test, `after deletion: ${testName}`, url, expectedStatus, expectedOutput, input, async () => {
await handleStreamerMessage(STREAMER_MESSAGE);
await handleStreamerMessage(TEST_DELETION_STREAMER_MESSAGE);
});
}
});

function testRequestWithCompressHistory(testName, url, expectedStatus, expectedOutput, input = null) {
testRequestImpl(`after compression: ${testName}`, url, expectedStatus, expectedOutput, input, async () => {
const testRequestWithCompressHistory = customTest((test, testName, url, expectedStatus, expectedOutput, input = null) => {
testRequestImpl(test, `after compression: ${testName}`, url, expectedStatus, expectedOutput, input, async () => {
await handleStreamerMessage(STREAMER_MESSAGE, { historyLength: 1 });
await handleStreamerMessage(TEST_DELETION_STREAMER_MESSAGE, { historyLength: 1 });
});
}
});

function testViewMethod(methodName, expectedStatus, expectedOutput, input = null) {
const testViewMethod = customTest((test, methodName, expectedStatus, expectedOutput, input = null) => {
const url = `/account/test.near/view/${methodName}`;
testRequest(`call view method ${methodName}`, url, expectedStatus, expectedOutput, input);
}
testRequestImpl(test, `call view method ${methodName}`, url, expectedStatus, expectedOutput, input, async () => {
await handleStreamerMessage(STREAMER_MESSAGE);
});
});

testViewMethod('no-such-method', 404, 'method no-such-method not found');
testViewMethod('fibonacci', 200, Buffer.from([13, 0, 0, 0, 0, 0, 0, 0,]), Buffer.from([7]));
Expand Down
13 changes: 13 additions & 0 deletions test/utils/custom-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const test = require('tape');

function customTest(fn) {
const result = function(...args) {
fn(test, ...args);
}
result.only = function(...args) {
fn(test.only, ...args);
}
return result;
}

module.exports = { customTest };

0 comments on commit be8cf7e

Please sign in to comment.