From 39ae04fcc1e25d6b252a7432dfc8257e41237750 Mon Sep 17 00:00:00 2001 From: aur-build Date: Fri, 20 Nov 2026 20:33:11 +0000 Subject: db: move the state-enum tests into the model that declares them MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit tests/state_machine.rs was built around a database, so two tests that asserted nothing about a database still sat there: the column-text round trip, and the enumeration of the transition table. Both moved into src/db/model/build_state.rs, where `BuildState` and its table are declared. Making them in-module tests is what let the table stop being public. `ALL` and `can_transition` had no reader outside db/model at all — production enforces transitions by asking `predecessors(to)` and putting the answer in a SQL `WHERE state IN (...)` guard — so both are now private, and the five state enums' `ALL` consts with them. One assertion moved rather than being dropped. `a_terminal_build_is_history_and_does_not_move` made two claims per state: a table claim (`!Succeeded.can_transition(to)`) and a database claim (`set_state(...).is_err()`). The table claim now lives with the table; the database claim stayed where the database is. Neither was weakened. Two findings, recorded rather than quietly acted on: `ReleaseState::can_transition` and `SubscriptionState::can_transition` are consulted by nothing — not by `predecessors`, not by a test. They are dead tables for machines nothing enforces. Left `pub` with a NOTE and handed to the disposition table, because deleting a stated invariant and wiring up an unwired one are both decisions above this commit. Added two round-trip tests (StepState, and the three enums declared in state.rs) so the newly private `ALL` consts are still exercised. db: 202 pub / 13 test-only -> 201 pub / 11 test-only. Suite: 283 passed, 0 failed, 0 ignored. Clippy clean. --- src/db/model/state.rs | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'src/db/model/state.rs') diff --git a/src/db/model/state.rs b/src/db/model/state.rs index 777840f..f773f5b 100644 --- a/src/db/model/state.rs +++ b/src/db/model/state.rs @@ -14,7 +14,7 @@ pub enum CandidateState { } impl CandidateState { - pub const ALL: &'static [CandidateState] = &[ + const ALL: &'static [CandidateState] = &[ CandidateState::PendingReview, CandidateState::Approved, CandidateState::Rejected, @@ -40,7 +40,7 @@ impl CandidateState { *self == CandidateState::PendingReview } - pub fn can_transition(&self, to: CandidateState) -> bool { + fn can_transition(&self, to: CandidateState) -> bool { use CandidateState::*; if *self == to { return true; @@ -82,7 +82,7 @@ pub enum ReleaseState { } impl ReleaseState { - pub const ALL: &'static [ReleaseState] = &[ + const ALL: &'static [ReleaseState] = &[ ReleaseState::Published, ReleaseState::Withdrawn, ReleaseState::FailedUnbuilt, @@ -100,6 +100,9 @@ impl ReleaseState { ReleaseState::ALL.iter().copied().find(|r| r.as_str() == s) } + /// NOTE: nothing consults this table today — release transitions are + /// enforced elsewhere. Left `pub` and dead rather than quietly deleted + /// or quietly wired up; it is a disposition-table decision. pub fn can_transition(&self, to: ReleaseState) -> bool { use ReleaseState::*; if *self == to { @@ -158,7 +161,7 @@ pub enum SubscriptionState { } impl SubscriptionState { - pub const ALL: &'static [SubscriptionState] = &[ + const ALL: &'static [SubscriptionState] = &[ SubscriptionState::Active, SubscriptionState::Paused, SubscriptionState::Orphaned, @@ -178,6 +181,7 @@ impl SubscriptionState { SubscriptionState::ALL.iter().copied().find(|st| st.as_str() == text) } + /// NOTE: dead in the same way as `ReleaseState::can_transition`. pub fn can_transition(&self, to: SubscriptionState) -> bool { use SubscriptionState::*; if *self == to { @@ -198,3 +202,28 @@ impl std::fmt::Display for SubscriptionState { } } +#[cfg(test)] +mod tests { + use super::*; + + // The three enums declared here keep their `ALL` private, so the round trip + // through column text — the property that makes a stored row readable back + // — is checked here rather than from outside. BuildState and StepState are + // covered in build_state.rs. + + #[test] + fn every_state_declared_here_round_trips_through_its_column_text() { + for s in CandidateState::ALL { + assert_eq!(CandidateState::parse(s.as_str()), Some(*s), "candidate {s:?}"); + } + for s in ReleaseState::ALL { + assert_eq!(ReleaseState::parse(s.as_str()), Some(*s), "release {s:?}"); + } + for s in SubscriptionState::ALL { + assert_eq!(SubscriptionState::parse(s.as_str()), Some(*s), "subscription {s:?}"); + } + assert_eq!(CandidateState::parse("exploded"), None); + assert_eq!(ReleaseState::parse("exploded"), None); + assert_eq!(SubscriptionState::parse("exploded"), None); + } +} -- cgit v1.2.3