|
| 1 | +import mongoose from 'mongoose'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { EmployeeModel, historySchema, ProjectModel } from './FinAppSchemas'; |
| 5 | + |
| 6 | +// Helper to validate a model instance without saving to DB |
| 7 | +async function validateDoc( |
| 8 | + doc: mongoose.Document, |
| 9 | +): Promise<mongoose.Error.ValidationError | null> { |
| 10 | + try { |
| 11 | + await doc.validate(); |
| 12 | + |
| 13 | + return null; |
| 14 | + } catch (err) { |
| 15 | + if (err instanceof mongoose.Error.ValidationError) { |
| 16 | + return err; |
| 17 | + } |
| 18 | + throw err; // rethrow unexpected errors |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +describe('FinApp Schemas', () => { |
| 23 | + describe('EmployeeModel', () => { |
| 24 | + it('should require redmine_id', async () => { |
| 25 | + const doc = new EmployeeModel({}); |
| 26 | + const err = await validateDoc(doc); |
| 27 | + |
| 28 | + expect(err).toBeTruthy(); |
| 29 | + expect(err?.errors.redmine_id).toBeDefined(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should accept valid employee', async () => { |
| 33 | + const doc = new EmployeeModel({ |
| 34 | + redmine_id: 123, |
| 35 | + history: { rate: { '2024-01-01': 100 } }, |
| 36 | + }); |
| 37 | + const err = await validateDoc(doc); |
| 38 | + |
| 39 | + expect(err).toBeNull(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should reject non-number redmine_id', async () => { |
| 43 | + const doc = new EmployeeModel({ redmine_id: 'abc' }); |
| 44 | + const err = await validateDoc(doc); |
| 45 | + |
| 46 | + expect(err).toBeTruthy(); |
| 47 | + expect(err?.errors.redmine_id).toBeDefined(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('ProjectModel', () => { |
| 52 | + it('should require redmine_id', async () => { |
| 53 | + const doc = new ProjectModel({}); |
| 54 | + const err = await validateDoc(doc); |
| 55 | + |
| 56 | + expect(err).toBeTruthy(); |
| 57 | + expect(err?.errors.redmine_id).toBeDefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should accept valid project', async () => { |
| 61 | + const doc = new ProjectModel({ |
| 62 | + redmine_id: 456, |
| 63 | + quick_books_id: 789, |
| 64 | + history: { rate: { '2024-01-01': 200 } }, |
| 65 | + }); |
| 66 | + const err = await validateDoc(doc); |
| 67 | + |
| 68 | + expect(err).toBeNull(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should reject non-number redmine_id', async () => { |
| 72 | + const doc = new ProjectModel({ redmine_id: 'xyz' }); |
| 73 | + const err = await validateDoc(doc); |
| 74 | + |
| 75 | + expect(err).toBeTruthy(); |
| 76 | + expect(err?.errors.redmine_id).toBeDefined(); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('historySchema', () => { |
| 81 | + it('should accept a valid history object', async () => { |
| 82 | + const TestModel = mongoose.model( |
| 83 | + 'TestHistory', |
| 84 | + new mongoose.Schema({ history: historySchema }), |
| 85 | + ); |
| 86 | + const doc = new TestModel({ history: { rate: { '2024-01-01': 123 } } }); |
| 87 | + const err = await validateDoc(doc); |
| 88 | + |
| 89 | + expect(err).toBeNull(); |
| 90 | + mongoose.deleteModel('TestHistory'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should reject non-number rate values', async () => { |
| 94 | + const TestModel = mongoose.model( |
| 95 | + 'TestHistory2', |
| 96 | + new mongoose.Schema({ history: historySchema }), |
| 97 | + ); |
| 98 | + const doc = new TestModel({ |
| 99 | + history: { rate: { '2024-01-01': 'not-a-number' } }, |
| 100 | + }); |
| 101 | + const err = await validateDoc(doc); |
| 102 | + |
| 103 | + expect(err).toBeTruthy(); |
| 104 | + mongoose.deleteModel('TestHistory2'); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments