Refactoring

This commit is contained in:
DahoudG
2025-09-17 17:54:06 +00:00
parent 12d514d866
commit 63fe107f98
165 changed files with 54220 additions and 276 deletions

View File

@@ -0,0 +1,42 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:unionflow_mobile_apps/features/solidarite/domain/entities/demande_aide.dart';
void main() {
group('DemandeAide Entity', () {
test('doit créer une instance simple', () {
final demande = DemandeAide(
id: 'test-id',
numeroReference: 'REF-001',
titre: 'Test',
description: 'Description test',
typeAide: TypeAide.aideFinanciereUrgente,
statut: StatutAide.brouillon,
priorite: PrioriteAide.normale,
demandeurId: 'user-1',
nomDemandeur: 'Test User',
organisationId: 'org-1',
dateCreation: DateTime.now(),
dateModification: DateTime.now(),
);
expect(demande.id, 'test-id');
expect(demande.titre, 'Test');
expect(demande.typeAide, TypeAide.aideFinanciereUrgente);
expect(demande.statut, StatutAide.brouillon);
});
test('doit tester les enums de base', () {
// Test TypeAide
expect(TypeAide.values.isNotEmpty, true);
expect(TypeAide.aideFinanciereUrgente.toString(), contains('aideFinanciereUrgente'));
// Test StatutAide
expect(StatutAide.values.isNotEmpty, true);
expect(StatutAide.brouillon.toString(), contains('brouillon'));
// Test PrioriteAide
expect(PrioriteAide.values.isNotEmpty, true);
expect(PrioriteAide.normale.toString(), contains('normale'));
});
});
}

View File

