|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 2 | +import prisma, { connectDB, disconnectDB } from '../src/db/client.js'; |
| 3 | + |
| 4 | +// Skip if no DATABASE_URL is set |
| 5 | +const hasDb = !!process.env.DATABASE_URL; |
| 6 | + |
| 7 | +describe.runIf(hasDb)('Database Integration: Prisma + Postgres', () => { |
| 8 | + beforeAll(async () => { |
| 9 | + await connectDB(); |
| 10 | + // In a real integration test, we might run migrations here or use a shadow DB |
| 11 | + }); |
| 12 | + |
| 13 | + afterAll(async () => { |
| 14 | + await disconnectDB(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should create and retrieve a user', async () => { |
| 18 | + const publicKey = 'G' + Math.random().toString(36).substring(2, 12).toUpperCase(); |
| 19 | + |
| 20 | + const user = await prisma.user.create({ |
| 21 | + data: { publicKey } |
| 22 | + }); |
| 23 | + |
| 24 | + expect(user).toHaveProperty('id'); |
| 25 | + expect(user.publicKey).toBe(publicKey); |
| 26 | + |
| 27 | + const retrieved = await prisma.user.findUnique({ |
| 28 | + where: { id: user.id } |
| 29 | + }); |
| 30 | + expect(retrieved.publicKey).toBe(publicKey); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should manage user settings with cascade delete', async () => { |
| 34 | + const user = await prisma.user.create({ |
| 35 | + data: { |
| 36 | + publicKey: 'G' + Math.random().toString(36).substring(2, 12).toUpperCase(), |
| 37 | + settings: { |
| 38 | + create: { defaultAsset: 'USDC' } |
| 39 | + } |
| 40 | + }, |
| 41 | + include: { settings: true } |
| 42 | + }); |
| 43 | + |
| 44 | + expect(user.settings.defaultAsset).toBe('USDC'); |
| 45 | + |
| 46 | + // Delete user and verify settings are gone |
| 47 | + await prisma.user.delete({ where: { id: user.id } }); |
| 48 | + |
| 49 | + const settings = await prisma.setting.findUnique({ |
| 50 | + where: { userId: user.id } |
| 51 | + }); |
| 52 | + expect(settings).toBeNull(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should record a transaction between two users', async () => { |
| 56 | + const sender = await prisma.user.create({ data: { publicKey: 'GSENDER' + Math.random().toString(36).substring(2, 12).toUpperCase() } }); |
| 57 | + const recipient = await prisma.user.create({ data: { publicKey: 'GRECIPIENT' + Math.random().toString(36).substring(2, 12).toUpperCase() } }); |
| 58 | + |
| 59 | + const txHash = 'hash' + Math.random().toString(36).substring(2, 12); |
| 60 | + |
| 61 | + const tx = await prisma.transaction.create({ |
| 62 | + data: { |
| 63 | + hash: txHash, |
| 64 | + amount: 100.50, |
| 65 | + senderId: sender.id, |
| 66 | + recipientId: recipient.id, |
| 67 | + successful: true |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + expect(tx.senderId).toBe(sender.id); |
| 72 | + expect(tx.recipientId).toBe(recipient.id); |
| 73 | + expect(tx.amount.toString()).toBe('100.5'); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +if (!hasDb) { |
| 78 | + describe('Database Integration: Prisma + Postgres', () => { |
| 79 | + it.skip('Skipped: DATABASE_URL not set', () => {}); |
| 80 | + }); |
| 81 | +} |
0 commit comments