feat: PostgreSQL withTenant-Helper und ClickHouse-Client
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
36
src/server/db/tenant.test.ts
Normal file
36
src/server/db/tenant.test.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
vi.mock('./client', () => ({
|
||||
db: {
|
||||
execute: vi.fn(),
|
||||
query: vi.fn().mockResolvedValue([{ id: '1' }]),
|
||||
},
|
||||
}))
|
||||
|
||||
import { withTenant } from './tenant'
|
||||
import { db } from './client'
|
||||
|
||||
describe('withTenant', () => {
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
it('setzt search_path auf tenant-Schema', async () => {
|
||||
await withTenant('abc123', () => db.query('SELECT 1', []))
|
||||
expect(db.execute).toHaveBeenCalledWith('SET search_path = tenant_abc123, public')
|
||||
})
|
||||
|
||||
it('setzt search_path zurück auf public nach Ausführung', async () => {
|
||||
await withTenant('abc123', () => db.query('SELECT 1', []))
|
||||
expect(db.execute).toHaveBeenLastCalledWith('SET search_path = public')
|
||||
})
|
||||
|
||||
it('setzt search_path zurück auch bei Fehler', async () => {
|
||||
const fn = vi.fn().mockRejectedValue(new Error('DB-Fehler'))
|
||||
await expect(withTenant('abc123', fn)).rejects.toThrow('DB-Fehler')
|
||||
expect(db.execute).toHaveBeenLastCalledWith('SET search_path = public')
|
||||
})
|
||||
|
||||
it('gibt Rückgabewert der Funktion zurück', async () => {
|
||||
const result = await withTenant('abc123', async () => 'testdata')
|
||||
expect(result).toBe('testdata')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user