@@ -19,3 +19,70 @@ async fn test_query_simple() -> anyhow::Result<()> {
1919
2020 Ok ( ( ) )
2121}
22+
23+ #[ sqlx_macros:: test]
24+ async fn test_query_datetime ( ) -> anyhow:: Result < ( ) > {
25+ let mut conn = new :: < Mssql > ( ) . await ?;
26+
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" ) ;
32+
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 ?;
39+
40+ assert_eq ! ( record. dt, Some ( expected_naive_dt) ) ;
41+
42+ Ok ( ( ) )
43+ }
44+
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+ }
53+
54+ #[ sqlx_macros:: test]
55+ async fn test_query_as_from_issue ( ) -> anyhow:: Result < ( ) > {
56+ let mut conn = new :: < Mssql > ( ) . await ?;
57+
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#"
70+ SELECT
71+ 1 AS id,
72+ 'test_key' AS config_key,
73+ 'test_value' AS config_value,
74+ CAST('2023-01-01 10:00:00.000' AS DATETIME2(3)) AS created_on,
75+ CAST('2023-01-02 11:30:00.500' AS DATETIME2(3)) AS last_updated
76+ "#
77+ )
78+ . fetch_one ( & mut conn)
79+ . await ?;
80+
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) ) ;
86+
87+ Ok ( ( ) )
88+ }
0 commit comments