Skip to main content

impl_test_model

Macro impl_test_model 

Source
macro_rules! impl_test_model {
    (
		$model:ident,
		$pk:ty,
		$table:expr,
		$app:expr,
		relationships: [
			$(($rel_type:ident, $rel_name:expr, $related:expr, $fk:expr, $back_pop:expr)),* $(,)?
		],
		many_to_many: [
			$(($m2m_name:expr, $m2m_related:expr, $m2m_through:expr, $m2m_source:expr, $m2m_target:expr)),* $(,)?
		]
	) => { ... };
    (
		$model:ident,
		$pk:ty,
		$table:expr,
		$app:expr,
		relationships: [
			$(($rel_type:ident, $rel_name:expr, $related:expr, $fk:expr, $back_pop:expr)),* $(,)?
		]
	) => { ... };
    (
		$model:ident,
		$pk:ty,
		$table:expr,
		$app:expr,
		many_to_many: [
			$(($rel_name:expr, $related:expr, $through:expr, $source:expr, $target:expr)),* $(,)?
		]
	) => { ... };
    ($model:ident, $pk:ty, $table:expr, $app:expr) => { ... };
    ($model:ident, $pk:ty, $table:expr) => { ... };
    ($model:ident, $pk:ty, $table:expr, $app:expr, non_option_pk) => { ... };
    ($model:ident, $pk:ty, $table:expr, non_option_pk) => { ... };
}
Expand description

Helper macro for implementing Model trait with empty Fields for test models

This macro generates the boilerplate code needed for test models that don’t use the full #[model(...)] macro. It creates an empty field selector struct and implements the required Model trait methods.

§Usage

#[derive(Debug, Clone, Serialize, Deserialize)]
struct TestUser {
    id: Option<i64>,
    name: String,
}

impl_test_model!(TestUser, i64, "test_users");

This expands to:

  • A TestUserFields struct that implements FieldSelector
  • A complete Model trait implementation for TestUser

§Parameters

  • $model: The model struct name
  • $pk: The primary key type
  • $table: The table name as a string literal
  • $app: The application label as a string literal (optional, defaults to “default”)
  • relationships: Optional relationship definitions (see examples below)

§Constraints

  • Model must have an id: Option<PrimaryKey> field
  • Primary key field name is fixed to "id"

§Examples

§Basic usage

#[derive(Debug, Clone, Serialize, Deserialize)]
struct User {
    id: Option<i64>,
    name: String,
}

// With app_label
reinhardt_testkit::impl_test_model!(User, i64, "users", "auth");

// Without app_label (defaults to "default")
reinhardt_testkit::impl_test_model!(Product, i32, "products");

§With relationships

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Author {
    id: Option<i32>,
    name: String,
}

// OneToMany relationship
reinhardt_testkit::impl_test_model!(
    Author, i32, "authors", "test",
    relationships: [
        (OneToMany, "books", "Book", "author_id", "author")
    ]
);