|
| 1 | +import { projectRoutes } from '@backend/api/project' |
| 2 | +import { closeDb, initializeDb } from '@backend/plugins/database' |
| 3 | +import { treaty } from '@elysiajs/eden' |
| 4 | +import { afterEach, beforeEach, describe, expect, test } from 'bun:test' |
| 5 | +import { Elysia } from 'elysia' |
| 6 | +import { tmpdir } from 'node:os' |
| 7 | + |
| 8 | +interface TestData { |
| 9 | + name: string |
| 10 | + email: string |
| 11 | + city: string |
| 12 | +} |
| 13 | + |
| 14 | +const TEST_DATA: TestData[] = [ |
| 15 | + { name: 'John Doe', email: 'john@example.com', city: 'New York' }, |
| 16 | + { name: 'Jane Smith', email: 'jane@example.com', city: 'Los Angeles' }, |
| 17 | + { name: 'Bob Johnson', email: 'bob@example.com', city: 'New York' }, |
| 18 | + { name: 'Alice Brown', email: 'alice@test.com', city: 'Chicago' }, |
| 19 | + { name: 'Charlie Davis', email: 'charlie@example.com', city: 'New York' }, |
| 20 | +] |
| 21 | + |
| 22 | +const createTestApi = () => { |
| 23 | + return treaty(new Elysia().use(projectRoutes)).api |
| 24 | +} |
| 25 | + |
| 26 | +const tempFilePath = tmpdir() + '/test-data.json' |
| 27 | + |
| 28 | +describe('Project API - Uppercase Conversion', () => { |
| 29 | + let api: ReturnType<typeof createTestApi> |
| 30 | + let projectId: string |
| 31 | + |
| 32 | + const importTestData = async () => { |
| 33 | + await Bun.write(tempFilePath, JSON.stringify(TEST_DATA)) |
| 34 | + |
| 35 | + const { status, error } = await api.project({ projectId }).import.post({ |
| 36 | + filePath: tempFilePath, |
| 37 | + }) |
| 38 | + |
| 39 | + expect(error).toBeNull() |
| 40 | + expect(status).toBe(201) |
| 41 | + } |
| 42 | + |
| 43 | + beforeEach(async () => { |
| 44 | + await initializeDb(':memory:') |
| 45 | + api = createTestApi() |
| 46 | + |
| 47 | + const { data, status, error } = await api.project.post({ |
| 48 | + name: 'Test Project for uppercase', |
| 49 | + }) |
| 50 | + expect(error).toBeNull() |
| 51 | + expect(status).toBe(201) |
| 52 | + projectId = (data as any)!.data!.id as string |
| 53 | + |
| 54 | + await importTestData() |
| 55 | + }) |
| 56 | + |
| 57 | + afterEach(async () => { |
| 58 | + await closeDb() |
| 59 | + }) |
| 60 | + |
| 61 | + test('should perform basic uppercase conversion', async () => { |
| 62 | + const { data, status, error } = await api.project({ projectId }).uppercase.post({ |
| 63 | + column: 'name', |
| 64 | + }) |
| 65 | + |
| 66 | + expect(status).toBe(200) |
| 67 | + expect(error).toBeNull() |
| 68 | + expect(data).toEqual({ |
| 69 | + affectedRows: 5, |
| 70 | + }) |
| 71 | + |
| 72 | + // Verify the data was actually changed |
| 73 | + const { data: projectData } = await api.project({ projectId }).get({ |
| 74 | + query: { offset: 0, limit: 25 }, |
| 75 | + }) |
| 76 | + |
| 77 | + expect(projectData).toHaveProperty( |
| 78 | + 'data', |
| 79 | + expect.arrayContaining([ |
| 80 | + expect.objectContaining({ name: 'JOHN DOE' }), |
| 81 | + expect.objectContaining({ name: 'JANE SMITH' }), |
| 82 | + expect.objectContaining({ name: 'BOB JOHNSON' }), |
| 83 | + expect.objectContaining({ name: 'ALICE BROWN' }), |
| 84 | + expect.objectContaining({ name: 'CHARLIE DAVIS' }), |
| 85 | + ]), |
| 86 | + ) |
| 87 | + }) |
| 88 | + |
| 89 | + test('should return 400 for non-existent column', async () => { |
| 90 | + const { data, status, error } = await api.project({ projectId }).uppercase.post({ |
| 91 | + column: 'nonexistent_column', |
| 92 | + }) |
| 93 | + |
| 94 | + expect(status).toBe(400) |
| 95 | + expect(data).toBeNull() |
| 96 | + expect(error).toHaveProperty('status', 400) |
| 97 | + expect(error).toHaveProperty('value', [ |
| 98 | + { |
| 99 | + code: 'VALIDATION', |
| 100 | + message: 'Column not found', |
| 101 | + details: [`Column 'nonexistent_column' does not exist in table 'project_${projectId}'`], |
| 102 | + }, |
| 103 | + ]) |
| 104 | + }) |
| 105 | + |
| 106 | + test('should return 422 for missing required fields', async () => { |
| 107 | + const { data, status, error } = await api.project({ projectId }).uppercase.post({ |
| 108 | + column: '', |
| 109 | + }) |
| 110 | + |
| 111 | + expect(status).toBe(422) |
| 112 | + expect(data).toBeNull() |
| 113 | + expect(error).toHaveProperty('status', 422) |
| 114 | + expect(error).toHaveProperty('value', expect.arrayContaining([ |
| 115 | + expect.objectContaining({ |
| 116 | + message: 'Expected string length greater or equal to 1', |
| 117 | + path: '/column', |
| 118 | + }), |
| 119 | + ])) |
| 120 | + }) |
| 121 | +}) |
0 commit comments