Files
sta/backend/src/domain/relay/repository/label.rs
Lucien Cartier-Tilet 982baec8a2 test(infrastructure): write RelayLabelRepository trait tests
Add reusable test suite with 18 test functions covering get_label(),
save_label(), delete_label(), and get_all_labels() methods. Tests
verify contract compliance for any repository implementation.

Added delete_label() method to trait interface and implemented it in
MockRelayLabelRepository to support complete CRUD operations.

TDD phase: RED - Tests written before SQLite implementation (T036)

Ref: T035 (specs/001-modbus-relay-control/tasks.md)
2026-03-04 12:27:00 +01:00

51 lines
1.9 KiB
Rust

use async_trait::async_trait;
use crate::domain::relay::types::{RelayId, RelayLabel};
use super::RepositoryError;
/// Repository trait for persisting and retrieving relay labels.
///
/// This trait abstracts data persistence operations for relay labels,
/// enabling different storage implementations (e.g., `SQLite`, `PostgreSQL`, in-memory).
/// Implementations must be thread-safe (`Send + Sync`) for use in async contexts.
#[async_trait]
pub trait RelayLabelRepository: Send + Sync {
/// Retrieves the label for a specific relay.
///
/// Returns `None` if no label has been set for the relay.
///
/// # Errors
///
/// Returns `RepositoryError::DatabaseError` if the database operation fails.
async fn get_label(&self, id: RelayId) -> Result<Option<RelayLabel>, RepositoryError>;
/// Saves or updates the label for a specific relay.
///
/// If a label already exists for the relay, it will be overwritten.
///
/// # Errors
///
/// Returns `RepositoryError::DatabaseError` if the database operation fails.
async fn save_label(&self, id: RelayId, label: RelayLabel) -> Result<(), RepositoryError>;
/// Deletes the label for a specific relay.
///
/// If no label exists for the relay, this operation succeeds without error.
///
/// # Errors
///
/// Returns `RepositoryError::DatabaseError` if the database operation fails.
async fn delete_label(&self, id: RelayId) -> Result<(), RepositoryError>;
/// Retrieves all relay labels from the repository.
///
/// Returns a vector of tuples containing relay IDs and their corresponding labels.
/// Relays without labels are not included in the result.
///
/// # Errors
///
/// Returns `RepositoryError::DatabaseError` if the database operation fails.
async fn get_all_labels(&self) -> Result<Vec<(RelayId, RelayLabel)>, RepositoryError>;
}