Moved Query into separate actions.
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
This commit is contained in:
parent
7f7e10a373
commit
5ffb79b7f2
3
src/action.rs
Normal file
3
src/action.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
mod query;
|
||||||
|
|
||||||
|
pub use query::Query;
|
||||||
81
src/action/query.rs
Normal file
81
src/action/query.rs
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
use crate::{message::wrapper::Calculation, name::NameType};
|
||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Query {
|
||||||
|
data: HashMap<NameType, Calculation>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Query {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
data: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add<NT>(&mut self, name: NT, operation: Calculation)
|
||||||
|
where
|
||||||
|
NT: Into<NameType>,
|
||||||
|
{
|
||||||
|
self.data.insert(name.into(), operation);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn get<NT>(&self, name: NT) -> Option<Calculation>
|
||||||
|
where
|
||||||
|
NT: Into<NameType>,
|
||||||
|
{
|
||||||
|
match self.data.get(&name.into()) {
|
||||||
|
Some(calc) => Some(calc.clone()),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn field_ids(&self) -> HashSet<&NameType> {
|
||||||
|
self.data.keys().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = (&NameType, &Calculation)> {
|
||||||
|
self.data.iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod queries {
|
||||||
|
use super::*;
|
||||||
|
use crate::{document::field::Field, message::wrapper::Operand, name::Name};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn holds_calculation_to_run_query() {
|
||||||
|
let name = Name::english(Uuid::new_v4().to_string().as_str());
|
||||||
|
let data = Uuid::new_v4();
|
||||||
|
let mut bad_data = data.clone();
|
||||||
|
while bad_data == data {
|
||||||
|
bad_data = Uuid::new_v4();
|
||||||
|
}
|
||||||
|
let mut query = Query::new();
|
||||||
|
let mut calc = Calculation::new(Operand::Equal);
|
||||||
|
calc.add_value(data.clone()).unwrap();
|
||||||
|
query.add(name.clone(), calc);
|
||||||
|
match query.get(&name) {
|
||||||
|
Some(op) => {
|
||||||
|
let expected: Field = true.into();
|
||||||
|
let mut holder = op.clone();
|
||||||
|
holder.add_value(data).unwrap();
|
||||||
|
assert_eq!(holder.calculate(&Field::None), expected);
|
||||||
|
}
|
||||||
|
None => unreachable!("should have returned a calculation"),
|
||||||
|
}
|
||||||
|
match query.get(&name) {
|
||||||
|
Some(op) => {
|
||||||
|
let expected: Field = false.into();
|
||||||
|
let mut holder = op.clone();
|
||||||
|
holder.add_value(bad_data).unwrap();
|
||||||
|
assert_eq!(holder.calculate(&Field::None), expected);
|
||||||
|
}
|
||||||
|
None => unreachable!("should have returned a calculation"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -29,7 +29,10 @@ impl Clock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn gen_message() -> Message {
|
fn gen_message() -> Message {
|
||||||
Message::new(Clock::get_name(), MsgAction::OnUpdate(Records::new(Names::new())))
|
Message::new(
|
||||||
|
Clock::get_name(),
|
||||||
|
MsgAction::OnUpdate(Records::new(Names::new())),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_path() -> Path {
|
pub fn get_path() -> Path {
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
|
action::Query,
|
||||||
document::{
|
document::{
|
||||||
definition::{DocDef, DocFuncType},
|
definition::{DocDef, DocFuncType},
|
||||||
field::Field,
|
field::Field,
|
||||||
@ -6,8 +7,8 @@ use crate::{
|
|||||||
message::{
|
message::{
|
||||||
action::{Action, MsgAction},
|
action::{Action, MsgAction},
|
||||||
wrapper::{
|
wrapper::{
|
||||||
CalcValue, Calculation, InternalRecord, InternalRecords, Message, Oid, Query, Records,
|
CalcValue, Calculation, InternalRecord, InternalRecords, Message, Oid, Records, Reply,
|
||||||
Reply, Update,
|
Update,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mtterror::MTTError,
|
mtterror::MTTError,
|
||||||
|
|||||||
@ -382,7 +382,7 @@ impl DocDef {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod docdefs {
|
mod docdefs {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::message::wrapper::{Query, Update};
|
use crate::{action::Query, message::wrapper::Update};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_field_be_added() {
|
fn can_field_be_added() {
|
||||||
|
|||||||
@ -435,6 +435,10 @@ impl Revision {
|
|||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Self {}
|
Self {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn revision(&self) -> isize {
|
||||||
|
0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -444,5 +448,6 @@ mod revisions {
|
|||||||
#[test]
|
#[test]
|
||||||
fn can_create_empty_revision() {
|
fn can_create_empty_revision() {
|
||||||
let rev = Revision::new();
|
let rev = Revision::new();
|
||||||
|
assert_eq!(rev.revision(), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
|
action::Query,
|
||||||
document::{
|
document::{
|
||||||
clock::Clock,
|
clock::Clock,
|
||||||
create::IndexType,
|
create::IndexType,
|
||||||
@ -7,7 +8,7 @@ use crate::{
|
|||||||
},
|
},
|
||||||
message::{
|
message::{
|
||||||
action::Action,
|
action::Action,
|
||||||
wrapper::{CalcValue, Calculation, Delete, Message, Operand, Query, Update},
|
wrapper::{CalcValue, Calculation, Delete, Message, Operand, Update},
|
||||||
},
|
},
|
||||||
name::{Name, NameType},
|
name::{Name, NameType},
|
||||||
queue::{
|
queue::{
|
||||||
@ -88,10 +89,15 @@ impl Session {
|
|||||||
mod sessions {
|
mod sessions {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{
|
use crate::{
|
||||||
document::{clock::{Clock, clock_test_support::gen_clock_message}, create::CreateDoc, field::Field},
|
action::Query,
|
||||||
|
document::{
|
||||||
|
clock::{clock_test_support::gen_clock_message, Clock},
|
||||||
|
create::CreateDoc,
|
||||||
|
field::Field,
|
||||||
|
},
|
||||||
message::{
|
message::{
|
||||||
action::MsgAction,
|
action::MsgAction,
|
||||||
wrapper::{Addition, Query, Records},
|
wrapper::{Addition, Records},
|
||||||
},
|
},
|
||||||
mtterror::MTTError,
|
mtterror::MTTError,
|
||||||
name::{Name, NameType, Names},
|
name::{Name, NameType, Names},
|
||||||
|
|||||||
30
src/lib.rs
30
src/lib.rs
@ -1,21 +1,24 @@
|
|||||||
mod document;
|
pub mod action;
|
||||||
|
pub mod document;
|
||||||
mod message;
|
mod message;
|
||||||
mod mtterror;
|
mod mtterror;
|
||||||
mod name;
|
mod name;
|
||||||
mod queue;
|
mod queue;
|
||||||
|
|
||||||
|
use action::Query;
|
||||||
use document::{
|
use document::{
|
||||||
clock::Clock,
|
clock::Clock,
|
||||||
create::CreateDoc,
|
create::CreateDoc,
|
||||||
|
definition::DocDef,
|
||||||
field::{Field, FieldType},
|
field::{Field, FieldType},
|
||||||
session::Session,
|
session::Session,
|
||||||
};
|
};
|
||||||
use message::{
|
use message::{
|
||||||
action::{Action, MsgAction},
|
action::{Action, MsgAction},
|
||||||
wrapper::{Addition, CalcValue, Calculation, Message, Operand, Query},
|
wrapper::{Addition, CalcValue, Calculation, Message, Operand},
|
||||||
};
|
};
|
||||||
pub use mtterror::MTTError;
|
pub use mtterror::MTTError;
|
||||||
use name::{Name, NameType};
|
pub use name::{Name, NameType};
|
||||||
use queue::{
|
use queue::{
|
||||||
data_director::{Include, Path, RegMsg, Register},
|
data_director::{Include, Path, RegMsg, Register},
|
||||||
router::Queue,
|
router::Queue,
|
||||||
@ -30,6 +33,17 @@ mod support_tests {
|
|||||||
pub static TIMEOUT: Duration = Duration::from_millis(500);
|
pub static TIMEOUT: Duration = Duration::from_millis(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum UserAction {
|
||||||
|
CreateDocument(DocDef),
|
||||||
|
Query(Query),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<DocDef> for UserAction {
|
||||||
|
fn from(value: DocDef) -> Self {
|
||||||
|
Self::CreateDocument(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct MoreThanText {
|
pub struct MoreThanText {
|
||||||
queue: Queue,
|
queue: Queue,
|
||||||
@ -105,6 +119,16 @@ impl MoreThanText {
|
|||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn request<UA>(&mut self, req: UA)
|
||||||
|
where
|
||||||
|
UA: Into<UserAction>,
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_document(&mut self, docdef: DocDef) -> Result<(), MTTError> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_document(&self, name: &str, id: &str) -> Result<String, MTTError> {
|
pub fn get_document(&self, name: &str, id: &str) -> Result<String, MTTError> {
|
||||||
if name == "page" {
|
if name == "page" {
|
||||||
Ok("something".to_string())
|
Ok("something".to_string())
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
|
action::Query,
|
||||||
document::definition::DocDef,
|
document::definition::DocDef,
|
||||||
message::wrapper::{Addition, Delete, Query, Records, Reply, Update},
|
message::wrapper::{Addition, Delete, Records, Reply, Update},
|
||||||
mtterror::MTTError,
|
mtterror::MTTError,
|
||||||
queue::data_director::Register,
|
queue::data_director::Register,
|
||||||
|
UserAction,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
@ -123,6 +125,15 @@ impl From<Update> for MsgAction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<UserAction> for MsgAction {
|
||||||
|
fn from(value: UserAction) -> Self {
|
||||||
|
match value {
|
||||||
|
UserAction::CreateDocument(data) => Self::Create(data),
|
||||||
|
UserAction::Query(data) => Self::Query(data),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod msgactions {
|
mod msgactions {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
|
action::Query,
|
||||||
document::field::{Field, FieldType},
|
document::field::{Field, FieldType},
|
||||||
message::action::MsgAction,
|
message::action::MsgAction,
|
||||||
mtterror::MTTError,
|
mtterror::MTTError,
|
||||||
@ -1008,84 +1009,6 @@ impl From<HashSet<Oid>> for QueryType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct Query {
|
|
||||||
data: HashMap<NameType, Calculation>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Query {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
data: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add<NT>(&mut self, name: NT, operation: Calculation)
|
|
||||||
where
|
|
||||||
NT: Into<NameType>,
|
|
||||||
{
|
|
||||||
self.data.insert(name.into(), operation);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn get<NT>(&self, name: NT) -> Option<Calculation>
|
|
||||||
where
|
|
||||||
NT: Into<NameType>,
|
|
||||||
{
|
|
||||||
match self.data.get(&name.into()) {
|
|
||||||
Some(calc) => Some(calc.clone()),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn field_ids(&self) -> HashSet<&NameType> {
|
|
||||||
self.data.keys().collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn iter(&self) -> impl Iterator<Item = (&NameType, &Calculation)> {
|
|
||||||
self.data.iter()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod queries {
|
|
||||||
use super::*;
|
|
||||||
use crate::name::Name;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn holds_calculation_to_run_query() {
|
|
||||||
let name = Name::english(Uuid::new_v4().to_string().as_str());
|
|
||||||
let data = Uuid::new_v4();
|
|
||||||
let mut bad_data = data.clone();
|
|
||||||
while bad_data == data {
|
|
||||||
bad_data = Uuid::new_v4();
|
|
||||||
}
|
|
||||||
let mut query = Query::new();
|
|
||||||
let mut calc = Calculation::new(Operand::Equal);
|
|
||||||
calc.add_value(data.clone()).unwrap();
|
|
||||||
query.add(name.clone(), calc);
|
|
||||||
match query.get(&name) {
|
|
||||||
Some(op) => {
|
|
||||||
let expected: Field = true.into();
|
|
||||||
let mut holder = op.clone();
|
|
||||||
holder.add_value(data).unwrap();
|
|
||||||
assert_eq!(holder.calculate(&Field::None), expected);
|
|
||||||
}
|
|
||||||
None => unreachable!("should have returned a calculation"),
|
|
||||||
}
|
|
||||||
match query.get(&name) {
|
|
||||||
Some(op) => {
|
|
||||||
let expected: Field = false.into();
|
|
||||||
let mut holder = op.clone();
|
|
||||||
holder.add_value(bad_data).unwrap();
|
|
||||||
assert_eq!(holder.calculate(&Field::None), expected);
|
|
||||||
}
|
|
||||||
None => unreachable!("should have returned a calculation"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Reply {
|
pub struct Reply {
|
||||||
|
|||||||
@ -96,11 +96,7 @@ impl Queue {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod routers {
|
mod routers {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{
|
use crate::{action::Query, message::action::MsgAction, name::Name, support_tests::TIMEOUT};
|
||||||
message::{action::MsgAction, wrapper::Query},
|
|
||||||
name::Name,
|
|
||||||
support_tests::TIMEOUT,
|
|
||||||
};
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
sync::mpsc::{Receiver, RecvTimeoutError},
|
sync::mpsc::{Receiver, RecvTimeoutError},
|
||||||
|
|||||||
@ -1,19 +1,15 @@
|
|||||||
use morethantext::{MTTError, MoreThanText};
|
use morethantext::{
|
||||||
|
action::Query,
|
||||||
#[test]
|
document::{definition::DocDef, field::FieldType},
|
||||||
fn get_home_page() {
|
MTTError, MoreThanText, Name,
|
||||||
let mtt = MoreThanText::new();
|
};
|
||||||
match mtt.get_document("page", "home") {
|
use uuid::Uuid;
|
||||||
Ok(_) => {}
|
|
||||||
Err(err) => assert!(false, "got error {:?}", err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn errors_on_missing_page() {
|
fn errors_on_missing_page() {
|
||||||
let mtt = MoreThanText::new();
|
let mtt = MoreThanText::new();
|
||||||
let doc_name = "missing";
|
let doc_name = "missing";
|
||||||
match mtt.get_document(doc_name, "home") {
|
match mtt.get_document(doc_name, "something") {
|
||||||
Ok(data) => assert!(false, "got '{}', should have been not found error", data),
|
Ok(data) => assert!(false, "got '{}', should have been not found error", data),
|
||||||
Err(err) => match err {
|
Err(err) => match err {
|
||||||
MTTError::DocumentNotFound(result) => assert_eq!(result, doc_name),
|
MTTError::DocumentNotFound(result) => assert_eq!(result, doc_name),
|
||||||
@ -21,3 +17,13 @@ fn errors_on_missing_page() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn can_create_document() {
|
||||||
|
let mut mtt = MoreThanText::new();
|
||||||
|
let doc_name = Name::english(Uuid::new_v4().to_string().as_str());
|
||||||
|
let mut docdef = DocDef::new(doc_name.clone());
|
||||||
|
let field_name = Name::english(Uuid::new_v4().to_string().as_str());
|
||||||
|
docdef.add_field(field_name.clone(), FieldType::Uuid);
|
||||||
|
mtt.request(docdef);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user