Пример #1
0
class TestEvernoteDetach(ElementTreeEqualExtension):
    @patch('my_evernote.EvernoteApiWrapper._init_en_client')
    @patch('wordpress.WordPressApiWrapper._init_wp_client')
    @patch('common.logging')
    def setUp(self, mock_logging, mock_init_wp_client, mock_init_en_client):
        super(TestEvernoteDetach, self).setUp()
        wordpress_evernote.logger = self.wp_en_logger = MagicMock()
        self.evernote = EvernoteApiWrapper(token='123')
        self.evernote.get_note = MagicMock(side_effect=mocked_get_note)
        self.evernote.updateNote = MagicMock()
        self.wordpress = WordPressApiWrapper('xmlrpc.php', 'user', 'password')
        self.adaptor = EvernoteWordpressAdaptor(self.evernote, self.wordpress)
    
    def test_eekie_detach(self):
        note_to_detach = EvernoteNote(
            guid='abcd1234-0011-2233-4455-abcd1234abcd',
            title='',
            notebookGuid='abcd1234-5678-1928-7890-abcd1234abcd',
            content='note-to-detach.xml')
        attrs_to_update = {'id': '<auto>',
                           'link': '<auto>',
                           'last_modified': '<auto>',
                           'published_date':  '<auto>',}
        self.adaptor.update_note_metdata(note_to_detach, attrs_to_update)
        expected_detached_note = EvernoteNote(
            guid='abcd1234-0011-2233-4455-abcd1234abcd',
            title='',
            notebookGuid='abcd1234-5678-1928-7890-abcd1234abcd',
            content='note-detached.xml')
        self.assertETfromStrEqual(expected_detached_note.content,
                                  note_to_detach.content)
