update grid4 & heroledger models
This commit is contained in:
97
heromodels/tests/heroledger_postgres.rs
Normal file
97
heromodels/tests/heroledger_postgres.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
use heromodels::db::postgres::{Config, Postgres};
|
||||
use heromodels::db::{Collection, Db};
|
||||
use heromodels::models::heroledger::user::user_index::username;
|
||||
use heromodels::models::heroledger::user::User;
|
||||
use heromodels_core::Model;
|
||||
|
||||
// NOTE: Requires a local Postgres running with user=postgres password=test123 host=localhost port=5432
|
||||
// Marked ignored by default. Run with: cargo test -p heromodels --test heroledger_postgres -- --ignored
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn heroledger_user_postgres_roundtrip() {
|
||||
// Connect
|
||||
let db = Postgres::new(
|
||||
Config::new()
|
||||
.user(Some("postgres".into()))
|
||||
.password(Some("test123".into()))
|
||||
.host(Some("localhost".into()))
|
||||
.port(Some(5432)),
|
||||
)
|
||||
.expect("can connect to Postgres");
|
||||
|
||||
// Open collection (will create table and indexes for top-level fields)
|
||||
let users = db.collection::<User>().expect("can open user collection");
|
||||
|
||||
// Clean slate
|
||||
if let Ok(existing) = users.get_all() {
|
||||
for u in existing {
|
||||
let _ = users.delete_by_id(u.get_id());
|
||||
}
|
||||
}
|
||||
|
||||
// Unique suffix to avoid collisions with any pre-existing rows
|
||||
let uniq = format!("{}", std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos());
|
||||
let alice = format!("alice_{}", uniq);
|
||||
let bob = format!("bob_{}", uniq);
|
||||
let carol = format!("carol_{}", uniq);
|
||||
|
||||
// Build and store multiple users
|
||||
let u1 = User::new(0)
|
||||
.username(&alice)
|
||||
.pubkey("PUBKEY_A")
|
||||
.add_email("alice@example.com")
|
||||
.build();
|
||||
let u2 = User::new(0)
|
||||
.username(&bob)
|
||||
.pubkey("PUBKEY_B")
|
||||
.add_email("bob@example.com")
|
||||
.build();
|
||||
let u3 = User::new(0)
|
||||
.username(&carol)
|
||||
.pubkey("PUBKEY_C")
|
||||
.add_email("carol@example.com")
|
||||
.build();
|
||||
|
||||
let (id1, db_u1) = users.set(&u1).expect("store u1");
|
||||
let (id2, db_u2) = users.set(&u2).expect("store u2");
|
||||
let (id3, db_u3) = users.set(&u3).expect("store u3");
|
||||
assert!(id1 > 0 && id2 > 0 && id3 > 0);
|
||||
|
||||
// Fetch by id
|
||||
assert_eq!(users.get_by_id(id1).unwrap().unwrap().username, alice);
|
||||
assert_eq!(users.get_by_id(id2).unwrap().unwrap().username, bob);
|
||||
assert_eq!(users.get_by_id(id3).unwrap().unwrap().username, carol);
|
||||
|
||||
// Fetch by index (top-level username)
|
||||
let by_username = users.get::<username, _>(&alice).expect("by username");
|
||||
assert_eq!(by_username.len(), 1);
|
||||
assert_eq!(by_username[0].get_id(), id1);
|
||||
|
||||
// Update one
|
||||
let updated = db_u1.clone().add_email("work@alice.example");
|
||||
let (id1b, updated_back) = users.set(&updated).expect("update alice");
|
||||
assert_eq!(id1b, id1);
|
||||
assert!(updated_back.email.len() >= 2);
|
||||
|
||||
// Targeted queries to avoid legacy rows in the same table
|
||||
// Verify three users exist via index queries
|
||||
assert_eq!(users.get::<username, _>(&alice).unwrap().len(), 1);
|
||||
assert_eq!(users.get::<username, _>(&bob).unwrap().len(), 1);
|
||||
assert_eq!(users.get::<username, _>(&carol).unwrap().len(), 1);
|
||||
|
||||
// Delete by id
|
||||
users.delete_by_id(id2).expect("delete bob by id");
|
||||
assert!(users.get_by_id(id2).unwrap().is_none());
|
||||
|
||||
// Delete by index (username)
|
||||
users.delete::<username, _>(&carol).expect("delete carol by username");
|
||||
assert!(users.get_by_id(id3).unwrap().is_none());
|
||||
|
||||
// Remaining should be just alice; verify via index
|
||||
let remain = users.get::<username, _>(&alice).expect("get alice after delete");
|
||||
assert_eq!(remain.len(), 1);
|
||||
assert_eq!(remain[0].get_id(), id1);
|
||||
}
|
Reference in New Issue
Block a user