@@ -330,89 +330,6 @@ impl<'client> Provider<'client> for DeleteOpenMessageProvider<'client> {
330
330
}
331
331
}
332
332
333
- /// ## Open message repository
334
- ///
335
- /// This is a business oriented layer to perform actions on the database through
336
- /// providers.
337
- pub struct OpenMessageRepository {
338
- connection : Arc < Mutex < Connection > > ,
339
- }
340
-
341
- impl OpenMessageRepository {
342
- /// Instanciate service
343
- pub fn new ( connection : Arc < Mutex < Connection > > ) -> Self {
344
- Self { connection }
345
- }
346
-
347
- /// Return the latest [OpenMessage] for the given Epoch and [SignedEntityType].
348
- pub async fn get_open_message (
349
- & self ,
350
- signed_entity_type : & SignedEntityType ,
351
- ) -> StdResult < Option < OpenMessage > > {
352
- let lock = self . connection . lock ( ) . await ;
353
- let provider = OpenMessageProvider :: new ( & lock) ;
354
- let filters = provider
355
- . get_epoch_condition ( signed_entity_type. get_epoch ( ) )
356
- . and_where ( provider. get_signed_entity_type_condition ( signed_entity_type) ) ;
357
- let mut messages = provider. find ( filters) ?;
358
-
359
- Ok ( messages. next ( ) )
360
- }
361
-
362
- /// Create a new [OpenMessage] in the database.
363
- pub async fn create_open_message (
364
- & self ,
365
- epoch : Epoch ,
366
- signed_entity_type : & SignedEntityType ,
367
- protocol_message : & ProtocolMessage ,
368
- ) -> StdResult < OpenMessage > {
369
- let lock = self . connection . lock ( ) . await ;
370
- let provider = InsertOpenMessageProvider :: new ( & lock) ;
371
- let filters = provider. get_insert_condition ( epoch, signed_entity_type, protocol_message) ?;
372
- let mut cursor = provider. find ( filters) ?;
373
-
374
- cursor
375
- . next ( )
376
- . ok_or_else ( || panic ! ( "Inserting an open_message should not return nothing." ) )
377
- }
378
-
379
- /// Updates an [OpenMessage] in the database.
380
- pub async fn update_open_message ( & self , open_message : & OpenMessage ) -> StdResult < OpenMessage > {
381
- let lock = self . connection . lock ( ) . await ;
382
- let provider = UpdateOpenMessageProvider :: new ( & lock) ;
383
- let filters = provider. get_update_condition ( open_message) ?;
384
- let mut cursor = provider. find ( filters) ?;
385
-
386
- cursor
387
- . next ( )
388
- . ok_or_else ( || panic ! ( "Updating an open_message should not return nothing." ) )
389
- }
390
-
391
- /// Remove all the [OpenMessage] for the given Epoch in the database.
392
- /// It returns the number of messages removed.
393
- pub async fn clean_epoch ( & self , epoch : Epoch ) -> StdResult < usize > {
394
- let lock = self . connection . lock ( ) . await ;
395
- let provider = DeleteOpenMessageProvider :: new ( & lock) ;
396
- let filters = provider. get_epoch_condition ( epoch) ;
397
- let cursor = provider. find ( filters) ?;
398
-
399
- Ok ( cursor. count ( ) )
400
- }
401
-
402
- /// Return an open message with its associated single signatures if any.
403
- pub async fn get_open_message_with_single_signatures (
404
- & self ,
405
- signed_entity_type : & SignedEntityType ,
406
- ) -> StdResult < Option < OpenMessageWithSingleSignatures > > {
407
- let lock = self . connection . lock ( ) . await ;
408
- let provider = OpenMessageWithSingleSignaturesProvider :: new ( & lock) ;
409
- let filters = provider. get_signed_entity_type_condition ( signed_entity_type) ;
410
- let mut messages = provider. find ( filters) ?;
411
-
412
- Ok ( messages. next ( ) )
413
- }
414
- }
415
-
416
333
/// Open Message with associated single signatures if any.
417
334
#[ derive( Debug , Clone ) ]
418
335
pub struct OpenMessageWithSingleSignatures {
@@ -550,12 +467,99 @@ order by open_message.created_at desc, open_message.rowid desc
550
467
}
551
468
}
552
469
470
+ /// ## Open message repository
471
+ ///
472
+ /// This is a business oriented layer to perform actions on the database through
473
+ /// providers.
474
+ pub struct OpenMessageRepository {
475
+ connection : Arc < Mutex < Connection > > ,
476
+ }
477
+
478
+ impl OpenMessageRepository {
479
+ /// Instanciate service
480
+ pub fn new ( connection : Arc < Mutex < Connection > > ) -> Self {
481
+ Self { connection }
482
+ }
483
+
484
+ /// Return the latest [OpenMessage] for the given Epoch and [SignedEntityType].
485
+ pub async fn get_open_message (
486
+ & self ,
487
+ signed_entity_type : & SignedEntityType ,
488
+ ) -> StdResult < Option < OpenMessage > > {
489
+ let lock = self . connection . lock ( ) . await ;
490
+ let provider = OpenMessageProvider :: new ( & lock) ;
491
+ let filters = provider
492
+ . get_epoch_condition ( signed_entity_type. get_epoch ( ) )
493
+ . and_where ( provider. get_signed_entity_type_condition ( signed_entity_type) ) ;
494
+ let mut messages = provider. find ( filters) ?;
495
+
496
+ Ok ( messages. next ( ) )
497
+ }
498
+
499
+ /// Create a new [OpenMessage] in the database.
500
+ pub async fn create_open_message (
501
+ & self ,
502
+ epoch : Epoch ,
503
+ signed_entity_type : & SignedEntityType ,
504
+ protocol_message : & ProtocolMessage ,
505
+ ) -> StdResult < OpenMessage > {
506
+ let lock = self . connection . lock ( ) . await ;
507
+ let provider = InsertOpenMessageProvider :: new ( & lock) ;
508
+ let filters = provider. get_insert_condition ( epoch, signed_entity_type, protocol_message) ?;
509
+ let mut cursor = provider. find ( filters) ?;
510
+
511
+ cursor
512
+ . next ( )
513
+ . ok_or_else ( || panic ! ( "Inserting an open_message should not return nothing." ) )
514
+ }
515
+
516
+ /// Updates an [OpenMessage] in the database.
517
+ pub async fn update_open_message ( & self , open_message : & OpenMessage ) -> StdResult < OpenMessage > {
518
+ let lock = self . connection . lock ( ) . await ;
519
+ let provider = UpdateOpenMessageProvider :: new ( & lock) ;
520
+ let filters = provider. get_update_condition ( open_message) ?;
521
+ let mut cursor = provider. find ( filters) ?;
522
+
523
+ cursor
524
+ . next ( )
525
+ . ok_or_else ( || panic ! ( "Updating an open_message should not return nothing." ) )
526
+ }
527
+
528
+ /// Remove all the [OpenMessage] for the given Epoch in the database.
529
+ /// It returns the number of messages removed.
530
+ pub async fn clean_epoch ( & self , epoch : Epoch ) -> StdResult < usize > {
531
+ let lock = self . connection . lock ( ) . await ;
532
+ let provider = DeleteOpenMessageProvider :: new ( & lock) ;
533
+ let filters = provider. get_epoch_condition ( epoch) ;
534
+ let cursor = provider. find ( filters) ?;
535
+
536
+ Ok ( cursor. count ( ) )
537
+ }
538
+
539
+ /// Return an open message with its associated single signatures if any.
540
+ pub async fn get_open_message_with_single_signatures (
541
+ & self ,
542
+ signed_entity_type : & SignedEntityType ,
543
+ ) -> StdResult < Option < OpenMessageWithSingleSignatures > > {
544
+ let lock = self . connection . lock ( ) . await ;
545
+ let provider = OpenMessageWithSingleSignaturesProvider :: new ( & lock) ;
546
+ let filters = provider. get_signed_entity_type_condition ( signed_entity_type) ;
547
+ let mut messages = provider. find ( filters) ?;
548
+
549
+ Ok ( messages. next ( ) )
550
+ }
551
+ }
552
+
553
553
#[ cfg( test) ]
554
554
mod tests {
555
555
use mithril_common:: { entities:: Beacon , sqlite:: SourceAlias } ;
556
556
557
557
use crate :: { dependency_injection:: DependenciesBuilder , Configuration } ;
558
558
559
+ use crate :: database:: provider:: test_helper:: {
560
+ setup_single_signature_db, setup_single_signature_records,
561
+ } ;
562
+
559
563
use super :: * ;
560
564
561
565
#[ test]
@@ -824,9 +828,52 @@ mod tests {
824
828
assert_eq ! ( 2 , count) ;
825
829
}
826
830
827
- /*
828
831
#[ tokio:: test]
829
- async fn repository_get_open_message_with_single_signatures() {
830
- todo!()
831
- } */
832
+ async fn repository_get_open_message_with_single_signatures_when_signatures_exist ( ) {
833
+ let single_signature_records = setup_single_signature_records ( 1 , 1 , 4 ) ;
834
+
835
+ let connection = Connection :: open ( ":memory:" ) . unwrap ( ) ;
836
+ setup_single_signature_db ( & connection, single_signature_records. clone ( ) ) . unwrap ( ) ;
837
+ let repository = OpenMessageRepository :: new ( Arc :: new ( Mutex :: new ( connection) ) ) ;
838
+
839
+ let mut open_message = OpenMessage :: dummy ( ) ;
840
+ open_message. open_message_id = single_signature_records[ 0 ] . open_message_id ;
841
+ repository. update_open_message ( & open_message) . await . unwrap ( ) ;
842
+
843
+ let open_message_with_single_signatures = repository
844
+ . get_open_message_with_single_signatures ( & open_message. signed_entity_type )
845
+ . await
846
+ . unwrap ( )
847
+ . unwrap ( ) ;
848
+ assert_eq ! (
849
+ 4 ,
850
+ open_message_with_single_signatures. single_signatures. len( )
851
+ )
852
+ }
853
+
854
+ #[ tokio:: test]
855
+ async fn repository_get_open_message_with_single_signatures_when_signatures_not_exist ( ) {
856
+ let connection = Connection :: open ( ":memory:" ) . unwrap ( ) ;
857
+ setup_single_signature_db ( & connection, Vec :: new ( ) ) . unwrap ( ) ;
858
+ let repository = OpenMessageRepository :: new ( Arc :: new ( Mutex :: new ( connection) ) ) ;
859
+
860
+ let open_message = OpenMessage :: dummy ( ) ;
861
+ repository
862
+ . create_open_message (
863
+ open_message. epoch ,
864
+ & open_message. signed_entity_type ,
865
+ & open_message. protocol_message ,
866
+ )
867
+ . await
868
+ . unwrap ( ) ;
869
+
870
+ let open_message_with_single_signatures = repository
871
+ . get_open_message_with_single_signatures ( & open_message. signed_entity_type )
872
+ . await
873
+ . unwrap ( )
874
+ . unwrap ( ) ;
875
+ assert ! ( open_message_with_single_signatures
876
+ . single_signatures
877
+ . is_empty( ) )
878
+ }
832
879
}
0 commit comments