Skip to content

Commit 4128dfe

Browse files
committed
Introduce tests/test_utils/mod.rs and use it consistently
To share helper macros between various tests/* we added a new module (tests/macros/mod.rs). This made the prologue to be used in tests quite long and a little weird: ``` #[macro_use] #[path = "macros/mod.rs"] mod macros; use sqlparser::test_utils::*; ``` This simplifies it to: ``` #[macro_use] mod test_utils; use test_utils::*; ``` - and switches all existing tests to the new prologue simultaneously... ...while fixing a few other inconsistencies and adding a few comments about the way `test_utils` work.
1 parent 99fb633 commit 4128dfe

File tree

10 files changed

+47
-11
lines changed

10 files changed

+47
-11
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ pub mod tokenizer;
4343
#[doc(hidden)]
4444
// This is required to make utilities accessible by both the crate-internal
4545
// unit-tests and by the integration tests <https://stackoverflow.com/a/44541071/1026>
46+
// External users are not supposed to rely on this module.
4647
pub mod test_utils;

src/test_utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13+
/// This module contains internal utilities used for testing the library.
14+
/// While technically public, the library's users are not supposed to rely
15+
/// on this module, as it will change without notice.
16+
//
17+
// Integration tests (i.e. everything under `tests/`) import this
18+
// via `tests/test_utils/mod.rs`.
1319
use std::fmt::Debug;
1420

1521
use super::ast::*;

tests/sqlparser_common.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919
//! dialect-specific parsing rules).
2020
2121
#[macro_use]
22-
#[path = "macros/mod.rs"]
23-
mod macros;
22+
mod test_utils;
23+
use test_utils::{all_dialects, expr_from_projection, join, number, only, table};
2424

2525
use matches::assert_matches;
2626
use sqlparser::ast::*;
2727
use sqlparser::dialect::keywords::ALL_KEYWORDS;
2828
use sqlparser::parser::ParserError;
29-
use sqlparser::test_utils::{all_dialects, expr_from_projection, join, number, only, table};
3029

3130
#[test]
3231
fn parse_insert_values() {

tests/sqlparser_mssql.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
//! Test SQL syntax specific to Microsoft's T-SQL. The parser based on the
1515
//! generic dialect is also tested (on the inputs it can handle).
1616
17+
#[macro_use]
18+
mod test_utils;
19+
use test_utils::*;
20+
1721
use sqlparser::ast::*;
1822
use sqlparser::dialect::{GenericDialect, MsSqlDialect};
19-
use sqlparser::test_utils::*;
2023

2124
#[test]
2225
fn parse_mssql_identifiers() {

tests/sqlparser_mysql.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
// limitations under the License.
1212

1313
#![warn(clippy::all)]
14-
1514
//! Test SQL syntax specific to MySQL. The parser based on the generic dialect
1615
//! is also tested (on the inputs it can handle).
1716
17+
#[macro_use]
18+
mod test_utils;
19+
use test_utils::*;
20+
1821
use sqlparser::ast::*;
1922
use sqlparser::dialect::{GenericDialect, MySqlDialect};
20-
use sqlparser::test_utils::*;
2123
use sqlparser::tokenizer::Token;
2224

2325
#[test]

tests/sqlparser_postgres.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
//! Test SQL syntax specific to PostgreSQL. The parser based on the
1515
//! generic dialect is also tested (on the inputs it can handle).
1616
17+
#[macro_use]
18+
mod test_utils;
19+
use test_utils::*;
20+
1721
use sqlparser::ast::*;
1822
use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
1923
use sqlparser::parser::ParserError;
20-
use sqlparser::test_utils::*;
2124

2225
#[test]
2326
fn parse_create_table_with_defaults() {

tests/sqlparser_regression.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13+
#![warn(clippy::all)]
14+
1315
use sqlparser::dialect::GenericDialect;
1416
use sqlparser::parser::Parser;
1517

tests/sqlparser_snowflake.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13+
#![warn(clippy::all)]
14+
//! Test SQL syntax specific to Snowflake. The parser based on the
15+
//! generic dialect is also tested (on the inputs it can handle).
16+
1317
#[macro_use]
14-
#[path = "macros/mod.rs"]
15-
mod macros;
18+
mod test_utils;
19+
use test_utils::*;
1620

17-
use sqlparser::test_utils::*;
1821
use sqlparser::ast::*;
1922
use sqlparser::dialect::{GenericDialect, SnowflakeDialect};
2023
use sqlparser::tokenizer::*;

tests/sqlparser_sqlite.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
//! Test SQL syntax specific to SQLite. The parser based on the
1515
//! generic dialect is also tested (on the inputs it can handle).
1616
17+
#[macro_use]
18+
mod test_utils;
19+
use test_utils::*;
20+
1721
use sqlparser::ast::*;
1822
use sqlparser::dialect::{GenericDialect, SQLiteDialect};
19-
use sqlparser::test_utils::*;
2023
use sqlparser::tokenizer::Token;
2124

2225
#[test]

tests/macros/mod.rs renamed to tests/test_utils/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13+
// Re-export everything from `src/test_utils.rs`.
14+
pub use sqlparser::test_utils::*;
15+
16+
// For the test-only macros we take a different approach of keeping them here
17+
// rather than in the library crate.
18+
//
19+
// This is because we don't need any of them to be shared between the
20+
// integration tests (i.e. `tests/*`) and the unit tests (i.e. `src/*`),
21+
// but also because Rust doesn't scope macros to a particular module
22+
// (and while we export internal helpers as sqlparser::test_utils::<...>,
23+
// expecting our users to abstain from relying on them, exporting internal
24+
// macros at the top level, like `sqlparser::nest` was deemed too confusing).
25+
26+
#[macro_export]
1327
macro_rules! nest {
1428
($base:expr $(, $join:expr)*) => {
1529
TableFactor::NestedJoin(Box::new(TableWithJoins {

0 commit comments

Comments
 (0)