use std::collections::{HashMap, HashSet};
use datafusion::arrow::array::{Array, ArrayRef, AsArray, Int64Array, StringArray};
use datafusion::arrow::compute::cast;
use datafusion::arrow::datatypes::{DataType, Int64Type, UInt64Type};
use datafusion::error::{DataFusionError, Result};
use datafusion::prelude::SessionContext;
use ursa_viz::model::{
EdgeBatch, GraphDelta, GraphResult, Key, KeyType, NodeBatch, Op, Provenance, NO_ROW,
};
use super::{plan_err, thousands, RunSql};
use crate::graph::provenance as meta;
pub const EXPAND_LIMIT: usize = 100;
pub const MAX_EXPAND: usize = 5_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
In,
Out,
Both,
}
impl Direction {
pub fn name(self) -> &'static str {
match self {
Direction::In => "in",
Direction::Out => "out",
Direction::Both => "both",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Command {
Expand {
id: String,
direction: Direction,
limit: usize,
},
Hide(Vec<String>),
Show(Vec<String>),
Undo,
Redo,
History,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Parsed {
pub command: Command,
pub sql_only: bool,
pub text: String,
}
fn words(text: &str) -> Result<Vec<String>> {
let mut out = Vec::new();
let mut chars = text.chars().peekable();
while let Some(&c) = chars.peek() {
if c.is_whitespace() {
chars.next();
continue;
}
let mut word = String::new();
if c == '\'' || c == '"' {
chars.next();
loop {
match chars.next() {
None => return plan_err(format!("a quote ({c}) is not closed in: {text}")),
Some(x) if x == c => {
if chars.peek() == Some(&c) {
chars.next();
word.push(c);
} else {
break;
}
}
Some(x) => word.push(x),
}
}
} else {
while let Some(&x) = chars.peek() {
if x.is_whitespace() {
break;
}
word.push(x);
chars.next();
}
}
out.push(word);
}
Ok(out)
}
impl Command {
pub const NAMES: [&'static str; 5] = [".expand", ".hide", ".show", ".undo", ".redo"];
pub fn parse(line: &str) -> Result<Option<Parsed>> {
let text = line.trim().trim_end_matches(';').trim_end();
let (name, rest) = match text.find(char::is_whitespace) {
Some(at) => (&text[..at], text[at..].trim()),
None => (text, ""),
};
let name = name.to_ascii_lowercase();
if name == ".graph" {
if !rest
.split_whitespace()
.next()
.is_some_and(|w| w.eq_ignore_ascii_case("history"))
{
return Ok(None);
}
} else if !Self::NAMES.contains(&name.as_str()) {
return Ok(None);
}
let mut args = words(rest)?;
let before = args.len();
args.retain(|a| a != "--sql");
let sql_only = args.len() != before;
let command = match name.as_str() {
".expand" => {
const USAGE: &str = "usage: .expand ID [in|out|both] [LIMIT n]";
let mut args = args.into_iter();
let Some(id) = args.next() else {
return plan_err(USAGE);
};
let mut direction = None;
let mut limit = None;
while let Some(word) = args.next() {
match word.to_ascii_lowercase().as_str() {
"in" if direction.is_none() => direction = Some(Direction::In),
"out" if direction.is_none() => direction = Some(Direction::Out),
"both" if direction.is_none() => direction = Some(Direction::Both),
"limit" if limit.is_none() => {
let n = args.next().and_then(|n| n.parse::<usize>().ok());
match n {
Some(n) if (1..=MAX_EXPAND).contains(&n) => limit = Some(n),
_ => {
return plan_err(format!(
"LIMIT takes a number of neighbours from 1 to {}; {USAGE}",
thousands(MAX_EXPAND)
))
}
}
}
_ => {
return plan_err(format!(
"'{word}' is not part of .expand (one node at a time); {USAGE}"
))
}
}
}
Command::Expand {
id,
direction: direction.unwrap_or(Direction::Both),
limit: limit.unwrap_or(EXPAND_LIMIT),
}
}
".hide" => {
if args.is_empty() {
return plan_err("usage: .hide ID…");
}
Command::Hide(args)
}
".show" => {
if args.is_empty() {
return plan_err("usage: .show ID… or .show all");
}
if args.len() == 1 && args[0].eq_ignore_ascii_case("all") {
Command::Show(Vec::new())
} else {
Command::Show(args)
}
}
".undo" | ".redo" => {
if !args.is_empty() {
return plan_err(format!("usage: {name}"));
}
if name == ".undo" {
Command::Undo
} else {
Command::Redo
}
}
_ => {
if args.len() != 1 {
return plan_err("usage: .graph history");
}
Command::History
}
};
Ok(Some(Parsed {
command,
sql_only,
text: text.to_string(),
}))
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Step {
pub command: String,
pub sql: Vec<String>,
pub delta: GraphDelta,
pub nodes: i64,
pub edges: i64,
}
impl Step {
pub fn inverse(&self) -> GraphDelta {
self.delta.invert()
}
pub fn summary(&self) -> String {
changed(self.nodes, self.edges)
}
}
fn changed(nodes: i64, edges: i64) -> String {
let part = |n: i64, one: &str| {
let sign = if n < 0 { '-' } else { '+' };
let s = if n.unsigned_abs() == 1 { "" } else { "s" };
format!("{sign}{} {one}{s}", thousands(n.unsigned_abs() as usize))
};
format!("{}, {}", part(nodes, "node"), part(edges, "edge"))
}
#[derive(Debug, Clone, PartialEq)]
pub struct Outcome {
pub text: String,
pub delta: Option<GraphDelta>,
}
impl Outcome {
fn said(text: impl Into<String>) -> Outcome {
Outcome {
text: text.into(),
delta: None,
}
}
}
fn pair(a: u32, b: u32) -> (u32, u32) {
(a.min(b), a.max(b))
}
#[derive(Debug, Default)]
struct EdgeIndex {
rows: HashSet<(u16, u64)>,
pairs: HashMap<(u32, u32), u32>,
}
impl EdgeIndex {
fn of(result: &GraphResult) -> EdgeIndex {
let mut index = EdgeIndex::default();
let (src, dst, label) = (result.src(), result.dst(), result.edge_label());
for i in 0..result.n_edges() {
index.add(src[i], dst[i], label[i], result.edge_row(i));
}
index
}
fn add(&mut self, src: u32, dst: u32, label: u16, row: Option<u64>) {
match row {
Some(row) => {
self.rows.insert((label, row));
}
None => *self.pairs.entry(pair(src, dst)).or_insert(0) += 1,
}
}
fn remove(&mut self, src: u32, dst: u32, label: u16, row: Option<u64>) {
match row {
Some(row) => {
self.rows.remove(&(label, row));
}
None => {
let pair = pair(src, dst);
if let Some(count) = self.pairs.get_mut(&pair) {
*count -= 1;
if *count == 0 {
self.pairs.remove(&pair);
}
}
}
}
}
fn track(&mut self, delta: &GraphDelta) {
let each = |batch: &EdgeBatch, f: &mut dyn FnMut(u32, u32, u16, Option<u64>)| {
for i in 0..batch.len() {
let row = batch
.rows
.as_ref()
.map(|rows| rows[i])
.filter(|&r| r != NO_ROW);
f(batch.src[i], batch.dst[i], batch.labels[i], row);
}
};
for op in &delta.ops {
match op {
Op::AddEdges(batch) => each(batch, &mut |s, d, l, r| self.add(s, d, l, r)),
Op::PopEdges(batch) => each(batch, &mut |s, d, l, r| self.remove(s, d, l, r)),
_ => {}
}
}
}
}
enum Ids {
Int(Int64Array),
Text(StringArray),
}
impl Ids {
fn of(array: &ArrayRef) -> Result<Ids> {
let integers = matches!(
array.data_type(),
DataType::Int8
| DataType::Int16
| DataType::Int32
| DataType::Int64
| DataType::UInt8
| DataType::UInt16
| DataType::UInt32
);
if integers {
let cast = cast(array, &DataType::Int64)?;
Ok(Ids::Int(cast.as_primitive::<Int64Type>().clone()))
} else {
let cast = cast(array, &DataType::Utf8)?;
Ok(Ids::Text(cast.as_string::<i32>().clone()))
}
}
fn key(&self, i: usize, ty: KeyType) -> Option<Key> {
match (self, ty) {
(Ids::Int(a), _) if a.is_null(i) => None,
(Ids::Text(a), _) if a.is_null(i) => None,
(Ids::Int(a), KeyType::Int) => Some(Key::Int(a.value(i))),
(Ids::Int(a), KeyType::Text) => Some(Key::Text(a.value(i).to_string())),
(Ids::Text(a), ty) => Key::parse(ty, a.value(i)).ok(),
}
}
}
fn literal(out: &mut String, key: &str, ty: KeyType) {
match ty {
KeyType::Int => out.push_str(key),
KeyType::Text => {
out.push('\'');
out.push_str(&key.replace('\'', "''"));
out.push('\'');
}
}
}
fn model(e: ursa_viz::model::ModelError) -> DataFusionError {
DataFusionError::Plan(format!("the graph cannot be changed: {e}"))
}
#[derive(Debug)]
pub struct Session {
result: GraphResult,
steps: Vec<Step>,
done: usize,
edges: Option<EdgeIndex>,
degrees: Vec<u32>,
}
const NO_DEGREE: u32 = u32::MAX;
const DEGREES_BY_KEY: usize = 1_000;
impl Session {
pub fn new(base: GraphResult) -> Session {
Session {
result: base,
steps: Vec::new(),
done: 0,
edges: None,
degrees: Vec::new(),
}
}
pub fn result(&self) -> &GraphResult {
&self.result
}
pub fn steps(&self) -> &[Step] {
&self.steps
}
pub fn applied(&self) -> usize {
self.done
}
pub fn shown(&self) -> (usize, usize) {
let r = &self.result;
let nodes = r.node_flags().iter().filter(|&&f| f == 0).count();
let edges = (0..r.n_edges()).filter(|&i| r.edge_is_shown(i)).count();
(nodes, edges)
}
pub async fn not_shown(&mut self, ctx: &SessionContext, sql: &dyn RunSql) -> Result<Vec<u32>> {
let Some(graph) = self.result.graph() else {
return Ok(Vec::new());
};
let n = self.result.n_nodes();
self.degrees.truncate(n);
let known = self.degrees.len();
if known < n {
let graph = format!("\"{}\"", graph.replace('"', "\"\""));
let mut query = format!("SELECT id, degree FROM degree({graph}, direction => 'both')");
let one_type = (known..n).all(|i| self.key_type(i) == self.key_type(known));
if n - known <= DEGREES_BY_KEY && one_type {
query.push_str(" WHERE id IN (");
for i in known..n {
if i > known {
query.push_str(", ");
}
literal(&mut query, self.result.node_keys().get(i), self.key_type(i));
}
query.push(')');
}
let (_, batches) = sql.run(ctx, &query).await?;
let r = &self.result;
let mut degrees = vec![NO_DEGREE; n - known];
for batch in &batches {
let ids = Ids::of(batch.column(0))?;
let degree = cast(batch.column(1), &DataType::UInt64)?;
let degree = degree.as_primitive::<UInt64Type>();
for i in 0..batch.num_rows() {
let found = r.node_labels().iter().zip(0u16..).find_map(|(l, at)| {
let key = ids.key(i, l.key)?;
r.node(at, &key)
});
if let Some(at) = found.filter(|&at| at as usize >= known) {
degrees[at as usize - known] =
degree.value(i).min(u64::from(u32::MAX - 1)) as u32;
}
}
}
self.degrees.extend(degrees);
}
let r = &self.result;
let mut shown = vec![0u32; n];
let (src, dst) = (r.src(), r.dst());
for e in 0..r.n_edges() {
if r.edge_is_shown(e) {
shown[src[e] as usize] += 1;
shown[dst[e] as usize] += 1;
}
}
Ok((0..n)
.map(|i| match self.degrees[i] {
NO_DEGREE => 0,
_ if r.node_is_removed(i) => 0,
degree => degree.saturating_sub(shown[i]),
})
.collect())
}
fn find(&self, id: &str) -> Option<u32> {
let r = &self.result;
r.node_labels().iter().zip(0u16..).find_map(|(label, l)| {
let key = Key::parse(label.key, id).ok()?;
r.node(l, &key)
})
}
fn resolve(&self, id: &str) -> Result<u32> {
match self.find(id) {
Some(node) => Ok(node),
None => plan_err(format!(
"no node '{id}' in the graph that is drawn; a node is named by its key, as the \
graph view shows it"
)),
}
}
fn key_type(&self, i: usize) -> KeyType {
self.result.node_labels()[self.result.node_label()[i] as usize].key
}
fn keys(&self, hidden_too: bool) -> String {
let r = &self.result;
let mut out = String::with_capacity(r.n_nodes() * 8);
for i in 0..r.n_nodes() {
if !hidden_too && r.node_is_removed(i) {
continue;
}
if !out.is_empty() {
out.push_str(", ");
}
literal(&mut out, r.node_keys().get(i), self.key_type(i));
}
out
}
fn step(&mut self, command: &str, sql: Vec<String>, delta: GraphDelta) -> Result<Outcome> {
let before = self.shown();
self.result.apply(&delta).map_err(model)?;
self.degrees.truncate(self.result.n_nodes());
if let Some(edges) = &mut self.edges {
edges.track(&delta);
}
let after = self.shown();
let step = Step {
command: command.to_string(),
sql,
delta: delta.clone(),
nodes: after.0 as i64 - before.0 as i64,
edges: after.1 as i64 - before.1 as i64,
};
let text = step.summary();
self.steps.truncate(self.done);
self.steps.push(step);
self.done += 1;
Ok(Outcome {
text,
delta: Some(delta),
})
}
pub async fn run(
&mut self,
ctx: &SessionContext,
sql: &dyn RunSql,
line: &str,
) -> Result<Outcome> {
let Some(parsed) = Command::parse(line)? else {
return plan_err(format!(
"'{}' is not an explore command; they are .expand, .hide, .show, .undo, .redo \
and .graph history",
line.trim()
));
};
let Parsed {
command,
sql_only,
text,
} = parsed;
match command {
Command::Expand {
id,
direction,
limit,
} => {
self.expand(ctx, sql, &text, &id, direction, limit, sql_only)
.await
}
_ if sql_only => {
let name = text.split_whitespace().next().unwrap_or_default();
Ok(Outcome::said(format!(
"-- {name} runs no SQL: it changes what is shown of the graph"
)))
}
Command::Hide(ids) => self.set_hidden(&text, &ids, true),
Command::Show(ids) => self.set_hidden(&text, &ids, false),
Command::Undo => self.undo(),
Command::Redo => self.redo(),
Command::History => Ok(Outcome::said(self.history())),
}
}
fn set_hidden(&mut self, command: &str, ids: &[String], hide: bool) -> Result<Outcome> {
let r = &self.result;
let mut nodes: Vec<u32> = if ids.is_empty() {
(0..r.n_nodes() as u32).collect()
} else {
let mut nodes = Vec::with_capacity(ids.len());
for id in ids {
nodes.push(self.resolve(id)?);
}
nodes
};
nodes.sort_unstable();
nodes.dedup();
nodes.retain(|&i| r.node_is_removed(i as usize) != hide);
if nodes.is_empty() {
let (state, named) = if hide {
("hidden", ids.join(", "))
} else if ids.is_empty() {
return Ok(Outcome::said("Nothing changed: no node is hidden"));
} else {
("shown", ids.join(", "))
};
let verb = if ids.len() == 1 { "is" } else { "are" };
return Ok(Outcome::said(format!(
"Nothing changed: {named} {verb} already {state}"
)));
}
let op = if hide {
Op::RemoveNodes(nodes)
} else {
Op::RestoreNodes(nodes)
};
self.step(command, Vec::new(), GraphDelta::new(vec![op]))
}
fn undo(&mut self) -> Result<Outcome> {
if self.done == 0 {
return Ok(Outcome::said("Nothing to undo"));
}
let step = &self.steps[self.done - 1];
let delta = step.inverse();
let text = format!(
"Undid {}: {}",
step.command,
changed(-step.nodes, -step.edges)
);
self.result.apply(&delta).map_err(model)?;
self.degrees.truncate(self.result.n_nodes());
if let Some(edges) = &mut self.edges {
edges.track(&delta);
}
self.done -= 1;
Ok(Outcome {
text,
delta: Some(delta),
})
}
fn redo(&mut self) -> Result<Outcome> {
let Some(step) = self.steps.get(self.done) else {
return Ok(Outcome::said("Nothing to redo"));
};
let delta = step.delta.clone();
let text = format!("Redid {}: {}", step.command, step.summary());
self.result.apply(&delta).map_err(model)?;
self.degrees.truncate(self.result.n_nodes());
if let Some(edges) = &mut self.edges {
edges.track(&delta);
}
self.done += 1;
Ok(Outcome {
text,
delta: Some(delta),
})
}
fn history(&self) -> String {
let r = &self.result;
let (nodes, edges) = self.shown();
let count = |n: usize, one: &str| {
let s = if n == 1 { "" } else { "s" };
format!("{} {one}{s}", thousands(n))
};
let mut out = match r.provenance().sql.as_deref() {
Some(sql) => format!("Explored from: {}", sql.replace('\n', " ")),
None => match r.graph() {
Some(graph) => format!("Explored from: the graph {graph}"),
None => "Explored from: a result".to_string(),
},
};
if self.steps.is_empty() {
out.push_str("\nNo steps yet: .expand ID adds a node's neighbours");
}
let width = self.steps.len().to_string().len();
let longest = self
.steps
.iter()
.map(|s| s.command.chars().count())
.max()
.unwrap_or(0);
for (i, step) in self.steps.iter().enumerate() {
let pad = " ".repeat(longest - step.command.chars().count());
let undone = if i < self.done { "" } else { " (undone)" };
out.push_str(&format!(
"\n{:>width$} {}{pad} {}{undone}",
i + 1,
step.command,
step.summary()
));
}
out.push_str(&format!(
"\nShown: {}, {}",
count(nodes, "node"),
count(edges, "edge")
));
let hidden = r.n_nodes() - nodes;
if hidden > 0 {
out.push_str(&format!(" ({} hidden)", thousands(hidden)));
}
out
}
#[allow(clippy::too_many_arguments)]
async fn expand(
&mut self,
ctx: &SessionContext,
sql: &dyn RunSql,
command: &str,
id: &str,
direction: Direction,
limit: usize,
sql_only: bool,
) -> Result<Outcome> {
let node = self.resolve(id)? as usize;
let Some(graph) = self.result.graph() else {
return plan_err(
"this drawing is not of a property graph, so there are no neighbours to add; \
draw a graph's nodes first: .graph NAME, or .graph after a query on one \
(hop, a path, an algorithm)",
);
};
if self.result.node_is_removed(node) {
return plan_err(format!("node {id} is hidden; .show {id} first"));
}
let graph = format!("\"{}\"", graph.replace('"', "\"\""));
let mut seed = String::new();
literal(
&mut seed,
self.result.node_keys().get(node),
self.key_type(node),
);
let neighbours = format!(
"SELECT h.id, count(*) OVER () AS neighbours \
FROM hop({graph}, seeds => [{seed}], steps => 1, direction => '{}') AS h \
JOIN degree({graph}, direction => 'both') AS d ON d.id = h.id \
WHERE h.id NOT IN ({}) \
ORDER BY d.degree DESC, h.id LIMIT {limit}",
direction.name(),
self.keys(false),
);
let (schema, batches) = sql.run(ctx, &neighbours).await?;
let named = schema.field(0).metadata().get(meta::LABEL);
let r = &self.result;
let label = named
.and_then(|name| r.node_labels().iter().position(|l| &l.name == name))
.map_or(r.node_label()[node], |l| l as u16);
let key_type = r.node_labels()[label as usize].key;
let mut restored: Vec<u32> = Vec::new();
let mut added = NodeBatch::new();
let mut new: HashMap<Key, u32> = HashMap::new();
let mut not_shown = 0usize;
for batch in &batches {
let ids = Ids::of(batch.column(0))?;
let total = cast(batch.column(1), &DataType::UInt64)?;
let total = total.as_primitive::<UInt64Type>();
for i in 0..batch.num_rows() {
not_shown = not_shown.max(total.value(i) as usize);
let found = r.node_labels().iter().zip(0u16..).find_map(|(l, at)| {
let key = ids.key(i, l.key)?;
r.node(at, &key)
});
match found {
Some(at) if r.node_is_removed(at as usize) => restored.push(at),
Some(_) => {}
None => {
let Some(key) = ids.key(i, key_type) else {
continue;
};
let at = (r.n_nodes() + added.len()) as u32;
if new.insert(key.clone(), at).is_none() {
added.push(label, &key);
}
}
}
}
}
restored.sort_unstable();
restored.dedup();
let mut all = self.keys(true);
for key in added.keys.iter() {
if !all.is_empty() {
all.push_str(", ");
}
literal(&mut all, key, key_type);
}
let among = format!("SELECT src, dst, edge FROM induced_edges({graph}, nodes => [{all}])");
drop(all);
if sql_only {
return Ok(Outcome::said(format!("{neighbours};\n{among};")));
}
let (schema, batches) = sql.run(ctx, &among).await?;
let edges = self.edges.get_or_insert_with(|| EdgeIndex::of(r));
let edge_label = schema.field(2).metadata().get(meta::LABEL);
let at = edge_label.and_then(|name| r.edge_labels().iter().position(|l| l == name));
let new_label = match (edge_label, at) {
(Some(name), None) => Some(name.clone()),
_ => None,
};
let e_label = match (&new_label, at) {
(Some(_), _) => r.edge_labels().len() as u16,
(None, at) => at.unwrap_or(0) as u16,
};
let node_of = |ids: &Ids, i: usize| -> Option<u32> {
let found = r.node_labels().iter().zip(0u16..).find_map(|(l, at)| {
let key = ids.key(i, l.key)?;
r.node(at, &key)
});
found.or_else(|| new.get(&ids.key(i, key_type)?).copied())
};
let mut batch = EdgeBatch::new();
let mut rows = Vec::new();
let mut matched: HashMap<(u32, u32), u32> = HashMap::new();
for b in &batches {
let (s, d) = (Ids::of(b.column(0))?, Ids::of(b.column(1))?);
let row = cast(b.column(2), &DataType::UInt64)?;
let row = row.as_primitive::<UInt64Type>();
for i in 0..b.num_rows() {
let (Some(s), Some(d)) = (node_of(&s, i), node_of(&d, i)) else {
continue;
};
let row = row.is_valid(i).then(|| row.value(i));
if row.is_some_and(|row| edges.rows.contains(&(e_label, row))) {
continue;
}
if let Some(&have) = edges.pairs.get(&pair(s, d)) {
let used = matched.entry(pair(s, d)).or_insert(0);
if *used < have {
*used += 1;
continue;
}
}
batch.src.push(s);
batch.dst.push(d);
batch.labels.push(e_label);
rows.push(row.unwrap_or(NO_ROW));
}
}
batch.rows = Some(rows);
let mut ops = Vec::new();
if !restored.is_empty() {
ops.push(Op::RestoreNodes(restored));
}
let new_nodes = added.len();
if !added.is_empty() {
ops.push(Op::AddNodes(added));
}
if !batch.is_empty() {
if let Some(name) = new_label {
ops.push(Op::AddEdgeLabel(name));
}
ops.push(Op::AddEdges(batch));
}
if ops.is_empty() {
let whose = match direction {
Direction::Both => "neighbour",
Direction::In => "in-neighbour",
Direction::Out => "out-neighbour",
};
return Ok(Outcome::said(format!(
"Nothing changed: every {whose} of {id} is shown already, with the edges among \
them"
)));
}
let statements = vec![neighbours, among];
let delta = GraphDelta::new(ops).with_provenance(Provenance {
sql: Some(statements.join(";\n")),
..Provenance::default()
});
let mut outcome = self.step(command, statements, delta)?;
let brought = new_nodes + outcome_restored(&outcome);
if not_shown > brought {
let more = not_shown - brought;
let are = if more == 1 {
"neighbour of {id} is"
} else {
"neighbours of {id} are"
};
outcome.text.push_str(&format!(
" ({} more {} not shown: .expand {id} again adds the next)",
thousands(more),
are.replace("{id}", id)
));
}
Ok(outcome)
}
}
fn outcome_restored(outcome: &Outcome) -> usize {
outcome.delta.as_ref().map_or(0, |delta| {
delta
.ops
.iter()
.map(|op| match op {
Op::RestoreNodes(nodes) => nodes.len(),
_ => 0,
})
.sum()
})
}
#[cfg(test)]
mod tests {
use super::*;
fn parsed(line: &str) -> Command {
Command::parse(line).unwrap().unwrap().command
}
#[test]
fn commands_are_read_from_their_lines() {
assert_eq!(
parsed(".expand 33"),
Command::Expand {
id: "33".into(),
direction: Direction::Both,
limit: EXPAND_LIMIT
}
);
assert_eq!(
parsed(" .EXPAND 'a b' In limit 7 ; "),
Command::Expand {
id: "a b".into(),
direction: Direction::In,
limit: 7
}
);
assert_eq!(
parsed(".hide 1 \"it's\" 'o''neil'"),
Command::Hide(vec!["1".into(), "it's".into(), "o'neil".into()])
);
assert_eq!(parsed(".show ALL"), Command::Show(Vec::new()));
assert_eq!(
parsed(".show all 3"),
Command::Show(vec!["all".into(), "3".into()])
);
assert_eq!(parsed(".undo;"), Command::Undo);
assert_eq!(parsed(".redo"), Command::Redo);
assert_eq!(parsed(".graph History"), Command::History);
let sql = Command::parse(".expand 5 --sql out").unwrap().unwrap();
assert!(sql.sql_only);
assert_eq!(sql.text, ".expand 5 --sql out");
assert_eq!(
sql.command,
Command::Expand {
id: "5".into(),
direction: Direction::Out,
limit: EXPAND_LIMIT
}
);
}
#[test]
fn other_lines_are_not_explore_commands() {
for line in [
".graph",
".graph karate",
".graph SELECT history FROM t",
".tables",
"SELECT 1",
"",
] {
assert_eq!(Command::parse(line).unwrap(), None, "{line}");
}
for line in [
".expand",
".expand 1 2",
".expand 1 in out",
".expand 1 LIMIT",
".expand 1 LIMIT x",
".hide",
".hide 'open",
".redo 2",
] {
assert!(Command::parse(line).is_err(), "{line}");
}
}
#[test]
fn changes_are_signed_and_counted() {
assert_eq!(changed(16, 41), "+16 nodes, +41 edges");
assert_eq!(changed(-1, -1), "-1 node, -1 edge");
assert_eq!(changed(0, 1200), "+0 nodes, +1,200 edges");
}
}