@@ -7,7 +7,7 @@ fn make_endorsement(
77 pool : & crate :: db:: PgPool ,
88 work_id : Uuid ,
99 endorsement_ordinal : i32 ,
10- author_name : Option < String > ,
10+ author_name : String ,
1111) -> Endorsement {
1212 let data = NewEndorsement {
1313 work_id,
@@ -87,7 +87,7 @@ mod policy {
8787 let work = create_work ( pool. as_ref ( ) , & imprint) ;
8888 let data = NewEndorsement {
8989 work_id : work. work_id ,
90- author_name : Some ( "Author" . to_string ( ) ) ,
90+ author_name : "Author" . to_string ( ) ,
9191 author_role : Some ( "Role" . to_string ( ) ) ,
9292 author_orcid : None ,
9393 author_institution_id : None ,
@@ -122,8 +122,7 @@ mod policy {
122122 let publisher = create_publisher ( pool. as_ref ( ) ) ;
123123 let imprint = create_imprint ( pool. as_ref ( ) , & publisher) ;
124124 let work = create_work ( pool. as_ref ( ) , & imprint) ;
125- let endorsement =
126- make_endorsement ( pool. as_ref ( ) , work. work_id , 1 , Some ( "Author" . to_string ( ) ) ) ;
125+ let endorsement = make_endorsement ( pool. as_ref ( ) , work. work_id , 1 , "Author" . to_string ( ) ) ;
127126
128127 let patch = PatchEndorsement {
129128 endorsement_id : endorsement. endorsement_id ,
@@ -142,7 +141,7 @@ mod policy {
142141
143142 let data = NewEndorsement {
144143 work_id : work. work_id ,
145- author_name : Some ( "Author" . to_string ( ) ) ,
144+ author_name : "Author" . to_string ( ) ,
146145 author_role : Some ( "Role" . to_string ( ) ) ,
147146 author_orcid : None ,
148147 author_institution_id : None ,
@@ -208,7 +207,7 @@ mod policy {
208207
209208 let data = NewEndorsement {
210209 work_id : chapter. work_id ,
211- author_name : Some ( "Author" . to_string ( ) ) ,
210+ author_name : "Author" . to_string ( ) ,
212211 author_role : Some ( "Role" . to_string ( ) ) ,
213212 author_orcid : None ,
214213 author_institution_id : None ,
@@ -244,7 +243,7 @@ mod crud {
244243
245244 let data = NewEndorsement {
246245 work_id : work. work_id ,
247- author_name : Some ( "Author" . to_string ( ) ) ,
246+ author_name : "Author" . to_string ( ) ,
248247 author_role : Some ( "Role" . to_string ( ) ) ,
249248 author_orcid : Some (
250249 crate :: model:: Orcid :: from_str ( "https://orcid.org/0000-0002-1234-5678" ) . unwrap ( ) ,
@@ -280,6 +279,54 @@ mod crud {
280279 assert ! ( Endorsement :: from_id( pool. as_ref( ) , & deleted. endorsement_id) . is_err( ) ) ;
281280 }
282281
282+ #[ test]
283+ fn crud_rejects_empty_author_name ( ) {
284+ let ( _guard, pool) = setup_test_db ( ) ;
285+
286+ let publisher = create_publisher ( pool. as_ref ( ) ) ;
287+ let imprint = create_imprint ( pool. as_ref ( ) , & publisher) ;
288+ let work = create_work ( pool. as_ref ( ) , & imprint) ;
289+
290+ let data = NewEndorsement {
291+ work_id : work. work_id ,
292+ author_name : "" . to_string ( ) ,
293+ author_role : Some ( "Role" . to_string ( ) ) ,
294+ author_orcid : None ,
295+ author_institution_id : None ,
296+ url : Some ( "https://example.com/endorsement" . to_string ( ) ) ,
297+ text : Some ( "Endorsement text" . to_string ( ) ) ,
298+ endorsement_ordinal : 1 ,
299+ } ;
300+
301+ let create_error = Endorsement :: create ( pool. as_ref ( ) , & data) . unwrap_err ( ) ;
302+ assert ! ( matches!(
303+ create_error,
304+ thoth_errors:: ThothError :: DatabaseConstraintError ( ref msg)
305+ if msg. as_ref( ) == "Author name must not be an empty string."
306+ ) ) ;
307+
308+ let endorsement = make_endorsement ( pool. as_ref ( ) , work. work_id , 1 , "Author" . to_string ( ) ) ;
309+ let patch = PatchEndorsement {
310+ endorsement_id : endorsement. endorsement_id ,
311+ work_id : endorsement. work_id ,
312+ author_name : "" . to_string ( ) ,
313+ author_role : endorsement. author_role . clone ( ) ,
314+ author_orcid : endorsement. author_orcid . clone ( ) ,
315+ author_institution_id : endorsement. author_institution_id ,
316+ url : endorsement. url . clone ( ) ,
317+ text : endorsement. text . clone ( ) ,
318+ endorsement_ordinal : endorsement. endorsement_ordinal ,
319+ } ;
320+ let ctx = test_context ( pool. clone ( ) , "test-user" ) ;
321+
322+ let update_error = endorsement. update ( & ctx, & patch) . unwrap_err ( ) ;
323+ assert ! ( matches!(
324+ update_error,
325+ thoth_errors:: ThothError :: DatabaseConstraintError ( ref msg)
326+ if msg. as_ref( ) == "Author name must not be an empty string."
327+ ) ) ;
328+ }
329+
283330 #[ test]
284331 fn deleting_author_institution_nulls_relation ( ) {
285332 let ( _guard, pool) = setup_test_db ( ) ;
@@ -293,7 +340,7 @@ mod crud {
293340 pool. as_ref ( ) ,
294341 & NewEndorsement {
295342 work_id : work. work_id ,
296- author_name : Some ( "Author" . to_string ( ) ) ,
343+ author_name : "Author" . to_string ( ) ,
297344 author_role : Some ( "Role" . to_string ( ) ) ,
298345 author_orcid : Some (
299346 crate :: model:: Orcid :: from_str ( "https://orcid.org/0000-0002-1234-5678" ) . unwrap ( ) ,
@@ -324,8 +371,8 @@ mod crud {
324371 let imprint = create_imprint ( pool. as_ref ( ) , & publisher) ;
325372 let work = create_work ( pool. as_ref ( ) , & imprint) ;
326373
327- let first = make_endorsement ( pool. as_ref ( ) , work. work_id , 1 , Some ( "Author 1" . to_string ( ) ) ) ;
328- let second = make_endorsement ( pool. as_ref ( ) , work. work_id , 2 , Some ( "Author 2" . to_string ( ) ) ) ;
374+ let first = make_endorsement ( pool. as_ref ( ) , work. work_id , 1 , "Author 1" . to_string ( ) ) ;
375+ let second = make_endorsement ( pool. as_ref ( ) , work. work_id , 2 , "Author 2" . to_string ( ) ) ;
329376 let ctx = test_context ( pool. clone ( ) , "test-user" ) ;
330377
331378 let moved = second
0 commit comments