-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support checking error message for statement & bump to 0.8.0 #102
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ tempfile = "3" | |
thiserror = "1" | ||
futures = "0.3" | ||
libtest-mimic = "0.6" | ||
regex = "1.7.0" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,29 +111,47 @@ impl TestError { | |
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum RecordKind { | ||
Statement, | ||
Query, | ||
} | ||
|
||
impl std::fmt::Display for RecordKind { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
RecordKind::Statement => write!(f, "statement"), | ||
RecordKind::Query => write!(f, "query"), | ||
} | ||
} | ||
} | ||
|
||
/// The error kind for running sqllogictest. | ||
#[derive(thiserror::Error, Debug, Clone)] | ||
pub enum TestErrorKind { | ||
#[error("parse error: {0}")] | ||
ParseError(ParseErrorKind), | ||
#[error("statement is expected to fail, but actually succeed:\n[SQL] {sql}")] | ||
StatementOk { sql: String }, | ||
#[error("statement failed: {err}\n[SQL] {sql}")] | ||
StatementFail { | ||
#[error("{kind} is expected to fail, but actually succeed:\n[SQL] {sql}")] | ||
Ok { sql: String, kind: RecordKind }, | ||
#[error("{kind} failed: {err}\n[SQL] {sql}")] | ||
Fail { | ||
sql: String, | ||
err: Arc<dyn std::error::Error + Send + Sync>, | ||
kind: RecordKind, | ||
}, | ||
#[error("{kind} is expected to fail with error:\n\t\x1b[91m{expected_err}\x1b[0m\nbut got error:\n\t\x1b[91m{err}\x1b[0m\n[SQL] {sql}")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be better if this can be controlled by flags, otherwise will mess up people who pipe the output to a file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. A similar problem: #104 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix later |
||
ErrorMismatch { | ||
sql: String, | ||
err: Arc<dyn std::error::Error + Send + Sync>, | ||
expected_err: String, | ||
kind: RecordKind, | ||
}, | ||
#[error("statement is expected to affect {expected} rows, but actually {actual}\n[SQL] {sql}")] | ||
StatementResultMismatch { | ||
sql: String, | ||
expected: u64, | ||
actual: String, | ||
}, | ||
#[error("query failed: {err}\n[SQL] {sql}")] | ||
QueryFail { | ||
sql: String, | ||
err: Arc<dyn std::error::Error + Send + Sync>, | ||
}, | ||
#[error("query result mismatch:\n[SQL] {sql}\n[Diff]\n{}", difference::Changeset::new(.expected, .actual, "\n"))] | ||
QueryResultMismatch { | ||
sql: String, | ||
|
@@ -214,17 +232,24 @@ impl<D: AsyncDB> Runner<D> { | |
match record { | ||
Record::Statement { conditions, .. } if self.should_skip(&conditions) => {} | ||
Record::Statement { | ||
error, | ||
conditions: _, | ||
|
||
expected_error, | ||
sql, | ||
loc, | ||
expected_count, | ||
.. | ||
} => { | ||
let sql = self.replace_keywords(sql); | ||
let ret = self.db.run(&sql).await; | ||
match ret { | ||
Ok(_) if error => return Err(TestErrorKind::StatementOk { sql }.at(loc)), | ||
Ok(count_str) => { | ||
match (ret, expected_error) { | ||
(Ok(_), Some(_)) => { | ||
return Err(TestErrorKind::Ok { | ||
sql, | ||
kind: RecordKind::Statement, | ||
} | ||
.at(loc)) | ||
} | ||
(Ok(count_str), None) => { | ||
if let Some(expected_count) = expected_count { | ||
if expected_count.to_string() != count_str { | ||
return Err(TestErrorKind::StatementResultMismatch { | ||
|
@@ -236,38 +261,76 @@ impl<D: AsyncDB> Runner<D> { | |
} | ||
} | ||
} | ||
Err(e) if !error => { | ||
return Err(TestErrorKind::StatementFail { | ||
(Err(e), Some(expected_error)) => { | ||
if !expected_error.is_match(&e.to_string()) { | ||
return Err(TestErrorKind::ErrorMismatch { | ||
sql, | ||
err: Arc::new(e), | ||
expected_err: expected_error.to_string(), | ||
kind: RecordKind::Statement, | ||
} | ||
.at(loc)); | ||
} | ||
} | ||
(Err(e), None) => { | ||
return Err(TestErrorKind::Fail { | ||
sql, | ||
err: Arc::new(e), | ||
kind: RecordKind::Statement, | ||
} | ||
.at(loc)); | ||
} | ||
_ => {} | ||
} | ||
if let Some(hook) = &mut self.hook { | ||
hook.on_stmt_complete(&sql).await; | ||
} | ||
} | ||
Record::Query { conditions, .. } if self.should_skip(&conditions) => {} | ||
Record::Query { | ||
conditions: _, | ||
|
||
loc, | ||
sql, | ||
expected_error, | ||
expected_results, | ||
sort_mode, | ||
.. | ||
|
||
// not handle yet, | ||
type_string: _, | ||
label: _, | ||
} => { | ||
let sql = self.replace_keywords(sql); | ||
let output = match self.db.run(&sql).await { | ||
Ok(output) => output, | ||
Err(e) => { | ||
return Err(TestErrorKind::QueryFail { | ||
let output = match (self.db.run(&sql).await, expected_error) { | ||
(Ok(_), Some(_)) => { | ||
return Err(TestErrorKind::Ok { | ||
sql, | ||
kind: RecordKind::Query, | ||
} | ||
.at(loc)) | ||
} | ||
(Ok(output), None) => output, | ||
(Err(e), Some(expected_error)) => { | ||
if !expected_error.is_match(&e.to_string()) { | ||
return Err(TestErrorKind::ErrorMismatch { | ||
sql, | ||
err: Arc::new(e), | ||
expected_err: expected_error.to_string(), | ||
kind: RecordKind::Query, | ||
} | ||
.at(loc)); | ||
} | ||
return Ok(()); | ||
} | ||
(Err(e), None) => { | ||
return Err(TestErrorKind::Fail { | ||
sql, | ||
err: Arc::new(e), | ||
kind: RecordKind::Query, | ||
} | ||
.at(loc)); | ||
} | ||
}; | ||
|
||
let mut output = split_lines_and_normalize(&output); | ||
let mut expected_results = split_lines_and_normalize(&expected_results); | ||
match sort_mode.as_ref().or(self.sort_mode.as_ref()) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this won't always work, since we do
line.split_whitespace()
. 😅 But I don't bother fixing it now.