forked from wangziqi/gongxue-base
refactor(classes): preserve student membership history
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { getDataSourceToken } from '@nestjs/typeorm';
|
||||
import { DatabaseMigrationsService } from './database-migrations.service';
|
||||
|
||||
function createRunner(tableExists: boolean, columns: string[] = []) {
|
||||
return {
|
||||
connect: jest.fn(),
|
||||
release: jest.fn(),
|
||||
getTables: jest.fn().mockResolvedValue(tableExists ? [{ name: 'class_student' }] : []),
|
||||
getTable: jest.fn().mockResolvedValue({
|
||||
name: 'class_student',
|
||||
columns: columns.map((name) => ({ name })),
|
||||
}),
|
||||
dropColumn: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
}
|
||||
|
||||
async function createService(runner: ReturnType<typeof createRunner>) {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
DatabaseMigrationsService,
|
||||
{
|
||||
provide: getDataSourceToken(),
|
||||
useValue: {
|
||||
options: { type: 'better-sqlite3' },
|
||||
createQueryRunner: jest.fn().mockReturnValue(runner),
|
||||
},
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
return module.get(DatabaseMigrationsService) as DatabaseMigrationsService & {
|
||||
removeUnusedClassStudentColumns(): Promise<void>;
|
||||
};
|
||||
}
|
||||
|
||||
describe('DatabaseMigrationsService — class student cleanup', () => {
|
||||
it('drops the unused enrollment_id column', async () => {
|
||||
const runner = createRunner(true, ['id', 'enrollment_id']);
|
||||
const service = await createService(runner);
|
||||
|
||||
await service.removeUnusedClassStudentColumns();
|
||||
|
||||
expect(runner.dropColumn).toHaveBeenCalledWith('class_student', 'enrollment_id');
|
||||
expect(runner.release).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does nothing when the table is absent', async () => {
|
||||
const runner = createRunner(false);
|
||||
const service = await createService(runner);
|
||||
|
||||
await service.removeUnusedClassStudentColumns();
|
||||
|
||||
expect(runner.dropColumn).not.toHaveBeenCalled();
|
||||
expect(runner.release).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user