Files
loongyan/test/unit/domain/repositories/album_repository_test.dart
lzw-723 120278aa85
Some checks failed
Dart CI / build (push) Failing after 11s
Docker Build / docker (push) Successful in 9m6s
格式化代码
2026-04-04 13:36:58 +08:00

139 lines
4.3 KiB
Dart

import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import '../../../../bin/domain/entities/album.dart';
import '../../../../bin/domain/repositories/album_repository.dart';
void main() {
late Directory tempDir;
late AlbumRepository repository;
setUp(() async {
// Create a temporary directory for each test
tempDir = await Directory.systemTemp.createTemp('album_repo_test_');
repository = AlbumRepository(basePath: tempDir.path);
});
tearDown(() async {
// Clean up the temporary directory
if (await tempDir.exists()) {
await tempDir.delete(recursive: true);
}
});
group('getAllAlbums', () {
test('should return empty list when directory does not exist', () async {
final repo = AlbumRepository(
basePath: '/nonexistent/path/that/does/not/exist',
);
final albums = await repo.getAllAlbums();
expect(albums, isEmpty);
});
test('should return empty list when directory is empty', () async {
final albums = await repository.getAllAlbums();
expect(albums, isEmpty);
});
test('should return albums for each subdirectory', () async {
// Create test directories
await Directory(p.join(tempDir.path, 'vacation')).create();
await Directory(p.join(tempDir.path, 'birthday')).create();
await Directory(p.join(tempDir.path, 'family')).create();
final albums = await repository.getAllAlbums();
expect(albums.length, 3);
expect(
albums.map((a) => a.name),
containsAll(['vacation', 'birthday', 'family']),
);
});
test('should not include files as albums', () async {
// Create a directory and a file
await Directory(p.join(tempDir.path, 'vacation')).create();
await File(p.join(tempDir.path, 'somefile.txt')).writeAsString('test');
final albums = await repository.getAllAlbums();
expect(albums.length, 1);
expect(albums.first.name, 'vacation');
});
test('should generate consistent id from directory name', () async {
await Directory(p.join(tempDir.path, 'test_album')).create();
final albums1 = await repository.getAllAlbums();
final albums2 = await repository.getAllAlbums();
expect(albums1.first.id, albums2.first.id);
expect(albums1.first.id, 'test_album'.hashCode);
});
test('should set createdAt and updatedAt from directory stat', () async {
await Directory(p.join(tempDir.path, 'recent')).create();
final albums = await repository.getAllAlbums();
expect(albums.first.createdAt, isA<DateTime>());
expect(albums.first.updatedAt, isA<DateTime>());
});
test('should return Album objects', () async {
await Directory(p.join(tempDir.path, 'my_album')).create();
final albums = await repository.getAllAlbums();
expect(albums.first, isA<Album>());
expect(albums.first.id, isA<int>());
expect(albums.first.name, 'my_album');
});
});
group('getAlbumById', () {
test('should return null when directory does not exist', () async {
final repo = AlbumRepository(basePath: '/nonexistent/path');
final album = await repo.getAlbumById(123);
expect(album, isNull);
});
test('should return null when no albums match the id', () async {
await Directory(p.join(tempDir.path, 'album1')).create();
final album = await repository.getAlbumById(999999);
expect(album, isNull);
});
test('should return album when id matches', () async {
await Directory(p.join(tempDir.path, 'target_album')).create();
final allAlbums = await repository.getAllAlbums();
final targetId = allAlbums.first.id;
final album = await repository.getAlbumById(targetId);
expect(album, isNotNull);
expect(album!.name, 'target_album');
expect(album.id, targetId);
});
test('should return correct album among multiple albums', () async {
await Directory(p.join(tempDir.path, 'alpha')).create();
await Directory(p.join(tempDir.path, 'beta')).create();
await Directory(p.join(tempDir.path, 'gamma')).create();
final allAlbums = await repository.getAllAlbums();
final betaAlbum = allAlbums.firstWhere((a) => a.name == 'beta');
final album = await repository.getAlbumById(betaAlbum.id);
expect(album, isNotNull);
expect(album!.name, 'beta');
});
});
}