Merge pull request #429 from mchangrh/redis-test

add redis tests
This commit is contained in:
Ajay Ramachandran
2021-12-20 22:50:21 -05:00
committed by GitHub
10 changed files with 61 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
import { db } from "../../src/databases/databases";
import { partialDeepEquals } from "../utils/partialDeepEquals";
import { partialDeepEquals, arrayPartialDeepEquals } from "../utils/partialDeepEquals";
import { getHash } from "../../src/utils/getHash";
import { ImportMock, } from "ts-mock-imports";
import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
@@ -434,7 +434,7 @@ describe("getSkipSegmentsByHash", () => {
}]
}];
assert.ok(partialDeepEquals(data, expected, false) || partialDeepEquals(data, expected2, false));
assert.ok(arrayPartialDeepEquals(data, expected) || arrayPartialDeepEquals(data, expected2));
assert.strictEqual(data[0].segments.length, 3);
done();
})

View File

@@ -3,7 +3,7 @@ import { getHash } from "../../../src/utils/getHash";
import assert from "assert";
import { client } from "../../utils/httpClient";
import { AxiosResponse } from "axios";
import { partialDeepEquals } from "../../utils/partialDeepEquals";
import { partialDeepEquals, arrayPartialDeepEquals } from "../../utils/partialDeepEquals";
const endpoint = "/api/ratings/rate";
const getRating = (hash: string, params?: unknown): Promise<AxiosResponse> => client.get(`${endpoint}/${hash}`, { params });
@@ -58,6 +58,9 @@ describe("getRating", () => {
.catch(err => done(err));
});
/*
This test will fail if tests are already ran with redis.
*/
it("Should be able to bulk fetch", (done) => {
getBulkRating([videoOnePartialHash, videoTwoPartialHash])
.then(res => {
@@ -80,7 +83,7 @@ describe("getRating", () => {
count: 10,
hash: videoOneIDHash,
}];
assert.ok(partialDeepEquals(res.data, expected));
assert.ok(arrayPartialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));

32
test/cases/redisTest.ts Normal file
View File

@@ -0,0 +1,32 @@
import { config } from "../../src/config";
import redis from "../../src/utils/redis";
import crypto from "crypto";
import assert from "assert";
const genRandom = (bytes=8) => crypto.pseudoRandomBytes(bytes).toString("hex");
const randKey1 = genRandom();
const randValue1 = genRandom();
const randKey2 = genRandom(16);
describe("redis test", function() {
before(async function() {
if (!config.redis) this.skip();
await redis.setAsync(randKey1, randValue1);
});
it("Should get stored value", (done) => {
redis.getAsync(randKey1)
.then(res => {
if (res.err) assert.fail(res.err);
assert.strictEqual(res.reply, randValue1);
done();
});
});
it("Should not be able to get not stored value", (done) => {
redis.getAsync(randKey2)
.then(res => {
if (res.reply || res.err ) assert.fail("Value should not be found")
done();
});
})
});

View File

@@ -9,6 +9,7 @@ import { initDb } from "../src/databases/databases";
import { ImportMock } from "ts-mock-imports";
import * as rateLimitMiddlewareModule from "../src/middleware/requestRateLimit";
import rateLimit from "express-rate-limit";
import redis from "../src/utils/redis";
async function init() {
ImportMock.mockFunction(rateLimitMiddlewareModule, "rateLimitMiddleware", rateLimit({
@@ -56,6 +57,7 @@ async function init() {
mocha.run((failures) => {
mockServer.close();
server.close();
redis.close(true);
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
});
});

View File

@@ -22,6 +22,12 @@ export const partialDeepEquals = (actual: Record<string, any>, expected: Record<
return true;
};
export const arrayPartialDeepEquals = (actual: Array<any>, expected: Array<any>): boolean => {
for (const value of expected)
if (!actual.some(a => partialDeepEquals(a, value, false))) return false;
return true;
};
export const arrayDeepEquals = (actual: Record<string, any>, expected: Record<string, any>, print = true): boolean => {
if (actual.length !== expected.length) return false;
let flag = true;