@@ -0,0 +1,356 @@
import 'package:dartz/dartz.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:unionflow_mobile_apps/core/error/failures.dart';
import 'package:unionflow_mobile_apps/features/solidarite/domain/entities/demande_aide.dart';
import 'package:unionflow_mobile_apps/features/solidarite/domain/repositories/solidarite_repository.dart';
import 'package:unionflow_mobile_apps/features/solidarite/domain/usecases/gerer_demandes_aide_usecase.dart';
import 'creer_demande_aide_usecase_test.mocks.dart';
@GenerateMocks([SolidariteRepository])
void main() {
group('CreerDemandeAideUseCase', () {
late CreerDemandeAideUseCase usecase;
late MockSolidariteRepository mockRepository;
setUp(() {
mockRepository = MockSolidariteRepository();
usecase = CreerDemandeAideUseCase(mockRepository);
});
final tDemande = DemandeAide(
id: '',
numeroReference: '',
titre: 'Aide médicale urgente',
description: 'Besoin d\'aide pour frais médicaux',
typeAide: TypeAide.aideFinanciereMedicale,
statut: StatutAide.brouillon,
priorite: PrioriteAide.haute,
estUrgente: true,
montantDemande: 500000.0,
dateCreation: DateTime.now(),
dateModification: DateTime.now(),
organisationId: 'org-1',
demandeurId: 'user-1',
nomDemandeur: 'Marie Kouassi',
emailDemandeur: 'marie@example.com',
telephoneDemandeur: '+225123456789',
beneficiaires: const [],
evaluations: const [],
commentairesInternes: const [],
historiqueStatuts: const [],
piecesJustificatives: const [],
tags: const [],
metadonnees: const {},
);
final tDemandeCreee = tDemande.copyWith(
id: 'demande-123',
numeroReference: 'REF-2024-001',
);
test('doit créer une demande d\'aide avec succès', () async {
// arrange
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => Right(tDemandeCreee));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemande));
// assert
expect(result, Right(tDemandeCreee));
verify(mockRepository.creerDemandeAide(tDemande));
verifyNoMoreInteractions(mockRepository);
});
test('doit retourner ValidationFailure quand les données sont invalides', () async {
// arrange
final tDemandeInvalide = tDemande.copyWith(titre: '');
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => const Left(ValidationFailure('Le titre est requis')));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemandeInvalide));
// assert
expect(result, const Left(ValidationFailure('Le titre est requis')));
verify(mockRepository.creerDemandeAide(tDemandeInvalide));
verifyNoMoreInteractions(mockRepository);
});
test('doit retourner ServerFailure quand le serveur échoue', () async {
// arrange
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => const Left(ServerFailure('Erreur serveur')));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemande));
// assert
expect(result, const Left(ServerFailure('Erreur serveur')));
verify(mockRepository.creerDemandeAide(tDemande));
verifyNoMoreInteractions(mockRepository);
});
test('doit retourner NetworkFailure quand il n\'y a pas de connexion', () async {
// arrange
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => const Left(NetworkFailure('Pas de connexion internet')));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemande));
// assert
expect(result, const Left(NetworkFailure('Pas de connexion internet')));
verify(mockRepository.creerDemandeAide(tDemande));
verifyNoMoreInteractions(mockRepository);
});
group('validation des paramètres', () {
test('doit valider que le titre n\'est pas vide', () async {
// arrange
final tDemandeInvalide = tDemande.copyWith(titre: '');
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => const Left(ValidationFailure('Le titre est requis')));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemandeInvalide));
// assert
expect(result.isLeft(), true);
result.fold(
(failure) => expect(failure, isA<ValidationFailure>()),
(success) => fail('Devrait échouer avec ValidationFailure'),
);
});
test('doit valider que la description n\'est pas vide', () async {
// arrange
final tDemandeInvalide = tDemande.copyWith(description: '');
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => const Left(ValidationFailure('La description est requise')));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemandeInvalide));
// assert
expect(result.isLeft(), true);
result.fold(
(failure) => expect(failure, isA<ValidationFailure>()),
(success) => fail('Devrait échouer avec ValidationFailure'),
);
});
test('doit valider que le montant est positif', () async {
// arrange
final tDemandeInvalide = tDemande.copyWith(montantDemande: -100.0);
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => const Left(ValidationFailure('Le montant doit être positif')));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemandeInvalide));
// assert
expect(result.isLeft(), true);
result.fold(
(failure) => expect(failure, isA<ValidationFailure>()),
(success) => fail('Devrait échouer avec ValidationFailure'),
);
});
test('doit valider que l\'email du demandeur est valide', () async {
// arrange
final tDemandeInvalide = tDemande.copyWith(emailDemandeur: 'email-invalide');
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => const Left(ValidationFailure('Email invalide')));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemandeInvalide));
// assert
expect(result.isLeft(), true);
result.fold(
(failure) => expect(failure, isA<ValidationFailure>()),
(success) => fail('Devrait échouer avec ValidationFailure'),
);
});
test('doit valider que le téléphone du demandeur est valide', () async {
// arrange
final tDemandeInvalide = tDemande.copyWith(telephoneDemandeur: '123');
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => const Left(ValidationFailure('Numéro de téléphone invalide')));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemandeInvalide));
// assert
expect(result.isLeft(), true);
result.fold(
(failure) => expect(failure, isA<ValidationFailure>()),
(success) => fail('Devrait échouer avec ValidationFailure'),
);
});
});
group('gestion des cas limites', () {
test('doit gérer une demande avec montant null', () async {
// arrange
final tDemandeSansMontant = tDemande.copyWith(montantDemande: null);
final tDemandeCreeSansMontant = tDemandeSansMontant.copyWith(
id: 'demande-123',
numeroReference: 'REF-2024-001',
);
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => Right(tDemandeCreeSansMontant));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemandeSansMontant));
// assert
expect(result, Right(tDemandeCreeSansMontant));
verify(mockRepository.creerDemandeAide(tDemandeSansMontant));
});
test('doit gérer une demande avec justification null', () async {
// arrange
final tDemandeSansJustification = tDemande.copyWith(justification: null);
final tDemandeCreeSansJustification = tDemandeSansJustification.copyWith(
id: 'demande-123',
numeroReference: 'REF-2024-001',
);
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => Right(tDemandeCreeSansJustification));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemandeSansJustification));
// assert
expect(result, Right(tDemandeCreeSansJustification));
verify(mockRepository.creerDemandeAide(tDemandeSansJustification));
});
test('doit gérer une demande avec bénéficiaires multiples', () async {
// arrange
final tBeneficiaires = [
const BeneficiaireAide(
prenom: 'Jean',
nom: 'Kouassi',
age: 25,
),
const BeneficiaireAide(
prenom: 'Marie',
nom: 'Kouassi',
age: 23,
),
];
final tDemandeAvecBeneficiaires = tDemande.copyWith(beneficiaires: tBeneficiaires);
final tDemandeCreeeAvecBeneficiaires = tDemandeAvecBeneficiaires.copyWith(
id: 'demande-123',
numeroReference: 'REF-2024-001',
);
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => Right(tDemandeCreeeAvecBeneficiaires));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemandeAvecBeneficiaires));
// assert
expect(result, Right(tDemandeCreeeAvecBeneficiaires));
verify(mockRepository.creerDemandeAide(tDemandeAvecBeneficiaires));
});
test('doit gérer une demande avec contact d\'urgence', () async {
// arrange
const tContactUrgence = ContactUrgence(
prenom: 'Paul',
nom: 'Kouassi',
telephone: '+225987654321',
email: 'paul@example.com',
relation: 'Frère',
);
final tDemandeAvecContact = tDemande.copyWith(contactUrgence: tContactUrgence);
final tDemandeCreeeAvecContact = tDemandeAvecContact.copyWith(
id: 'demande-123',
numeroReference: 'REF-2024-001',
);
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => Right(tDemandeCreeeAvecContact));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemandeAvecContact));
// assert
expect(result, Right(tDemandeCreeeAvecContact));
verify(mockRepository.creerDemandeAide(tDemandeAvecContact));
});
test('doit gérer une demande avec localisation', () async {
// arrange
const tLocalisation = Localisation(
adresse: '123 Rue de la Paix',
ville: 'Abidjan',
codePostal: '00225',
pays: 'Côte d\'Ivoire',
latitude: 5.3600,
longitude: -4.0083,
);
final tDemandeAvecLocalisation = tDemande.copyWith(localisation: tLocalisation);
final tDemandeCreeeAvecLocalisation = tDemandeAvecLocalisation.copyWith(
id: 'demande-123',
numeroReference: 'REF-2024-001',
);
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => Right(tDemandeCreeeAvecLocalisation));
// act
final result = await usecase(CreerDemandeAideParams(demande: tDemandeAvecLocalisation));
// assert
expect(result, Right(tDemandeCreeeAvecLocalisation));
verify(mockRepository.creerDemandeAide(tDemandeAvecLocalisation));
});
});
group('performance et concurrence', () {
test('doit gérer les appels concurrents', () async {
// arrange
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async => Right(tDemandeCreee));
// act
final futures = List.generate(5, (index) {
final demande = tDemande.copyWith(titre: 'Demande $index');
return usecase(CreerDemandeAideParams(demande: demande));
});
final results = await Future.wait(futures);
// assert
expect(results.length, 5);
for (final result in results) {
expect(result.isRight(), true);
}
verify(mockRepository.creerDemandeAide(any)).called(5);
});
test('doit gérer les timeouts', () async {
// arrange
when(mockRepository.creerDemandeAide(any))
.thenAnswer((_) async {
await Future.delayed(const Duration(seconds: 10));
return Right(tDemandeCreee);
});
// act & assert
expect(
() => usecase(CreerDemandeAideParams(demande: tDemande))
.timeout(const Duration(seconds: 5)),
throwsA(isA<TimeoutException>()),
);
});
});
});
}