3
3
from hashlib import md5
4
4
5
5
import pytest
6
+ from evernote .edam .error .ttypes import EDAMErrorCode , EDAMSystemException
6
7
from evernote .edam .type .ttypes import (
7
8
Data ,
8
9
LinkedNotebook ,
@@ -588,15 +589,15 @@ def fake_slow_get_note(note_guid):
588
589
589
590
590
591
@pytest .mark .usefixtures ("fake_init_db" )
591
- def test_sync_exception_while_download (
592
- cli_invoker , mock_evernote_client , fake_storage , mocker
592
+ def test_sync_unknown_exception_while_download (
593
+ cli_invoker , mock_evernote_client , fake_storage , mocker , caplog
593
594
):
594
- test_notes = [Note (guid = f"id{ i } " , title = "test" ) for i in range (100 )]
595
+ test_notes = [Note (guid = f"id{ i } " , title = "test" ) for i in range (10 )]
595
596
596
597
mock_evernote_client .fake_notes .extend (test_notes )
597
598
598
599
def fake_get_note (note_guid ):
599
- if note_guid == "id10 " :
600
+ if note_guid == "id3 " :
600
601
raise RuntimeError ("Test error" )
601
602
602
603
return Note (
@@ -613,22 +614,99 @@ def fake_get_note(note_guid):
613
614
)
614
615
mock_get_note .side_effect = fake_get_note
615
616
617
+ # with caplog.at_level(logging.DEBUG):
616
618
with pytest .raises (RuntimeError ) as excinfo :
617
619
cli_invoker ("sync" , "--database" , "fake_db" )
618
620
619
621
assert str (excinfo .value ) == "Test error"
622
+ assert "Unknown exception caught" in caplog .text
623
+
624
+
625
+ @pytest .mark .usefixtures ("fake_init_db" )
626
+ def test_sync_edam_exception_while_download (
627
+ cli_invoker , mock_evernote_client , fake_storage , mocker , caplog
628
+ ):
629
+ test_notes = [Note (guid = f"id{ i } " , title = "test" ) for i in range (10 )]
630
+
631
+ mock_evernote_client .fake_notes .extend (test_notes )
632
+
633
+ def fake_get_note (note_guid ):
634
+ if note_guid == "id3" :
635
+ raise EDAMSystemException (
636
+ errorCode = EDAMErrorCode .INTERNAL_ERROR , message = "Test error"
637
+ )
638
+
639
+ return Note (
640
+ guid = note_guid ,
641
+ title = "test" ,
642
+ content = "test" ,
643
+ notebookGuid = "test" ,
644
+ contentLength = 100 ,
645
+ active = True ,
646
+ )
647
+
648
+ mock_get_note = mocker .patch (
649
+ "evernote_backup.evernote_client_sync.EvernoteClientSync.get_note"
650
+ )
651
+ mock_get_note .side_effect = fake_get_note
652
+
653
+ cli_invoker ("sync" , "--database" , "fake_db" )
654
+
655
+ assert "INTERNAL_ERROR - Test error" in caplog .text
656
+ assert (
657
+ "Note 'test' will be skipped for this run and retried during the next sync"
658
+ in caplog .text
659
+ )
660
+
661
+
662
+ @pytest .mark .usefixtures ("fake_init_db" )
663
+ def test_sync_edam_rate_limit_exception_while_download (
664
+ cli_invoker , mock_evernote_client , fake_storage , mocker , caplog
665
+ ):
666
+ test_notes = [Note (guid = f"id{ i } " , title = "test" ) for i in range (10 )]
667
+
668
+ mock_evernote_client .fake_notes .extend (test_notes )
669
+
670
+ def fake_get_note (note_guid ):
671
+ if note_guid == "id3" :
672
+ raise EDAMSystemException (
673
+ errorCode = EDAMErrorCode .RATE_LIMIT_REACHED ,
674
+ message = "Test rate limit" ,
675
+ rateLimitDuration = 10 ,
676
+ )
677
+
678
+ return Note (
679
+ guid = note_guid ,
680
+ title = "test" ,
681
+ content = "test" ,
682
+ notebookGuid = "test" ,
683
+ contentLength = 100 ,
684
+ active = True ,
685
+ )
686
+
687
+ mock_get_note = mocker .patch (
688
+ "evernote_backup.evernote_client_sync.EvernoteClientSync.get_note"
689
+ )
690
+ mock_get_note .side_effect = fake_get_note
691
+
692
+ with pytest .raises (EDAMSystemException ) as excinfo :
693
+ cli_invoker ("sync" , "--database" , "fake_db" )
694
+
695
+ assert excinfo .value .message == "Test rate limit"
696
+ assert excinfo .value .errorCode == EDAMErrorCode .RATE_LIMIT_REACHED
697
+ assert excinfo .value .rateLimitDuration == 10
620
698
621
699
622
700
@pytest .mark .usefixtures ("fake_init_db" )
623
701
def test_sync_exception_while_download_retry_fail (
624
- cli_invoker , mock_evernote_client , fake_storage , mocker
702
+ cli_invoker , mock_evernote_client , fake_storage , mocker , caplog
625
703
):
626
- test_notes = [Note (guid = f"id{ i } " , title = "test" ) for i in range (100 )]
704
+ test_notes = [Note (guid = f"id{ i } " , title = "test" ) for i in range (10 )]
627
705
628
706
mock_evernote_client .fake_notes .extend (test_notes )
629
707
630
708
def fake_get_note (note_guid ):
631
- if note_guid == "id10 " :
709
+ if note_guid == "id3 " :
632
710
raise struct .error
633
711
634
712
return Note (
@@ -645,10 +723,13 @@ def fake_get_note(note_guid):
645
723
)
646
724
mock_get_note .side_effect = fake_get_note
647
725
648
- with pytest .raises (RuntimeError ) as excinfo :
649
- cli_invoker ("sync" , "--database" , "fake_db" )
726
+ cli_invoker ("sync" , "--database" , "fake_db" )
650
727
651
- assert "Failed to download note" in str (excinfo .value )
728
+ assert "Failed to download note [id3] after 5 attempts" in caplog .text
729
+ assert (
730
+ "Note 'test' will be skipped for this run and retried during the next sync"
731
+ in caplog .text
732
+ )
652
733
653
734
654
735
@pytest .mark .usefixtures ("fake_init_db" )
0 commit comments