Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import type { TestingModule } from '@nestjs/testing'
import type { DeepMockProxy } from 'vitest-mock-extended'
import { Test } from '@nestjs/testing'
import { beforeEach, describe, expect, it } from 'vitest'
import { mockDeep } from 'vitest-mock-extended'
import { PrismaService } from '../infrastructure/database/prisma.service.js'
import { DeploymentDatastoreService } from './deployment-datastore.service.js'

describe('deploymentDatastoreService', () => {
let module: TestingModule
let service: DeploymentDatastoreService
let prisma: DeepMockProxy<PrismaService>

beforeEach(async () => {
prisma = mockDeep<PrismaService>()

module = await Test.createTestingModule({
providers: [
DeploymentDatastoreService,
{ provide: PrismaService, useValue: prisma },
],
}).compile()

service = module.get(DeploymentDatastoreService)
})

describe('getDeploymentById', () => {
it('should return a deployment with sources and repository', async () => {
const deployment = { id: 'dep1' }

prisma.deployment.findUnique.mockResolvedValue(deployment as any)

const result = await service.getDeploymentById('dep1')

expect(prisma.deployment.findUnique).toHaveBeenCalledWith({
where: { id: 'dep1' },
include: {
deploymentSources: {
include: { repository: true },
},
},
})
expect(result).toEqual(deployment)
})
})

describe('getDeploymentsByProjectId', () => {
it('should return deployments for a project', async () => {
const deployments = [{ id: 'dep1' }]

prisma.deployment.findMany.mockResolvedValue(deployments as any)

const result = await service.getDeploymentsByProjectId('project1')

expect(prisma.deployment.findMany).toHaveBeenCalledWith({
where: { projectId: 'project1' },
include: {
deploymentSources: {
include: { repository: true },
},
},
})
expect(result).toEqual(deployments)
})
})

describe('createDeployment', () => {
it('should create a deployment', async () => {
const data = { name: 'test' }
const created = { id: 'dep1', ...data }

prisma.deployment.create.mockResolvedValue(created as any)

const result = await service.createDeployment(data as any)

expect(prisma.deployment.create).toHaveBeenCalledWith({ data })
expect(result).toEqual(created)
})
})

describe('updateDeployment', () => {
it('should update a deployment', async () => {
const updated = { id: 'dep1', name: 'updated' }

prisma.deployment.update.mockResolvedValue(updated as any)

const result = await service.updateDeployment('dep1', {
name: 'updated',
} as any)

expect(prisma.deployment.update).toHaveBeenCalledWith({
where: { id: 'dep1' },
data: { name: 'updated' },
})
expect(result).toEqual(updated)
})
})

describe('deleteDeployment', () => {
it('should delete a deployment', async () => {
const deleted = { id: 'dep1' }

prisma.deployment.delete.mockResolvedValue(deleted as any)

const result = await service.deleteDeployment('dep1')

expect(prisma.deployment.delete).toHaveBeenCalledWith({
where: { id: 'dep1' },
})
expect(result).toEqual(deleted)
})
})

describe('deleteAllDeploymentsByProjectId', () => {
it('should delete all deployments for a project', async () => {
const response = { count: 3 }

prisma.deployment.deleteMany.mockResolvedValue(response)

const result = await service.deleteAllDeploymentsByProjectId('project1')

expect(prisma.deployment.deleteMany).toHaveBeenCalledWith({
where: { projectId: 'project1' },
})
expect(result).toEqual(response)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { Prisma } from '@prisma/client'
import { Inject, Injectable } from '@nestjs/common'
import { PrismaService } from '../infrastructure/database/prisma.service.js'

@Injectable()
export class DeploymentDatastoreService {
constructor(@Inject(PrismaService) private readonly prisma: PrismaService) {}

getDeploymentById(deploymentId: string) {
return this.prisma.deployment.findUnique({
where: { id: deploymentId },
include: {
deploymentSources: {
include: { repository: true },
},
},
})
}

getDeploymentsByProjectId(projectId: string) {
return this.prisma.deployment.findMany({
where: { projectId },
include: {
deploymentSources: {
include: { repository: true },
},
},
})
}

createDeployment(data: Prisma.DeploymentCreateInput) {
return this.prisma.deployment.create({ data })
}

updateDeployment(deploymentId: string, data: Prisma.DeploymentUpdateInput) {
return this.prisma.deployment.update({
where: { id: deploymentId },
data,
})
}

deleteDeployment(deploymentId: string) {
return this.prisma.deployment.delete({
where: { id: deploymentId },
})
}

deleteAllDeploymentsByProjectId(projectId: string) {
return this.prisma.deployment.deleteMany({
where: { projectId },
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { TestingModule } from '@nestjs/testing'
import { Test } from '@nestjs/testing'
import { beforeEach, describe, expect, it } from 'vitest'
import { DeploymentController } from './deployment.controller.js'

describe('deploymentController', () => {
let module: TestingModule

beforeEach(async () => {
module = await Test.createTestingModule({
controllers: [
DeploymentController,
],
}).compile()
})

it('should be defined', () => {
expect(module).toBeDefined()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Controller } from '@nestjs/common'

@Controller('api/v1/deployments')
export class DeploymentController {
}
14 changes: 14 additions & 0 deletions apps/server-nestjs/src/cpin-module/deployment/deployment.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common'
import { DeploymentDatastoreService } from './deployment-datastore.service.js'
import { DeploymentController } from './deployment.controller.js'
import { DeploymentService } from './deployment.service.js'

@Module({
imports: [],
controllers: [DeploymentController],
providers: [
DeploymentDatastoreService,
DeploymentService,
],
})
export class DeploymentModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { TestingModule } from '@nestjs/testing'
import { Test } from '@nestjs/testing'
import { beforeEach, describe, expect, it } from 'vitest'
import { DeploymentService } from './deployment.service.js'

describe('deploymentService', () => {
let module: TestingModule

beforeEach(async () => {
module = await Test.createTestingModule({
providers: [
DeploymentService,
],
}).compile()
})

it('should be defined', () => {
expect(module).toBeDefined()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Injectable } from '@nestjs/common'

@Injectable()
export class DeploymentService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-- CreateEnum
CREATE TYPE "DeploymentSourceType" AS ENUM ('git', 'oci');

-- CreateTable
CREATE TABLE "Deployment" (
"id" UUID NOT NULL,
"projectId" UUID NOT NULL,
"name" VARCHAR(11) NOT NULL,
"memory" REAL NOT NULL,
"cpu" REAL NOT NULL,
"gpu" REAL NOT NULL,
"autosync" BOOLEAN NOT NULL DEFAULT true,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"environmentId" UUID NOT NULL,

CONSTRAINT "Deployment_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "DeploymentSource" (
"id" UUID NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"deploymentId" UUID NOT NULL,
"repositoryId" UUID NOT NULL,
"type" "DeploymentSourceType" NOT NULL,
"targetRevision" TEXT NOT NULL DEFAULT '',
"path" TEXT NOT NULL DEFAULT '',

CONSTRAINT "DeploymentSource_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "Deployment" ADD CONSTRAINT "Deployment_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Deployment" ADD CONSTRAINT "Deployment_environmentId_fkey" FOREIGN KEY ("environmentId") REFERENCES "Environment"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "DeploymentSource" ADD CONSTRAINT "DeploymentSource_repositoryId_fkey" FOREIGN KEY ("repositoryId") REFERENCES "Repository"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "DeploymentSource" ADD CONSTRAINT "DeploymentSource_deploymentId_fkey" FOREIGN KEY ("deploymentId") REFERENCES "Deployment"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Loading
Loading