diff --git a/include/database/transaction.h b/include/database/transaction.h index 8b46b02..716b319 100644 --- a/include/database/transaction.h +++ b/include/database/transaction.h @@ -10,6 +10,20 @@ #include +/** + * @brief Indique si une transaction est actuellement active. + * + * Cette fonction ne modifie ni la connexion ni son erreur courante. + * + * @param database Connexion à inspecter. + * + * @return true lorsqu’une transaction est active, sinon false. + * Une connexion NULL retourne false. + */ +bool database_transaction_is_active( + const Database *database +); + /** * @brief Démarre une transaction d'écriture immédiate. * diff --git a/labfy-investigation b/labfy-investigation index 5fe153d..52c223d 100755 Binary files a/labfy-investigation and b/labfy-investigation differ diff --git a/src/database/transaction.c b/src/database/transaction.c index 33d2c6f..ac1516e 100644 --- a/src/database/transaction.c +++ b/src/database/transaction.c @@ -88,6 +88,20 @@ static bool database_transaction_execute( return true; } +bool database_transaction_is_active( + const Database *database +) +{ + if (database == NULL) + { + return false; + } + + return database_get_transaction_active( + database + ); +} + bool database_transaction_begin( Database *database ) diff --git a/tests/test_database b/tests/test_database index a93ca32..7599e29 100755 Binary files a/tests/test_database and b/tests/test_database differ diff --git a/tests/test_error b/tests/test_error index 674f2f5..5ad3581 100755 Binary files a/tests/test_error and b/tests/test_error differ diff --git a/tests/test_evidence_dao b/tests/test_evidence_dao index 33d02a4..b90e043 100755 Binary files a/tests/test_evidence_dao and b/tests/test_evidence_dao differ diff --git a/tests/test_investigation_dao b/tests/test_investigation_dao index 368bb9c..0559b8e 100755 Binary files a/tests/test_investigation_dao and b/tests/test_investigation_dao differ diff --git a/tests/test_investigation_project b/tests/test_investigation_project index a5ef20f..e10c1b2 100755 Binary files a/tests/test_investigation_project and b/tests/test_investigation_project differ diff --git a/tests/test_investigation_session b/tests/test_investigation_session index a1e875f..4d68d86 100755 Binary files a/tests/test_investigation_session and b/tests/test_investigation_session differ diff --git a/tests/test_statement b/tests/test_statement index 5d67d15..4bc6b75 100755 Binary files a/tests/test_statement and b/tests/test_statement differ diff --git a/tests/test_transaction b/tests/test_transaction index d434f54..dac2078 100755 Binary files a/tests/test_transaction and b/tests/test_transaction differ diff --git a/tests/test_transaction.c b/tests/test_transaction.c index f3149f2..a00d2ec 100644 --- a/tests/test_transaction.c +++ b/tests/test_transaction.c @@ -10,6 +10,7 @@ #include #include +#include /** * @brief Crée la table utilisée par les tests. @@ -306,12 +307,158 @@ static void test_transaction_error_state(void) database_close(database); } +/** + * @brief Vérifie l’exposition publique de l’état de transaction. + */ +static void test_transaction_active_state(void) +{ + Database *database = NULL; + + const char *error_message = NULL; + + database = + database_open( + ":memory:" + ); + + assert(database != NULL); + + /* + * Une connexion nouvelle ne possède aucune transaction active. + */ + assert( + !database_transaction_is_active( + database + ) + ); + + /* + * Une connexion NULL est considérée comme inactive. + */ + assert( + !database_transaction_is_active( + NULL + ) + ); + + /* + * La consultation de l’état ne doit pas modifier l’erreur courante. + */ + database_error_set( + database, + DATABASE_ERROR_INVALID_ARGUMENT, + "erreur témoin" + ); + + assert( + !database_transaction_is_active( + database + ) + ); + + assert( + database_error_get_code( + database + ) == + DATABASE_ERROR_INVALID_ARGUMENT + ); + + error_message = + database_error_get_message( + database + ); + + assert(error_message != NULL); + + assert( + strcmp( + error_message, + "erreur témoin" + ) == 0 + ); + + database_error_clear( + database + ); + + /* + * L’état devient actif après BEGIN. + */ + assert( + database_transaction_begin( + database + ) + ); + + assert( + database_transaction_is_active( + database + ) + ); + + /* + * Un simple contrôle ne doit pas terminer la transaction. + */ + assert( + database_transaction_is_active( + database + ) + ); + + /* + * L’état redevient inactif après COMMIT. + */ + assert( + database_transaction_commit( + database + ) + ); + + assert( + !database_transaction_is_active( + database + ) + ); + + /* + * Même vérification avec un ROLLBACK. + */ + assert( + database_transaction_begin( + database + ) + ); + + assert( + database_transaction_is_active( + database + ) + ); + + assert( + database_transaction_rollback( + database + ) + ); + + assert( + !database_transaction_is_active( + database + ) + ); + + database_close( + database + ); +} + int main(void) { test_transaction_commit(); test_transaction_rollback(); test_invalid_transaction_states(); test_transaction_error_state(); + test_transaction_active_state(); printf( "DatabaseTransaction : tous les tests sont valides.\n"