@@ -297,27 +297,64 @@ mod test {
297297 assert ! ( r. is_ok( ) ) ;
298298 }
299299
300+ // Use gen_random_uuid() to generate uuid
301+ #[ test]
302+ fn test_uuid_gen ( ) -> crate :: Result < ( ) > {
303+ use crate :: Connection ;
304+
305+ let db = Connection :: open_in_memory ( ) ?;
306+ db. execute_batch ( "CREATE TABLE foo (id uuid NOT NULL);" ) ?;
307+
308+ db. execute ( "INSERT INTO foo (id) VALUES (gen_random_uuid())" , [ ] ) ?;
309+
310+ let mut stmt = db. prepare ( "SELECT id FROM foo" ) ?;
311+ let mut rows = stmt. query ( [ ] ) ?;
312+ let row = rows. next ( ) ?. unwrap ( ) ;
313+ let found_id: String = row. get_unwrap ( 0 ) ;
314+ assert_eq ! ( found_id. len( ) , 36 ) ;
315+ Ok ( ( ) )
316+ }
317+
300318 #[ cfg( feature = "uuid" ) ]
301319 #[ test]
302- fn test_uuid ( ) -> crate :: Result < ( ) > {
320+ fn test_uuid_blob_type ( ) -> crate :: Result < ( ) > {
303321 use crate :: { params, Connection } ;
304322 use uuid:: Uuid ;
305323
306324 let db = Connection :: open_in_memory ( ) ?;
307- db. execute_batch ( "CREATE TABLE foo (id BLOB CONSTRAINT uuidchk CHECK (octet_length(id) < = 16), label TEXT);" ) ?;
325+ db. execute_batch ( "CREATE TABLE foo (id BLOB CONSTRAINT uuidchk CHECK (octet_length(id) = 16), label TEXT);" ) ?;
308326
309327 let id = Uuid :: new_v4 ( ) ;
328+ let id_vec = id. as_bytes ( ) . to_vec ( ) ;
329+ db. execute ( "INSERT INTO foo (id, label) VALUES (?, ?)" , params ! [ id_vec, "target" ] ) ?;
310330
331+ let mut stmt = db. prepare ( "SELECT id, label FROM foo WHERE id = ?" ) ?;
332+ let mut rows = stmt. query ( params ! [ id_vec] ) ?;
333+ let row = rows. next ( ) ?. unwrap ( ) ;
334+ let found_id: Uuid = row. get_unwrap ( 0 ) ;
335+ let found_label: String = row. get_unwrap ( 1 ) ;
336+ assert_eq ! ( found_id, id) ;
337+ assert_eq ! ( found_label, "target" ) ;
338+ Ok ( ( ) )
339+ }
340+
341+ #[ cfg( feature = "uuid" ) ]
342+ #[ test]
343+ fn test_uuid_type ( ) -> crate :: Result < ( ) > {
344+ use crate :: { params, Connection } ;
345+ use uuid:: Uuid ;
346+
347+ let db = Connection :: open_in_memory ( ) ?;
348+ db. execute_batch ( "CREATE TABLE foo (id uuid, label TEXT);" ) ?;
349+
350+ let id = Uuid :: new_v4 ( ) ;
311351 db. execute ( "INSERT INTO foo (id, label) VALUES (?, ?)" , params ! [ id, "target" ] ) ?;
312352
313353 let mut stmt = db. prepare ( "SELECT id, label FROM foo WHERE id = ?" ) ?;
314-
315354 let mut rows = stmt. query ( params ! [ id] ) ?;
316355 let row = rows. next ( ) ?. unwrap ( ) ;
317-
318356 let found_id: Uuid = row. get_unwrap ( 0 ) ;
319357 let found_label: String = row. get_unwrap ( 1 ) ;
320-
321358 assert_eq ! ( found_id, id) ;
322359 assert_eq ! ( found_label, "target" ) ;
323360 Ok ( ( ) )
0 commit comments