67 lines
1.4 KiB
Rust
67 lines
1.4 KiB
Rust
|
#![allow(dead_code)]
|
||
|
|
||
|
extern crate core;
|
||
|
|
||
|
pub mod database;
|
||
|
pub mod error;
|
||
|
pub mod metadata;
|
||
|
pub mod migration;
|
||
|
pub mod model;
|
||
|
pub mod query;
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod test {
|
||
|
use crate::database::Database;
|
||
|
use crate::model::JdbModel;
|
||
|
use lazy_static::lazy_static;
|
||
|
use serde::{Deserialize, Serialize};
|
||
|
use std::path::Path;
|
||
|
use std::sync::Mutex;
|
||
|
|
||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||
|
pub struct User {
|
||
|
id: Option<u64>,
|
||
|
pub name: String,
|
||
|
pub age: u16,
|
||
|
}
|
||
|
|
||
|
impl User {
|
||
|
pub fn new(name: &str, age: u16) -> Self {
|
||
|
Self {
|
||
|
id: None,
|
||
|
name: name.to_string(),
|
||
|
age,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl JdbModel for User {
|
||
|
fn id(&self) -> Option<u64> {
|
||
|
self.id
|
||
|
}
|
||
|
|
||
|
fn set_id(&mut self, id: u64) {
|
||
|
self.id = Some(id)
|
||
|
}
|
||
|
|
||
|
fn tree() -> String {
|
||
|
"User".to_string()
|
||
|
}
|
||
|
|
||
|
fn check_unique(&self, other: &Self) -> bool {
|
||
|
self.name != other.name
|
||
|
}
|
||
|
}
|
||
|
|
||
|
lazy_static! {
|
||
|
pub static ref DB: Database = Database::new(Path::new("test_database")).unwrap();
|
||
|
pub static ref LOCK: Mutex<()> = Mutex::default();
|
||
|
}
|
||
|
|
||
|
pub fn cleanup() {
|
||
|
DB.clear_tree::<User>().unwrap();
|
||
|
DB.db.clear().unwrap();
|
||
|
DB.db.flush().unwrap();
|
||
|
}
|
||
|
}
|