@@ -20,69 +20,74 @@ async fn test_query_simple() -> anyhow::Result<()> {
2020 Ok ( ( ) )
2121}
2222
23- #[ sqlx_macros :: test ]
24- async fn test_query_datetime ( ) -> anyhow :: Result < ( ) > {
25- let mut conn = new :: < Mssql > ( ) . await ? ;
23+ #[ cfg ( feature = "chrono" ) ]
24+ mod macro_chrono {
25+ use super :: * ;
2626
27- // Define the expected NaiveDateTime value
28- let expected_naive_dt = sqlx_oldapi:: types:: chrono:: NaiveDate :: from_ymd_opt ( 2024 , 7 , 15 )
29- . expect ( "Invalid date" )
30- . and_hms_milli_opt ( 10 , 30 , 0 , 123 )
31- . expect ( "Invalid time" ) ;
27+ #[ sqlx_macros:: test]
28+ async fn test_query_datetime ( ) -> anyhow:: Result < ( ) > {
29+ let mut conn = new :: < Mssql > ( ) . await ?;
3230
33- // Use DATETIME2(3) for precise millisecond storage in MSSQL.
34- // The query! macro requires a string literal.
35- let record =
36- sqlx_oldapi:: query!( "SELECT CAST('2024-07-15 10:30:00.123' AS DATETIME2(3)) as dt" )
37- . fetch_one ( & mut conn)
38- . await ?;
31+ // Define the expected NaiveDateTime value
32+ let expected_naive_dt = sqlx_oldapi:: types:: chrono:: NaiveDate :: from_ymd_opt ( 2024 , 7 , 15 )
33+ . expect ( "Invalid date" )
34+ . and_hms_milli_opt ( 10 , 30 , 0 , 123 )
35+ . expect ( "Invalid time" ) ;
3936
40- assert_eq ! ( record. dt, Some ( expected_naive_dt) ) ;
37+ // Use DATETIME2(3) for precise millisecond storage in MSSQL.
38+ // The query! macro requires a string literal.
39+ let record =
40+ sqlx_oldapi:: query!( "SELECT CAST('2024-07-15 10:30:00.123' AS DATETIME2(3)) as dt" )
41+ . fetch_one ( & mut conn)
42+ . await ?;
4143
42- Ok ( ( ) )
43- }
44+ assert_eq ! ( record. dt, Some ( expected_naive_dt) ) ;
4445
45- #[ derive( sqlx_oldapi:: FromRow , Debug , Clone , PartialEq ) ]
46- pub struct LogNotificationConfig {
47- pub id : i32 ,
48- pub config_key : String ,
49- pub config_value : String ,
50- pub created_on : Option < sqlx_oldapi:: types:: chrono:: NaiveDateTime > ,
51- pub last_updated : Option < sqlx_oldapi:: types:: chrono:: NaiveDateTime > ,
52- }
46+ Ok ( ( ) )
47+ }
5348
54- #[ sqlx_macros:: test]
55- async fn test_query_as_from_issue ( ) -> anyhow:: Result < ( ) > {
56- let mut conn = new :: < Mssql > ( ) . await ?;
49+ #[ derive( sqlx_oldapi:: FromRow , Debug , Clone , PartialEq ) ]
50+ pub struct LogNotificationConfig {
51+ pub id : i32 ,
52+ pub config_key : String ,
53+ pub config_value : String ,
54+ pub created_on : Option < sqlx_oldapi:: types:: chrono:: NaiveDateTime > ,
55+ pub last_updated : Option < sqlx_oldapi:: types:: chrono:: NaiveDateTime > ,
56+ }
5757
58- let expected_created_on = sqlx_oldapi:: types:: chrono:: NaiveDate :: from_ymd_opt ( 2023 , 1 , 1 )
59- . unwrap ( )
60- . and_hms_milli_opt ( 10 , 0 , 0 , 0 )
61- . unwrap ( ) ;
62- let expected_last_updated = sqlx_oldapi:: types:: chrono:: NaiveDate :: from_ymd_opt ( 2023 , 1 , 2 )
63- . unwrap ( )
64- . and_hms_milli_opt ( 11 , 30 , 0 , 500 )
65- . unwrap ( ) ;
66-
67- let result = sqlx_oldapi:: query_as!(
68- LogNotificationConfig ,
69- r#"
58+ #[ sqlx_macros:: test]
59+ async fn test_query_as_from_issue ( ) -> anyhow:: Result < ( ) > {
60+ let mut conn = new :: < Mssql > ( ) . await ?;
61+
62+ let expected_created_on = sqlx_oldapi:: types:: chrono:: NaiveDate :: from_ymd_opt ( 2023 , 1 , 1 )
63+ . unwrap ( )
64+ . and_hms_milli_opt ( 10 , 0 , 0 , 0 )
65+ . unwrap ( ) ;
66+ let expected_last_updated = sqlx_oldapi:: types:: chrono:: NaiveDate :: from_ymd_opt ( 2023 , 1 , 2 )
67+ . unwrap ( )
68+ . and_hms_milli_opt ( 11 , 30 , 0 , 500 )
69+ . unwrap ( ) ;
70+
71+ let result = sqlx_oldapi:: query_as!(
72+ LogNotificationConfig ,
73+ r#"
7074 SELECT
7175 1 AS id,
7276 'test_key' AS config_key,
7377 'test_value' AS config_value,
7478 CAST('2023-01-01 10:00:00.000' AS DATETIME2(3)) AS created_on,
7579 CAST('2023-01-02 11:30:00.500' AS DATETIME2(3)) AS last_updated
7680 "#
77- )
78- . fetch_one ( & mut conn)
79- . await ?;
81+ )
82+ . fetch_one ( & mut conn)
83+ . await ?;
8084
81- assert_eq ! ( result. id, 1 ) ;
82- assert_eq ! ( result. config_key, "test_key" ) ;
83- assert_eq ! ( result. config_value, "test_value" ) ;
84- assert_eq ! ( result. created_on, Some ( expected_created_on) ) ;
85- assert_eq ! ( result. last_updated, Some ( expected_last_updated) ) ;
85+ assert_eq ! ( result. id, 1 ) ;
86+ assert_eq ! ( result. config_key, "test_key" ) ;
87+ assert_eq ! ( result. config_value, "test_value" ) ;
88+ assert_eq ! ( result. created_on, Some ( expected_created_on) ) ;
89+ assert_eq ! ( result. last_updated, Some ( expected_last_updated) ) ;
8690
87- Ok ( ( ) )
91+ Ok ( ( ) )
92+ }
8893}
0 commit comments