You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.4 KiB

use cassandra_cpp::{Cluster, Session, stmt};
pub mod todo_repository;
pub mod user_repository;
// Ideally read this from config
const KEYSPACE_NAME: &str = "rust_solid_cassandra";
fn init() -> Session {
let mut cluster = Cluster::default();
cluster.set_contact_points("127.0.0.1").unwrap(); // Panic if not successful
let session = cluster.connect().unwrap(); // Session is used to exec queries
let query = stmt!(&format!(
"CREATE KEYSPACE IF NOT EXISTS {KEYSPACE_NAME}
WITH replication = {{'class': 'SimpleStrategy', 'replication_factor': 1}};"
));
session
.execute(&query)
.wait()
.expect("Should create default keyspace if not exists");
session
}
#[cfg(test)]
mod tests {
use cassandra_cpp::{stmt, Cluster};
use crate::model::user::User;
use super::*;
#[test]
fn init_user_repo() {
let session = init();
let ur = user_repository::UserRepository::new(&session);
}
#[test]
fn user_repo_create() {
let session = init();
let ur = user_repository::UserRepository::new(&session);
let u = User::new("phga".to_string(), "1337".to_string(), "salzig".to_string());
if let Err(err) = ur.create(&u) {
panic!("Creating a user failed {err}");
}
}
#[test]
fn user_repo_read_all() {
let session = init();
let ur = user_repository::UserRepository::new(&session);
match ur.read_all() {
Ok(rows) => println!("{rows}"),
Err(err) => panic!("Reading from user table failed: {err}"),
};
}
#[test]
fn initialize_database() {
let create_query = stmt!(
"CREATE KEYSPACE IF NOT EXISTS test1337
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};"
);
let check_query = stmt!(
"SELECT keyspace_name FROM system_schema.keyspaces
WHERE keyspace_name = 'test1337';"
);
let mut cluster = Cluster::default();
cluster.set_contact_points("127.0.0.1").unwrap();
let session = cluster.connect().unwrap(); // Session is used to exec queries
let result = session.execute(&create_query).wait().unwrap();
println!("CREATE: {}", result);
let result = session.execute(&check_query).wait().unwrap();
println!("CHECK: {}", result);
}
}