Added a table to the database.
This commit is contained in:
@ -1,16 +1,37 @@
|
||||
pub mod error;
|
||||
|
||||
use async_std::sync::{Arc, RwLock};
|
||||
use error::MTTError;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MoreThanText;
|
||||
pub struct MoreThanText {
|
||||
tables: Arc<RwLock<HashMap<String, Table>>>,
|
||||
}
|
||||
|
||||
impl MoreThanText {
|
||||
pub async fn new() -> Self {
|
||||
Self {}
|
||||
Self {
|
||||
tables: Arc::new(RwLock::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn new_table(&self, name: &str) -> Result<Table, MTTError> {
|
||||
let mut tables = self.tables.write().await;
|
||||
match tables.get(name) {
|
||||
Some(_) => Err(MTTError::new(format!("table {} already exists", name))),
|
||||
None => {
|
||||
tables.insert(name.to_string(), Table::new().await);
|
||||
Ok(Table::new().await)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Table;
|
||||
pub struct Table;
|
||||
|
||||
impl Table {
|
||||
async fn new() -> Self {
|
||||
pub async fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
@ -20,8 +41,31 @@ mod database {
|
||||
use super::*;
|
||||
|
||||
#[async_std::test]
|
||||
async fn create() {
|
||||
MoreThanText::new().await;
|
||||
async fn create_table() {
|
||||
let db = MoreThanText::new().await;
|
||||
db.new_table("william").await.unwrap();
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn table_names_are_unique() -> Result<(), String> {
|
||||
let db = MoreThanText::new().await;
|
||||
let name = "alexandar";
|
||||
let msg = format!("table {} already exists", name);
|
||||
db.new_table(name).await.unwrap();
|
||||
match db.new_table(name).await {
|
||||
Ok(_) => Err("Duplicate table names are not allowed.".to_string()),
|
||||
Err(err) => {
|
||||
if err.to_string() == msg {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!(
|
||||
"Error message is incorrect: Got: '{}' Want: '{}'",
|
||||
err.to_string(),
|
||||
msg
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user