add redis tests

This commit is contained in:
Michael C 2021-12-19 02:03:50 -05:00
parent caf94a7a93
commit 68bc6469ce
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
6 changed files with 35 additions and 1 deletions

View file

@ -16,6 +16,10 @@
"host": "localhost",
"port": 5432
},
"redis": {
"host": "localhost",
"port": 6379
},
"createDatabaseIfNotExist": true,
"schemaFolder": "./databases",
"dbSchema": "./databases/_sponsorTimes.db.sql",

View file

@ -6,4 +6,8 @@ services:
- POSTGRES_USER=${PG_USER}
- POSTGRES_PASSWORD=${PG_PASS}
ports:
- 5432:5432
- 5432:5432
redis:
image: redis:alpine
ports:
- 6379:6379

View file

@ -8,6 +8,7 @@
"dev": "nodemon",
"dev:bash": "nodemon -x 'npm test ; npm start'",
"postgres:docker": "docker run --rm -p 5432:5432 -e POSTGRES_USER=ci_db_user -e POSTGRES_PASSWORD=ci_db_pass postgres:alpine",
"redis:docker": "docker run --rm -p 6379:6379 redis:alpine",
"start": "ts-node src/index.ts",
"tsc": "tsc -p tsconfig.json",
"lint": "eslint src test",

View file

@ -8,6 +8,7 @@ interface RedisSB {
set(key: string, value: string, callback?: Callback<string | null>): void;
setAsync?(key: string, value: string): Promise<{err: Error | null, reply: string | null}>;
delAsync?(...keys: [string]): Promise<Error | null>;
close?(flush?: boolean): void;
}
let exportObject: RedisSB = {
@ -29,6 +30,7 @@ if (config.redis) {
exportObject.getAsync = (key) => new Promise((resolve) => client.get(key, (err, reply) => resolve({ err, reply })));
exportObject.setAsync = (key, value) => new Promise((resolve) => client.set(key, value, (err, reply) => resolve({ err, reply })));
exportObject.delAsync = (...keys) => new Promise((resolve) => client.del(keys, (err) => resolve(err)));
exportObject.close = (flush) => client.end(flush);
client.on("error", function(error) {
Logger.error(error);

21
test/cases/redisTest.ts Normal file
View file

@ -0,0 +1,21 @@
import { config } from "../../src/config";
import redis from "../../src/utils/redis";
import crypto from "crypto";
import assert from "assert";
const randomID = crypto.pseudoRandomBytes(8).toString("hex");
describe("redis test", function() {
before(async function() {
if (!config.redis) this.skip();
await redis.setAsync(randomID, "test");
});
it("Should get stored value", (done) => {
redis.getAsync(randomID)
.then(res => {
if (res.err) assert.fail(res.err);
assert.strictEqual(res.reply, "test");
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
});
});