Removed the unused interprater code.

This commit is contained in:
Jeff Baskin 2022-12-30 22:09:36 -05:00
parent 085972060b
commit bab5c27cee
2 changed files with 0 additions and 185 deletions

View File

@ -1,179 +0,0 @@
use async_graphql::{Context, EmptySubscription, Error, Object, Result, Schema};
use async_std::sync::RwLock;
use serde_json;
mod database;
#[derive(Clone)]
struct Table {
name: String,
}
impl Table {
async fn new(name: String) -> Self {
Self { name: name }
}
}
#[Object]
impl Table {
async fn name(&self) -> String {
self.name.to_string()
}
async fn describe(&self) -> Vec<u64> {
Vec::new()
}
}
struct Query;
#[Object]
impl Query {
async fn table(&self, ctx: &Context<'_>, name: String) -> Result<Option<Table>> {
let tbls = ctx
.data::<RwLock<Vec<Table>>>()
.unwrap()
.read()
.await
.to_vec();
match tbls.binary_search_by(|t| t.name.cmp(&name)) {
Ok(idx) => Ok(Some(tbls[idx].clone())),
Err(_) => Ok(None),
}
}
async fn tables(&self, ctx: &Context<'_>) -> Vec<Table> {
ctx.data::<RwLock<Vec<Table>>>()
.unwrap()
.read()
.await
.to_vec()
}
}
struct Mutation;
#[Object]
impl Mutation {
async fn create_table(&self, ctx: &Context<'_>, name: String) -> Result<Option<Table>> {
let mut tables = ctx.data::<RwLock<Vec<Table>>>().unwrap().write().await;
match tables.binary_search_by(|t| t.name.cmp(&name)) {
Ok(_) => Err(Error::new(format!("Table {} already exists.", &name))),
Err(_) => {
let output = Table::new(name).await;
tables.push(output.clone());
tables.sort_by_key(|k| k.name.clone());
Ok(Some(output))
}
}
}
}
#[derive(Clone)]
pub struct MoreThanText {
schema: Schema<Query, Mutation, EmptySubscription>,
}
impl MoreThanText {
pub fn new() -> Self {
let tables: Vec<Table> = Vec::new();
Self {
schema: Schema::build(Query, Mutation, EmptySubscription)
.data(RwLock::new(tables))
.finish(),
}
}
pub async fn execute(&self, qry: &str) -> String {
let res = self.schema.execute(qry).await;
serde_json::to_string(&res).unwrap()
}
}
#[cfg(test)]
mod support {
use super::*;
pub fn compare(db: &MoreThanText, output: &str, expected: &str) {
assert!(
output == expected,
"\n\n{}\nGot: {}\nWant: {}\n\n",
db.schema.sdl(),
output,
expected
);
}
}
#[cfg(test)]
mod queries {
use super::*;
#[async_std::test]
async fn list_table() {
let db = MoreThanText::new();
db.execute(r#"mutation {createTable(name: "wilma"){name}}"#)
.await;
db.execute(r#"mutation {createTable(name: "betty"){name}}"#)
.await;
let output = db.execute(r#"{table(name: "wilma"){name}}"#).await;
let expected = r#"{"data":{"table":{"name":"wilma"}}}"#;
support::compare(&db, &output, &expected);
}
#[async_std::test]
async fn list_no_table() {
let db = MoreThanText::new();
let output = db.execute(r#"{table(name: "slade"){name}}"#).await;
let expected = r#"{"data":{"table":null}}"#;
support::compare(&db, &output, &expected);
}
#[async_std::test]
async fn list_tables() {
let db = MoreThanText::new();
db.execute(r#"mutation {createTable(name: "fred"){name}}"#)
.await;
db.execute(r#"mutation {createTable(name: "barney"){name}}"#)
.await;
let output = db.execute(r#"{tables{name}}"#).await;
let expected = r#"{"data":{"tables":[{"name":"barney"},{"name":"fred"}]}}"#;
support::compare(&db, &output, &expected);
}
#[async_std::test]
async fn empty_table_description() {
let db = MoreThanText::new();
let output = db
.execute(r#"mutation {createTable(name: "pebbles"){name describe}}"#)
.await;
let expected = r#"{"data":{"createTable":{"name":"pebbles","describe":[]}}}"#;
support::compare(&db, &output, &expected);
}
}
#[cfg(test)]
mod mutations {
use super::*;
#[async_std::test]
async fn add_table() {
let db = MoreThanText::new();
let output = db
.execute(r#"mutation {createTable(name: "william"){name}}"#)
.await;
let expected = r#"{"data":{"createTable":{"name":"william"}}}"#;
support::compare(&db, &output, &expected);
}
#[async_std::test]
async fn cannot_add_duplicate_table() {
let db = MoreThanText::new();
let qry = r#"mutation {createTable(name: "gadzoo"){name}}"#;
db.execute(&qry).await;
let output = db.execute(qry).await;
let expected = r#"{"data":null,"errors":[{"message":"Table gadzoo already exists.","locations":[{"line":1,"column":11}],"path":["createTable"]}]}"#;
support::compare(&db, &output, &expected);
}
}

View File

@ -1,6 +0,0 @@
char = _{ ASCII_ALPHANUMERIC | "_" }
whitespace = _{" " | "\t" | "\r" | "\n"}
name = {char+}
command = {"create database" ~ whitespace+ ~ name ~ ";"}
script = {command+}