ORM-18: Add relationship test fixtures (Profile, Post, Tag) #18

Open
opened 2026-10-13 12:49:39 +01:00 by NoctisWolfshadow · 0 comments
Owner

Depends on: #17

Context

Every relation issue needs the same test models and schema: 1-1 (User has one Profile), 1-n (User has many Posts) and n-m (Post has many Tags through a pivot table). Building them once, with real foreign keys, also proves that FK enforcement (#5) and cascades work.

Tasks

  • Models Profile, Post and Tag in tests/common, each with a ModelState field and typed Column consts (Post::USER_ID, Profile::USER_ID, ...).
  • Migrations for the four tables below, with ON DELETE CASCADE on every foreign key.
  • A seed helper with a few users, posts, tags and attachments.

Example

// tests/common/mod.rs
#[derive(Debug, Clone, PartialEq)]
pub struct Post {
    pub id: i64,
    pub user_id: i64,
    pub title: String,
    pub published: bool,
    state: ModelState,
}

impl Post {
    pub const ID: Column<Post, i64> = Column::new("id");
    pub const USER_ID: Column<Post, i64> = Column::new("user_id");
    pub const TITLE: Column<Post, String> = Column::new("title");
    pub const PUBLISHED: Column<Post, bool> = Column::new("published");

    pub fn new(user_id: i64, title: &str, published: bool) -> Self {
        Post { id: 0, user_id, title: title.into(), published, state: ModelState::new() }
    }
}
CREATE TABLE profiles (
    id      INTEGER PRIMARY KEY,
    user_id INTEGER NOT NULL UNIQUE REFERENCES users(id) ON DELETE CASCADE,
    bio     TEXT
);
CREATE TABLE posts (
    id        INTEGER PRIMARY KEY,
    user_id   INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    title     TEXT NOT NULL,
    published INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE tags (
    id   INTEGER PRIMARY KEY,
    name TEXT NOT NULL UNIQUE
);
CREATE TABLE post_tags (
    post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
    tag_id  INTEGER NOT NULL REFERENCES tags(id)  ON DELETE CASCADE,
    PRIMARY KEY (post_id, tag_id)
);
CREATE INDEX posts_user_id_index ON posts (user_id);
CREATE INDEX post_tags_tag_id_index ON post_tags (tag_id);
#[test]
fn foreign_keys_are_enforced() {
    setup_with_relations();
    let err = Post::create(Post::new(9999, "orphan", false)).unwrap_err();
    assert!(err.to_string().contains("FOREIGN KEY"));
}

#[test]
fn deleting_a_user_cascades() {
    setup_with_relations();
    Database::execute("DELETE FROM users WHERE id = 1", []).unwrap();
    assert_eq!(count("posts WHERE user_id = 1"), 0);
    assert_eq!(count("profiles WHERE user_id = 1"), 0);
    assert_eq!(count("post_tags WHERE post_id IN (1, 2)"), 0); // posts 1 and 2 belonged to user 1
}

Acceptance criteria

  • A post with a nonexistent user_id fails with a foreign-key error.
  • Deleting a user removes their profile, posts and pivot rows.
  • A second profile for the same user fails on UNIQUE.
**Depends on:** #17 ## Context Every relation issue needs the same test models and schema: 1-1 (`User` has one `Profile`), 1-n (`User` has many `Post`s) and n-m (`Post` has many `Tag`s through a pivot table). Building them once, with real foreign keys, also proves that FK enforcement (#5) and cascades work. ## Tasks - [ ] Models `Profile`, `Post` and `Tag` in `tests/common`, each with a `ModelState` field and typed `Column` consts (`Post::USER_ID`, `Profile::USER_ID`, ...). - [ ] Migrations for the four tables below, with `ON DELETE CASCADE` on every foreign key. - [ ] A seed helper with a few users, posts, tags and attachments. ## Example ```rust // tests/common/mod.rs #[derive(Debug, Clone, PartialEq)] pub struct Post { pub id: i64, pub user_id: i64, pub title: String, pub published: bool, state: ModelState, } impl Post { pub const ID: Column<Post, i64> = Column::new("id"); pub const USER_ID: Column<Post, i64> = Column::new("user_id"); pub const TITLE: Column<Post, String> = Column::new("title"); pub const PUBLISHED: Column<Post, bool> = Column::new("published"); pub fn new(user_id: i64, title: &str, published: bool) -> Self { Post { id: 0, user_id, title: title.into(), published, state: ModelState::new() } } } ``` ```sql CREATE TABLE profiles ( id INTEGER PRIMARY KEY, user_id INTEGER NOT NULL UNIQUE REFERENCES users(id) ON DELETE CASCADE, bio TEXT ); CREATE TABLE posts ( id INTEGER PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, title TEXT NOT NULL, published INTEGER NOT NULL DEFAULT 0 ); CREATE TABLE tags ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); CREATE TABLE post_tags ( post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE, tag_id INTEGER NOT NULL REFERENCES tags(id) ON DELETE CASCADE, PRIMARY KEY (post_id, tag_id) ); CREATE INDEX posts_user_id_index ON posts (user_id); CREATE INDEX post_tags_tag_id_index ON post_tags (tag_id); ``` ```rust #[test] fn foreign_keys_are_enforced() { setup_with_relations(); let err = Post::create(Post::new(9999, "orphan", false)).unwrap_err(); assert!(err.to_string().contains("FOREIGN KEY")); } #[test] fn deleting_a_user_cascades() { setup_with_relations(); Database::execute("DELETE FROM users WHERE id = 1", []).unwrap(); assert_eq!(count("posts WHERE user_id = 1"), 0); assert_eq!(count("profiles WHERE user_id = 1"), 0); assert_eq!(count("post_tags WHERE post_id IN (1, 2)"), 0); // posts 1 and 2 belonged to user 1 } ``` ## Acceptance criteria - [ ] A post with a nonexistent `user_id` fails with a foreign-key error. - [ ] Deleting a user removes their profile, posts and pivot rows. - [ ] A second profile for the same user fails on `UNIQUE`.
Sign in to join this conversation.
No description provided.