#![allow(dead_code)]
use pgorm::{FromRow, InsertModel, Model, ModelPk, UpdateModel, WriteReport, WriteStepReport};
#[derive(Debug, FromRow, Model)]
#[orm(table = "orders")]
struct Order {
#[orm(id)]
id: i64,
user_id: i64,
total_cents: i64,
}
#[derive(Clone, InsertModel)]
#[orm(table = "order_items")]
struct NewOrderItem {
order_id: Option<i64>,
sku: String,
qty: i32,
}
#[derive(InsertModel)]
#[orm(table = "orders")]
struct NewOrderBasic {
user_id: i64,
total_cents: i64,
}
#[test]
fn test_insert_model_basic_compiles() {
let _order = NewOrderBasic {
user_id: 1,
total_cents: 1000,
};
assert_eq!(NewOrderBasic::TABLE, "orders");
assert_eq!(NewOrderItem::TABLE, "order_items");
}
#[derive(Debug, FromRow, Model)]
#[orm(table = "users")]
struct User {
#[orm(id)]
id: i64,
username: String,
}
#[derive(Clone, InsertModel)]
#[orm(table = "user_profiles")]
struct NewUserProfile {
user_id: Option<i64>,
bio: String,
}
#[derive(InsertModel)]
#[orm(table = "users")]
struct NewUserBasic {
username: String,
}
#[test]
fn test_insert_model_user_basic_compiles() {
let _user = NewUserBasic {
username: "alice".into(),
};
assert_eq!(NewUserBasic::TABLE, "users");
}
#[derive(Debug, FromRow, Model)]
#[orm(table = "categories")]
struct Category {
#[orm(id)]
id: i64,
name: String,
}
#[derive(Clone, InsertModel)]
#[orm(table = "categories", returning = "Category")]
struct NewCategory {
name: String,
}
#[derive(Debug, FromRow, Model)]
#[orm(table = "products")]
struct Product {
#[orm(id)]
id: i64,
name: String,
category_id: Option<i64>,
}
#[derive(InsertModel)]
#[orm(table = "products", returning = "Product")]
#[orm(graph_root_key = "id")]
#[orm(belongs_to(
NewCategory,
field = "category",
set_fk_field = "category_id",
mode = "insert_returning",
required = false
))]
struct NewProduct {
name: String,
category_id: Option<i64>,
category: Option<NewCategory>,
}
#[test]
fn test_insert_model_with_belongs_to_compiles() {
let _product1 = NewProduct {
name: "Widget".into(),
category_id: Some(1),
category: None,
};
let _product2 = NewProduct {
name: "Gadget".into(),
category_id: None,
category: Some(NewCategory {
name: "Electronics".into(),
}),
};
assert_eq!(NewProduct::TABLE, "products");
}
#[derive(Clone, InsertModel)]
#[orm(table = "audit_logs")]
struct NewAuditLog {
action: String,
entity: String,
}
#[derive(InsertModel)]
#[orm(table = "users", returning = "User")]
#[orm(graph_root_key = "id")]
#[orm(after_insert(NewAuditLog, field = "audit", mode = "insert"))]
struct NewUserWithAudit {
username: String,
audit: Option<NewAuditLog>,
}
#[test]
fn test_insert_model_with_after_insert_compiles() {
let _user = NewUserWithAudit {
username: "bob".into(),
audit: Some(NewAuditLog {
action: "CREATE".into(),
entity: "user".into(),
}),
};
assert_eq!(NewUserWithAudit::TABLE, "users");
}
#[derive(Clone, InsertModel)]
#[orm(table = "order_items", conflict_target = "order_id, sku")]
struct NewOrderItemUpsert {
order_id: i64,
sku: String,
qty: i32,
}
#[test]
fn test_insert_model_with_composite_conflict_target_compiles() {
let _item = NewOrderItemUpsert {
order_id: 1,
sku: "ABC123".into(),
qty: 5,
};
assert_eq!(NewOrderItemUpsert::TABLE, "order_items");
}
#[derive(Clone, InsertModel)]
#[orm(table = "tags", conflict_constraint = "tags_name_unique")]
struct NewTagWithConstraint {
name: String,
color: Option<String>,
}
#[test]
fn test_insert_model_with_conflict_constraint_compiles() {
let _tag = NewTagWithConstraint {
name: "rust".into(),
color: Some("orange".into()),
};
assert_eq!(NewTagWithConstraint::TABLE, "tags");
}
#[derive(UpdateModel)]
#[orm(table = "orders", model = "Order", returning = "Order")]
struct OrderPatchBasic {
total_cents: Option<i64>,
}
#[test]
fn test_update_model_basic_compiles() {
let _patch = OrderPatchBasic {
total_cents: Some(1500),
};
assert_eq!(OrderPatchBasic::TABLE, "orders");
}
#[derive(UpdateModel)]
#[orm(table = "users", model = "User", returning = "User")]
struct UserPatchBasic {
username: Option<String>,
}
#[test]
fn test_update_model_user_basic_compiles() {
let _patch = UserPatchBasic {
username: Some("new_name".into()),
};
assert_eq!(UserPatchBasic::TABLE, "users");
}
#[test]
fn test_write_report_type_exists() {
let report: WriteReport<i32> = WriteReport {
affected: 5,
steps: vec![
WriteStepReport {
tag: "graph:root:test",
affected: 1,
},
WriteStepReport {
tag: "graph:has_many:items",
affected: 4,
},
],
root: Some(42),
};
assert_eq!(report.affected, 5);
assert_eq!(report.steps.len(), 2);
assert_eq!(report.root, Some(42));
}
#[derive(InsertModel)]
#[orm(table = "orders", returning = "Order")]
#[orm(graph_root_key = "id")]
#[orm(has_many(NewOrderItem, field = "items", fk_field = "order_id", fk_wrap = "some"))]
struct NewOrderWithItems {
user_id: i64,
total_cents: i64,
items: Option<Vec<NewOrderItem>>,
}
#[test]
fn test_insert_model_with_has_many_compiles() {
let _order = NewOrderWithItems {
user_id: 1,
total_cents: 1000,
items: Some(vec![
NewOrderItem {
order_id: None,
sku: "A".into(),
qty: 1,
},
NewOrderItem {
order_id: None,
sku: "B".into(),
qty: 2,
},
]),
};
assert_eq!(NewOrderWithItems::TABLE, "orders");
}
#[derive(InsertModel)]
#[orm(table = "users", returning = "User")]
#[orm(graph_root_key = "id")]
#[orm(has_one(
NewUserProfile,
field = "profile",
fk_field = "user_id",
fk_wrap = "some"
))]
struct NewUserWithProfile {
username: String,
profile: Option<NewUserProfile>,
}
#[test]
fn test_insert_model_with_has_one_compiles() {
let _user = NewUserWithProfile {
username: "alice".into(),
profile: Some(NewUserProfile {
user_id: None,
bio: "Hello world".into(),
}),
};
assert_eq!(NewUserWithProfile::TABLE, "users");
}
#[test]
fn test_model_pk_trait() {
let order = Order {
id: 42,
user_id: 1,
total_cents: 1000,
};
let pk: &i64 = order.pk();
assert_eq!(*pk, 42);
fn assert_model_pk<M: ModelPk<Id = i64>>(_: &M) {}
assert_model_pk(&order);
}
#[test]
fn test_with_setters() {
let item1 = NewOrderItem {
order_id: None,
sku: "default".into(),
qty: 0,
};
let item1 = item1.with_sku("ABC123".to_string());
assert_eq!(item1.sku, "ABC123");
let item2 = NewOrderItem {
order_id: None,
sku: "test".into(),
qty: 1,
};
let item2 = item2.with_order_id(42);
assert_eq!(item2.order_id, Some(42));
let item3 = NewOrderItem {
order_id: Some(1),
sku: "test".into(),
qty: 1,
};
let item3 = item3.with_order_id_opt(None);
assert_eq!(item3.order_id, None);
}
#[derive(Clone, InsertModel)]
#[orm(
table = "order_items",
conflict_target = "order_id, sku",
conflict_update = "qty"
)]
struct NewOrderItemUpsertPartial {
order_id: i64,
sku: String,
qty: i32,
notes: String,
}
#[test]
fn test_conflict_update_attribute() {
let _item = NewOrderItemUpsertPartial {
order_id: 1,
sku: "ABC123".into(),
qty: 5,
notes: "test".into(),
};
assert_eq!(NewOrderItemUpsertPartial::TABLE, "order_items");
}
#[derive(Clone, InsertModel)]
#[orm(table = "order_items", conflict_target = "order_id, sku")]
struct NewOrderItemForUpdate {
order_id: Option<i64>,
sku: String,
qty: i32,
}
#[test]
fn test_insert_model_for_update_compiles() {
let _item = NewOrderItemForUpdate {
order_id: Some(1),
sku: "ABC".into(),
qty: 1,
};
assert_eq!(NewOrderItemForUpdate::TABLE, "order_items");
}
#[derive(Clone, InsertModel)]
#[orm(table = "user_profiles", conflict_target = "user_id")]
struct NewUserProfileForUpdate {
user_id: Option<i64>,
bio: String,
}
#[test]
fn test_insert_model_for_profile_update_compiles() {
let _profile = NewUserProfileForUpdate {
user_id: Some(1),
bio: "Test bio".into(),
};
assert_eq!(NewUserProfileForUpdate::TABLE, "user_profiles");
}
#[test]
fn test_diff_helper_method_exists() {
assert_eq!(NewOrderItemForUpdate::TABLE, "order_items");
}
use chrono::{DateTime, Utc};
#[derive(InsertModel)]
#[orm(table = "posts", returning = "Post")]
struct NewPostWithTimestamps {
title: String,
content: String,
#[orm(auto_now_add)]
created_at: Option<DateTime<Utc>>,
#[orm(auto_now_add)]
updated_at: Option<DateTime<Utc>>,
}
#[derive(Debug, FromRow, Model)]
#[orm(table = "posts")]
struct Post {
#[orm(id)]
id: i64,
title: String,
content: String,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
}
#[test]
fn test_insert_model_with_auto_now_add_compiles() {
let _post1 = NewPostWithTimestamps {
title: "Hello".into(),
content: "World".into(),
created_at: None,
updated_at: None,
};
let fixed_time = Utc::now();
let _post2 = NewPostWithTimestamps {
title: "Fixed".into(),
content: "Time".into(),
created_at: Some(fixed_time),
updated_at: Some(fixed_time),
};
assert_eq!(NewPostWithTimestamps::TABLE, "posts");
}
use chrono::NaiveDateTime;
#[derive(InsertModel)]
#[orm(table = "events")]
struct NewEvent {
name: String,
#[orm(auto_now_add)]
created_at: Option<NaiveDateTime>,
}
#[test]
fn test_insert_model_with_auto_now_add_naive_compiles() {
let _event = NewEvent {
name: "Launch".into(),
created_at: None,
};
assert_eq!(NewEvent::TABLE, "events");
}
#[derive(UpdateModel)]
#[orm(table = "posts", model = "Post", returning = "Post")]
struct PostPatchWithAutoNow {
title: Option<String>,
#[orm(auto_now)]
updated_at: Option<DateTime<Utc>>,
}
#[test]
fn test_update_model_with_auto_now_compiles() {
let _patch1 = PostPatchWithAutoNow {
title: Some("New title".into()),
updated_at: None,
};
let _patch2 = PostPatchWithAutoNow {
title: None,
updated_at: None,
};
let fixed_time = Utc::now();
let _patch3 = PostPatchWithAutoNow {
title: Some("Another".into()),
updated_at: Some(fixed_time),
};
assert_eq!(PostPatchWithAutoNow::TABLE, "posts");
}
#[derive(UpdateModel)]
#[orm(table = "events", id_column = "id")]
struct EventPatch {
name: Option<String>,
#[orm(auto_now)]
modified_at: Option<NaiveDateTime>,
}
#[test]
fn test_update_model_with_auto_now_naive_compiles() {
let _patch = EventPatch {
name: Some("Updated".into()),
modified_at: None,
};
assert_eq!(EventPatch::TABLE, "events");
}