-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathdelete-database.test.ts
More file actions
86 lines (73 loc) · 1.94 KB
/
delete-database.test.ts
File metadata and controls
86 lines (73 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { prisma } = vi.hoisted(() => ({
prisma: {
database: {
findUnique: vi.fn(),
update: vi.fn(),
},
},
}))
vi.mock('@/lib/db', () => ({
prisma,
}))
import { deleteDatabaseCommand } from '@/lib/platform/control/commands/database/delete-database'
describe('deleteDatabaseCommand', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('returns an error when the database does not exist', async () => {
prisma.database.findUnique.mockResolvedValue(null)
const result = await deleteDatabaseCommand({
userId: 'user-1',
databaseId: 'database-1',
})
expect(result).toEqual({
success: false,
error: 'Database not found',
})
expect(prisma.database.update).not.toHaveBeenCalled()
})
it('returns an error when the database belongs to another user', async () => {
prisma.database.findUnique.mockResolvedValue({
id: 'database-1',
project: {
userId: 'other-user',
},
})
const result = await deleteDatabaseCommand({
userId: 'user-1',
databaseId: 'database-1',
})
expect(result).toEqual({
success: false,
error: 'Unauthorized',
})
expect(prisma.database.update).not.toHaveBeenCalled()
})
it('marks the database as terminating and clears the lock', async () => {
prisma.database.findUnique.mockResolvedValue({
id: 'database-1',
project: {
userId: 'user-1',
},
})
prisma.database.update.mockResolvedValue({
id: 'database-1',
})
const result = await deleteDatabaseCommand({
userId: 'user-1',
databaseId: 'database-1',
})
expect(result).toEqual({
success: true,
data: undefined,
})
expect(prisma.database.update).toHaveBeenCalledWith({
where: { id: 'database-1' },
data: {
status: 'TERMINATING',
lockedUntil: null,
},
})
})
})