Пример #2
0
class TestEvernoteWordPressParser(ElementTreeEqualExtension):
    @patch('my_evernote.EvernoteApiWrapper._init_en_client')
    def setUp(self, mock_init_en_client):
        wordpress.logger = Mock()
        wordpress_evernote.logger = Mock()
        self.evernote = EvernoteApiWrapper(token='123')
        self.evernote.get_note = MagicMock(side_effect=mocked_get_note)
        self.adaptor = EvernoteWordpressAdaptor(self.evernote, None)
    
    def test_evernote_wpitem_normalize(self):
        note = test_notes['note-with-id-thumbnail-attached-image-body-link']
        normalized_tree = self.adaptor._parse_note_xml(note.content)
        expected_note = EvernoteNote(
            guid=note.guid,
            title=note.title,
            notebookGuid=note.notebookGuid,
            content='normalized-note-1.xml')
        expected_tree = self.adaptor._parse_xml_from_string(
            expected_note.content)
        self.assertElementTreeEqual(expected_tree, normalized_tree)
    
    def test_evernote_image_parser(self):
        note = test_notes['image-with-id']
        wp_image = self.adaptor.wp_item_from_note(note.guid)
        self.assertIsInstance(wp_image, WordPressImageAttachment)
        self.assertEqual(277, wp_image.id)
        self.assertEqual('Test image', wp_image.title)
        self.assertEqual('test.png', wp_image._filename)
        self.assertEqual('http://www.ostricher.com/images/test.png',
                         wp_image.link)
        self.assertEqual('Image caption', wp_image.caption)
        self.assertIsNone(wp_image.published_date)
        self.assertEqual('Description of test image',
                         wp_image.description)
        self.assertIsInstance(wp_image.parent, WordPressPost)
        self.assertEqual(544, wp_image.parent.id)
        self.assertDictEqual(dict(), wp_image._ref_wp_items)
    
    def test_evernote_post_parser(self):
        note = test_notes['note-with-id-thumbnail-attached-image-body-link']
        wp_post = self.adaptor.wp_item_from_note(note.guid)
        self.assertIsInstance(wp_post, WordPressPost)
        self.assertEqual('post', wp_post.post_type)
        self.assertEqual('markdown', wp_post.content_format)
        self.assertEqual('Test Post with Title out of Div and = Symbol',
                         wp_post.title)
        self.assertEqual(8, wp_post.hemingway_grade)
        self.assertListEqual(['Meta'], wp_post.categories)
        self.assertListEqual(['Multiword, Tag','test-tag'], wp_post.tags)
        self.assertEqual(544, wp_post.id)
        self.assertEqual('test-post-with-title-out-of-div-and-symbol',
                         wp_post.slug)
        self.assertIsInstance(wp_post.thumbnail, WordPressImageAttachment)
        self.assertEqual('http://www.ostricher.com/images/test.png',
                         wp_post.thumbnail.link)
        with open('test-data/post-content/post-note-1.md', 'r') as content_f:
            expected_content = content_f.read()
        self.assertListEqual(expected_content.split('\n'),
                             wp_post.content.split('\n'))
        # The thumbnail image is **also** expected in _ref_wp_items because
        #  it is also used as an image in the post content.
        links = ['evernote:///view/123/s123/abcd1234-1234-abcd-1234-'
                 'abcd1234abcd/abcd1234-1234-abcd-1234-abcd1234abcd/',
                 'evernote:///view/123/s123/abcd1234-5678-0000-7890-'
                 'abcd1234abcd/abcd1234-5678-0000-7890-abcd1234abcd/']
        self.assertEqual(2, len(wp_post._ref_wp_items))
        for link in links:
            self.assertIn(link, wp_post._ref_wp_items)
            self.assertTrue(hasattr(wp_post._ref_wp_items[link], '__call__'))
    
    def test_evernote_page_parser(self):
        note = test_notes['project-page-with-id-nothumb']
        wp_post = self.adaptor.wp_item_from_note(note.guid)
        self.assertIsInstance(wp_post, WordPressPost)
        self.assertEqual('page', wp_post.post_type)
        self.assertEqual('markdown', wp_post.content_format)
        self.assertEqual('Project index', wp_post.title)
        self.assertIsNone(wp_post.hemingway_grade)
        self.assertListEqual([], wp_post.categories)
        self.assertListEqual([], wp_post.tags)
        self.assertEqual(583, wp_post.id)
        self.assertEqual('project-index',
                         wp_post.slug)
        self.assertIsNone(wp_post.thumbnail)
        self.assertEqual('Nothing to see here.', wp_post.content)
        self.assertDictEqual(dict(), wp_post._ref_wp_items)
    
    def test_evernote_project_post_parser(self):
        note = test_notes['project-note-with-id-nothumb']
        wp_post = self.adaptor.wp_item_from_note(note.guid)
        self.assertIsInstance(wp_post, WordPressPost)
        self.assertEqual('post', wp_post.post_type)
        self.assertEqual('markdown', wp_post.content_format)
        self.assertEqual('Another test note', wp_post.title)
        self.assertEqual(8, wp_post.hemingway_grade)
        self.assertListEqual([], wp_post.categories)
        self.assertListEqual([], wp_post.tags)
        self.assertEqual(303, wp_post.id)
        self.assertEqual('another-test-note', wp_post.slug)
        self.assertIsNone(wp_post.thumbnail)
        self.assertEqual('Nothing to see here .', wp_post.content)
        self.assertIsInstance(wp_post.project, WordPressPost)
        self.assertEqual(583, wp_post.project.id)
        self.assertDictEqual(dict(), wp_post._ref_wp_items)
    
    def test_evernote_link_processor_parser(self):
        note = test_notes['project-note-noid']
        wp_post = self.adaptor.wp_item_from_note(note.guid)
        self.assertIsInstance(wp_post, WordPressPost)
        self.assertEqual('post', wp_post.post_type)
        self.assertEqual('markdown', wp_post.content_format)
        self.assertEqual('New project note', wp_post.title)
        self.assertIsNone(wp_post.id)
        self.assertEqual('new-project-note', wp_post.slug)
        self.assertIsNone(wp_post.thumbnail)
        self.assertEqual("Nothing to see here 583.", wp_post.content)
        self.assertIsInstance(wp_post.project, WordPressPost)
        self.assertEqual(583, wp_post.project.id)
        link = ('evernote:///view/123/s123/abcd1234-aaaa-0000-ffff-'
                'abcd1234abcd/abcd1234-aaaa-0000-ffff-abcd1234abcd/')
        self.assertEqual(1, len(wp_post._ref_wp_items))
        self.assertIn(link, wp_post._ref_wp_items)
        self.assertTrue(hasattr(wp_post._ref_wp_items[link], '__call__'))
        self.assertEqual(
            self.adaptor.cache['abcd1234-aaaa-0000-ffff-abcd1234abcd'],
            wp_post._ref_wp_items[link]())
    
    def test_regression_nested_elements(self):
        note = test_notes['regression-projnote-span-br']
        wp_post = self.adaptor.wp_item_from_note(note.guid)
        self.assertIsInstance(wp_post, WordPressPost)
        self.assertEqual('post', wp_post.post_type)
        self.assertEqual('markdown', wp_post.content_format)
        self.assertEqual('A post in the project', wp_post.title)
        self.assertEqual('ItamarO', wp_post.author)
        self.assertEqual(9, wp_post.hemingway_grade)
        self.assertListEqual(['Meta'], wp_post.categories)
        self.assertListEqual(['side-projects'], wp_post.tags)
        self.assertIsNone(wp_post.id)
        self.assertIsNone(wp_post.published_date)
        self.assertIsNone(wp_post.last_modified)
        self.assertIsNone(wp_post.parent)
        self.assertIsNone(wp_post.thumbnail)
        expected_content_lines = [
            'Nothing to see here.', '',
            '[sb_easy_image ids="277" size="medium" columns="1" '
            'link="Lightbox"]', '',
            'Hello.', '',
            '[sb_easy_image ids="277" size="medium" columns="1" '
            'link="Lightbox"]', '',
            'Media tag in span '
            ]
        self.assertListEqual(expected_content_lines,
                             wp_post.content.split('\n'))
        project = wp_post.project
        self.assertIsInstance(project, WordPressPost)
        self.assertIsNone(project.thumbnail)
        self.assertEqual('The Ostrich Website', project.title)
        self.assertEqual('This is the index page for "The Ostrich" website '
                         'project.', project.content)
    
    def test_regression_divless_link_attr(self):
        note = EvernoteNote(
            guid='1',
            title='1',
            notebookGuid='abcd1234-5678-abef-7890-abcd1234abcd',
            content='regression-projnote-divless-link.xml')
        normalized_tree = self.adaptor._parse_note_xml(note.content)
        expected_note = EvernoteNote(
            guid=note.guid,
            title=note.title,
            notebookGuid=note.notebookGuid,
            content='normalized-projnote-divless-link.xml')
        expected_tree = self.adaptor._parse_xml_from_string(
            expected_note.content)
        self.assertElementTreeEqual(expected_tree, normalized_tree)
    
    def test_regression_losing_whitespace(self):
        self.evernote.updateNote = MagicMock()
        note = EvernoteNote(
            guid='2',
            title='2',
            notebookGuid='abcd1234-5678-abef-7890-abcd1234abcd',
            content='regression-losing-whitespace.xml')
        expected_content = note.content.replace('123', '456').encode('utf-8')
        self.adaptor.update_note_metdata(note, {'id': '456'})
        self.assertListEqual(expected_content.split('\n'),
                             note.content.split('\n'))
        self.assertIsInstance(note.content, str)
    
    def test_regression_div_br_redundant_blanks(self):
        expected_content_lines = [
            'Nothing to see here.',
            'Div with text that ends with "br" tag should not result two '
            'blank lines!',
            'Am I right?',
            ]
        note = EvernoteNote(
            guid='3',
            title='3',
            notebookGuid='abcd1234-3432-abef-fede-abcd1234abcd',
            content='regression-note-div-br.xml')
        wp_post = self.adaptor.wp_item_from_note(note)
        self.assertIsInstance(wp_post, WordPressPost)
        self.assertListEqual(expected_content_lines,
                             wp_post.content.split('\n'))