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 {
|
||||
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 {
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
use crate::{
|
||||
action::Query,
|
||||
document::{
|
||||
definition::{DocDef, DocFuncType},
|
||||
field::Field,
|
||||
@ -6,8 +7,8 @@ use crate::{
|
||||
message::{
|
||||
action::{Action, MsgAction},
|
||||
wrapper::{
|
||||
CalcValue, Calculation, InternalRecord, InternalRecords, Message, Oid, Query, Records,
|
||||
Reply, Update,
|
||||
CalcValue, Calculation, InternalRecord, InternalRecords, Message, Oid, Records, Reply,
|
||||
Update,
|
||||
},
|
||||
},
|
||||
mtterror::MTTError,
|
||||
|
||||
@ -382,7 +382,7 @@ impl DocDef {
|
||||
#[cfg(test)]
|
||||
mod docdefs {
|
||||
use super::*;
|
||||
use crate::message::wrapper::{Query, Update};
|
||||
use crate::{action::Query, message::wrapper::Update};
|
||||
|
||||
#[test]
|
||||
fn can_field_be_added() {
|
||||
|
||||
@ -435,6 +435,10 @@ impl Revision {
|
||||
fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
fn revision(&self) -> isize {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -444,5 +448,6 @@ mod revisions {
|
||||
#[test]
|
||||
fn can_create_empty_revision() {
|
||||
let rev = Revision::new();
|
||||
assert_eq!(rev.revision(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
use crate::{
|
||||
action::Query,
|
||||
document::{
|
||||
clock::Clock,
|
||||
create::IndexType,
|
||||
@ -7,7 +8,7 @@ use crate::{
|
||||
},
|
||||
message::{
|
||||
action::Action,
|
||||
wrapper::{CalcValue, Calculation, Delete, Message, Operand, Query, Update},
|
||||
wrapper::{CalcValue, Calculation, Delete, Message, Operand, Update},
|
||||
},
|
||||
name::{Name, NameType},
|
||||
queue::{
|
||||
@ -88,10 +89,15 @@ impl Session {
|
||||
mod sessions {
|
||||
use super::*;
|
||||
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::{
|
||||
action::MsgAction,
|
||||
wrapper::{Addition, Query, Records},
|
||||
wrapper::{Addition, Records},
|
||||
},
|
||||
mtterror::MTTError,
|
||||
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 mtterror;
|
||||
mod name;
|
||||
mod queue;
|
||||
|
||||
use action::Query;
|
||||
use document::{
|
||||
clock::Clock,
|
||||
create::CreateDoc,
|
||||
definition::DocDef,
|
||||
field::{Field, FieldType},
|
||||
session::Session,
|
||||
};
|
||||
use message::{
|
||||
action::{Action, MsgAction},
|
||||
wrapper::{Addition, CalcValue, Calculation, Message, Operand, Query},
|
||||
wrapper::{Addition, CalcValue, Calculation, Message, Operand},
|
||||
};
|
||||
pub use mtterror::MTTError;
|
||||
use name::{Name, NameType};
|
||||
pub use name::{Name, NameType};
|
||||
use queue::{
|
||||
data_director::{Include, Path, RegMsg, Register},
|
||||
router::Queue,
|
||||
@ -30,6 +33,17 @@ mod support_tests {
|
||||
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)]
|
||||
pub struct MoreThanText {
|
||||
queue: Queue,
|
||||
@ -105,6 +119,16 @@ impl MoreThanText {
|
||||
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> {
|
||||
if name == "page" {
|
||||
Ok("something".to_string())
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
use crate::{
|
||||
action::Query,
|
||||
document::definition::DocDef,
|
||||
message::wrapper::{Addition, Delete, Query, Records, Reply, Update},
|
||||
message::wrapper::{Addition, Delete, Records, Reply, Update},
|
||||
mtterror::MTTError,
|
||||
queue::data_director::Register,
|
||||
UserAction,
|
||||
};
|
||||
|
||||
#[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)]
|
||||
mod msgactions {
|
||||
use super::*;
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
use crate::{
|
||||
action::Query,
|
||||
document::field::{Field, FieldType},
|
||||
message::action::MsgAction,
|
||||
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)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Reply {
|
||||
|
||||
@ -96,11 +96,7 @@ impl Queue {
|
||||
#[cfg(test)]
|
||||
mod routers {
|
||||
use super::*;
|
||||
use crate::{
|
||||
message::{action::MsgAction, wrapper::Query},
|
||||
name::Name,
|
||||
support_tests::TIMEOUT,
|
||||
};
|
||||
use crate::{action::Query, message::action::MsgAction, name::Name, support_tests::TIMEOUT};
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
sync::mpsc::{Receiver, RecvTimeoutError},
|
||||
|
||||
@ -1,19 +1,15 @@
|
||||
use morethantext::{MTTError, MoreThanText};
|
||||
|
||||
#[test]
|
||||
fn get_home_page() {
|
||||
let mtt = MoreThanText::new();
|
||||
match mtt.get_document("page", "home") {
|
||||
Ok(_) => {}
|
||||
Err(err) => assert!(false, "got error {:?}", err),
|
||||
}
|
||||
}
|
||||
use morethantext::{
|
||||
action::Query,
|
||||
document::{definition::DocDef, field::FieldType},
|
||||
MTTError, MoreThanText, Name,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[test]
|
||||
fn errors_on_missing_page() {
|
||||
let mtt = MoreThanText::new();
|
||||
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),
|
||||
Err(err) => match err {
|
||||
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