Skip to content

Commit

Permalink
test: add unit and integration tests
Browse files Browse the repository at this point in the history
Co-Authored-By: irenjj <renj.jiang@gmail.com>
  • Loading branch information
CookiePieWw and irenjj committed Nov 7, 2024
1 parent ee68f5d commit 2e49660
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/datatypes/src/schema/column_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,10 @@ pub struct FulltextOptions {

impl fmt::Display for FulltextOptions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "enable: {}", self.enable)?;
write!(f, "enable={}", self.enable)?;
if self.enable {
write!(f, ", analyzer: {}", self.analyzer)?;
write!(f, ", case_sensitive: {}", self.case_sensitive)?;
write!(f, ", analyzer={}", self.analyzer)?;
write!(f, ", case_sensitive={}", self.case_sensitive)?;
}
Ok(())
}
Expand Down
55 changes: 55 additions & 0 deletions src/sql/src/parsers/alter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ mod tests {
use std::assert_matches::assert_matches;

use common_error::ext::ErrorExt;
use datatypes::schema::{FulltextAnalyzer, FulltextOptions};
use sqlparser::ast::{ColumnOption, DataType};

use super::*;
Expand Down Expand Up @@ -550,4 +551,58 @@ mod tests {
)
.unwrap_err();
}

#[test]
fn test_parse_alter_column_fulltext() {
let sql = "ALTER TABLE test_table MODIFY COLUMN a SET FULLTEXT WITH(enable='true',analyzer='English',case_sensitive='false')";
let mut result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap();

assert_eq!(1, result.len());
let statement = result.remove(0);
assert_matches!(statement, Statement::Alter { .. });
match statement {
Statement::Alter(alter_table) => {
assert_eq!("test_table", alter_table.table_name().0[0].value);

let alter_operation = alter_table.alter_operation();
assert_matches!(
alter_operation,
AlterTableOperation::ChangeColumnFulltext { .. }
);
match alter_operation {
AlterTableOperation::ChangeColumnFulltext {
column_name,
options,
} => {
assert_eq!("a", column_name.value);
assert_eq!(
FulltextOptions {
enable: true,
analyzer: FulltextAnalyzer::English,
case_sensitive: false
},
*options
);
}
_ => unreachable!(),
}
}
_ => unreachable!(),
}

let invalid_sql = "ALTER TABLE test_table MODIFY COLUMN a SET FULLTEXT WITH('abcd'='true')";
let result = ParserContext::create_with_dialect(
invalid_sql,
&GreptimeDbDialect {},
ParseOptions::default(),
)
.unwrap_err();
let err = result.to_string();
assert_eq!(
err,
"Invalid column option, column name: a, error: invalid FULLTEXT option: abcd"
);
}
}
21 changes: 21 additions & 0 deletions src/sql/src/statements/alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,5 +242,26 @@ ALTER TABLE monitor RENAME monitor_new"#,
unreachable!();
}
}

let sql = "ALTER TABLE monitor MODIFY COLUMN a SET FULLTEXT WITH(enable='true',analyzer='English',case_sensitive='false')";
let stmts =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap();
assert_eq!(1, stmts.len());
assert_matches!(&stmts[0], Statement::Alter { .. });

match &stmts[0] {
Statement::Alter(set) => {
let new_sql = format!("\n{}", set);
assert_eq!(
r#"
ALTER TABLE monitor MODIFY COLUMN a SET FULLTEXT WITH(enable=true, analyzer=English, case_sensitive=false)"#,
&new_sql
);
}
_ => {
unreachable!();
}
}
}
}
26 changes: 26 additions & 0 deletions src/store-api/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,32 @@ mod test {
.column_schema
.data_type;
assert_eq!(ConcreteDataType::string_datatype(), *b_type);

let mut builder = RegionMetadataBuilder::from_existing(metadata);
builder
.alter(AlterKind::ChangeColumnFulltext {
column_name: "b".to_string(),
options: FulltextOptions {
enable: true,
analyzer: datatypes::schema::FulltextAnalyzer::Chinese,
case_sensitive: true,
},
})
.unwrap();
let metadata = builder.build().unwrap();
let a_fulltext_options = metadata
.column_by_name("b")
.unwrap()
.column_schema
.fulltext_options()
.unwrap()
.unwrap();
assert!(a_fulltext_options.enable);
assert_eq!(
datatypes::schema::FulltextAnalyzer::Chinese,
a_fulltext_options.analyzer
);
assert!(a_fulltext_options.case_sensitive);
}

#[test]
Expand Down
21 changes: 20 additions & 1 deletion src/store-api/src/region_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ mod tests {
use api::v1::region::RegionColumnDef;
use api::v1::{ColumnDataType, ColumnDef};
use datatypes::prelude::ConcreteDataType;
use datatypes::schema::ColumnSchema;
use datatypes::schema::{ColumnSchema, FulltextAnalyzer};

use super::*;
use crate::metadata::RegionMetadataBuilder;
Expand Down Expand Up @@ -1187,4 +1187,23 @@ mod tests {

assert!(create.validate().is_err());
}

#[test]
fn test_validate_change_column_fulltext_options() {
let kind = AlterKind::ChangeColumnFulltext {
column_name: "tag_0".to_string(),
options: FulltextOptions {
enable: true,
analyzer: FulltextAnalyzer::Chinese,
case_sensitive: false,
},
};
let request = RegionAlterRequest {
schema_version: 1,
kind,
};
let mut metadata = new_metadata();
metadata.schema_version = 1;
request.validate(&metadata).unwrap();
}
}
51 changes: 51 additions & 0 deletions src/table/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,4 +1420,55 @@ mod tests {
assert_eq!(&[0, 1], &new_meta.primary_key_indices[..]);
assert_eq!(&[2, 3, 4], &new_meta.value_indices[..]);
}

#[test]
fn test_alter_column_fulltext_options() {
let schema = Arc::new(new_test_schema());
let meta = TableMetaBuilder::default()
.schema(schema)
.primary_key_indices(vec![0])
.engine("engine")
.next_column_id(3)
.build()
.unwrap();

let alter_kind = AlterKind::ChangeColumnFulltext {
column_name: "col1".to_string(),
options: FulltextOptions::default(),
};
let err = meta
.builder_with_alter_kind("my_table", &alter_kind, false)
.err()
.unwrap();
assert_eq!("Failed to set fulltext options for column col1, reason: column col1 is not a string type, but Int32(Int32Type)", err.to_string());

// Add a string column and make it fulltext indexed
let new_meta = add_columns_to_meta_with_location(&meta);
assert_eq!(meta.region_numbers, new_meta.region_numbers);

let alter_kind = AlterKind::ChangeColumnFulltext {
column_name: "my_tag_first".to_string(),
options: FulltextOptions {
enable: true,
analyzer: datatypes::schema::FulltextAnalyzer::Chinese,
case_sensitive: true,
},
};
let new_meta = new_meta
.builder_with_alter_kind("my_table", &alter_kind, false)
.unwrap()
.build()
.unwrap();
let column_schema = new_meta
.schema
.column_schema_by_name("my_tag_first")
.unwrap();
let fulltext_options = column_schema.fulltext_options().unwrap().unwrap();
assert!(fulltext_options.enable);
assert_eq!(
datatypes::schema::FulltextAnalyzer::Chinese,
fulltext_options.analyzer
);
assert!(fulltext_options.case_sensitive);
}
}
Loading

0 comments on commit 2e49660

Please sign in to comment.