-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
405 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Project } from '$core/schema' | ||
import createCommitFn from '$core/services/commits/create' | ||
|
||
import { createProject, ICreateProject } from './projects' | ||
|
||
export type ICreateDraft = { | ||
project?: Project | ICreateProject | ||
} | ||
export async function createDraft(commitData: Partial<ICreateDraft> = {}) { | ||
let project = commitData.project ?? {} | ||
if (!('id' in project)) { | ||
const { project: newProject } = await createProject(project) | ||
project = newProject | ||
} | ||
|
||
const result = await createCommitFn({ projectId: (project as Project).id }) | ||
const commit = result.unwrap() | ||
|
||
return { commit } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './users' | ||
export * from './workspaces' | ||
export * from './projects' | ||
export * from './commits' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { faker } from '@faker-js/faker' | ||
import { database as db } from '$core/client' | ||
import { getUser } from '$core/data-access' | ||
import { Result, Transaction } from '$core/lib' | ||
import { Project, projects, Workspace, type SafeUser } from '$core/schema' | ||
|
||
import { createWorkspace, type ICreateWorkspace } from './workspaces' | ||
|
||
// TODO: Replace with actual service | ||
const createProjectFn = ({ | ||
name, | ||
workspaceId, | ||
}: { | ||
name: string | ||
workspaceId: number | ||
}) => { | ||
return Transaction.call<Project>(async (tx) => { | ||
const insertedProjects = await tx | ||
.insert(projects) | ||
.values({ name, workspaceId }) | ||
.returning() | ||
|
||
const newProject = insertedProjects[0]! | ||
|
||
return Result.ok(newProject) | ||
}, db) | ||
} | ||
|
||
export type ICreateProject = { | ||
name?: string | ||
workspace?: Workspace | ICreateWorkspace | ||
} | ||
export async function createProject(projectData: Partial<ICreateProject> = {}) { | ||
let workspaceData = projectData.workspace ?? {} | ||
let user: SafeUser | ||
let workspace: Workspace | ||
|
||
if ('id' in workspaceData) { | ||
user = (await getUser(workspaceData.creatorId!)) as SafeUser | ||
workspace = workspaceData as Workspace | ||
} else { | ||
const newWorkspace = await createWorkspace(workspaceData) | ||
workspace = newWorkspace.workspace | ||
user = newWorkspace.userData | ||
} | ||
|
||
const randomName = faker.commerce.department() | ||
const { name } = projectData | ||
|
||
const result = await createProjectFn({ | ||
name: name ?? randomName, | ||
workspaceId: workspace.id, | ||
}) | ||
const project = result.unwrap() | ||
|
||
return { project, user, workspace } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { faker } from '@faker-js/faker' | ||
import { createUser as createUserFn } from '$core/services/users/createUser' | ||
|
||
function makeRandomUserData() { | ||
return { | ||
name: faker.person.firstName(), | ||
email: faker.internet.email(), | ||
password: faker.internet.password(), | ||
} | ||
} | ||
export type ICreateUser = { | ||
name?: string | ||
email?: string | ||
password?: string | ||
} | ||
export async function createUser(userData: Partial<ICreateUser> = {}) { | ||
const randomUserData = makeRandomUserData() | ||
const data = { ...randomUserData, ...userData } | ||
|
||
const result = await createUserFn(data) | ||
return result.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { faker } from '@faker-js/faker' | ||
import { type SafeUser } from '$core/schema' | ||
import { createWorkspace as createWorkspaceFn } from '$core/services/workspaces/create' | ||
|
||
import { createUser, type ICreateUser } from './users' | ||
|
||
export type ICreateWorkspace = { | ||
name?: string | ||
creator?: SafeUser | ICreateUser | ||
} | ||
export async function createWorkspace( | ||
workspaceData: Partial<ICreateWorkspace> = {}, | ||
) { | ||
let userData = workspaceData.creator ?? {} | ||
if (!('id' in userData)) { | ||
userData = await createUser(userData) | ||
} | ||
|
||
const randomName = faker.company.name() | ||
const { name } = workspaceData | ||
const result = await createWorkspaceFn({ | ||
name: name ?? randomName, | ||
creatorId: userData.id, | ||
}) | ||
const workspace = result.unwrap() | ||
|
||
return { workspace, userData } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// vitest-env.d.ts | ||
import { beforeEach } from 'vitest' | ||
|
||
import * as factories from './factories' | ||
import useTestDatabase from './useTestDatabase' | ||
|
||
// This do rollback stragegy for each test. Faster than truncate. | ||
useTestDatabase() | ||
|
||
beforeEach((ctx) => { | ||
ctx.factories = factories | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
// TODO: Remove when adding an actual test suite | ||
describe('placeholder', () => { | ||
it('works', () => { | ||
expect(true).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
close, | ||
patchPgForTransactions, | ||
rollbackTransaction, | ||
startTransaction, | ||
unpatchPgForTransactions, | ||
} from 'pg-transactional-tests' | ||
import { afterAll, afterEach, beforeAll, beforeEach } from 'vitest' | ||
|
||
export default function useTestDatabase() { | ||
beforeAll(async () => { | ||
patchPgForTransactions() | ||
await startTransaction() | ||
}) | ||
beforeEach(startTransaction) | ||
afterEach(rollbackTransaction) | ||
afterAll(async () => { | ||
await rollbackTransaction() | ||
unpatchPgForTransactions() | ||
await close() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'vitest' | ||
|
||
import * as factories from './factories' | ||
|
||
declare module 'vitest' { | ||
export interface TestContext { | ||
factories: typeof factories | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/// <reference types="vitest" /> | ||
import { fileURLToPath } from 'url' | ||
import { dirname } from 'path' | ||
import { defineConfig } from 'vitest/config' | ||
import tsconfigPaths from 'vite-tsconfig-paths' | ||
|
||
const filename = fileURLToPath(import.meta.url) | ||
const root = dirname(filename) | ||
|
||
export default defineConfig({ | ||
plugins: [tsconfigPaths({ root })], | ||
test: { | ||
globals: true, | ||
testTimeout: 5000, | ||
environment: 'node', | ||
setupFiles: ['./src/tests/setup.ts'], | ||
include: ['./src/**/*.test.ts'], | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.