add arrayDeepPartialEquals

This commit is contained in:
Michael C 2021-12-19 19:37:22 -05:00
parent 68bc6469ce
commit 1f9dc92074
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
4 changed files with 28 additions and 8 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));
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));

View file

@ -3,19 +3,30 @@ import redis from "../../src/utils/redis";
import crypto from "crypto";
import assert from "assert";
const randomID = crypto.pseudoRandomBytes(8).toString("hex");
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(randomID, "test");
await redis.setAsync(randKey1, randValue1);
});
it("Should get stored value", (done) => {
redis.getAsync(randomID)
redis.getAsync(randKey1)
.then(res => {
if (res.err) assert.fail(res.err);
assert.strictEqual(res.reply, "test");
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

@ -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;