Skip to content

Commit

Permalink
test(cognitarium): put expression under testing
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Aug 22, 2024
1 parent 41a0a78 commit 73df19c
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 26 deletions.
167 changes: 167 additions & 0 deletions contracts/axone-cognitarium/src/querier/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ impl PartialOrd<Term> for Term {
#[cfg(test)]
mod tests {
use super::*;
use crate::querier::variable::ResolvedVariable;
use crate::state::{InMemoryNamespaceSolver, Node, Object};
use std::collections::BTreeSet;

#[test]
Expand Down Expand Up @@ -216,6 +218,171 @@ mod tests {
}
}

#[test]
fn expression_evaluate() {
let cases = vec![
(
Expression::Constant(Term::Boolean(true)),
Ok(Term::Boolean(true)),
),
(
Expression::Variable(0),
Ok(Term::String("http:://example.com/foo".to_string())),
),
(
Expression::Variable(1),
Err(StdError::not_found("Namespace")),
),
(
Expression::Variable(12),
Err(StdError::generic_err("Unbound filter variable")),
),
(
Expression::And(vec![
Expression::Constant(Term::Boolean(true)),
Expression::Constant(Term::Boolean(true)),
]),
Ok(Term::Boolean(true)),
),
(Expression::And(vec![]), Ok(Term::Boolean(true))),
(
Expression::And(vec![
Expression::Constant(Term::Boolean(true)),
Expression::Constant(Term::Boolean(false)),
]),
Ok(Term::Boolean(false)),
),
(
Expression::Or(vec![
Expression::Constant(Term::Boolean(true)),
Expression::Constant(Term::Boolean(false)),
]),
Ok(Term::Boolean(true)),
),
(Expression::Or(vec![]), Ok(Term::Boolean(false))),
(
Expression::Or(vec![
Expression::Constant(Term::Boolean(false)),
Expression::Constant(Term::Boolean(false)),
]),
Ok(Term::Boolean(false)),
),
(
Expression::Equal(
Box::new(Expression::Constant(Term::String("foo".to_string()))),
Box::new(Expression::Constant(Term::String("foo".to_string()))),
),
Ok(Term::Boolean(true)),
),
(
Expression::Equal(
Box::new(Expression::Constant(Term::String("foo".to_string()))),
Box::new(Expression::Constant(Term::String("bar".to_string()))),
),
Ok(Term::Boolean(false)),
),
(
Expression::Greater(
Box::new(Expression::Constant(Term::String("1".to_string()))),
Box::new(Expression::Constant(Term::String("1".to_string()))),
),
Ok(Term::Boolean(false)),
),
(
Expression::Greater(
Box::new(Expression::Constant(Term::String("2".to_string()))),
Box::new(Expression::Constant(Term::String("1".to_string()))),
),
Ok(Term::Boolean(true)),
),
(
Expression::GreaterOrEqual(
Box::new(Expression::Constant(Term::String("1".to_string()))),
Box::new(Expression::Constant(Term::String("2".to_string()))),
),
Ok(Term::Boolean(false)),
),
(
Expression::GreaterOrEqual(
Box::new(Expression::Constant(Term::String("1".to_string()))),
Box::new(Expression::Constant(Term::String("1".to_string()))),
),
Ok(Term::Boolean(true)),
),
(
Expression::GreaterOrEqual(
Box::new(Expression::Constant(Term::String("2".to_string()))),
Box::new(Expression::Constant(Term::String("1".to_string()))),
),
Ok(Term::Boolean(true)),
),
(
Expression::Less(
Box::new(Expression::Constant(Term::String("1".to_string()))),
Box::new(Expression::Constant(Term::String("2".to_string()))),
),
Ok(Term::Boolean(true)),
),
(
Expression::Less(
Box::new(Expression::Constant(Term::String("1".to_string()))),
Box::new(Expression::Constant(Term::String("1".to_string()))),
),
Ok(Term::Boolean(false)),
),
(
Expression::LessOrEqual(
Box::new(Expression::Constant(Term::String("1".to_string()))),
Box::new(Expression::Constant(Term::String("2".to_string()))),
),
Ok(Term::Boolean(true)),
),
(
Expression::LessOrEqual(
Box::new(Expression::Constant(Term::String("1".to_string()))),
Box::new(Expression::Constant(Term::String("1".to_string()))),
),
Ok(Term::Boolean(true)),
),
(
Expression::LessOrEqual(
Box::new(Expression::Constant(Term::String("2".to_string()))),
Box::new(Expression::Constant(Term::String("1".to_string()))),
),
Ok(Term::Boolean(false)),
),
(
Expression::Not(Box::new(Expression::Constant(Term::Boolean(true)))),
Ok(Term::Boolean(false)),
),
(
Expression::Not(Box::new(Expression::Constant(Term::Boolean(false)))),
Ok(Term::Boolean(true)),
),
];

let mut vars = ResolvedVariables::with_capacity(2);
vars.merge_index(
0,
ResolvedVariable::Object(Object::Named(Node {
namespace: 0,
value: "foo".to_string(),
})),
);
vars.merge_index(
1,
ResolvedVariable::Object(Object::Named(Node {
namespace: 12,
value: "foo".to_string(),
})),
);

let mut ns_solver = InMemoryNamespaceSolver::with(vec![(0, "http:://example.com/")]);
for case in cases {
assert_eq!(case.0.evaluate(&vars, &mut ns_solver), case.1);
}
}

#[test]
fn term_from_iri() {
let cases = vec![
Expand Down
29 changes: 3 additions & 26 deletions contracts/axone-cognitarium/src/querier/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub trait HasBoundVariables {
#[cfg(test)]
mod tests {
use super::*;
use crate::state::{Literal, Namespace, Node};
use crate::state::{InMemoryNamespaceSolver, Literal, Node};
use cosmwasm_std::StdError;

#[test]
Expand Down Expand Up @@ -246,29 +246,6 @@ mod tests {
}
}

struct TestNamespaceSolver;
impl NamespaceSolver for TestNamespaceSolver {
fn resolve_from_key(&mut self, key: u128) -> StdResult<Namespace> {
match key {
0 => Ok(Namespace {
key: 0,
value: "foo".to_string(),
counter: 1,
}),
1 => Ok(Namespace {
key: 1,
value: "bar".to_string(),
counter: 1,
}),
_ => Err(StdError::not_found("Namespace")),
}
}

fn resolve_from_val(&mut self, _value: String) -> StdResult<Namespace> {
Err(StdError::not_found("Namespace"))
}
}

#[test]
fn values() {
let cases = vec![
Expand Down Expand Up @@ -380,7 +357,7 @@ mod tests {
];

let mut id_issuer = IdentifierIssuer::new("b", 0u128);
let mut ns_solver = TestNamespaceSolver;
let mut ns_solver = InMemoryNamespaceSolver::with(vec![(0, "foo"), (1, "bar")]);
for (var, expected) in cases {
assert_eq!(var.as_value(&mut ns_solver, &mut id_issuer), expected)
}
Expand Down Expand Up @@ -511,7 +488,7 @@ mod tests {
),
];

let mut ns_solver = TestNamespaceSolver;
let mut ns_solver = InMemoryNamespaceSolver::with(vec![(0, "foo"), (1, "bar")]);
for (var, expected) in cases {
assert_eq!(var.as_term(&mut ns_solver), expected)
}
Expand Down
5 changes: 5 additions & 0 deletions contracts/axone-cognitarium/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ pub use blank_nodes::*;
pub use namespaces::*;
pub use store::*;
pub use triples::*;

#[cfg(test)]
mod test_util;
#[cfg(test)]
pub use test_util::*;
41 changes: 41 additions & 0 deletions contracts/axone-cognitarium/src/state/test_util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::state::{Namespace, NamespaceSolver};
use cosmwasm_std::{StdError, StdResult};
use std::collections::BTreeMap;

pub struct InMemoryNamespaceSolver {
by_val: BTreeMap<String, Namespace>,
by_key: BTreeMap<u128, Namespace>,
}

impl InMemoryNamespaceSolver {
pub fn with(namespaces: Vec<(u128, &str)>) -> Self {
let mut by_val = BTreeMap::new();
let mut by_key = BTreeMap::new();
for (key, value) in namespaces {
let ns = Namespace {
value: value.to_string(),
key,
counter: 1,
};
by_val.insert(value.to_string(), ns.clone());
by_key.insert(key, ns);
}
Self { by_val, by_key }
}
}

impl NamespaceSolver for InMemoryNamespaceSolver {
fn resolve_from_key(&mut self, key: u128) -> StdResult<Namespace> {
self.by_key
.get(&key)
.ok_or_else(|| StdError::not_found("Namespace"))
.cloned()
}

fn resolve_from_val(&mut self, _value: String) -> StdResult<Namespace> {
self.by_val
.get(&_value)
.ok_or_else(|| StdError::not_found("Namespace"))
.cloned()
}
}

0 comments on commit 73df19c

Please sign in to comment.