@@ -3,6 +3,7 @@ use quickcheck::quickcheck;
33use schema:: connection;
44use yaqb:: * ;
55use yaqb:: result:: Error ;
6+ use yaqb:: types:: structs:: * ;
67use yaqb:: types:: { NativeSqlType , ToSql , Nullable , Array } ;
78
89fn test_type_round_trips < ST , T > ( value : T , type_name : & str ) -> bool where
@@ -16,7 +17,7 @@ fn test_type_round_trips<ST, T>(value: T, type_name: &str) -> bool where
1617 Ok ( mut val) => value == val. nth ( 0 ) . unwrap ( ) ,
1718 Err ( Error :: DatabaseError ( msg) ) =>
1819 & msg == "ERROR: invalid byte sequence for encoding \" UTF8\" : 0x00\n " ,
19- _ => false ,
20+ Err ( e ) => panic ! ( "Query failed: {:?}" , e ) ,
2021 }
2122}
2223
@@ -53,48 +54,35 @@ test_round_trip!(string_roundtrips, VarChar, String, "varchar");
5354test_round_trip ! ( text_roundtrips, Text , String , "text" ) ;
5455test_round_trip ! ( binary_roundtrips, Binary , Vec <u8 >, "bytea" ) ;
5556
56- #[ test]
57- fn timestamp_roundtrip ( ) {
58- use yaqb:: types:: structs:: PgTimestamp ;
57+ macro_rules! test_newtype_round_trip {
58+ ( $test_name: ident, $sql_type: ident, $newtype: ident, $tpe: ty, $sql_type_name: expr) => {
59+ #[ test]
60+ fn $test_name( ) {
61+ fn round_trip( val: $tpe) -> bool {
62+ test_type_round_trips:: <types:: $sql_type, _>( $newtype( val) , $sql_type_name)
63+ }
5964
60- fn round_trip ( val : i64 ) -> bool {
61- test_type_round_trips :: < types:: Timestamp , _ > ( PgTimestamp ( val) , "timestamp" )
62- }
65+ fn option_round_trip( val: Option <$tpe>) -> bool {
66+ let val = val. map( $newtype) ;
67+ test_type_round_trips:: <Nullable <types:: $sql_type>, _>( val, $sql_type_name)
68+ }
6369
64- fn option_round_trip ( val : Option < i64 > ) -> bool {
65- let val = val. map ( PgTimestamp ) ;
66- test_type_round_trips :: < Nullable < types:: Timestamp > , _ > ( val, "timestamp" )
67- }
70+ fn vec_round_trip ( val: Vec <$tpe >) -> bool {
71+ let val: Vec <_> = val. into_iter ( ) . map( $newtype ) . collect ( ) ;
72+ test_type_round_trips:: <Array <types:: $sql_type >, _>( val, concat! ( $sql_type_name , "[]" ) )
73+ }
6874
69- fn vec_round_trip ( val : Vec < i64 > ) -> bool {
70- let val: Vec < _ > = val. into_iter ( ) . map ( PgTimestamp ) . collect ( ) ;
71- test_type_round_trips :: < Array < types:: Timestamp > , _ > ( val, "timestamp[]" )
75+ quickcheck( round_trip as fn ( $tpe) -> bool ) ;
76+ quickcheck( option_round_trip as fn ( Option <$tpe>) -> bool ) ;
77+ quickcheck( vec_round_trip as fn ( Vec <$tpe>) -> bool ) ;
78+ }
7279 }
73-
74- quickcheck ( round_trip as fn ( i64 ) -> bool ) ;
75- quickcheck ( option_round_trip as fn ( Option < i64 > ) -> bool ) ;
76- quickcheck ( vec_round_trip as fn ( Vec < i64 > ) -> bool ) ;
7780}
7881
79- #[ test]
80- fn date_roundtrip ( ) {
81- use yaqb:: types:: structs:: PgDate ;
82-
83- fn round_trip ( val : i32 ) -> bool {
84- test_type_round_trips :: < types:: Date , _ > ( PgDate ( val) , "date" )
85- }
86-
87- fn option_round_trip ( val : Option < i32 > ) -> bool {
88- let val = val. map ( PgDate ) ;
89- test_type_round_trips :: < Nullable < types:: Date > , _ > ( val, "date" )
90- }
91-
92- fn vec_round_trip ( val : Vec < i32 > ) -> bool {
93- let val: Vec < _ > = val. into_iter ( ) . map ( PgDate ) . collect ( ) ;
94- test_type_round_trips :: < Array < types:: Date > , _ > ( val, "date[]" )
95- }
96-
97- quickcheck ( round_trip as fn ( i32 ) -> bool ) ;
98- quickcheck ( option_round_trip as fn ( Option < i32 > ) -> bool ) ;
99- quickcheck ( vec_round_trip as fn ( Vec < i32 > ) -> bool ) ;
82+ fn to_pg_time ( int : i64 ) -> :: yaqb:: types:: structs:: PgTime {
83+ PgTime ( :: std:: cmp:: max ( 0 , int) )
10084}
85+
86+ test_newtype_round_trip ! ( date_roundtrips, Date , PgDate , i32 , "date" ) ;
87+ test_newtype_round_trip ! ( time_roundtrips, Time , to_pg_time, i64 , "time" ) ;
88+ test_newtype_round_trip ! ( timestamp_roundtrips, Timestamp , PgTimestamp , i64 , "timestamp" ) ;
0 commit comments