@@ -1053,39 +1053,58 @@ mod tests {
10531053 // -------------------------------------------------------------------------
10541054
10551055 #[ test]
1056- fn test_condition_must_not_exist ( ) {
1057- let condition = inferadb_ledger_types:: SetCondition :: MustNotExist ;
1058- let proto: proto:: SetCondition = ( & condition) . into ( ) ;
1059-
1060- assert ! ( matches!( proto. condition, Some ( proto:: set_condition:: Condition :: NotExists ( true ) ) ) ) ;
1061- }
1062-
1063- #[ test]
1064- fn test_condition_must_exist ( ) {
1065- let condition = inferadb_ledger_types:: SetCondition :: MustExist ;
1066- let proto: proto:: SetCondition = ( & condition) . into ( ) ;
1067-
1068- assert ! ( matches!( proto. condition, Some ( proto:: set_condition:: Condition :: MustExists ( true ) ) ) ) ;
1069- }
1070-
1071- #[ test]
1072- fn test_condition_version_equals ( ) {
1073- let condition = inferadb_ledger_types:: SetCondition :: VersionEquals ( 42 ) ;
1074- let proto: proto:: SetCondition = ( & condition) . into ( ) ;
1075-
1076- assert ! ( matches!( proto. condition, Some ( proto:: set_condition:: Condition :: Version ( 42 ) ) ) ) ;
1077- }
1078-
1079- #[ test]
1080- fn test_condition_value_equals ( ) {
1081- let condition = inferadb_ledger_types:: SetCondition :: ValueEquals ( vec ! [ 1 , 2 , 3 , 4 ] ) ;
1082- let proto: proto:: SetCondition = ( & condition) . into ( ) ;
1056+ #[ allow( clippy:: type_complexity) ]
1057+ fn test_set_condition_to_proto ( ) {
1058+ let cases: & [ (
1059+ & str ,
1060+ inferadb_ledger_types:: SetCondition ,
1061+ Box < dyn Fn ( & proto:: SetCondition ) > ,
1062+ ) ] = & [
1063+ (
1064+ "MustNotExist" ,
1065+ inferadb_ledger_types:: SetCondition :: MustNotExist ,
1066+ Box :: new ( |p| {
1067+ assert ! (
1068+ matches!( p. condition, Some ( proto:: set_condition:: Condition :: NotExists ( true ) ) )
1069+ ) ;
1070+ } ) ,
1071+ ) ,
1072+ (
1073+ "MustExist" ,
1074+ inferadb_ledger_types:: SetCondition :: MustExist ,
1075+ Box :: new ( |p| {
1076+ assert ! ( matches!(
1077+ p. condition,
1078+ Some ( proto:: set_condition:: Condition :: MustExists ( true ) )
1079+ ) ) ;
1080+ } ) ,
1081+ ) ,
1082+ (
1083+ "VersionEquals(42)" ,
1084+ inferadb_ledger_types:: SetCondition :: VersionEquals ( 42 ) ,
1085+ Box :: new ( |p| {
1086+ assert ! (
1087+ matches!( p. condition, Some ( proto:: set_condition:: Condition :: Version ( 42 ) ) )
1088+ ) ;
1089+ } ) ,
1090+ ) ,
1091+ (
1092+ "ValueEquals([1,2,3,4])" ,
1093+ inferadb_ledger_types:: SetCondition :: ValueEquals ( vec ! [ 1 , 2 , 3 , 4 ] ) ,
1094+ Box :: new ( |p| match & p. condition {
1095+ Some ( proto:: set_condition:: Condition :: ValueEquals ( bytes) ) => {
1096+ assert_eq ! ( bytes, & [ 1 , 2 , 3 , 4 ] ) ;
1097+ } ,
1098+ other => panic ! ( "expected ValueEquals, got {other:?}" ) ,
1099+ } ) ,
1100+ ) ,
1101+ ] ;
10831102
1084- match proto . condition {
1085- Some ( proto:: set_condition :: Condition :: ValueEquals ( bytes ) ) => {
1086- assert_eq ! ( bytes , vec! [ 1 , 2 , 3 , 4 ] ) ;
1087- } ,
1088- _ => panic ! ( "Expected ValueEquals condition" ) ,
1103+ for ( label , condition, check ) in cases {
1104+ let proto: proto :: SetCondition = condition . into ( ) ;
1105+ check ( & proto ) ;
1106+ // Label used for diagnostics on failure
1107+ let _ = label ;
10891108 }
10901109 }
10911110
@@ -1424,62 +1443,50 @@ mod tests {
14241443 // -------------------------------------------------------------------------
14251444
14261445 #[ test]
1427- fn test_proto_to_condition_not_exists ( ) {
1428- let proto = proto:: SetCondition {
1429- condition : Some ( proto:: set_condition:: Condition :: NotExists ( true ) ) ,
1430- } ;
1431- let result: Option < inferadb_ledger_types:: SetCondition > = ( & proto) . into ( ) ;
1432- assert_eq ! ( result, Some ( inferadb_ledger_types:: SetCondition :: MustNotExist ) ) ;
1433- }
1434-
1435- #[ test]
1436- fn test_proto_to_condition_must_exists ( ) {
1437- let proto = proto:: SetCondition {
1438- condition : Some ( proto:: set_condition:: Condition :: MustExists ( true ) ) ,
1439- } ;
1440- let result: Option < inferadb_ledger_types:: SetCondition > = ( & proto) . into ( ) ;
1441- assert_eq ! ( result, Some ( inferadb_ledger_types:: SetCondition :: MustExist ) ) ;
1442- }
1443-
1444- #[ test]
1445- fn test_proto_to_condition_version ( ) {
1446- let proto =
1447- proto:: SetCondition { condition : Some ( proto:: set_condition:: Condition :: Version ( 42 ) ) } ;
1448- let result: Option < inferadb_ledger_types:: SetCondition > = ( & proto) . into ( ) ;
1449- assert_eq ! ( result, Some ( inferadb_ledger_types:: SetCondition :: VersionEquals ( 42 ) ) ) ;
1450- }
1451-
1452- #[ test]
1453- fn test_proto_to_condition_value_equals ( ) {
1454- let proto = proto:: SetCondition {
1455- condition : Some ( proto:: set_condition:: Condition :: ValueEquals ( vec ! [ 1 , 2 , 3 ] ) ) ,
1456- } ;
1457- let result: Option < inferadb_ledger_types:: SetCondition > = ( & proto) . into ( ) ;
1458- assert_eq ! ( result, Some ( inferadb_ledger_types:: SetCondition :: ValueEquals ( vec![ 1 , 2 , 3 ] ) ) ) ;
1459- }
1460-
1461- #[ test]
1462- fn test_proto_to_condition_none_returns_none ( ) {
1463- let proto = proto:: SetCondition { condition : None } ;
1464- let result: Option < inferadb_ledger_types:: SetCondition > = ( & proto) . into ( ) ;
1465- assert ! ( result. is_none( ) ) ;
1466- }
1467-
1468- #[ test]
1469- fn test_proto_to_condition_inverted_booleans ( ) {
1470- // NotExists(false) should map to MustExist
1471- let proto = proto:: SetCondition {
1472- condition : Some ( proto:: set_condition:: Condition :: NotExists ( false ) ) ,
1473- } ;
1474- let result: Option < inferadb_ledger_types:: SetCondition > = ( & proto) . into ( ) ;
1475- assert_eq ! ( result, Some ( inferadb_ledger_types:: SetCondition :: MustExist ) ) ;
1446+ fn test_proto_to_set_condition ( ) {
1447+ let cases: & [ ( & str , Option < proto:: set_condition:: Condition > , Option < inferadb_ledger_types:: SetCondition > ) ] = & [
1448+ (
1449+ "NotExists(true) → MustNotExist" ,
1450+ Some ( proto:: set_condition:: Condition :: NotExists ( true ) ) ,
1451+ Some ( inferadb_ledger_types:: SetCondition :: MustNotExist ) ,
1452+ ) ,
1453+ (
1454+ "MustExists(true) → MustExist" ,
1455+ Some ( proto:: set_condition:: Condition :: MustExists ( true ) ) ,
1456+ Some ( inferadb_ledger_types:: SetCondition :: MustExist ) ,
1457+ ) ,
1458+ (
1459+ "Version(42) → VersionEquals(42)" ,
1460+ Some ( proto:: set_condition:: Condition :: Version ( 42 ) ) ,
1461+ Some ( inferadb_ledger_types:: SetCondition :: VersionEquals ( 42 ) ) ,
1462+ ) ,
1463+ (
1464+ "ValueEquals([1,2,3]) → ValueEquals([1,2,3])" ,
1465+ Some ( proto:: set_condition:: Condition :: ValueEquals ( vec ! [ 1 , 2 , 3 ] ) ) ,
1466+ Some ( inferadb_ledger_types:: SetCondition :: ValueEquals ( vec ! [ 1 , 2 , 3 ] ) ) ,
1467+ ) ,
1468+ (
1469+ "None → None" ,
1470+ None ,
1471+ None ,
1472+ ) ,
1473+ (
1474+ "NotExists(false) → MustExist (inverted)" ,
1475+ Some ( proto:: set_condition:: Condition :: NotExists ( false ) ) ,
1476+ Some ( inferadb_ledger_types:: SetCondition :: MustExist ) ,
1477+ ) ,
1478+ (
1479+ "MustExists(false) → MustNotExist (inverted)" ,
1480+ Some ( proto:: set_condition:: Condition :: MustExists ( false ) ) ,
1481+ Some ( inferadb_ledger_types:: SetCondition :: MustNotExist ) ,
1482+ ) ,
1483+ ] ;
14761484
1477- // MustExists(false) should map to MustNotExist
1478- let proto = proto:: SetCondition {
1479- condition : Some ( proto:: set_condition:: Condition :: MustExists ( false ) ) ,
1480- } ;
1481- let result: Option < inferadb_ledger_types:: SetCondition > = ( & proto) . into ( ) ;
1482- assert_eq ! ( result, Some ( inferadb_ledger_types:: SetCondition :: MustNotExist ) ) ;
1485+ for ( label, proto_condition, expected) in cases {
1486+ let proto_sc = proto:: SetCondition { condition : proto_condition. clone ( ) } ;
1487+ let result: Option < inferadb_ledger_types:: SetCondition > = ( & proto_sc) . into ( ) ;
1488+ assert_eq ! ( result, * expected, "case: {label}" ) ;
1489+ }
14831490 }
14841491
14851492 // -------------------------------------------------------------------------
@@ -1681,51 +1688,66 @@ mod tests {
16811688 // -------------------------------------------------------------------------
16821689
16831690 #[ test]
1684- fn test_event_scope_system_to_proto ( ) {
1685- let proto_scope = proto:: EventScope :: from ( EventScope :: System ) ;
1686- assert_eq ! ( proto_scope, proto:: EventScope :: System ) ;
1687- }
1691+ fn test_event_scope_to_proto ( ) {
1692+ let cases: & [ ( & str , EventScope , proto:: EventScope ) ] = & [
1693+ ( "System" , EventScope :: System , proto:: EventScope :: System ) ,
1694+ ( "Organization" , EventScope :: Organization , proto:: EventScope :: Organization ) ,
1695+ ] ;
16881696
1689- # [ test ]
1690- fn test_event_scope_organization_to_proto ( ) {
1691- let proto_scope = proto :: EventScope :: from ( EventScope :: Organization ) ;
1692- assert_eq ! ( proto_scope , proto :: EventScope :: Organization ) ;
1697+ for ( label , domain , expected ) in cases {
1698+ let proto_scope = proto :: EventScope :: from ( * domain ) ;
1699+ assert_eq ! ( proto_scope, * expected , "case: {label}" ) ;
1700+ }
16931701 }
16941702
16951703 #[ test]
16961704 fn test_event_scope_proto_to_domain ( ) {
1697- assert_eq ! ( EventScope :: from( proto:: EventScope :: System ) , EventScope :: System ) ;
1698- assert_eq ! ( EventScope :: from( proto:: EventScope :: Organization ) , EventScope :: Organization ) ;
1699- }
1705+ let cases: & [ ( & str , proto:: EventScope , EventScope ) ] = & [
1706+ ( "System → System" , proto:: EventScope :: System , EventScope :: System ) ,
1707+ (
1708+ "Organization → Organization" ,
1709+ proto:: EventScope :: Organization ,
1710+ EventScope :: Organization ,
1711+ ) ,
1712+ (
1713+ "Unspecified → Organization (default)" ,
1714+ proto:: EventScope :: Unspecified ,
1715+ EventScope :: Organization ,
1716+ ) ,
1717+ ] ;
17001718
1701- # [ test ]
1702- fn test_event_scope_unspecified_defaults_to_organization ( ) {
1703- assert_eq ! ( EventScope :: from ( proto :: EventScope :: Unspecified ) , EventScope :: Organization ) ;
1719+ for ( label , proto_scope , expected ) in cases {
1720+ assert_eq ! ( EventScope :: from ( * proto_scope ) , * expected , "case: {label}" ) ;
1721+ }
17041722 }
17051723
17061724 // -------------------------------------------------------------------------
17071725 // EventOutcome conversion tests
17081726 // -------------------------------------------------------------------------
17091727
17101728 #[ test]
1711- fn test_event_outcome_success_to_proto ( ) {
1712- let proto_outcome = proto:: EventOutcome :: from ( & EventOutcome :: Success ) ;
1713- assert_eq ! ( proto_outcome, proto:: EventOutcome :: Success ) ;
1714- }
1715-
1716- #[ test]
1717- fn test_event_outcome_failed_to_proto ( ) {
1718- let outcome =
1719- EventOutcome :: Failed { code : "E1001" . to_string ( ) , detail : "disk full" . to_string ( ) } ;
1720- let proto_outcome = proto:: EventOutcome :: from ( & outcome) ;
1721- assert_eq ! ( proto_outcome, proto:: EventOutcome :: Failed ) ;
1722- }
1729+ fn test_event_outcome_to_proto ( ) {
1730+ let cases: & [ ( & str , EventOutcome , proto:: EventOutcome ) ] = & [
1731+ ( "Success" , EventOutcome :: Success , proto:: EventOutcome :: Success ) ,
1732+ (
1733+ "Failed" ,
1734+ EventOutcome :: Failed {
1735+ code : "E1001" . to_string ( ) ,
1736+ detail : "disk full" . to_string ( ) ,
1737+ } ,
1738+ proto:: EventOutcome :: Failed ,
1739+ ) ,
1740+ (
1741+ "Denied" ,
1742+ EventOutcome :: Denied { reason : "rate limited" . to_string ( ) } ,
1743+ proto:: EventOutcome :: Denied ,
1744+ ) ,
1745+ ] ;
17231746
1724- #[ test]
1725- fn test_event_outcome_denied_to_proto ( ) {
1726- let outcome = EventOutcome :: Denied { reason : "rate limited" . to_string ( ) } ;
1727- let proto_outcome = proto:: EventOutcome :: from ( & outcome) ;
1728- assert_eq ! ( proto_outcome, proto:: EventOutcome :: Denied ) ;
1747+ for ( label, outcome, expected) in cases {
1748+ let proto_outcome = proto:: EventOutcome :: from ( outcome) ;
1749+ assert_eq ! ( proto_outcome, * expected, "case: {label}" ) ;
1750+ }
17291751 }
17301752
17311753 // -------------------------------------------------------------------------
@@ -2086,27 +2108,19 @@ mod tests {
20862108 }
20872109
20882110 #[ test]
2089- fn test_region_from_i32_unspecified_returns_error ( ) {
2090- let result = super :: region_from_i32 ( 0 ) ;
2091- let err = result. unwrap_err ( ) ;
2092- assert_eq ! ( err. code( ) , tonic:: Code :: InvalidArgument ) ;
2093- assert ! ( err. message( ) . contains( "region must be specified" ) ) ;
2094- }
2095-
2096- #[ test]
2097- fn test_region_from_i32_unknown_returns_error ( ) {
2098- let result = super :: region_from_i32 ( 999 ) ;
2099- let err = result. unwrap_err ( ) ;
2100- assert_eq ! ( err. code( ) , tonic:: Code :: InvalidArgument ) ;
2101- assert ! ( err. message( ) . contains( "unknown region value: 999" ) ) ;
2102- }
2111+ fn test_region_from_i32_invalid_returns_error ( ) {
2112+ let cases: & [ ( & str , i32 , & str ) ] = & [
2113+ ( "unspecified (0)" , 0 , "region must be specified" ) ,
2114+ ( "unknown (999)" , 999 , "unknown region value: 999" ) ,
2115+ ( "negative (-1)" , -1 , "unknown region value: -1" ) ,
2116+ ] ;
21032117
2104- # [ test ]
2105- fn test_region_from_i32_negative_returns_error ( ) {
2106- let result = super :: region_from_i32 ( - 1 ) ;
2107- let err = result . unwrap_err ( ) ;
2108- assert_eq ! ( err. code ( ) , tonic :: Code :: InvalidArgument ) ;
2109- assert ! ( err . message ( ) . contains ( "unknown region value: -1" ) ) ;
2118+ for ( label , value , expected_msg ) in cases {
2119+ let result = super :: region_from_i32 ( * value ) ;
2120+ let err = result . unwrap_err ( ) ;
2121+ assert_eq ! ( err . code ( ) , tonic :: Code :: InvalidArgument , "case: {label}" ) ;
2122+ assert ! ( err. message ( ) . contains ( expected_msg ) , "case: {label}" ) ;
2123+ }
21102124 }
21112125
21122126 #[ test]
0 commit comments