@@ -68,3 +68,85 @@ impl From<uuid::Uuid> for UuidV7 {
6868 Self { uuid }
6969 }
7070}
71+
72+ #[ cfg( test) ]
73+ mod tests {
74+ use coset:: cbor:: Value ;
75+ use uuid:: Uuid ;
76+
77+ use super :: * ;
78+ use crate :: uuid:: UUID_CBOR_TAG ;
79+
80+ #[ test]
81+ fn test_invalid_uuid ( ) {
82+ let invalid_uuid = UuidV7 :: invalid ( ) ;
83+ assert ! ( !invalid_uuid. is_valid( ) , "Invalid UUID should not be valid" ) ;
84+ assert_eq ! (
85+ invalid_uuid. uuid( ) ,
86+ INVALID_UUID ,
87+ "Invalid UUID should match INVALID_UUID"
88+ ) ;
89+ }
90+
91+ #[ test]
92+ fn test_valid_uuid ( ) {
93+ // Generate a UUIDv7 (this assumes you have access to a valid UUIDv7 library)
94+ let valid_uuid = UuidV7 :: from (
95+ Uuid :: try_parse ( "017f22e3-79b0-7cc7-98cf-e0bbf8a1c5f1" ) . unwrap ( ) , // Example UUIDv7
96+ ) ;
97+ assert ! ( valid_uuid. is_valid( ) , "Valid UUID should be valid" ) ;
98+ }
99+
100+ #[ test]
101+ fn test_invalid_version_uuid ( ) {
102+ let invalid_version_uuid = UuidV7 :: from ( Uuid :: from_u128 ( 0 ) ) ; // Zero UUID is not valid.
103+ assert ! (
104+ !invalid_version_uuid. is_valid( ) ,
105+ "Zero UUID should not be valid"
106+ ) ;
107+ }
108+
109+ #[ test]
110+ fn test_display_trait ( ) {
111+ let valid_uuid =
112+ UuidV7 :: from ( Uuid :: try_parse ( "017f22e3-79b0-7cc7-98cf-e0bbf8a1c5f1" ) . unwrap ( ) ) ;
113+ assert_eq ! (
114+ format!( "{}" , valid_uuid) ,
115+ valid_uuid. uuid( ) . to_string( ) ,
116+ "Display implementation should match UUID string"
117+ ) ;
118+ }
119+
120+ #[ test]
121+ fn test_try_from_cbor_valid_uuid ( ) {
122+ let uuid = Uuid :: try_parse ( "017f22e3-79b0-7cc7-98cf-e0bbf8a1c5f1" ) . unwrap ( ) ;
123+ let cbor_value = Value :: Tag (
124+ UUID_CBOR_TAG ,
125+ Box :: new ( Value :: Bytes ( uuid. as_bytes ( ) . to_vec ( ) ) ) ,
126+ ) ;
127+ let result = UuidV7 :: try_from ( & cbor_value) ;
128+
129+ assert ! (
130+ result. is_ok( ) ,
131+ "Should successfully parse valid UUID from CBOR"
132+ ) ;
133+ let uuid_v7 = result. unwrap ( ) ;
134+ assert ! ( uuid_v7. is_valid( ) , "Parsed UUIDv7 should be valid" ) ;
135+ assert_eq ! (
136+ uuid_v7. uuid( ) ,
137+ uuid,
138+ "Parsed UUID should match original UUID"
139+ ) ;
140+ }
141+
142+ #[ test]
143+ fn test_try_from_cbor_invalid_uuid ( ) {
144+ let cbor_value = Value :: Bytes ( vec ! [ 0 ; 16 ] ) ; // Zeroed-out UUID bytes
145+ let result = UuidV7 :: try_from ( & cbor_value) ;
146+
147+ assert ! (
148+ result. is_err( ) ,
149+ "Should fail to parse invalid UUID from CBOR"
150+ ) ;
151+ }
152+ }
0 commit comments