#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
use crate::drivers::postgres::{Database, Postgres as TestDriver};
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
use crate::drivers::sqlite::{Database, Sqlite as TestDriver};
use crate::{DatabaseSetup, Table};
use easy_macros::{add_code, always_context};
#[cfg(not(feature = "migrations"))]
#[always_context(skip(!))]
#[no_context]
#[add_code(after = {
Database::setup_for_testing::<DocUserTable>().await?;
assert_eq!(
<DocUserTable as Table<TestDriver>>::table_name(),
"doc_users"
);
Ok(())
})]
#[tokio::test]
#[docify::export_content]
async fn table_basic_example() -> anyhow::Result<()> {
#[derive(Table)]
#[sql(table_name = "doc_users")]
struct DocUserTable {
#[sql(primary_key)]
#[sql(auto_increment)]
id: i32,
#[sql(unique)]
email: String,
#[sql(default = "guest".to_string())]
role: String,
nickname: Option<String>,
}
}
#[always_context(skip(!))]
#[no_context]
#[add_code(after = {
#[derive(DatabaseSetup)]
#[allow(dead_code)]
struct Tables{
post_table: PostTable,
comment_table: CommentTable,
}
Database::setup_for_testing::<Tables>().await?;
assert_eq!(
<CommentTable as Table<TestDriver>>::primary_keys(),
vec!["id"]
);
Ok(())
})]
#[tokio::test]
#[docify::export_content]
async fn table_foreign_key_example() -> anyhow::Result<()> {
#[derive(Table)]
#[sql(no_version)]
struct PostTable {
#[sql(primary_key)]
id: i32,
title: String,
}
#[derive(Table)]
#[sql(version = 2)]
#[sql(table_name = "doc_comments")]
#[sql(unique_id = "43af18d0-f056-4b18-9958-3fadb41e4334")]
struct CommentTable {
#[sql(primary_key)]
#[sql(auto_increment)]
id: i32,
#[sql(foreign_key = PostTable, cascade)]
post_id: i32,
body: String,
}
}
#[always_context(skip(!))]
#[no_context]
#[add_code(after = {
Database::setup_for_testing::<DocMembershipTable>().await?;
assert_eq!(
<DocMembershipTable as Table<TestDriver>>::primary_keys(),
vec!["org_id", "user_id"]
);
Ok(())
})]
#[cfg(not(feature = "migrations"))]
#[tokio::test]
#[docify::export_content]
async fn table_composite_primary_key_example() -> anyhow::Result<()> {
#[derive(Table)]
struct DocMembershipTable {
#[sql(primary_key)]
org_id: i32,
#[sql(primary_key)]
user_id: i32,
joined_at: i64,
}
}