chore: Typescript database models (#2886)

closes #2798
This commit is contained in:
Tom Moor
2022-01-06 18:24:28 -08:00
committed by GitHub
parent d3cbf250e6
commit b20a341f0c
207 changed files with 5624 additions and 5315 deletions

View File

@@ -17,7 +17,7 @@ import {
let count = 1;
export async function buildShare(overrides: Record<string, any> = {}) {
export async function buildShare(overrides: Partial<Share> = {}) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
@@ -64,7 +64,7 @@ export function buildTeam(overrides: Record<string, any> = {}) {
);
}
export function buildEvent(overrides: Record<string, any> = {}) {
export function buildEvent(overrides: Partial<Event> = {}) {
return Event.create({
name: "documents.publish",
ip: "127.0.0.1",
@@ -72,7 +72,7 @@ export function buildEvent(overrides: Record<string, any> = {}) {
});
}
export async function buildGuestUser(overrides: Record<string, any> = {}) {
export async function buildGuestUser(overrides: Partial<User> = {}) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
@@ -88,10 +88,14 @@ export async function buildGuestUser(overrides: Record<string, any> = {}) {
});
}
export async function buildUser(overrides: Record<string, any> = {}) {
export async function buildUser(overrides: Partial<User> = {}) {
let team;
if (!overrides.teamId) {
const team = await buildTeam();
team = await buildTeam();
overrides.teamId = team.id;
} else {
team = await Team.findByPk(overrides.teamId);
}
const authenticationProvider = await AuthenticationProvider.findOne({
@@ -109,7 +113,7 @@ export async function buildUser(overrides: Record<string, any> = {}) {
lastActiveAt: new Date("2018-01-01T00:00:00.000Z"),
authentications: [
{
authenticationProviderId: authenticationProvider.id,
authenticationProviderId: authenticationProvider!.id,
providerId: uuidv4(),
},
],
@@ -121,11 +125,11 @@ export async function buildUser(overrides: Record<string, any> = {}) {
);
}
export async function buildAdmin(overrides: Record<string, any> = {}) {
export async function buildAdmin(overrides: Partial<User> = {}) {
return buildUser({ ...overrides, isAdmin: true });
}
export async function buildInvite(overrides: Record<string, any> = {}) {
export async function buildInvite(overrides: Partial<User> = {}) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
@@ -140,7 +144,7 @@ export async function buildInvite(overrides: Record<string, any> = {}) {
});
}
export async function buildIntegration(overrides: Record<string, any> = {}) {
export async function buildIntegration(overrides: Partial<Integration> = {}) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
@@ -167,7 +171,9 @@ export async function buildIntegration(overrides: Record<string, any> = {}) {
});
}
export async function buildCollection(overrides: Record<string, any> = {}) {
export async function buildCollection(
overrides: Partial<Collection> & { userId?: string } = {}
) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
@@ -190,7 +196,9 @@ export async function buildCollection(overrides: Record<string, any> = {}) {
});
}
export async function buildGroup(overrides: Record<string, any> = {}) {
export async function buildGroup(
overrides: Partial<Group> & { userId?: string } = {}
) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
@@ -211,7 +219,9 @@ export async function buildGroup(overrides: Record<string, any> = {}) {
});
}
export async function buildGroupUser(overrides: Record<string, any> = {}) {
export async function buildGroupUser(
overrides: Partial<GroupUser> & { userId?: string; teamId?: string } = {}
) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
@@ -231,7 +241,9 @@ export async function buildGroupUser(overrides: Record<string, any> = {}) {
});
}
export async function buildDocument(overrides: Record<string, any> = {}) {
export async function buildDocument(
overrides: Partial<Document> & { userId?: string } = {}
) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
@@ -243,7 +255,10 @@ export async function buildDocument(overrides: Record<string, any> = {}) {
}
if (!overrides.collectionId) {
const collection = await buildCollection(overrides);
const collection = await buildCollection({
teamId: overrides.teamId,
userId: overrides.userId,
});
overrides.collectionId = collection.id;
}
@@ -258,7 +273,9 @@ export async function buildDocument(overrides: Record<string, any> = {}) {
});
}
export async function buildFileOperation(overrides: Record<string, any> = {}) {
export async function buildFileOperation(
overrides: Partial<FileOperation> = {}
) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
@@ -282,7 +299,7 @@ export async function buildFileOperation(overrides: Record<string, any> = {}) {
});
}
export async function buildAttachment(overrides: Record<string, any> = {}) {
export async function buildAttachment(overrides: Partial<Attachment> = {}) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
@@ -296,7 +313,10 @@ export async function buildAttachment(overrides: Record<string, any> = {}) {
}
if (!overrides.documentId) {
const document = await buildDocument(overrides);
const document = await buildDocument({
teamId: overrides.teamId,
userId: overrides.userId,
});
overrides.documentId = document.id;
}

View File

@@ -1,4 +1,5 @@
import "../env";
import "@server/database/sequelize";
// test environment variables
process.env.DATABASE_URL = process.env.DATABASE_URL_TEST;
@@ -7,5 +8,19 @@ process.env.GOOGLE_CLIENT_ID = "123";
process.env.SLACK_KEY = "123";
process.env.DEPLOYMENT = "";
process.env.ALLOWED_DOMAINS = "allowed-domain.com";
// This is needed for the relative manual mock to be picked up
jest.mock("../queues");
// We never want to make real S3 requests in test environment
jest.mock("aws-sdk", () => {
const mS3 = {
createPresignedPost: jest.fn(),
deleteObject: jest.fn().mockReturnThis(),
promise: jest.fn(),
};
return {
S3: jest.fn(() => mS3),
Endpoint: jest.fn(),
};
});

View File

@@ -1,13 +1,15 @@
import { v4 as uuidv4 } from "uuid";
import { sequelize } from "@server/database/sequelize";
import { User, Document, Collection, Team } from "@server/models";
import { sequelize } from "../sequelize";
const sql = sequelize.getQueryInterface();
const tables = Object.keys(sequelize.models).map((model) => {
const n = sequelize.models[model].getTableName();
return sql.queryGenerator.quoteTable(typeof n === "string" ? n : n.tableName);
return (sql.queryGenerator as any).quoteTable(
typeof n === "string" ? n : n.tableName
);
});
const flushQuery = `TRUNCATE ${tables.join(", ")}`;
const flushQuery = `TRUNCATE ${tables.join(", ")} CASCADE`;
export function flushdb() {
return sequelize.query(flushQuery);