chore: Improve perf of server tests (#5785)

This commit is contained in:
Tom Moor
2023-09-06 07:14:49 -04:00
committed by GitHub
parent a724a21c21
commit 3eb947e9a5
69 changed files with 2045 additions and 1551 deletions

View File

@@ -1,107 +1,12 @@
import TestServer from "fetch-test-server";
import { v4 as uuidv4 } from "uuid";
import { WhereOptions } from "sequelize";
import sharedEnv from "@shared/env";
import { CollectionPermission } from "@shared/types";
import env from "@server/env";
import { User, Document, Collection, Team } from "@server/models";
import { Event, Team } from "@server/models";
import onerror from "@server/onerror";
import webService from "@server/services/web";
import { sequelize } from "@server/storage/database";
export const seed = async () =>
sequelize.transaction(async (transaction) => {
const team = await Team.create(
{
name: "Team",
authenticationProviders: [
{
name: "slack",
providerId: uuidv4(),
},
],
},
{
transaction,
include: "authenticationProviders",
}
);
const authenticationProvider = team.authenticationProviders[0];
const admin = await User.create(
{
email: "admin@example.com",
name: "Admin User",
teamId: team.id,
isAdmin: true,
createdAt: new Date("2018-01-01T00:00:00.000Z"),
authentications: [
{
authenticationProviderId: authenticationProvider.id,
providerId: uuidv4(),
},
],
},
{
transaction,
include: "authentications",
}
);
const user = await User.create(
{
id: "46fde1d4-0050-428f-9f0b-0bf77f4bdf61",
email: "user1@example.com",
name: "User 1",
teamId: team.id,
createdAt: new Date("2018-01-02T00:00:00.000Z"),
authentications: [
{
authenticationProviderId: authenticationProvider.id,
providerId: uuidv4(),
},
],
},
{
transaction,
include: "authentications",
}
);
const collection = await Collection.create(
{
name: "Collection",
urlId: "collection",
teamId: team.id,
createdById: user.id,
permission: CollectionPermission.ReadWrite,
},
{
transaction,
}
);
const document = await Document.create(
{
parentDocumentId: null,
collectionId: collection.id,
teamId: team.id,
userId: collection.createdById,
lastModifiedById: collection.createdById,
createdById: collection.createdById,
title: "First ever document",
text: "# Much test support",
},
{ transaction }
);
await document.publish(collection.createdById, collection.id, {
transaction,
});
await collection.reload({ transaction });
return {
user,
admin,
collection,
document,
team,
};
});
export function getTestServer() {
const app = webService();
onerror(app);
@@ -136,8 +41,9 @@ export function setupTestDatabase() {
await sequelize.close();
};
beforeAll(flush);
afterAll(disconnect);
beforeEach(flush);
}
/**
@@ -150,6 +56,19 @@ export function setCloudHosted() {
/**
* Set the environment to be self hosted
*/
export function setSelfHosted() {
return (env.URL = sharedEnv.URL = "https://wiki.example.com");
export async function setSelfHosted() {
env.URL = sharedEnv.URL = "https://wiki.example.com";
// Self hosted deployments only have one team, to ensure behavior is correct
// we need to delete all teams before running tests
return Team.destroy({
truncate: true,
});
}
export function findLatestEvent(where: WhereOptions<Event> = {}) {
return Event.findOne({
where,
order: [["createdAt", "DESC"]],
});
}