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/build_state.rs | 68 +++++++++++++++++++++++++++++++++++++++--- src/db/model/state.rs | 37 ++++++++++++++++++++--- tests/architecture-budget.json | 20 ++++++------- tests/config.rs | 16 ---------- tests/state_machine.rs | 42 ++++---------------------- 5 files changed, 112 insertions(+), 71 deletions(-) diff --git a/src/db/model/build_state.rs b/src/db/model/build_state.rs index a5998de..d081bca 100644 --- a/src/db/model/build_state.rs +++ b/src/db/model/build_state.rs @@ -13,7 +13,9 @@ pub enum BuildState { } impl BuildState { - pub const ALL: &'static [BuildState] = &[ + /// Every state, in column-text order. `parse` and `predecessors` + /// below are its only production readers, so it is not exported. + const ALL: &'static [BuildState] = &[ BuildState::Queued, BuildState::Leased, BuildState::Running, @@ -46,7 +48,9 @@ impl BuildState { ) } - pub fn can_transition(&self, to: BuildState) -> bool { + /// The transition table itself. `predecessors` is how production + /// enforces it; nothing outside this module asks the question directly. + fn can_transition(&self, to: BuildState) -> bool { use BuildState::*; if *self == to { return true; @@ -98,7 +102,7 @@ pub enum StepState { } impl StepState { - pub const ALL: &'static [StepState] = &[ + const ALL: &'static [StepState] = &[ StepState::Pending, StepState::Running, StepState::Succeeded, @@ -126,7 +130,7 @@ impl StepState { matches!(self, StepState::Succeeded | StepState::Skipped) } - pub fn can_transition(&self, to: StepState) -> bool { + fn can_transition(&self, to: StepState) -> bool { use StepState::*; if *self == to { return true; @@ -167,3 +171,59 @@ pub const BUILD_STEPS: &[&str] = &[ "publish", ]; +#[cfg(test)] +mod tests { + use super::*; + + // Moved verbatim from tests/state_machine.rs. Both are properties of the + // state enum itself — a column-text round trip and the shape of the + // transition table — and neither needed the database the rest of that file + // was built around. + + #[test] + fn every_state_round_trips_through_its_column_text() { + for s in BuildState::ALL { + assert_eq!(BuildState::parse(s.as_str()), Some(*s), "{s:?} does not round-trip"); + } + assert_eq!(BuildState::parse("exploded"), None); + assert_eq!(BuildState::parse(""), None); + } + + #[test] + fn the_transition_table_is_the_machine_the_code_actually_uses() { + let reachable = |from: BuildState| -> Vec<&'static str> { + BuildState::ALL + .iter() + .filter(|to| from.can_transition(**to)) + .map(|to| to.as_str()) + .collect() + }; + assert_eq!(reachable(BuildState::Queued), ["queued", "leased", "running", "blocked_missing_dep", "cancelled"]); + assert_eq!( + reachable(BuildState::Leased), + ["queued", "leased", "running", "blocked_missing_dep", "succeeded", "failed", "cancelled"] + ); + assert_eq!( + reachable(BuildState::Running), + ["queued", "running", "blocked_missing_dep", "succeeded", "failed", "cancelled"] + ); + assert_eq!(reachable(BuildState::BlockedMissingDep), ["queued", "blocked_missing_dep", "cancelled"]); + assert_eq!(reachable(BuildState::Failed), ["queued", "running", "failed"]); + assert_eq!(reachable(BuildState::Cancelled), ["queued", "cancelled"]); + assert_eq!(reachable(BuildState::Succeeded), ["succeeded"]); + // The claim that `a_terminal_build_is_history_and_does_not_move` used + // to make here against the table, while its database-level twin stayed + // in tests/state_machine.rs. + for to in [BuildState::Running, BuildState::Queued, BuildState::Failed] { + assert!(!BuildState::Succeeded.can_transition(to), "succeeded must not transition to {to:?}"); + } + } + + #[test] + fn step_states_round_trip_through_their_column_text() { + for s in StepState::ALL { + assert_eq!(StepState::parse(s.as_str()), Some(*s), "{s:?} does not round-trip"); + } + assert_eq!(StepState::parse("exploded"), None); + } +} 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); + } +} diff --git a/tests/architecture-budget.json b/tests/architecture-budget.json index c71c764..f21be23 100644 --- a/tests/architecture-budget.json +++ b/tests/architecture-budget.json @@ -89,15 +89,15 @@ "test": 3 }, "db": { - "dead": 21, - "deadonly": 8, + "dead": 20, + "deadonly": 9, "glob": 0, "largest": 364, "largest_file": "src/db/candidates.rs", - "lines": 3770, + "lines": 3859, "priv": 10, - "pub": 202, - "test": 13 + "pub": 201, + "test": 11 }, "error": { "dead": 0, @@ -310,15 +310,15 @@ } }, "totals": { - "dead": 98, - "deadonly": 33, + "dead": 97, + "deadonly": 34, "edges": 87, "glob": 0, "largest": 667, "largest_file": "src/config.rs", - "lines": 14838, + "lines": 14927, "priv": 109, - "pub": 497, - "test": 65 + "pub": 496, + "test": 63 } } diff --git a/tests/config.rs b/tests/config.rs index a45ac1b..6c58122 100644 --- a/tests/config.rs +++ b/tests/config.rs @@ -116,22 +116,6 @@ fn an_absolute_root_is_used_verbatim() { assert_eq!(Config::load(dir).unwrap().root, dir); } -fn load_sandbox(sandbox: &str) -> Result { - let tmp = tempfile::tempdir().unwrap(); - std::fs::write( - tmp.path().join("config.json"), - format!("{{\n \"sandbox\": {sandbox}\n}}\n"), - ) - .unwrap(); - Config::load(tmp.path()).map_err(|e| e.to_string()) -} - -fn load_raw(body: &str) -> Result { - let tmp = tempfile::tempdir().unwrap(); - std::fs::write(tmp.path().join("config.json"), body).unwrap(); - Config::load(tmp.path()).map_err(|e| e.to_string()) -} - #[test] fn serve_refuses_a_remote_config_that_never_asserted_the_workspace() { let tmp = tempfile::tempdir().unwrap(); diff --git a/tests/state_machine.rs b/tests/state_machine.rs index 70d7aa6..ccfdc42 100644 --- a/tests/state_machine.rs +++ b/tests/state_machine.rs @@ -2,6 +2,11 @@ mod common; use aur_build::db::{builds, model::{BuildState, CandidateState, StepState}}; use common::Env; +// What remains all moves real rows through real transitions against a +// database. The two tests that were only about the state enum — the column-text +// round trip and the shape of the transition table — moved into +// src/db/model/build_state.rs, where the enum and its table are declared, and +// so did the one pure table assertion that a fixture test was making. fn build(env: &Env, id: &str) -> String { env.exec( @@ -17,15 +22,6 @@ fn state(env: &Env, id: &str) -> BuildState { builds::get(&env.db, id).unwrap().expect("build").state } -#[test] -fn every_state_round_trips_through_its_column_text() { - for s in BuildState::ALL { - assert_eq!(BuildState::parse(s.as_str()), Some(*s), "{s:?} does not round-trip"); - } - assert_eq!(BuildState::parse("exploded"), None); - assert_eq!(BuildState::parse(""), None); -} - #[test] fn the_machine_refuses_a_transition_it_does_not_define() { let env = Env::new(); @@ -50,10 +46,6 @@ fn a_terminal_build_is_history_and_does_not_move() { assert_eq!(state(&env, "b1"), BuildState::Succeeded); for to in [BuildState::Running, BuildState::Queued, BuildState::Failed] { - assert!( - !BuildState::Succeeded.can_transition(to), - "succeeded must not transition to {to:?}" - ); assert!(builds::set_state(&env.db, &id, to, None).is_err(), "accepted {to:?} after success"); } assert_eq!(state(&env, "b1"), BuildState::Succeeded); @@ -87,30 +79,6 @@ fn a_state_the_code_does_not_know_is_an_error_not_an_empty_string() { assert!(aur_build::db::builds_for_pkgbase(&env.db, pb, 10).is_err()); } -#[test] -fn the_transition_table_is_the_machine_the_code_actually_uses() { - let reachable = |from: BuildState| -> Vec<&'static str> { - BuildState::ALL - .iter() - .filter(|to| from.can_transition(**to)) - .map(|to| to.as_str()) - .collect() - }; - assert_eq!(reachable(BuildState::Queued), ["queued", "leased", "running", "blocked_missing_dep", "cancelled"]); - assert_eq!( - reachable(BuildState::Leased), - ["queued", "leased", "running", "blocked_missing_dep", "succeeded", "failed", "cancelled"] - ); - assert_eq!( - reachable(BuildState::Running), - ["queued", "running", "blocked_missing_dep", "succeeded", "failed", "cancelled"] - ); - assert_eq!(reachable(BuildState::BlockedMissingDep), ["queued", "blocked_missing_dep", "cancelled"]); - assert_eq!(reachable(BuildState::Failed), ["queued", "running", "failed"]); - assert_eq!(reachable(BuildState::Cancelled), ["queued", "cancelled"]); - assert_eq!(reachable(BuildState::Succeeded), ["succeeded"]); -} - #[test] fn requeueing_a_succeeded_build_is_refused_not_silently_requeued() { let env = Env::new(); -- cgit v1.2.3