Reworking database to match field types.
This commit is contained in:
parent
dd1aa38c0e
commit
966c4bed2b
94
Pipfile.lock
generated
94
Pipfile.lock
generated
@ -18,11 +18,11 @@
|
|||||||
"default": {
|
"default": {
|
||||||
"attrs": {
|
"attrs": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4",
|
"sha256:29adc2665447e5191d0e7c568fde78b21f9672d344281d0c6e1ab085429b22b6",
|
||||||
"sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd"
|
"sha256:86efa402f67bf2df34f51a335487cf46b1ec130d02b8d39fd248abfd30da551c"
|
||||||
],
|
],
|
||||||
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'",
|
"markers": "python_version >= '3.5'",
|
||||||
"version": "==21.4.0"
|
"version": "==22.1.0"
|
||||||
},
|
},
|
||||||
"glob2": {
|
"glob2": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
@ -39,11 +39,11 @@
|
|||||||
},
|
},
|
||||||
"mako": {
|
"mako": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:23aab11fdbbb0f1051b93793a58323ff937e98e34aece1c4219675122e57e4ba",
|
"sha256:df3921c3081b013c8a2d5ff03c18375651684921ae83fd12e64800b7da923257",
|
||||||
"sha256:9a7c7e922b87db3686210cf49d5d767033a41d4010b284e747682c92bddd8b39"
|
"sha256:f054a5ff4743492f1aa9ecc47172cb33b42b9d993cffcc146c9de17e717b0307"
|
||||||
],
|
],
|
||||||
"markers": "python_version >= '3.7'",
|
"markers": "python_version >= '3.7'",
|
||||||
"version": "==1.2.0"
|
"version": "==1.2.1"
|
||||||
},
|
},
|
||||||
"markupsafe": {
|
"markupsafe": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
@ -148,11 +148,11 @@
|
|||||||
},
|
},
|
||||||
"pytest-bdd": {
|
"pytest-bdd": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:c7cf12209606421f61f36b5dc63beccd0c82d29446c0592cf68af2dad0a9761d",
|
"sha256:1dcea36d5fb59291a5d3c186f4cbbd04cdb62a304341f9a360a07682ba1aba18",
|
||||||
"sha256:fab7093ed3d5e51ee0c68de093c90e4f40de345bd9a54a188b2991ce2a2a39cf"
|
"sha256:f894e589c9ee9e6a66c54e4917fafbbd11801fce9ce01e9b56dc15ccc57e4620"
|
||||||
],
|
],
|
||||||
"index": "pypi",
|
"index": "pypi",
|
||||||
"version": "==5.0.0"
|
"version": "==6.0.1"
|
||||||
},
|
},
|
||||||
"six": {
|
"six": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
@ -174,40 +174,40 @@
|
|||||||
"develop": {
|
"develop": {
|
||||||
"astroid": {
|
"astroid": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:14ffbb4f6aa2cf474a0834014005487f7ecd8924996083ab411e7fa0b508ce0b",
|
"sha256:86b0a340a512c65abf4368b80252754cda17c02cdbbd3f587dddf98112233e7b",
|
||||||
"sha256:f4e4ec5294c4b07ac38bab9ca5ddd3914d4bf46f9006eb5c0ae755755061044e"
|
"sha256:bb24615c77f4837c707669d16907331374ae8a964650a66999da3f5ca68dc946"
|
||||||
],
|
],
|
||||||
"markers": "python_full_version >= '3.6.2'",
|
"markers": "python_full_version >= '3.6.2'",
|
||||||
"version": "==2.11.5"
|
"version": "==2.11.7"
|
||||||
},
|
},
|
||||||
"black": {
|
"black": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:06f9d8846f2340dfac80ceb20200ea5d1b3f181dd0556b47af4e8e0b24fa0a6b",
|
"sha256:074458dc2f6e0d3dab7928d4417bb6957bb834434516f21514138437accdbe90",
|
||||||
"sha256:10dbe6e6d2988049b4655b2b739f98785a884d4d6b85bc35133a8fb9a2233176",
|
"sha256:187d96c5e713f441a5829e77120c269b6514418f4513a390b0499b0987f2ff1c",
|
||||||
"sha256:2497f9c2386572e28921fa8bec7be3e51de6801f7459dffd6e62492531c47e09",
|
"sha256:2ea29072e954a4d55a2ff58971b83365eba5d3d357352a07a7a4df0d95f51c78",
|
||||||
"sha256:30d78ba6bf080eeaf0b7b875d924b15cd46fec5fd044ddfbad38c8ea9171043a",
|
"sha256:4af5bc0e1f96be5ae9bd7aaec219c901a94d6caa2484c21983d043371c733fc4",
|
||||||
"sha256:328efc0cc70ccb23429d6be184a15ce613f676bdfc85e5fe8ea2a9354b4e9015",
|
"sha256:560558527e52ce8afba936fcce93a7411ab40c7d5fe8c2463e279e843c0328ee",
|
||||||
"sha256:35020b8886c022ced9282b51b5a875b6d1ab0c387b31a065b84db7c33085ca79",
|
"sha256:568ac3c465b1c8b34b61cd7a4e349e93f91abf0f9371eda1cf87194663ab684e",
|
||||||
"sha256:5795a0375eb87bfe902e80e0c8cfaedf8af4d49694d69161e5bd3206c18618bb",
|
"sha256:6797f58943fceb1c461fb572edbe828d811e719c24e03375fd25170ada53825e",
|
||||||
"sha256:5891ef8abc06576985de8fa88e95ab70641de6c1fca97e2a15820a9b69e51b20",
|
"sha256:6c1734ab264b8f7929cef8ae5f900b85d579e6cbfde09d7387da8f04771b51c6",
|
||||||
"sha256:637a4014c63fbf42a692d22b55d8ad6968a946b4a6ebc385c5505d9625b6a464",
|
"sha256:6c6d39e28aed379aec40da1c65434c77d75e65bb59a1e1c283de545fb4e7c6c9",
|
||||||
"sha256:67c8301ec94e3bcc8906740fe071391bce40a862b7be0b86fb5382beefecd968",
|
"sha256:7ba9be198ecca5031cd78745780d65a3f75a34b2ff9be5837045dce55db83d1c",
|
||||||
"sha256:6d2fc92002d44746d3e7db7cf9313cf4452f43e9ea77a2c939defce3b10b5c82",
|
"sha256:94783f636bca89f11eb5d50437e8e17fbc6a929a628d82304c80fa9cd945f256",
|
||||||
"sha256:6ee227b696ca60dd1c507be80a6bc849a5a6ab57ac7352aad1ffec9e8b805f21",
|
"sha256:a218d7e5856f91d20f04e931b6f16d15356db1c846ee55f01bac297a705ca24f",
|
||||||
"sha256:863714200ada56cbc366dc9ae5291ceb936573155f8bf8e9de92aef51f3ad0f0",
|
"sha256:a3db5b6409b96d9bd543323b23ef32a1a2b06416d525d27e0f67e74f1446c8f2",
|
||||||
"sha256:9b542ced1ec0ceeff5b37d69838106a6348e60db7b8fdd245294dc1d26136265",
|
"sha256:ac609cf8ef5e7115ddd07d85d988d074ed00e10fbc3445aee393e70164a2219c",
|
||||||
"sha256:a6342964b43a99dbc72f72812bf88cad8f0217ae9acb47c0d4f141a6416d2d7b",
|
"sha256:b154e6bbde1e79ea3260c4b40c0b7b3109ffcdf7bc4ebf8859169a6af72cd70b",
|
||||||
"sha256:ad4efa5fad66b903b4a5f96d91461d90b9507a812b3c5de657d544215bb7877a",
|
"sha256:b270a168d69edb8b7ed32c193ef10fd27844e5c60852039599f9184460ce0807",
|
||||||
"sha256:bc58025940a896d7e5356952228b68f793cf5fcb342be703c3a2669a1488cb72",
|
"sha256:b9fd45787ba8aa3f5e0a0a98920c1012c884622c6c920dbe98dbd05bc7c70fbf",
|
||||||
"sha256:cc1e1de68c8e5444e8f94c3670bb48a2beef0e91dddfd4fcc29595ebd90bb9ce",
|
"sha256:c85928b9d5f83b23cee7d0efcb310172412fbf7cb9d9ce963bd67fd141781def",
|
||||||
"sha256:cee3e11161dde1b2a33a904b850b0899e0424cc331b7295f2a9698e79f9a69a0",
|
"sha256:c9a3ac16efe9ec7d7381ddebcc022119794872abce99475345c5a61aa18c45ad",
|
||||||
"sha256:e3556168e2e5c49629f7b0f377070240bd5511e45e25a4497bb0073d9dda776a",
|
"sha256:cfaf3895a9634e882bf9d2363fed5af8888802d670f58b279b0bece00e9a872d",
|
||||||
"sha256:e8477ec6bbfe0312c128e74644ac8a02ca06bcdb8982d4ee06f209be28cdf163",
|
"sha256:e439798f819d49ba1c0bd9664427a05aab79bfba777a6db94fd4e56fae0cb849",
|
||||||
"sha256:ee8f1f7228cce7dffc2b464f07ce769f478968bfb3dd1254a4c2eeed84928aad",
|
"sha256:f586c26118bc6e714ec58c09df0157fe2d9ee195c764f630eb0d8e7ccce72e69",
|
||||||
"sha256:fd57160949179ec517d32ac2ac898b5f20d68ed1a9c977346efbac9c2f1e779d"
|
"sha256:f6fe02afde060bbeef044af7996f335fbe90b039ccf3f5eb8f16df8b20f77666"
|
||||||
],
|
],
|
||||||
"index": "pypi",
|
"index": "pypi",
|
||||||
"version": "==22.3.0"
|
"version": "==22.6.0"
|
||||||
},
|
},
|
||||||
"click": {
|
"click": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
@ -308,11 +308,11 @@
|
|||||||
},
|
},
|
||||||
"pylint": {
|
"pylint": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:549261e0762c3466cc001024c4419c08252cb8c8d40f5c2c6966fea690e7fe2a",
|
"sha256:487ce2192eee48211269a0e976421f334cf94de1806ca9d0a99449adcdf0285e",
|
||||||
"sha256:bb71e6d169506de585edea997e48d9ff20c0dc0e2fbc1d166bad6b640120326b"
|
"sha256:fabe30000de7d07636d2e82c9a518ad5ad7908590fe135ace169b44839c15f90"
|
||||||
],
|
],
|
||||||
"index": "pypi",
|
"index": "pypi",
|
||||||
"version": "==2.14.1"
|
"version": "==2.14.5"
|
||||||
},
|
},
|
||||||
"tomli": {
|
"tomli": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
@ -324,19 +324,11 @@
|
|||||||
},
|
},
|
||||||
"tomlkit": {
|
"tomlkit": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:0f4050db66fd445b885778900ce4dd9aea8c90c4721141fde0d6ade893820ef1",
|
"sha256:1c5bebdf19d5051e2e1de6cf70adfc5948d47221f097fcff7a3ffc91e953eaf5",
|
||||||
"sha256:71ceb10c0eefd8b8f11fe34e8a51ad07812cb1dc3de23247425fbc9ddc47b9dd"
|
"sha256:61901f81ff4017951119cd0d1ed9b7af31c821d6845c8c477587bbdcd5e5854e"
|
||||||
],
|
],
|
||||||
"markers": "python_version >= '3.6' and python_version < '4.0'",
|
"markers": "python_version >= '3.6' and python_version < '4.0'",
|
||||||
"version": "==0.11.0"
|
"version": "==0.11.1"
|
||||||
},
|
|
||||||
"typing-extensions": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:6657594ee297170d19f67d55c05852a874e7eb634f4f753dbd667855e07c1708",
|
|
||||||
"sha256:f1c24655a0da0d1b67f07e17a5e6b2a105894e6824b92096378bb3668ef02376"
|
|
||||||
],
|
|
||||||
"markers": "python_version < '3.10'",
|
|
||||||
"version": "==4.2.0"
|
|
||||||
},
|
},
|
||||||
"wrapt": {
|
"wrapt": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
|
@ -23,11 +23,11 @@ impl From<StaticString> for FieldType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod fieldtypes {
|
mod converstion {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_static_string() {
|
fn from_static_string() {
|
||||||
let data = "a static string";
|
let data = "a static string";
|
||||||
let field = StaticString::new(data);
|
let field = StaticString::new(data);
|
||||||
let ftype: FieldType = field.into();
|
let ftype: FieldType = field.into();
|
||||||
|
@ -1,109 +1,3 @@
|
|||||||
/*
|
|
||||||
use async_std::sync::{Arc, RwLock};
|
|
||||||
use std::{collections::HashMap, error::Error, fmt, str::FromStr};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct DBError {
|
|
||||||
detail: String,
|
|
||||||
source: Option<Box<DBError>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DBError {
|
|
||||||
fn new(detail: String) -> Self {
|
|
||||||
Self {
|
|
||||||
detail: detail.to_string(),
|
|
||||||
source: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for DBError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
write!(f, "{}", &self.detail)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error for DBError {
|
|
||||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
||||||
match &self.source {
|
|
||||||
Some(err) => Some(err),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
|
||||||
pub enum FieldType {
|
|
||||||
Table,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for FieldType {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
FieldType::Table => write!(f, "table"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for FieldType {
|
|
||||||
type Err = DBError;
|
|
||||||
|
|
||||||
fn from_str(input: &str) -> Result<FieldType, Self::Err> {
|
|
||||||
match input {
|
|
||||||
"table" => Ok(FieldType::Table),
|
|
||||||
_ => Err(DBError::new(format!("field type {} does not exist", input))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Table {
|
|
||||||
fields: Arc<RwLock<HashMap<String, FieldType>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Table {
|
|
||||||
pub async fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
fields: Arc::new(RwLock::new(HashMap::new())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn update_field(&self, name: &str, ftype: &str) -> Result<(), Box<dyn Error>> {
|
|
||||||
let ftype = match FieldType::from_str(ftype) {
|
|
||||||
Ok(field) => field,
|
|
||||||
Err(err) => {
|
|
||||||
let mut error = DBError::new(format!("failed to add field {}", name));
|
|
||||||
error.source = Some(Box::new(err));
|
|
||||||
return Err(Box::new(error));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let mut fmap = self.fields.write().await;
|
|
||||||
match fmap.get(name) {
|
|
||||||
Some(_) => Err(Box::new(DBError::new(format!(
|
|
||||||
"field {} already exists",
|
|
||||||
name
|
|
||||||
)))),
|
|
||||||
None => {
|
|
||||||
fmap.insert(name.to_string(), ftype);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn fields(&self) -> HashMap<String, FieldType> {
|
|
||||||
let fmap = self.fields.read().await;
|
|
||||||
fmap.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
use async_std::sync::{Arc, RwLock};
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
pub mod error;
|
|
||||||
mod fieldtype;
|
|
||||||
|
|
||||||
use error::MTTError;
|
|
||||||
use fieldtype::FieldType;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct MoreThanText;
|
pub struct MoreThanText;
|
||||||
|
|
||||||
@ -111,288 +5,14 @@ impl MoreThanText {
|
|||||||
pub async fn new() -> Self {
|
pub async fn new() -> Self {
|
||||||
Self {}
|
Self {}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn add_table(&self, name: &str) -> Table {
|
|
||||||
Table::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_table(&self, name: &str) -> Table {
|
|
||||||
Table::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
|
||||||
struct FieldDef;
|
|
||||||
|
|
||||||
pub struct Table {
|
|
||||||
fields: Arc<RwLock<HashMap<String, FieldDef>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Table {
|
|
||||||
fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
fields: Arc::new(RwLock::new(HashMap::new())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn add_field(&self, name: &str) {
|
|
||||||
let mut field_defs = self.fields.write().await;
|
|
||||||
field_defs.insert(name.to_string(), FieldDef {});
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_field(&self, name: &str) -> Option<FieldDef> {
|
|
||||||
let field_defs = self.fields.read().await;
|
|
||||||
match field_defs.get(name) {
|
|
||||||
Some(def) => Some(def.clone()),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn new_record(&self) -> Record {
|
|
||||||
Record::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Record {
|
|
||||||
data: Arc<RwLock<HashMap<String, FieldType>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Record {
|
|
||||||
fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
data: Arc::new(RwLock::new(HashMap::new())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
async fn update_field(&self, name: String, data: FieldType) {
|
|
||||||
let mut map = self.data.write().await;
|
|
||||||
map.insert(name, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_field(&self, name: &str) -> Option<FieldType> {
|
|
||||||
let map = self.data.read().await;
|
|
||||||
match map.get(name) {
|
|
||||||
Some(field) => Some(field.clone()),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod databases {
|
mod database {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn new_database() {
|
async fn create() {
|
||||||
MoreThanText::new().await;
|
MoreThanText::new().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn add_table() {
|
|
||||||
let db = MoreThanText::new().await;
|
|
||||||
let name = "table";
|
|
||||||
db.add_table(name).await;
|
|
||||||
db.get_table(name).await;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tables {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn new_table() {
|
|
||||||
Table::new();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_std::test]
|
|
||||||
async fn add_field_definition() {
|
|
||||||
let tbl = Table::new();
|
|
||||||
let name = "field";
|
|
||||||
let expected = FieldDef {};
|
|
||||||
tbl.add_field(name).await;
|
|
||||||
let output = tbl.get_field(name).await.unwrap();
|
|
||||||
assert!(output == expected, "Did not return a field definition.");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_std::test]
|
|
||||||
async fn missing_field_definition() {
|
|
||||||
let tbl = Table::new();
|
|
||||||
let output = tbl.get_field("missing").await;
|
|
||||||
assert!(
|
|
||||||
output == None,
|
|
||||||
"Should return None if field does not exist."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_std::test]
|
|
||||||
async fn get_empty_record() {
|
|
||||||
let tbl = Table::new();
|
|
||||||
tbl.new_record().await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod records {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
/*
|
|
||||||
#[async_std::test]
|
|
||||||
async fn update_fields() {
|
|
||||||
let rec = Record::new();
|
|
||||||
let name = "elephant";
|
|
||||||
let data = "";
|
|
||||||
let sstr = StaticString::new();
|
|
||||||
rec.update_field(name.to_string(), sstr).await;
|
|
||||||
let output = rec.get_field(name).await.unwrap();
|
|
||||||
assert!(
|
|
||||||
output.to_string() == data,
|
|
||||||
"\n\nGot: {}\nWant: {}\n\n",
|
|
||||||
output.to_string(),
|
|
||||||
data
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_std::test]
|
|
||||||
async fn empty_field() {
|
|
||||||
let rec = Record::new();
|
|
||||||
let name = "mull";
|
|
||||||
let output = rec.get_field(name).await;
|
|
||||||
assert!(output == None, "Should return an option.");
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tables {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[async_std::test]
|
|
||||||
async fn new_table() {
|
|
||||||
Table::new().await;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_std::test]
|
|
||||||
async fn update_field() {
|
|
||||||
let table = Table::new().await;
|
|
||||||
let mut expected: HashMap<String, FieldType> = HashMap::new();
|
|
||||||
expected.insert("stan".to_string(), FieldType::Table);
|
|
||||||
expected.insert("lee".to_string(), FieldType::Table);
|
|
||||||
table.update_field("stan", "table").await.unwrap();
|
|
||||||
table.update_field("lee", "table").await.unwrap();
|
|
||||||
let output = table.fields().await;
|
|
||||||
assert!(output == expected, "Table did not get the fields added.");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_std::test]
|
|
||||||
async fn add_bad_field() -> Result<(), String> {
|
|
||||||
let table = Table::new().await;
|
|
||||||
let name = "failure";
|
|
||||||
let bad_type = "ljksdbtt";
|
|
||||||
let expected = format!("failed to add field {}", name);
|
|
||||||
let source = format!("field type {} does not exist", bad_type);
|
|
||||||
match table.update_field(name, bad_type).await {
|
|
||||||
Ok(_) => Err("A bad field type should not return successfully".to_string()),
|
|
||||||
Err(err) => {
|
|
||||||
if format!("{}", err) != expected {
|
|
||||||
Err(format!("Got: '{}' - Want: '{}'", err, expected))
|
|
||||||
} else if format!("{}", err.source().unwrap()) != source {
|
|
||||||
Err(format!(
|
|
||||||
"Got: '{}' - Want: '{}'",
|
|
||||||
err.source().unwrap(),
|
|
||||||
source
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_std::test]
|
|
||||||
async fn add_duplicate_field() -> Result<(), String> {
|
|
||||||
let table = Table::new().await;
|
|
||||||
let name = "twice";
|
|
||||||
let expected = format!("field {} already exists", name);
|
|
||||||
table.update_field(name, "table").await.unwrap();
|
|
||||||
match table.update_field(name, "table").await {
|
|
||||||
Ok(_) => Err(format!("Cannot have two fields with named '{}'", name)),
|
|
||||||
Err(err) => {
|
|
||||||
if format!("{}", err) == expected {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(format!("Got: '{}' - Want: '{}'", err, expected))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod databases {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[async_std::test]
|
|
||||||
async fn new_database() {
|
|
||||||
MoreThanText::new().await;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_std::test]
|
|
||||||
async fn add_table() {
|
|
||||||
let db = MoreThanText::new().await;
|
|
||||||
db.add_table("fred".to_string()).await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod fieldtypes {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
fn get_field_map() -> HashMap<String, FieldType> {
|
|
||||||
let mut fields: HashMap<String, FieldType> = HashMap::new();
|
|
||||||
fields.insert("table".to_string(), FieldType::Table);
|
|
||||||
return fields;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn convert_to_string() {
|
|
||||||
for (key, value) in get_field_map().iter() {
|
|
||||||
assert!(
|
|
||||||
key == &value.to_string(),
|
|
||||||
"\n\nGot: {}\nWant: {}\n\n",
|
|
||||||
value.to_string(),
|
|
||||||
key
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn convert_from_string() {
|
|
||||||
for (key, value) in get_field_map().iter() {
|
|
||||||
assert!(
|
|
||||||
&FieldType::from_str(key).unwrap() == value,
|
|
||||||
"\n\nDid not return a FieldType::{}",
|
|
||||||
key
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn convert_from_string_error() -> Result<(), String> {
|
|
||||||
let ftype = "lkjsdfh";
|
|
||||||
let expected = format!("field type {} does not exist", ftype);
|
|
||||||
match FieldType::from_str(ftype) {
|
|
||||||
Ok(_) => Err(format!("Found field type {}", ftype)),
|
|
||||||
Err(err) => {
|
|
||||||
if format!("{}", err) == expected {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(format!("Got: '{}' - Want: '{}'", err, expected))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
398
src/morethantext/old-mod.rs
Normal file
398
src/morethantext/old-mod.rs
Normal file
@ -0,0 +1,398 @@
|
|||||||
|
/*
|
||||||
|
use async_std::sync::{Arc, RwLock};
|
||||||
|
use std::{collections::HashMap, error::Error, fmt, str::FromStr};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct DBError {
|
||||||
|
detail: String,
|
||||||
|
source: Option<Box<DBError>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DBError {
|
||||||
|
fn new(detail: String) -> Self {
|
||||||
|
Self {
|
||||||
|
detail: detail.to_string(),
|
||||||
|
source: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for DBError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "{}", &self.detail)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for DBError {
|
||||||
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||||
|
match &self.source {
|
||||||
|
Some(err) => Some(err),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq)]
|
||||||
|
pub enum FieldType {
|
||||||
|
Table,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for FieldType {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
FieldType::Table => write!(f, "table"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for FieldType {
|
||||||
|
type Err = DBError;
|
||||||
|
|
||||||
|
fn from_str(input: &str) -> Result<FieldType, Self::Err> {
|
||||||
|
match input {
|
||||||
|
"table" => Ok(FieldType::Table),
|
||||||
|
_ => Err(DBError::new(format!("field type {} does not exist", input))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Table {
|
||||||
|
fields: Arc<RwLock<HashMap<String, FieldType>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Table {
|
||||||
|
pub async fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
fields: Arc::new(RwLock::new(HashMap::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_field(&self, name: &str, ftype: &str) -> Result<(), Box<dyn Error>> {
|
||||||
|
let ftype = match FieldType::from_str(ftype) {
|
||||||
|
Ok(field) => field,
|
||||||
|
Err(err) => {
|
||||||
|
let mut error = DBError::new(format!("failed to add field {}", name));
|
||||||
|
error.source = Some(Box::new(err));
|
||||||
|
return Err(Box::new(error));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut fmap = self.fields.write().await;
|
||||||
|
match fmap.get(name) {
|
||||||
|
Some(_) => Err(Box::new(DBError::new(format!(
|
||||||
|
"field {} already exists",
|
||||||
|
name
|
||||||
|
)))),
|
||||||
|
None => {
|
||||||
|
fmap.insert(name.to_string(), ftype);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn fields(&self) -> HashMap<String, FieldType> {
|
||||||
|
let fmap = self.fields.read().await;
|
||||||
|
fmap.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
use async_std::sync::{Arc, RwLock};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
pub mod error;
|
||||||
|
mod fieldtype;
|
||||||
|
|
||||||
|
use error::MTTError;
|
||||||
|
use fieldtype::FieldType;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct MoreThanText;
|
||||||
|
|
||||||
|
impl MoreThanText {
|
||||||
|
pub async fn new() -> Self {
|
||||||
|
Self {}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn add_table(&self, name: &str) -> Table {
|
||||||
|
Table::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_table(&self, name: &str) -> Table {
|
||||||
|
Table::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq)]
|
||||||
|
struct FieldDef;
|
||||||
|
|
||||||
|
pub struct Table {
|
||||||
|
fields: Arc<RwLock<HashMap<String, FieldDef>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Table {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
fields: Arc::new(RwLock::new(HashMap::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn add_field(&self, name: &str) {
|
||||||
|
let mut field_defs = self.fields.write().await;
|
||||||
|
field_defs.insert(name.to_string(), FieldDef {});
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_field(&self, name: &str) -> Option<FieldDef> {
|
||||||
|
let field_defs = self.fields.read().await;
|
||||||
|
match field_defs.get(name) {
|
||||||
|
Some(def) => Some(def.clone()),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn new_record(&self) -> Record {
|
||||||
|
Record::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Record {
|
||||||
|
data: Arc<RwLock<HashMap<String, FieldType>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Record {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
data: Arc::new(RwLock::new(HashMap::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
async fn update_field(&self, name: String, data: FieldType) {
|
||||||
|
let mut map = self.data.write().await;
|
||||||
|
map.insert(name, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_field(&self, name: &str) -> Option<FieldType> {
|
||||||
|
let map = self.data.read().await;
|
||||||
|
match map.get(name) {
|
||||||
|
Some(field) => Some(field.clone()),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod databases {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn new_database() {
|
||||||
|
MoreThanText::new().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn add_table() {
|
||||||
|
let db = MoreThanText::new().await;
|
||||||
|
let name = "table";
|
||||||
|
db.add_table(name).await;
|
||||||
|
db.get_table(name).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tables {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn new_table() {
|
||||||
|
Table::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn add_field_definition() {
|
||||||
|
let tbl = Table::new();
|
||||||
|
let name = "field";
|
||||||
|
let expected = FieldDef {};
|
||||||
|
tbl.add_field(name).await;
|
||||||
|
let output = tbl.get_field(name).await.unwrap();
|
||||||
|
assert!(output == expected, "Did not return a field definition.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn missing_field_definition() {
|
||||||
|
let tbl = Table::new();
|
||||||
|
let output = tbl.get_field("missing").await;
|
||||||
|
assert!(
|
||||||
|
output == None,
|
||||||
|
"Should return None if field does not exist."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn get_empty_record() {
|
||||||
|
let tbl = Table::new();
|
||||||
|
tbl.new_record().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod records {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
/*
|
||||||
|
#[async_std::test]
|
||||||
|
async fn update_fields() {
|
||||||
|
let rec = Record::new();
|
||||||
|
let name = "elephant";
|
||||||
|
let data = "";
|
||||||
|
let sstr = StaticString::new();
|
||||||
|
rec.update_field(name.to_string(), sstr).await;
|
||||||
|
let output = rec.get_field(name).await.unwrap();
|
||||||
|
assert!(
|
||||||
|
output.to_string() == data,
|
||||||
|
"\n\nGot: {}\nWant: {}\n\n",
|
||||||
|
output.to_string(),
|
||||||
|
data
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn empty_field() {
|
||||||
|
let rec = Record::new();
|
||||||
|
let name = "mull";
|
||||||
|
let output = rec.get_field(name).await;
|
||||||
|
assert!(output == None, "Should return an option.");
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tables {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn new_table() {
|
||||||
|
Table::new().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn update_field() {
|
||||||
|
let table = Table::new().await;
|
||||||
|
let mut expected: HashMap<String, FieldType> = HashMap::new();
|
||||||
|
expected.insert("stan".to_string(), FieldType::Table);
|
||||||
|
expected.insert("lee".to_string(), FieldType::Table);
|
||||||
|
table.update_field("stan", "table").await.unwrap();
|
||||||
|
table.update_field("lee", "table").await.unwrap();
|
||||||
|
let output = table.fields().await;
|
||||||
|
assert!(output == expected, "Table did not get the fields added.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn add_bad_field() -> Result<(), String> {
|
||||||
|
let table = Table::new().await;
|
||||||
|
let name = "failure";
|
||||||
|
let bad_type = "ljksdbtt";
|
||||||
|
let expected = format!("failed to add field {}", name);
|
||||||
|
let source = format!("field type {} does not exist", bad_type);
|
||||||
|
match table.update_field(name, bad_type).await {
|
||||||
|
Ok(_) => Err("A bad field type should not return successfully".to_string()),
|
||||||
|
Err(err) => {
|
||||||
|
if format!("{}", err) != expected {
|
||||||
|
Err(format!("Got: '{}' - Want: '{}'", err, expected))
|
||||||
|
} else if format!("{}", err.source().unwrap()) != source {
|
||||||
|
Err(format!(
|
||||||
|
"Got: '{}' - Want: '{}'",
|
||||||
|
err.source().unwrap(),
|
||||||
|
source
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn add_duplicate_field() -> Result<(), String> {
|
||||||
|
let table = Table::new().await;
|
||||||
|
let name = "twice";
|
||||||
|
let expected = format!("field {} already exists", name);
|
||||||
|
table.update_field(name, "table").await.unwrap();
|
||||||
|
match table.update_field(name, "table").await {
|
||||||
|
Ok(_) => Err(format!("Cannot have two fields with named '{}'", name)),
|
||||||
|
Err(err) => {
|
||||||
|
if format!("{}", err) == expected {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(format!("Got: '{}' - Want: '{}'", err, expected))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod databases {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn new_database() {
|
||||||
|
MoreThanText::new().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn add_table() {
|
||||||
|
let db = MoreThanText::new().await;
|
||||||
|
db.add_table("fred".to_string()).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod fieldtypes {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn get_field_map() -> HashMap<String, FieldType> {
|
||||||
|
let mut fields: HashMap<String, FieldType> = HashMap::new();
|
||||||
|
fields.insert("table".to_string(), FieldType::Table);
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_to_string() {
|
||||||
|
for (key, value) in get_field_map().iter() {
|
||||||
|
assert!(
|
||||||
|
key == &value.to_string(),
|
||||||
|
"\n\nGot: {}\nWant: {}\n\n",
|
||||||
|
value.to_string(),
|
||||||
|
key
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_from_string() {
|
||||||
|
for (key, value) in get_field_map().iter() {
|
||||||
|
assert!(
|
||||||
|
&FieldType::from_str(key).unwrap() == value,
|
||||||
|
"\n\nDid not return a FieldType::{}",
|
||||||
|
key
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_from_string_error() -> Result<(), String> {
|
||||||
|
let ftype = "lkjsdfh";
|
||||||
|
let expected = format!("field type {} does not exist", ftype);
|
||||||
|
match FieldType::from_str(ftype) {
|
||||||
|
Ok(_) => Err(format!("Found field type {}", ftype)),
|
||||||
|
Err(err) => {
|
||||||
|
if format!("{}", err) == expected {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(format!("Got: '{}' - Want: '{}'", err, expected))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
Loading…
Reference in New Issue
Block a user