ice/tests/unit/services/DatabaseService.test.ts
Claude Code cc5803ac63 Complete comprehensive test suite implementation
- Add integration tests for admin routes with authentication
- Add unit tests for DatabaseService with proper mocking
- Fix ProfanityFilterService tests to handle case variations
- Remove old JavaScript test files
- Add coverage reporting to gitignore
- All 123 tests passing with 76% overall coverage

Coverage achieved:
- Models: 69.5% statements
- Routes: 80.6% statements
- Services: 75% statements

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-05 21:43:09 -04:00

97 lines
No EOL
3.5 KiB
TypeScript

import DatabaseService from '../../../src/services/DatabaseService';
describe('DatabaseService', () => {
let databaseService: DatabaseService;
beforeEach(() => {
databaseService = new DatabaseService();
// Mock console methods to reduce test noise
jest.spyOn(console, 'log').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
try {
databaseService.close();
} catch (e) {
// Ignore close errors in tests
}
});
describe('initial state', () => {
it('should have null database connections initially', () => {
expect(databaseService.getMainDb()).toBeNull();
expect(databaseService.getProfanityDb()).toBeNull();
});
it('should throw error when accessing models before initialization', () => {
expect(() => databaseService.getLocationModel()).toThrow('Database not initialized. Call initialize() first.');
expect(() => databaseService.getProfanityWordModel()).toThrow('Database not initialized. Call initialize() first.');
});
});
describe('close method', () => {
it('should handle close when not initialized', () => {
expect(() => databaseService.close()).not.toThrow();
});
it('should be callable multiple times', () => {
expect(() => {
databaseService.close();
databaseService.close();
databaseService.close();
}).not.toThrow();
});
});
describe('type safety', () => {
it('should have correct method signatures', () => {
expect(typeof databaseService.initialize).toBe('function');
expect(typeof databaseService.initializeMainDatabase).toBe('function');
expect(typeof databaseService.initializeProfanityDatabase).toBe('function');
expect(typeof databaseService.getLocationModel).toBe('function');
expect(typeof databaseService.getProfanityWordModel).toBe('function');
expect(typeof databaseService.getMainDb).toBe('function');
expect(typeof databaseService.getProfanityDb).toBe('function');
expect(typeof databaseService.close).toBe('function');
});
it('should return correct types', () => {
expect(databaseService.getMainDb()).toBeNull();
expect(databaseService.getProfanityDb()).toBeNull();
});
});
describe('error handling', () => {
it('should handle method calls on uninitialized service', () => {
expect(() => databaseService.getLocationModel()).toThrow();
expect(() => databaseService.getProfanityWordModel()).toThrow();
});
it('should provide meaningful error messages', () => {
expect(() => databaseService.getLocationModel()).toThrow('Database not initialized. Call initialize() first.');
expect(() => databaseService.getProfanityWordModel()).toThrow('Database not initialized. Call initialize() first.');
});
});
describe('service instantiation', () => {
it('should create a new instance successfully', () => {
const newService = new DatabaseService();
expect(newService).toBeInstanceOf(DatabaseService);
expect(newService.getMainDb()).toBeNull();
expect(newService.getProfanityDb()).toBeNull();
});
it('should allow multiple instances', () => {
const service1 = new DatabaseService();
const service2 = new DatabaseService();
expect(service1).toBeInstanceOf(DatabaseService);
expect(service2).toBeInstanceOf(DatabaseService);
expect(service1).not.toBe(service2);
});
});
});