Make test compile

This commit is contained in:
Yuriy Dupyn 2024-02-01 14:57:45 +01:00
parent fbd7bf1f72
commit 22a2730b96
4 changed files with 128 additions and 143 deletions

View file

@ -2,7 +2,7 @@ use crate::internals::table::Table;
use crate::operation::{ColumnSelection, Condition, Operation}; use crate::operation::{ColumnSelection, Condition, Operation};
use crate::restricted_row::RestrictedRow; use crate::restricted_row::RestrictedRow;
use crate::result::DbResult; use crate::result::DbResult;
use crate::schema::{TableName, TablePosition, TableSchema}; use crate::schema::{Column, TableName, TablePosition, TableSchema};
use bimap::BiMap; use bimap::BiMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -212,9 +212,6 @@ mod tests {
#[test] #[test]
fn test_insert_select_basic1() { fn test_insert_select_basic1() {
use IndexableValue::*;
use Value::*;
let mut state = State::new(); let mut state = State::new();
let users_schema = users_schema(); let users_schema = users_schema();
let users = 0; let users = 0;
@ -224,9 +221,9 @@ mod tests {
.unwrap(); .unwrap();
let (id, name, age) = ( let (id, name, age) = (
Indexable(Uuid(0)), Value::Uuid(0),
Indexable(String("Plato".to_string())), Value::String("Plato".to_string()),
Indexable(Int(64)), Value::Int(64),
); );
state state
.interpret(Operation::Insert( .interpret(Operation::Insert(
@ -256,9 +253,7 @@ mod tests {
#[test] #[test]
fn test_insert_select_basic2() { fn test_insert_select_basic2() {
use Condition::*; use Condition::*;
use IndexableValue::*;
use Operation::*; use Operation::*;
use Value::*;
let mut state = State::new(); let mut state = State::new();
let users_schema = users_schema(); let users_schema = users_schema();
@ -270,9 +265,9 @@ mod tests {
state.interpret(CreateTable(users_schema.clone())).unwrap(); state.interpret(CreateTable(users_schema.clone())).unwrap();
let (id0, name0, age0) = ( let (id0, name0, age0) = (
Indexable(Uuid(0)), Value::Uuid(0),
Indexable(String("Plato".to_string())), Value::String("Plato".to_string()),
Indexable(Int(64)), Value::Int(64),
); );
state state
.interpret(Insert( .interpret(Insert(
@ -282,9 +277,9 @@ mod tests {
.unwrap(); .unwrap();
let (id1, name1, age1) = ( let (id1, name1, age1) = (
Indexable(Uuid(1)), Value::Uuid(1),
Indexable(String("Aristotle".to_string())), Value::String("Aristotle".to_string()),
Indexable(Int(20)), Value::Int(20),
); );
state state
.interpret(Insert( .interpret(Insert(
@ -366,9 +361,7 @@ mod tests {
#[test] #[test]
fn test_delete() { fn test_delete() {
use Condition::*; use Condition::*;
use IndexableValue::*;
use Operation::*; use Operation::*;
use Value::*;
let mut state = State::new(); let mut state = State::new();
let users_schema = users_schema(); let users_schema = users_schema();
@ -379,9 +372,9 @@ mod tests {
state.interpret(CreateTable(users_schema.clone())).unwrap(); state.interpret(CreateTable(users_schema.clone())).unwrap();
let (id0, name0, age0) = ( let (id0, name0, age0) = (
Indexable(Uuid(0)), Value::Uuid(0),
Indexable(String("Plato".to_string())), Value::String("Plato".to_string()),
Indexable(Int(64)), Value::Int(64),
); );
state state
.interpret(Insert( .interpret(Insert(
@ -391,9 +384,9 @@ mod tests {
.unwrap(); .unwrap();
let (id1, name1, age1) = ( let (id1, name1, age1) = (
Indexable(Uuid(1)), Value::Uuid(1),
Indexable(String("Aristotle".to_string())), Value::String("Aristotle".to_string()),
Indexable(Int(20)), Value::Int(20),
); );
state state
.interpret(Insert( .interpret(Insert(
@ -429,9 +422,7 @@ mod tests {
#[test] #[test]
fn test_index() { fn test_index() {
use IndexableValue::*;
use Operation::*; use Operation::*;
use Value::*;
let mut state = State::new(); let mut state = State::new();
let users_schema = users_schema(); let users_schema = users_schema();
@ -446,9 +437,9 @@ mod tests {
.unwrap(); .unwrap();
let (id0, name0, age0) = ( let (id0, name0, age0) = (
Indexable(Uuid(0)), Value::Uuid(0),
Indexable(String("Plato".to_string())), Value::String("Plato".to_string()),
Indexable(Int(64)), Value::Int(64),
); );
state state
.interpret(Insert( .interpret(Insert(
@ -458,9 +449,9 @@ mod tests {
.unwrap(); .unwrap();
let (id1, name1, age1) = ( let (id1, name1, age1) = (
Indexable(Uuid(1)), Value::Uuid(1),
Indexable(String("Aristotle".to_string())), Value::String("Aristotle".to_string()),
Indexable(Int(20)), Value::Int(20),
); );
state state
.interpret(Insert( .interpret(Insert(
@ -482,7 +473,7 @@ mod tests {
let aristotle_id = 1; let aristotle_id = 1;
let plato_ids = index let plato_ids = index
.get(&String("Plato".to_string())) .get(&IndexableValue::String("Plato".to_string()))
.cloned() .cloned()
.unwrap_or(HashSet::new()); .unwrap_or(HashSet::new());
assert!(plato_ids.contains(&plato_id)); assert!(plato_ids.contains(&plato_id));
@ -491,103 +482,98 @@ mod tests {
} }
} }
// TODO pub fn example() {
// pub fn example() { use crate::type_system::{DbType, Value};
// use crate::type_system::{DbType, IndexableValue, Value}; use Condition::*;
// use Condition::*; use Operation::*;
// use IndexableValue::*;
// use Operation::*;
// use Value::*;
// let id_column: Column = 0; let id_column: Column = 0;
// let name_column: Column = 1; let name_column: Column = 1;
// // let age_column: ColumnPosition = 2; // let age_column: ColumnPosition = 2;
// let users_schema: TableSchema = { let users_schema: TableSchema = {
// TableSchema::new( TableSchema::new(
// "users".to_string(), "users".to_string(),
// "id".to_string(), "id".to_string(),
// vec![ vec![
// "id".to_string(), // 0 "id".to_string(), // 0
// "name".to_string(), // 1 "name".to_string(), // 1
// "age".to_string(), // 2 "age".to_string(), // 2
// ], ],
// vec![DbType::Uuid, DbType::String, DbType::Int], vec![DbType::Uuid, DbType::String, DbType::Int],
// ) )
// }; };
// let users_position: TablePosition = 0; let users_position: TablePosition = 0;
// let mut state = State::new(); let mut state = State::new();
// state state
// .interpret(Operation::CreateTable(users_schema.clone())) .interpret(Operation::CreateTable(users_schema.clone()))
// .unwrap(); .unwrap();
// let (id0, name0, age0) = ( let (id0, name0, age0) = (
// Indexable(Uuid(0)), Value::Uuid(0),
// Indexable(String("Plato".to_string())), Value::String("Plato".to_string()),
// Indexable(Int(64)), Value::Int(64),
// ); );
// println!("==INSERT Plato=="); println!("==INSERT Plato==");
// state state
// .interpret(Insert( .interpret(Insert(
// users_position, users_position,
// vec![id0.clone(), name0.clone(), age0.clone()], vec![id0.clone(), name0.clone(), age0.clone()],
// )) ))
// .unwrap(); .unwrap();
// let (id1, name1, age1) = ( let (id1, name1, age1) = (
// Indexable(Uuid(1)), Value::Uuid(1),
// Indexable(String("Aristotle".to_string())), Value::String("Aristotle".to_string()),
// Indexable(Int(20)), Value::Int(20),
// ); );
// println!("==INSERT Aristotle=="); println!("==INSERT Aristotle==");
// state state
// .interpret(Insert( .interpret(Insert(
// users_position, users_position,
// vec![id1.clone(), name1.clone(), age1.clone()], vec![id1.clone(), name1.clone(), age1.clone()],
// )) ))
// .unwrap(); .unwrap();
// println!(); println!();
// { {
// let response: Response = state let response: Response = state
// .interpret(Operation::Select( .interpret(Operation::Select(
// users_position, users_position,
// users_schema.all_selection(), users_schema.all_selection(),
// None, None,
// )) ))
// .unwrap(); .unwrap();
// println!("==SELECT ALL=="); println!("==SELECT ALL==");
// println!("{:?}", response); println!("{:?}", response);
// println!(); println!();
// } }
// { {
// let response: Response = state let response: Response = state
// .interpret(Select( .interpret(Select(
// users_position, users_position,
// users_schema.all_selection(), users_schema.all_selection(),
// Some(Eq(id_column, id0.clone())), Some(Eq(id_column, id0.clone())),
// )) ))
// .unwrap(); .unwrap();
// println!("==SELECT Plato=="); println!("==SELECT Plato==");
// println!("{:?}", response); println!("{:?}", response);
// println!(); println!();
// } }
// { {
// { {
// // TODO: Why do I have to write these braces explicitely? Why doesn't Rust compiler let _delete_response: Response = state
// // "infer" them? .interpret(Delete(users_position, Some(Eq(id_column, id0.clone()))))
// let _delete_response: Response = state .unwrap();
// .interpret(Delete(users_position, Some(Eq(id_column, id0.clone())))) println!("==DELETE Plato==");
// .unwrap(); }
// println!("==DELETE Plato=="); let response: Response = state
// } .interpret(Select(users_position, vec![name_column, id_column], None))
// let response: Response = state .unwrap();
// .interpret(Select(users_position, vec![name_column, id_column], None)) println!("==SELECT All==");
// .unwrap(); println!("{:?}", response);
// println!("==SELECT All=="); println!();
// println!("{:?}", response); }
// println!(); }
// }
// }

View file

@ -239,9 +239,8 @@ impl TryFrom<String> for Value {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{IndexableValue, Value}; use super::Value;
use crate::error::TypeConversionError::UnknownType; use crate::error::TypeConversionError::UnknownType;
use crate::type_system::Value::{Indexable, Number};
#[test] #[test]
fn test_encode_number() { fn test_encode_number() {
@ -261,7 +260,7 @@ mod tests {
#[test] #[test]
fn test_encode_string() { fn test_encode_string() {
let value = Value::Indexable(IndexableValue::String("hello".to_string())); let value = Value::String("hello".to_string());
let vtype = value.to_type(); let vtype = value.to_type();
let oid = vtype.type_oid(); let oid = vtype.type_oid();
let size = vtype.type_size(); let size = vtype.type_size();
@ -277,7 +276,7 @@ mod tests {
#[test] #[test]
fn test_encode_string_utf8() { fn test_encode_string_utf8() {
let value = Value::Indexable(IndexableValue::String("#速度与激情9 早上好中国 现在我有冰激淋 我很喜欢冰激淋 但是《速度与激情9》比冰激淋 🍧🍦🍨".to_string())); let value = Value::String("#速度与激情9 早上好中国 现在我有冰激淋 我很喜欢冰激淋 但是《速度与激情9》比冰激淋 🍧🍦🍨".to_string());
let vtype = value.to_type(); let vtype = value.to_type();
let oid = vtype.type_oid(); let oid = vtype.type_oid();
let size = vtype.type_size(); let size = vtype.type_size();
@ -293,7 +292,7 @@ mod tests {
#[test] #[test]
fn test_encode_int() { fn test_encode_int() {
let value = Value::Indexable(IndexableValue::Int(123)); let value = Value::Int(123);
let vtype = value.to_type(); let vtype = value.to_type();
let oid = vtype.type_oid(); let oid = vtype.type_oid();
let size = vtype.type_size(); let size = vtype.type_size();
@ -309,7 +308,7 @@ mod tests {
#[test] #[test]
fn test_encode_uuid() { fn test_encode_uuid() {
let value = Value::Indexable(IndexableValue::Uuid(123)); let value = Value::Uuid(123);
let vtype = value.to_type(); let vtype = value.to_type();
let oid = vtype.type_oid(); let oid = vtype.type_oid();
let size = vtype.type_size(); let size = vtype.type_size();
@ -325,7 +324,7 @@ mod tests {
#[test] #[test]
fn test_mismatched_size() { fn test_mismatched_size() {
let value = Value::Indexable(IndexableValue::Uuid(123)); let value = Value::Uuid(123);
let vtype = value.to_type(); let vtype = value.to_type();
let oid = vtype.type_oid(); let oid = vtype.type_oid();
let size = 8; let size = 8;
@ -342,13 +341,13 @@ mod tests {
#[test] #[test]
fn test_value_stringification() { fn test_value_stringification() {
let pairs = vec![ let pairs = vec![
(Number(1.0), "Number(1)"), (Value::Number(1.0), "Number(1)"),
( (
Indexable(IndexableValue::String("hello".to_string())), Value::String("hello".to_string()),
"Indexable(String(hello))", "Indexable(String(hello))",
), ),
(Indexable(IndexableValue::Int(123)), "Indexable(Int(123))"), (Value::Int(123), "Indexable(Int(123))"),
(Indexable(IndexableValue::Uuid(123)), "Indexable(Uuid(123))"), (Value::Uuid(123), "Indexable(Uuid(123))"),
]; ];
for (value, string) in pairs { for (value, string) in pairs {

View file

@ -82,11 +82,11 @@ mod tests {
#[test] #[test]
fn test_parse_equality() { fn test_parse_equality() {
use minisql::type_system::{IndexableValue, Value}; use minisql::type_system::Value;
match parse_equality("id = 1") { match parse_equality("id = 1") {
Ok(("", Condition::Eq(column_name, value))) => { Ok(("", Condition::Eq(column_name, value))) => {
assert!(column_name.eq("id")); assert!(column_name.eq("id"));
assert_eq!(value, Value::Indexable(IndexableValue::Int(1))) assert_eq!(value, Value::Int(1))
} }
_ => { _ => {
panic!("should parse"); panic!("should parse");

View file

@ -57,7 +57,7 @@ pub fn parse_values(input: &str) -> IResult<&str, Vec<Value>> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use minisql::type_system::{IndexableValue, Value}; use minisql::type_system::Value;
use super::parse_insert; use super::parse_insert;
use crate::syntax::RawQuerySyntax; use crate::syntax::RawQuerySyntax;
@ -72,10 +72,10 @@ mod tests {
assert_eq!( assert_eq!(
insertion_values, insertion_values,
vec![ vec![
("id".to_string(), Value::Indexable(IndexableValue::Int(1))), ("id".to_string(), Value::Int(1)),
( (
"data".to_string(), "data".to_string(),
Value::Indexable(IndexableValue::String("Text".to_string())) Value::String("Text".to_string())
) )
] ]
); );
@ -97,10 +97,10 @@ mod tests {
assert_eq!( assert_eq!(
insertion_values, insertion_values,
vec![ vec![
("id".to_string(), Value::Indexable(IndexableValue::Int(1))), ("id".to_string(), Value::Int(1)),
( (
"data".to_string(), "data".to_string(),
Value::Indexable(IndexableValue::String("Text".to_string())) Value::String("Text".to_string())
) )
] ]
); );