jest.mock('@nestjs/swagger', () => ({ ApiProperty: () => () => {}, ApiPropertyOptional: () => () => {}, })); jest.mock('sequelize-typescript', () => ({ Table: () => () => {}, Column: () => () => {}, Model: class Model { static findByPk = jest.fn(); static findOne = jest.fn(); static findAll = jest.fn(); static findAndCountAll = jest.fn(); static create = jest.fn(); get = jest.fn(); update = jest.fn(); destroy = jest.fn(); }, DataType: { STRING: 'STRING', TEXT: 'TEXT', DATE: 'DATE', BOOLEAN: 'BOOLEAN', ENUM: () => 'ENUM', }, PrimaryKey: () => () => {}, ForeignKey: () => () => {}, BelongsTo: () => () => {}, HasMany: () => () => {}, CreatedAt: () => () => {}, UpdatedAt: () => () => {}, Default: () => () => {}, AllowNull: () => () => {}, })); jest.mock('sequelize', () => ({ Op: { substring: Symbol('Op.substring') }, DataTypes: {}, })); jest.mock('@tomei/general', () => ({ RepositoryBase: class RepositoryBase { constructor(_model: any) {} }, IRepositoryBase: {}, })); import { MediasRepository } from '../medias.repository'; import { MediasModel } from '../entities/medias.entity'; const mockModel = { MediaId: 'media-001', get: jest.fn().mockReturnValue({}), update: jest.fn(), destroy: jest.fn(), }; describe('MediasRepository', () => { let repo: MediasRepository; beforeEach(() => { jest.clearAllMocks(); repo = new MediasRepository(); }); describe('findByPk', () => { it('returns a MediasModel when found', async () => { (MediasModel.findOne as jest.Mock).mockResolvedValue(mockModel); const result = await repo.findByPk('media-001', undefined); expect(result).toBe(mockModel); expect(MediasModel.findOne).toHaveBeenCalledWith({ where: { MediaId: 'media-001' }, transaction: undefined, }); }); it('passes the transaction option', async () => { const tx = { id: 'tx-1' }; (MediasModel.findOne as jest.Mock).mockResolvedValue(mockModel); await repo.findByPk('media-001', tx); expect(MediasModel.findOne).toHaveBeenCalledWith({ where: { MediaId: 'media-001' }, transaction: tx, }); }); it('returns null when record is not found', async () => { (MediasModel.findOne as jest.Mock).mockResolvedValue(null); const result = await repo.findByPk('missing-id', undefined); expect(result).toBeNull(); }); it('throws when the database call fails', async () => { (MediasModel.findOne as jest.Mock).mockRejectedValue( new Error('DB error'), ); await expect(repo.findByPk('media-001', undefined)).rejects.toThrow( 'DB error', ); }); }); describe('delete', () => { it('destroys the record by MediaId', async () => { (MediasModel as any).destroy = jest.fn().mockResolvedValue(1); await repo.delete('media-001'); expect((MediasModel as any).destroy).toHaveBeenCalledWith( expect.objectContaining({ where: expect.objectContaining({ MediaId: 'media-001' }), }), ); }); it('passes the transaction option', async () => { const tx = { id: 'tx-2' }; (MediasModel as any).destroy = jest.fn().mockResolvedValue(1); await repo.delete('media-001', tx); expect((MediasModel as any).destroy).toHaveBeenCalledWith( expect.objectContaining({ transaction: tx }), ); }); it('throws when the database call fails', async () => { (MediasModel as any).destroy = jest .fn() .mockRejectedValue(new Error('Delete failed')); await expect(repo.delete('media-001')).rejects.toThrow('Delete failed'); }); }); describe('findAndCountAll', () => { it('returns count and rows with options', async () => { const mockResult = { count: 2, rows: [mockModel, mockModel] }; (MediasModel.findAndCountAll as jest.Mock).mockResolvedValue(mockResult); const result = await repo.findAndCountAll({ limit: 10, offset: 0 }); expect(result).toEqual(mockResult); expect(MediasModel.findAndCountAll).toHaveBeenCalledWith({ limit: 10, offset: 0, }); }); it('returns all records when no options are given', async () => { const mockResult = { count: 1, rows: [mockModel] }; (MediasModel.findAndCountAll as jest.Mock).mockResolvedValue(mockResult); const result = await repo.findAndCountAll(undefined); expect(result).toEqual(mockResult); expect(MediasModel.findAndCountAll).toHaveBeenCalledWith(); }); it('throws when the database call fails', async () => { (MediasModel.findAndCountAll as jest.Mock).mockRejectedValue( new Error('Query failed'), ); await expect(repo.findAndCountAll({ limit: 5 })).rejects.toThrow(); }); }); });