getViewsForUser

This commit is contained in:
Michael C 2023-09-30 21:20:42 -04:00
parent 467443a03f
commit 3c6803fb62
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4

View file

@ -1,7 +1,8 @@
import { getHash } from "../../src/utils/getHash";
import { db } from "../../src/databases/databases";
import { client } from "../utils/httpClient";
import assert from "assert";
import { genUsers, User } from "../utils/genUser";
import { insertSegment } from "../utils/segmentQueryGen";
// helpers
const endpoint = "/api/getViewsForUser";
@ -10,53 +11,46 @@ const getViewsForUser = (userID: string) => client({
params: { userID }
});
const getViewUserOne = "getViewUser1";
const userOneViewsFirst = 30;
const userOneViewsSecond = 0;
const cases = [
"u-1",
"u-2",
"u-3"
];
const users = genUsers("getViewUser", cases);
const getViewUserTwo = "getViewUser2";
const userTwoViews = 0;
const getViewUserThree = "getViewUser3";
// set views for users
users["u-1"].info["views1"] = 30;
users["u-1"].info["views2"] = 0;
users["u-1"].info["views"] = users["u-1"].info.views1 + users["u-1"].info.views2;
users["u-2"].info["views"] = 0;
users["u-3"].info["views"] = 0;
const checkUserViews = (user: User) =>
getViewsForUser(user.privID)
.then(result => {
assert.strictEqual(result.status, 200);
assert.strictEqual(result.data.viewCount, user.info.views);
});
describe("getViewsForUser", function() {
before(() => {
const insertSponsorTimeQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "actionType", "videoDuration", "shadowHidden", "hashedVideoID") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
db.prepare("run", insertSponsorTimeQuery, ["getViewUserVideo", 0, 1, 0, "getViewUserVideo0", getHash(getViewUserOne), 0, userOneViewsFirst, "sponsor", "skip", 0, 0, "getViewUserVideo"]);
db.prepare("run", insertSponsorTimeQuery, ["getViewUserVideo", 0, 1, 0, "getViewUserVideo1", getHash(getViewUserOne), 0, userOneViewsSecond, "sponsor", "skip", 0, 0, "getViewUserVideo"]);
db.prepare("run", insertSponsorTimeQuery, ["getViewUserVideo", 0, 1, 0, "getViewUserVideo2", getHash(getViewUserTwo), 0, userTwoViews, "sponsor", "skip", 0, 0, "getViewUserVideo"]);
// add views for users
insertSegment(db, { userID: users["u-1"].pubID, views: users["u-1"].info.views1 });
insertSegment(db, { userID: users["u-1"].pubID, views: users["u-1"].info.views2 });
insertSegment(db, { userID: users["u-2"].pubID, views: users["u-2"].info.views });
});
it("Should get back views for user one", (done) => {
getViewsForUser(getViewUserOne)
.then(result => {
assert.strictEqual(result.data.viewCount, userOneViewsFirst + userOneViewsSecond);
done();
})
.catch(err => done(err));
});
it("Should get back views for user two", (done) => {
getViewsForUser(getViewUserTwo)
.then(result => {
assert.strictEqual(result.data.viewCount, userTwoViews);
done();
})
.catch(err => done(err));
});
it("Should get 404 if no submissions", (done) => {
getViewsForUser(getViewUserThree)
.then(result => {
assert.strictEqual(result.status, 404);
done();
})
.catch(err => done(err));
});
it("Should return 400 if no userID provided", (done) => {
it("Should get back views for user one", () =>
checkUserViews(users["u-1"])
);
it("Should get back views for user two", () =>
checkUserViews(users["u-2"])
);
it("Should get 404 if no submissions", () =>
getViewsForUser(users["u-3"].pubID)
.then(result => assert.strictEqual(result.status, 404))
);
it("Should return 400 if no userID provided", () =>
client({ url: endpoint })
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
.then(res => assert.strictEqual(res.status, 400))
);
});