Esempio n. 1
0
class EditorTestCase(unittest.TestCase):
    def setUp(self):
        self.service = ProviderService()
        self.service._session = get_db_session()
        models.Note.session = self.service._session 
        self.app = app
        app.update(self.service)
        notebook = Notebook.from_tuple(
            self.service.create_notebook('test'),
        )
        self.note = Note.from_tuple(self.service.create_note(Note(
            id=NONE_ID,
            title='New note',
            content="New note content",
            tags=[],
            notebook=notebook.id,
            created=NONE_VAL,
            updated=NONE_VAL,
            place='',
        ).struct))

    def test_content_nochange(self):
        """Test content nochange"""
        editor = Editor(self.note)
        self.assertEqual(
            editor.note_edit.content,
            "New note content",
        )
        for content in CONTENTS:
            editor.note_edit.content = content
            self.assertEqual(
                editor.note_edit.content,
                content,
            )

    def test_content_changing(self):
        """Test content changing"""
        editor = Editor(self.note)
        for prev, current in CHANGING_CONTENTS:
            editor.note_edit.content = prev
            self.assertEqual(
                editor.note_edit.content,
                current,
            )

    def test_title_nochange(self):
        """Test title nochange"""
        editor = Editor(self.note)
        self.assertEqual(
            editor.note_edit.title,
            "New note",
        )
        for title in TITLES:
            editor.note_edit.title = title
            self.assertEqual(
                editor.note_edit.title,
                title,
            )
Esempio n. 2
0
 def setUp(self):
     self.service = ProviderService()
     self.session = get_db_session()
     self.service._session = self.session
     models.Note.session = self.session
     self.service.qobject = MagicMock()
     self.service.app = MagicMock()
     self.service.sync = MagicMock()
     factories.invoke_session(self.session)
Esempio n. 3
0
 def setUp(self):
     self.session = get_db_session(':memory:')
     self.store = get_note_store(token)
     self.sc = SyncThread()
     self.sc.session = self.session
     self.sc.sq = self.session.query
     self.sc.note_store = self.store
     self.sc.auth_token = token
     self.serv = ProviderService()
     self.serv._session = self.session
     models.Note.session = self.session  # hack for test
Esempio n. 4
0
 def setUp(self):
     self.service = ProviderService()
     self.session = get_db_session()
     self.service._session = self.session
     self.service.qobject = MagicMock()
     self.service.app = MagicMock()
     self.service.sync = MagicMock()
Esempio n. 5
0
 def setUp(self):
     self.service = ProviderService()
     self.session = get_db_session()
     self.service._session = self.session
     models.Note.session = self.session
     self.service.qobject = MagicMock()
     self.service.app = MagicMock()
     self.service.sync = MagicMock()
     factories.invoke_session(self.session)
Esempio n. 6
0
 def setUp(self):
     self.service = ProviderService()
     self.service._session = get_db_session()
     models.Note.session = self.service._session
     self.app = app
     self.app.update(self.service)
     notebook = Notebook.from_tuple(
         self.service.create_notebook('test', None),
     )
     self.note = Note.from_tuple(self.service.create_note(Note(
         id=NONE_ID,
         title='New note',
         content="New note content",
         tags=[],
         notebook=notebook.id,
         created=NONE_VAL,
         updated=NONE_VAL,
         place='',
     ).struct))
Esempio n. 7
0
 def setUp(self):
     self.session = get_db_session(':memory:')
     self.store = get_note_store(token)
     self.sc = SyncThread()
     self.sc.session = self.session
     self.sc.sq = self.session.query
     self.sc.note_store = self.store
     self.sc.auth_token = token
     self.serv = ProviderService()
     self.serv._session = self.session
     models.Note.session = self.session  # hack for test
Esempio n. 8
0
 def __init__(self, verbose, *args, **kwargs):
     App.__init__(self, *args, **kwargs)
     self.settings = QSettings('everpad', 'everpad-provider')
     self.verbose = verbose
     session_bus = dbus.SessionBus()
     self.bus = dbus.service.BusName("com.everpad.Provider", session_bus)
     self.service = ProviderService(self, session_bus, '/EverpadProvider')
     self.sync_thread = SyncThread(self)
     self.sync_thread.sync_state_changed.connect(
         Slot(int)(self.service.sync_state_changed), )
     self.sync_thread.data_changed.connect(
         Slot()(self.service.data_changed), )
     if get_auth_token():
         self.sync_thread.start()
     self.service.qobject.authenticate_signal.connect(
         self.on_authenticated, )
     self.service.qobject.remove_authenticate_signal.connect(
         self.on_remove_authenticated, )
Esempio n. 9
0
 def setUp(self):
     self.service = ProviderService()
     self.service._session = get_db_session()
     models.Note.session = self.service._session
     self.app = app
     app.update(self.service)
     notebook = Notebook.from_tuple(
         self.service.create_notebook('test', None),
     )
     self.note = Note.from_tuple(self.service.create_note(Note(
         id=NONE_ID,
         title='New note',
         content="New note content",
         tags=[],
         notebook=notebook.id,
         created=NONE_VAL,
         updated=NONE_VAL,
         place='',
     ).struct))
Esempio n. 10
0
class FindTestCase(unittest.TestCase):
    """Find notes method test case"""

    def setUp(self):
        self._create_service()
        self._create_notebooks()
        self._create_notes()

    def _create_notes(self):
        """Create notes"""
        notes = [
            self.service.create_note(btype.Note(
                title='New note',
                content="New note content",
                tags=['ab', 'cd'],
                notebook=self.notebook.id,
                created=const.NONE_VAL,
                updated=const.NONE_VAL,
                place='first',
                pinnded=False,
            ).struct),
            self.service.create_note(btype.Note(
                title='Old note',
                content="Old note content",
                tags=['ef', 'gh'],
                notebook=self.notebook2.id,
                created=const.NONE_VAL,
                updated=const.NONE_VAL,
                place='second',
                pinnded=False,
            ).struct),
            self.service.create_note(btype.Note(
                title='not',
                content="oke",
                tags=['ab', 'gh'],
                notebook=self.notebook.id,
                created=const.NONE_VAL,
                updated=const.NONE_VAL,
                place='second',
                pinnded=True,
            ).struct),
            self.service.create_note(btype.Note(
                title=u'Заметка',
                content=u"Заметка",
                tags=[u'тэг'],
                notebook=self.notebook.id,
                created=const.NONE_VAL,
                updated=const.NONE_VAL,
                place=u'место',
                pinnded=False,
            ).struct),
            self.service.create_note(btype.Note(
                title=u'заметка',
                content=u"заметка",
                tags=[u'тэг'],
                notebook=self.notebook.id,
                created=const.NONE_VAL,
                updated=const.NONE_VAL,
                place=u'место',
                pinnded=False,
            ).struct),
        ]
        self.notes = btype.Note.list << [
            self.service.update_note(note) for note in notes
        ]

    def _create_notebooks(self):
        """Create notebooks"""
        self.notebook =\
            btype.Notebook << self.service.create_notebook('test', None)
        self.notebook2 =\
            btype.Notebook << self.service.create_notebook('test2', None)
        self.notebook3 =\
            btype.Notebook << self.service.create_notebook(u'Блокнот', None)

    def _create_service(self):
        """Create service"""
        self.service = ProviderService()
        self.service._session = get_db_session()
        models.Note.session = self.service._session  # TODO: fix that shit

    def _to_ids(self, items):
        return set(map(lambda item: item.id, items))

    def _find(self, *args, **kwargs):
        return btype.Note.list << self.service.find_notes(*args, **kwargs)

    def test_by_words(self):
        """Test notes find by words"""
        all_notes = self._find(
            'not', dbus.Array([], signature='i'),
            dbus.Array([], signature='i'), 0,
            100, const.ORDER_UPDATED_DESC, -1,
        )
        self.assertItemsEqual(
            self._to_ids(all_notes), self._to_ids(self.notes[:-2]),
        )
        two = self._find(
            'note', dbus.Array([], signature='i'),
            dbus.Array([], signature='i'), 0,
            100, const.ORDER_UPDATED_DESC, -1,
        )
        self.assertItemsEqual(
            self._to_ids(two), self._to_ids(self.notes[:2]),
        )
        blank = self._find(
            'not note', dbus.Array([], signature='i'),
            dbus.Array([], signature='i'), 0,
            100, const.ORDER_UPDATED_DESC, -1,
        )
        self.assertEqual(len(blank), 0)

    def test_by_tags(self):
        """Test note find by tags"""
        tags = btype.Tag.list << self.service.list_tags()
        first_last = self._find(
            '', dbus.Array([], signature='i'),
            [tags[0].id], 0, 100, const.ORDER_UPDATED_DESC, -1,
        )
        self.assertItemsEqual(
            self._to_ids(first_last), [self.notes[0].id, self.notes[2].id],
        )
        second = self._find(
            '', dbus.Array([], signature='i'),
            [tags[2].id], 0, 100,
            const.ORDER_UPDATED_DESC, -1,
        )
        self.assertItemsEqual(
            self._to_ids(second), [self.notes[1].id],
        )
        all_notes = self._find(
            '', dbus.Array([], signature='i'),
            map(lambda tag: tag.id, tags), 0, 100,
            const.ORDER_UPDATED_DESC, -1,
        )
        self.assertItemsEqual(
            self._to_ids(all_notes), self._to_ids(self.notes),
        )

    def test_by_notebooks(self):
        """Test find note by notebooks"""
        all_notebooks = self._find(
            '', self._to_ids([self.notebook, self.notebook2]),
            dbus.Array([], signature='i'), 0,
            100, const.ORDER_UPDATED_DESC, -1,
        )
        self.assertItemsEqual(
            self._to_ids(all_notebooks), self._to_ids(self.notes),
        )
        second = self._find(
            '', [self.notebook2.id],
            dbus.Array([], signature='i'), 0,
            100, const.ORDER_UPDATED_DESC, -1,
        )
        self.assertItemsEqual(
            self._to_ids(second), [self.notes[1].id],
        )

    def test_combine(self):
        """Test find by combination"""
        places = btype.Place.list << self.service.list_places()
        tags = btype.Tag.list << self.service.list_tags()
        first = self._find(
            'new', [self.notebook.id], [tags[0].id], places[0].id,
            100, const.ORDER_UPDATED_DESC, False,
        )
        self.assertItemsEqual(
            self._to_ids(first), [self.notes[0].id],
        )
        last = self._find(
            'oke', [self.notebook.id], [tags[0].id], places[1].id,
            100, const.ORDER_UPDATED_DESC, True,
        )
        self.assertItemsEqual(
            self._to_ids(last), [self.notes[2].id],
        )

    def test_unicode_ignorecase(self):
        """Test unicode ignorecase"""
        all_notes = self._find(
            u'заметка', dbus.Array([], signature='i'),
            dbus.Array([], signature='i'), 0,
            100, const.ORDER_UPDATED_DESC, -1,
        )
        self.assertItemsEqual(
            self._to_ids(all_notes), self._to_ids(self.notes[-2:]),
        )
Esempio n. 11
0
class MethodsCase(unittest.TestCase):
    """Case for dbus shortcuts"""

    def setUp(self):
        self.service = ProviderService()
        self.session = get_db_session()
        self.service._session = self.session
        self.service.qobject = MagicMock()
        self.service.app = MagicMock()
        self.service.sync = MagicMock()

    def tearDown(self):
        self.session.flush()

    def _create_note(self, **kwargs):
        """Create note"""
        note = models.Note(
            title='note',
            action=const.ACTION_NONE,
            **kwargs
        )
        self.session.add(note)
        self.session.commit()
        return note

    def test_get_note(self):
        """Test get note method"""
        note = self._create_note()

        remote_note = btype.Note << self.service.get_note(note.id)
        self.assertEqual(remote_note.title, note.title)

    def test_get_note_by_guid(self):
        """Test get note method"""
        note = self._create_note(guid='guid')
        self.session.add(note)
        self.session.commit()

        remote_note = btype.Note << self.service.get_note_by_guid(note.guid)
        self.assertEqual(remote_note.title, note.title)

    def test_get_note_alternatives(self):
        """Test get note alternatives"""
        note = self._create_note(guid='guid')

        alternative = models.Note(
            title='title',
            guid='guid',
            action=const.ACTION_CONFLICT,
            conflict_parent_id=note.id,
        )
        self.session.add(alternative)
        self.session.commit()

        remote_notes = btype.Note.list << self.service.get_note_alternatives(
            note.id,
        )
        self.assertEqual(remote_notes[0].id, alternative.id)

    def test_list_notebooks(self):
        """Test list notebooks method"""
        notebooks = []
        for name in range(10):
            notebook = models.Notebook(
                name=str(name),
                action=const.ACTION_NONE,
            )
            self.session.add(notebook)
            self.session.commit()
            notebooks.append(notebook.id)

        remote_notebooks = btype.Notebook.list << self.service.list_notebooks()
        ids = [notebook.id for notebook in remote_notebooks]

        self.assertItemsEqual(notebooks, ids)

    def test_get_notebook(self):
        """Test get notebook method"""
        notebook = models.Notebook(
            name='notebook',
            action=const.ACTION_NONE,
        )
        deleted_notebook = models.Notebook(
            name='deleted notebook',
            action=const.ACTION_DELETE,
        )
        self.session.add(notebook)
        self.session.add(deleted_notebook)
        self.session.commit()

        remote_notebook = btype.Notebook << self.service.get_notebook(
            notebook.id,
        )
        self.assertEqual(notebook.name, remote_notebook.name)

        with self.assertRaises(DBusException):
            self.service.get_notebook(
                deleted_notebook.id,
            )

    def test_get_notebook_notes_count(self):
        """Test get notebook notes count method"""
        notebook = models.Notebook(
            name='notebook',
            action=const.ACTION_NONE,
        )
        self.session.add(notebook)

        count = 10
        for i in range(count):
            self.session.add(models.Note(
                title='note',
                action=const.ACTION_NONE,
                notebook=notebook,
            ))
        self.session.commit()

        self.assertEqual(
            self.service.get_notebook_notes_count(notebook.id), 10,
        )

    def test_update_notebook(self):
        """Test update notebook method"""
        notebook = models.Notebook(
            name='notebook',
            action=const.ACTION_NONE,
        )
        self.session.add(notebook)
        self.session.commit()

        new_name = 'name'

        notebook_btype = btype.Notebook.from_obj(notebook)
        notebook_btype.name = new_name

        notebook_btype = btype.Notebook << self.service.update_notebook(
            notebook_btype.struct,
        )

        self.assertEqual(notebook_btype.name, new_name)
        self.assertEqual(notebook.name, new_name)

    def test_delete_notebook(self):
        """Test delete notebook"""
        notebook = models.Notebook(
            name='notebook',
            action=const.ACTION_NONE,
        )
        self.session.add(notebook)
        self.session.commit()

        self.service.delete_notebook(notebook.id)

        self.assertEqual(notebook.action, const.ACTION_DELETE)

    def test_list_tags(self):
        """Test list tags"""
        tags = []

        for name in range(10):
            tag = models.Tag(
                name=str(name),
                action=const.ACTION_NONE,
            )
            self.session.add(tag)
            self.session.commit()
            tags.append(tag.id)

        remote_tags = btype.Tag.list << self.service.list_tags()
        tags_ids = [tag.id for tag in remote_tags]

        self.assertEqual(tags_ids, tags)

    def test_get_tag_notes_count(self):
        """Test get tag notes count method"""
        tag = models.Tag(
            name='tag',
            action=const.ACTION_NONE,
        )
        self.session.add(tag)

        count = 10
        for i in range(count):
            self.session.add(models.Note(
                title='note',
                action=const.ACTION_NONE,
                tags=[tag],
            ))
        self.session.commit()

        self.assertEqual(
            self.service.get_tag_notes_count(tag.id), 10,
        )

    def test_delete_tag(self):
        """Test delete tag"""
        tag = models.Tag(
            name='tag',
            action=const.ACTION_NONE,
        )
        self.session.add(tag)
        self.session.commit()

        self.service.delete_tag(tag.id)

        self.assertEqual(tag.action, const.ACTION_DELETE)

    def test_update_tag(self):
        """Test update tag method"""
        tag = models.Tag(
            name='tag',
            action=const.ACTION_NONE,
        )
        self.session.add(tag)
        self.session.commit()

        new_name = 'name'

        tag_btype = btype.Tag.from_obj(tag)
        tag_btype.name = new_name

        tag_btype = btype.Tag << self.service.update_tag(
            tag_btype.struct,
        )

        self.assertEqual(tag_btype.name, new_name)
        self.assertEqual(tag.name, new_name)

    def test_create_note(self):
        """Test create note"""
        title = 'note'
        note_btype = btype.Note(
            title=title,
            tags=[],
            id=const.NONE_ID,
        )

        note_btype = btype.Note << self.service.create_note(
            btype.Note >> note_btype,
        )
        note = self.session.query(models.Note).filter(
            models.Note.id == note_btype.id,
        ).one()

        self.assertEqual(note_btype.title, title)
        self.assertEqual(note.title, title)

    def test_update_note(self):
        """Test update note"""
        note = self._create_note()

        new_title = 'title'

        note_btype = btype.Note.from_obj(note)
        note_btype.title = new_title

        note_btype = btype.Note << self.service.update_note(
            note_btype.struct,
        )
        note = self.session.query(models.Note).filter(
            models.Note.id == note_btype.id,
        ).one()

        self.assertEqual(note_btype.title, new_title)
        self.assertEqual(note.title, new_title)

    def test_get_note_resources(self):
        """Test get note resources"""
        note = self._create_note()

        resource = models.Resource(
            file_name='name',
            action=const.ACTION_NONE,
            note_id=note.id,
        )
        self.session.add(resource)
        self.session.commit()

        resources_btype = btype.Resource.list << self.service.get_note_resources(
            note.id,
        )

        self.assertEqual(resources_btype[0].file_name, resource.file_name)

    def test_update_note_resources(self):
        """Test update note resources"""
        note = self._create_note()

        resource = btype.Resource(
            file_name='test',
        )

        self.service.update_note_resources(
            note.id, btype.Resource.list >> [resource],
        )

        resource = self.session.query(models.Resource).one()
        self.assertEqual(resource.file_name, 'test')

    def test_delete_note(self):
        """Test delete note"""
        note = self._create_note()

        self.service.delete_note(note.id)

        self.assertEqual(note.action, const.ACTION_DELETE)

    def test_create_notebook(self):
        """Test create notebook"""
        notebook_btype =\
            btype.Notebook << self.service.create_notebook('test', 'test')

        notebook = self.session.query(models.Notebook).filter(
            models.Notebook.id == notebook_btype.id,
        ).one()
        self.assertEqual(notebook.name, 'test')

    def test_authenticate(self):
        """Test authenticate"""
        self.service.authenticate('test')
        self.service.qobject\
            .remove_authenticate_signal.emit.assert_called_once_with()
        self.service.qobject\
            .authenticate_signal.emit.assert_called_once_with('test')

    def test_remove_authentication(self):
        """Test remove authentication"""
        self.service.remove_authentication()
        self.service.qobject\
            .remove_authenticate_signal.emit.assert_called_once_with()

    def test_list_places(self):
        """Test list places"""
        place_ids = []
        for num in range(10):
            place = models.Place(
                name='{}'.format(num),
            )
            self.session.add(place)
            self.session.commit()
            place_ids.append(place.id)

        places_btype = btype.Place.list << self.service.list_places()
        for place_btype in places_btype:
            self.assertIn(place_btype.id, place_ids)

    def test_share_note(self):
        """Test share note"""
        note = self._create_note()

        self.service.share_note(note.id)
        self.assertEqual(note.share_status, const.SHARE_NEED_SHARE)
        self.service.sync.assert_called_once_with()

    def test_stop_sharing_note(self):
        """Test stop sharing note"""
        note = self._create_note(share_status=const.SHARE_SHARED)

        self.service.stop_sharing_note(note.id)
        self.assertEqual(note.share_status, const.SHARE_NEED_STOP)
        self.service.sync.assert_called_once_with()

    def test_is_first_synced(self):
        """Test is first synced"""
        self.assertFalse(self.service.is_first_synced())
Esempio n. 12
0
class TestProvider(unittest.TestCase):
    def setUp(self):
        self.session = get_db_session(':memory:')
        self.store = get_note_store(token)
        self.sc = SyncThread()
        self.sc.session = self.session
        self.sc.sq = self.session.query
        self.sc.note_store = self.store
        self.sc.auth_token = token
        self.serv = ProviderService()
        self.serv._session = self.session
        models.Note.session = self.session  # hack for test

    def test_local_sync(self):
        note = models.Note(
            title='test', content='test',
            action=ACTION_CREATE,
        )
        self.session.add(note)
        self.sc.local_changes()
        self.assertEqual(
            'test',
            self.sc.note_store.getNote(
                token, note.guid,
                True, False, False, False,
            ).title,
            'sync simple note',
        )
        notebook = models.Notebook(
            name=str(datetime.now()),
            default=False,
            action=ACTION_CREATE,
        )
        self.session.add(notebook)
        note.notebook = notebook
        note.action = ACTION_CHANGE
        self.sc.local_changes()
        self.assertEqual(
            notebook.guid,
            self.sc.note_store.getNote(
                token, note.guid, True,
                False, False, False,
            ).notebookGuid,
            'sync note with notebook',
        )
        tag = models.Tag(
            name=str(datetime.now()),
            action=ACTION_CREATE,
        )
        self.session.add(tag)
        note.action = ACTION_CHANGE
        note.tags = [tag]
        self.sc.local_changes()
        self.assertEqual(
            [tag.guid],
            self.sc.note_store.getNote(
                token, note.guid, True,
                False, False, False,
            ).tagGuids,
            'sync note with tags',
        )
        notebook.name = str(datetime.now())
        notebook.action = ACTION_CHANGE
        self.sc.local_changes()
        self.assertEqual(
            notebook.name,
            self.sc.note_store.getNotebook(
                token, notebook.guid,
            ).name,
            'sync notebook change',
        )
        tag.name = str(datetime.now())
        tag.action = ACTION_CHANGE
        self.sc.local_changes()
        self.assertEqual(
            tag.name,
            self.sc.note_store.getTag(
                token, tag.guid,
            ).name,
            'sync tag change',
        )
        note.action = ACTION_DELETE
        self.sc.local_changes()
        self.assertIsNotNone(self.sc.note_store.getNote(
            token, note.guid, True,
            False, False, False,
        ), 'remove note')

    def test_remote_sync(self):
        notebook = self.sc.note_store.createNotebook(
            self.sc.auth_token, Notebook(
                name=str(datetime.now()),
                defaultNotebook=False,
            ),
        )
        self.sc.remote_changes()
        self.assertIsNotNone(self.sc.sq(models.Notebook).filter(
            models.Notebook.guid == notebook.guid,
        ).one(), 'sync remote notebook')
        tag = self.sc.note_store.createTag(
            self.sc.auth_token, Tag(name=str(datetime.now())),
        )
        self.sc.remote_changes()
        self.assertIsNotNone(self.sc.sq(models.Tag).filter(
            models.Tag.guid == tag.guid,
        ).one(), 'sync remote tag')
        note = self.sc.note_store.createNote(
            self.sc.auth_token, Note(
                title='asd',
                content="""
                    <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
                    <en-note>%s</en-note>
                """,
            )
        )
        self.sc.remote_changes()
        self.assertIsNotNone(self.sc.sq(models.Note).filter(
            models.Note.guid == note.guid,
        ).one(), 'sync remote note')
        notebook.name = str(datetime.now())
        self.sc.note_store.updateNotebook(
            self.sc.auth_token, notebook,
        )
        self.sc.remote_changes()
        self.assertEqual(self.sc.sq(models.Notebook).filter(
            models.Notebook.guid == notebook.guid,
        ).one().name, notebook.name, 'update local notebook')
        tag.name = str(datetime.now())
        self.sc.note_store.updateTag(
            self.sc.auth_token, tag,
        )
        self.sc.remote_changes()
        self.assertEqual(self.sc.sq(models.Tag).filter(
            models.Tag.guid == tag.guid,
        ).one().name, tag.name, 'update local tag')
        note.tagGuids = []
        self.sc.note_store.updateNote(
            self.sc.auth_token, note,
        )
        self.sc.remote_changes()
        self.assertEqual(self.sc.sq(models.Note).filter(
            models.Note.guid == note.guid,
        ).one().tags, [], 'update local note')
        self.sc.note_store.deleteNote(
            self.sc.auth_token, note.guid,
        )
        self.sc.remote_changes()
        # with self.assertRaises(NoResultFound):  fails via evernote issue =(
        #     sc.sq(models.Note).filter(
        #         models.Note.guid == note.guid,
        #     ).one().guid

    def test_service_get_note(self):
        note = models.Note(title='123', content='123', action=ACTION_CREATE)
        self.session.add(note)
        self.session.commit()
        self.assertEqual(
            self.serv.get_note(note.id),
            (note.id, '123', '123', None, None, None, []),
            'get note from service',
        )

    def test_service_find_notes(self):
        note = models.Note(title='q ab cd', content='123', action=ACTION_CREATE)
        tag = models.Tag(name='eee')
        notebook = models.Notebook(name='123')
        self.session.add_all([note, tag, notebook])
        note.tags = [tag]
        note.notebook = notebook
        self.session.commit()
        self.assertEqual(
            self.serv.find_notes('ab cd', [notebook.id], [tag.id]),
            [(note.id, u'q ab cd', u'123', None, None, notebook.id, [u'eee'])],
            'find notes via service',
        )

    def test_service_list_notebooks(self):
        notebook = models.Notebook(name='a123')
        self.session.add(notebook)
        self.session.commit()
        self.assertEqual(
            self.serv.list_notebooks(),
            [(notebook.id, u'a123', None)],
            'list notebooks',
        )

    def test_service_get_notebook(self):
        notebook = models.Notebook(name='a123', default=True)
        self.session.add(notebook)
        self.session.commit()
        self.assertEqual(
            self.serv.get_notebook(notebook.id),
            (notebook.id, u'a123', True),
            'get notebook',
        )

    def test_service_list_tags(self):
        tag = models.Tag(name='123')
        self.session.add(tag)
        self.session.commit()
        self.assertEqual(
            self.serv.list_tags(),
            [(tag.id, u'123')],
            'list tags'
        )

    def test_service_create_note(self):
        notebook = models.Notebook(name='a123', default=True)
        self.session.add(notebook)
        self.session.commit()
        note = (None, u'q ab cd', u'123', None, None, notebook.id, [u'eee'])
        self.assertEqual(
            self.serv.create_note(note),
            (1, u'q ab cd', u'123', None, None, notebook.id, [u'eee']),
            'create note via service',
        )

    def test_service_update_note(self):
        notebook = models.Notebook(name='a123', default=True)
        note = models.Note(
            title='123', content='123',
            notebook=notebook,
        )
        self.session.add_all([notebook, note])
        self.session.commit()
        result = self.serv.update_note(
            (note.id, u'q ab cd', u'123', None, None, notebook.id, [u'eee']),
        )
        self.assertEqual(result,
            (note.id, u'q ab cd', u'123', None, None, notebook.id, [u'eee']),
            'update note',
        )

    def test_service_delete_note(self):
        notebook = models.Notebook(name='a123', default=True)
        note = models.Note(
            title='123', content='123',
            notebook=notebook,
        )
        self.session.add_all([notebook, note])
        self.session.commit()
        self.serv.delete_note(note.id)
        self.assertEqual(note.action, ACTION_DELETE, 'delete note')

    def test_service_create_notebook(self):
        notebook = self.serv.create_notebook(
            (None, u'a123', True),
        )
        self.assertEqual(notebook,
            (1, u'a123', True),
            'create notebook',
        )
Esempio n. 13
0
    def __init__(self, verbose, *args, **kwargs):

        # non-kde:
        # from PySide.QtCore import QCoreApplication
        # AppClass = QCoreApplication
        AppClass.__init__(self, *args, **kwargs)

        # ************************************************************
        #                   Configure logger
        # ************************************************************
        # https://docs.python.org/2/library/logging.html
        # good ref:
        # http://victorlin.me/posts/2012/08/26/good-logging-practice-in-python
        # Yes, quite drawn out with all my if verbose, but readable for me when
        # I come back to this in a couple weeks or more

        #logging.basicConfig(level=logging.INFO)

        # create logger and set to debug
        self.logger = logging.getLogger('gevernote-provider')
        self.logger.setLevel(logging.DEBUG)

        fh = logging.FileHandler(
            os.path.expanduser('~/.everpad/logs/gevernote-provider.log'))
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(
            logging.Formatter(
                '%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
        self.logger.addHandler(fh)
        if verbose:
            ch = logging.StreamHandler()
            ch.setLevel(logging.DEBUG)
            ch.setFormatter(logging.Formatter('%(asctime)s - %(message)s'))
            self.logger.addHandler(ch)

        self.logger.info('Logging started.')

        # ref:  http://qt-project.org/doc/qt-4.8/qsettings.html
        #
        # For example, if your product is called Star Runner and your company
        # is called MySoft, you would construct the QSettings object as follows:
        #     QSettings settings("MySoft", "Star Runner");
        #  Backwards?
        self.settings = QSettings('everpad', 'everpad-provider')

        # going to do more here - gsettings
        self.logger.debug('Setting parsed.')

        # Ref: http://excid3.com/blog/an-actually-decent-python-dbus-tutorial/
        # SessionBus because service is a session level daemon
        session_bus = dbus.SessionBus()

        # for future name change
        #self.bus = dbus.service.BusName("com.gevernote.Provider", session_bus)
        #self.service = ProviderService(session_bus, '/GrevernoteProvider')
        self.bus = dbus.service.BusName("com.everpad.Provider", session_bus)
        self.service = ProviderService(session_bus, '/EverpadProvider')

        self.logger.debug("dbus setup complete")

        # subclass PySide.QtCore.QThread  - agent.py
        # setup Sync thread
        self.sync_thread = SyncThread()

        # connect Sync thread sync_state_changed
        self.sync_thread.sync_state_changed.connect(
            Slot(int)(self.service.sync_state_changed), )

        # connect Sync thread data_changed
        self.sync_thread.data_changed.connect(
            Slot()(self.service.data_changed), )

        self.logger.debug("SyncThread init complete")

        # Start Sync Thread if provider is authenticated
        if get_auth_token():
            self.logger.debug('Auth - Starting Sync Thread.')
            self.sync_thread.start()
        else:
            self.logger.debug('No Auth - Sync Thread not started.')

        # ************************************************************
        #    Authentication and Termination Signals Setup
        # ************************************************************

        # provider_authenticate @Slot
        self.service.qobject.authenticate_signal.connect(
            self.provider_authenticate, )
        # on_authenticated @Slot
        #self.service.qobject.authenticate_signal.connect(
        #    self.on_authenticated,
        #)
        # on_remove_authenticated @Slot
        self.service.qobject.remove_authenticate_signal.connect(
            self.on_remove_authenticated, )
        self.service.qobject.terminate.connect(self.terminate)

        self.logger.info('Provider started.')
Esempio n. 14
0
class FindTestCase(unittest.TestCase):
    def setUp(self):
        self.service = ProviderService()
        self.service._session = get_db_session()
        models.Note.session = self.service._session  # TODO: fix that shit
        self.notebook = Notebook.from_tuple(
            self.service.create_notebook('test', None), )
        self.notebook2 = Notebook.from_tuple(
            self.service.create_notebook('test2', None), )
        self.notebook3 = Notebook.from_tuple(
            self.service.create_notebook(u'Блокнот', None), )
        notes = [
            self.service.create_note(
                Note(
                    id=NONE_ID,
                    title='New note',
                    content="New note content",
                    tags=['ab', 'cd'],
                    notebook=self.notebook.id,
                    created=NONE_VAL,
                    updated=NONE_VAL,
                    place='first',
                    pinnded=False,
                ).struct),
            self.service.create_note(
                Note(
                    id=NONE_ID,
                    title='Old note',
                    content="Old note content",
                    tags=['ef', 'gh'],
                    notebook=self.notebook2.id,
                    created=NONE_VAL,
                    updated=NONE_VAL,
                    place='second',
                    pinnded=False,
                ).struct),
            self.service.create_note(
                Note(
                    id=NONE_ID,
                    title='not',
                    content="oke",
                    tags=['ab', 'gh'],
                    notebook=self.notebook.id,
                    created=NONE_VAL,
                    updated=NONE_VAL,
                    place='second',
                    pinnded=True,
                ).struct),
            self.service.create_note(
                Note(
                    id=NONE_ID,
                    title=u'Заметка',
                    content=u"Заметка",
                    tags=[u'тэг'],
                    notebook=self.notebook.id,
                    created=NONE_VAL,
                    updated=NONE_VAL,
                    place=u'место',
                    pinnded=False,
                ).struct),
            self.service.create_note(
                Note(
                    id=NONE_ID,
                    title=u'заметка',
                    content=u"заметка",
                    tags=[u'тэг'],
                    notebook=self.notebook.id,
                    created=NONE_VAL,
                    updated=NONE_VAL,
                    place=u'место',
                    pinnded=False,
                ).struct),
        ]
        self.notes = map(
            lambda note: Note.from_tuple(self.service.update_note(note)),
            notes)

    def _to_ids(self, items):
        return set(map(lambda item: item.id, items))

    def _find(self, *args, **kwargs):
        return map(Note.from_tuple, self.service.find_notes(*args, **kwargs))

    def test_by_words(self):
        """Test notes find by words"""
        all = self._find(
            'not',
            dbus.Array([], signature='i'),
            dbus.Array([], signature='i'),
            0,
            100,
            Note.ORDER_UPDATED_DESC,
            -1,
        )
        self.assertEqual(
            set(self._to_ids(all)),
            set(self._to_ids(self.notes[:-2])),
        )
        two = self._find(
            'note',
            dbus.Array([], signature='i'),
            dbus.Array([], signature='i'),
            0,
            100,
            Note.ORDER_UPDATED_DESC,
            -1,
        )
        self.assertEqual(
            set(self._to_ids(two)),
            set(self._to_ids(self.notes[:2])),
        )
        blank = self._find(
            'not note',
            dbus.Array([], signature='i'),
            dbus.Array([], signature='i'),
            0,
            100,
            Note.ORDER_UPDATED_DESC,
            -1,
        )
        self.assertEqual(len(blank), 0)

    def test_by_tags(self):
        """Test notef find by tags"""
        tags = map(Tag.from_tuple, self.service.list_tags())
        first_last = self._find(
            '',
            dbus.Array([], signature='i'),
            [tags[0].id],
            0,
            100,
            Note.ORDER_UPDATED_DESC,
            -1,
        )
        self.assertEqual(
            set(self._to_ids(first_last)),
            set(self._to_ids([self.notes[0], self.notes[2]])),
        )
        second = self._find(
            '',
            dbus.Array([], signature='i'),
            [tags[2].id],
            0,
            100,
            Note.ORDER_UPDATED_DESC,
            -1,
        )
        self.assertEqual(
            self._to_ids(second),
            set([self.notes[1].id]),
        )
        all = self._find(
            '',
            dbus.Array([], signature='i'),
            map(lambda tag: tag.id, tags),
            0,
            100,
            Note.ORDER_UPDATED_DESC,
            -1,
        )
        self.assertEqual(
            self._to_ids(all),
            self._to_ids(self.notes),
        )

    def test_by_notebooks(self):
        """Test find note by notebooks"""
        all = self._find(
            '',
            self._to_ids([self.notebook, self.notebook2]),
            dbus.Array([], signature='i'),
            0,
            100,
            Note.ORDER_UPDATED_DESC,
            -1,
        )
        self.assertEqual(
            self._to_ids(all),
            self._to_ids(self.notes),
        )
        second = self._find(
            '',
            [self.notebook2.id],
            dbus.Array([], signature='i'),
            0,
            100,
            Note.ORDER_UPDATED_DESC,
            -1,
        )
        self.assertEqual(
            self._to_ids(second),
            set([self.notes[1].id]),
        )

    def test_combine(self):
        """Test find by combination"""
        places = map(Place.from_tuple, self.service.list_places())
        tags = map(Tag.from_tuple, self.service.list_tags())
        first = self._find(
            'new',
            [self.notebook.id],
            [tags[0].id],
            places[0].id,
            100,
            Note.ORDER_UPDATED_DESC,
            False,
        )
        self.assertEqual(
            self._to_ids(first),
            set([self.notes[0].id]),
        )
        last = self._find(
            'oke',
            [self.notebook.id],
            [tags[0].id],
            places[1].id,
            100,
            Note.ORDER_UPDATED_DESC,
            True,
        )
        self.assertEqual(
            self._to_ids(last),
            set([self.notes[2].id]),
        )

    def test_unicode_ignorecase(self):
        """Test unicode ignorecase"""
        all = self._find(
            u'заметка',
            dbus.Array([], signature='i'),
            dbus.Array([], signature='i'),
            0,
            100,
            Note.ORDER_UPDATED_DESC,
            -1,
        )
        self.assertEqual(
            set(self._to_ids(all)),
            set(self._to_ids(self.notes[-2:])),
        )
Esempio n. 15
0
class FindTestCase(unittest.TestCase):
    def setUp(self):
        self.service = ProviderService()
        self.service._session = get_db_session()
        models.Note.session = self.service._session  # TODO: fix that shit
        self.notebook = Notebook.from_tuple(
            self.service.create_notebook('test'),
        )
        self.notebook2 = Notebook.from_tuple(
            self.service.create_notebook('test2'),
        )
        self.notebook3 = Notebook.from_tuple(
            self.service.create_notebook(u'Блокнот'),
        )
        notes = [
            self.service.create_note(Note(
                id=NONE_ID,
                title='New note',
                content="New note content",
                tags=['ab', 'cd'],
                notebook=self.notebook.id,
                created=NONE_VAL,
                updated=NONE_VAL,
                place='first',
                pinnded=False,
            ).struct),
            self.service.create_note(Note(
                id=NONE_ID,
                title='Old note',
                content="Old note content",
                tags=['ef', 'gh'],
                notebook=self.notebook2.id,
                created=NONE_VAL,
                updated=NONE_VAL,
                place='second',
                pinnded=False,
            ).struct),
            self.service.create_note(Note(
                id=NONE_ID,
                title='not',
                content="oke",
                tags=['ab', 'gh'],
                notebook=self.notebook.id,
                created=NONE_VAL,
                updated=NONE_VAL,
                place='second',
                pinnded=True,
            ).struct),
            self.service.create_note(Note(
                id=NONE_ID,
                title=u'Заметка',
                content=u"Заметка",
                tags=[u'тэг'],
                notebook=self.notebook.id,
                created=NONE_VAL,
                updated=NONE_VAL,
                place=u'место',
                pinnded=False,
            ).struct),
            self.service.create_note(Note(
                id=NONE_ID,
                title=u'заметка',
                content=u"заметка",
                tags=[u'тэг'],
                notebook=self.notebook.id,
                created=NONE_VAL,
                updated=NONE_VAL,
                place=u'место',
                pinnded=False,
            ).struct),
        ]
        self.notes = map(lambda note:
            Note.from_tuple(self.service.update_note(note)),
        notes)

    def _to_ids(self, items):
        return set(map(lambda item: item.id, items))

    def _find(self, *args, **kwargs):
        return map(Note.from_tuple,
            self.service.find_notes(*args, **kwargs))

    def test_by_words(self):
        """Test notes find by words"""
        all = self._find(
            'not', dbus.Array([], signature='i'),
            dbus.Array([], signature='i'), 0,
            100, Note.ORDER_UPDATED_DESC, -1,
        )
        self.assertEqual(
            set(self._to_ids(all)), 
            set(self._to_ids(self.notes[:-2])),
        )
        two = self._find(
            'note', dbus.Array([], signature='i'),
            dbus.Array([], signature='i'), 0,
            100, Note.ORDER_UPDATED_DESC, -1,
        )
        self.assertEqual(
            set(self._to_ids(two)),
            set(self._to_ids(self.notes[:2])),
        )
        blank = self._find(
            'not note', dbus.Array([], signature='i'),
            dbus.Array([], signature='i'), 0,
            100, Note.ORDER_UPDATED_DESC, -1,
        )
        self.assertEqual(len(blank), 0)

    def test_by_tags(self):
        """Test notef find by tags"""
        tags = map(Tag.from_tuple, self.service.list_tags())
        first_last = self._find(
            '', dbus.Array([], signature='i'),
            [tags[0].id], 0, 100, Note.ORDER_UPDATED_DESC, -1,
        )
        self.assertEqual(
            set(self._to_ids(first_last)), 
            set(self._to_ids([self.notes[0], self.notes[2]])),
        )
        second = self._find(
            '', dbus.Array([], signature='i'),
            [tags[2].id], 0, 100,
            Note.ORDER_UPDATED_DESC, -1,
        )
        self.assertEqual(
            self._to_ids(second), set([self.notes[1].id]),
        )
        all = self._find(
            '', dbus.Array([], signature='i'),
            map(lambda tag: tag.id, tags), 0, 100,
            Note.ORDER_UPDATED_DESC, -1,
        )
        self.assertEqual(
            self._to_ids(all), self._to_ids(self.notes),
        )

    def test_by_notebooks(self):
        """Test find note by notebooks"""
        all = self._find(
            '', self._to_ids([self.notebook, self.notebook2]),
            dbus.Array([], signature='i'), 0,
            100, Note.ORDER_UPDATED_DESC, -1,
        )
        self.assertEqual(
            self._to_ids(all), self._to_ids(self.notes),
        )
        second = self._find(
            '', [self.notebook2.id],
            dbus.Array([], signature='i'), 0,
            100, Note.ORDER_UPDATED_DESC, -1,
        )
        self.assertEqual(
            self._to_ids(second), set([self.notes[1].id]),
        )

    def test_combine(self):
        """Test find by combination"""
        places = map(Place.from_tuple, self.service.list_places())
        tags = map(Tag.from_tuple, self.service.list_tags())
        first = self._find(
            'new', [self.notebook.id], [tags[0].id], places[0].id,
            100, Note.ORDER_UPDATED_DESC, False,
        )
        self.assertEqual(
            self._to_ids(first), set([self.notes[0].id]),
        )
        last = self._find(
            'oke', [self.notebook.id], [tags[0].id], places[1].id,
            100, Note.ORDER_UPDATED_DESC, True,
        )
        self.assertEqual(
            self._to_ids(last), set([self.notes[2].id]),
        )

    def test_unicode_ignorecase(self):
        """Test unicode ignorecase"""
        all = self._find(
            u'заметка', dbus.Array([], signature='i'),
            dbus.Array([], signature='i'), 0,
            100, Note.ORDER_UPDATED_DESC, -1,
        )
        self.assertEqual(
            set(self._to_ids(all)), 
            set(self._to_ids(self.notes[-2:])),
        )
Esempio n. 16
0
class TestProvider(unittest.TestCase):
    def setUp(self):
        self.session = get_db_session(':memory:')
        self.store = get_note_store(token)
        self.sc = SyncThread()
        self.sc.session = self.session
        self.sc.sq = self.session.query
        self.sc.note_store = self.store
        self.sc.auth_token = token
        self.serv = ProviderService()
        self.serv._session = self.session
        models.Note.session = self.session  # hack for test

    def test_local_sync(self):
        note = models.Note(
            title='test',
            content='test',
            action=ACTION_CREATE,
        )
        self.session.add(note)
        self.sc.local_changes()
        self.assertEqual(
            'test',
            self.sc.note_store.getNote(
                token,
                note.guid,
                True,
                False,
                False,
                False,
            ).title,
            'sync simple note',
        )
        notebook = models.Notebook(
            name=str(datetime.now()),
            default=False,
            action=ACTION_CREATE,
        )
        self.session.add(notebook)
        note.notebook = notebook
        note.action = ACTION_CHANGE
        self.sc.local_changes()
        self.assertEqual(
            notebook.guid,
            self.sc.note_store.getNote(
                token,
                note.guid,
                True,
                False,
                False,
                False,
            ).notebookGuid,
            'sync note with notebook',
        )
        tag = models.Tag(
            name=str(datetime.now()),
            action=ACTION_CREATE,
        )
        self.session.add(tag)
        note.action = ACTION_CHANGE
        note.tags = [tag]
        self.sc.local_changes()
        self.assertEqual(
            [tag.guid],
            self.sc.note_store.getNote(
                token,
                note.guid,
                True,
                False,
                False,
                False,
            ).tagGuids,
            'sync note with tags',
        )
        notebook.name = str(datetime.now())
        notebook.action = ACTION_CHANGE
        self.sc.local_changes()
        self.assertEqual(
            notebook.name,
            self.sc.note_store.getNotebook(
                token,
                notebook.guid,
            ).name,
            'sync notebook change',
        )
        tag.name = str(datetime.now())
        tag.action = ACTION_CHANGE
        self.sc.local_changes()
        self.assertEqual(
            tag.name,
            self.sc.note_store.getTag(
                token,
                tag.guid,
            ).name,
            'sync tag change',
        )
        note.action = ACTION_DELETE
        self.sc.local_changes()
        self.assertIsNotNone(
            self.sc.note_store.getNote(
                token,
                note.guid,
                True,
                False,
                False,
                False,
            ), 'remove note')

    def test_remote_sync(self):
        notebook = self.sc.note_store.createNotebook(
            self.sc.auth_token,
            Notebook(
                name=str(datetime.now()),
                defaultNotebook=False,
            ),
        )
        self.sc.remote_changes()
        self.assertIsNotNone(
            self.sc.sq(models.Notebook).filter(
                models.Notebook.guid == notebook.guid, ).one(),
            'sync remote notebook')
        tag = self.sc.note_store.createTag(
            self.sc.auth_token,
            Tag(name=str(datetime.now())),
        )
        self.sc.remote_changes()
        self.assertIsNotNone(
            self.sc.sq(models.Tag).filter(models.Tag.guid == tag.guid, ).one(),
            'sync remote tag')
        note = self.sc.note_store.createNote(
            self.sc.auth_token,
            Note(
                title='asd',
                content="""
                    <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
                    <en-note>%s</en-note>
                """,
            ))
        self.sc.remote_changes()
        self.assertIsNotNone(
            self.sc.sq(models.Note).filter(
                models.Note.guid == note.guid, ).one(), 'sync remote note')
        notebook.name = str(datetime.now())
        self.sc.note_store.updateNotebook(
            self.sc.auth_token,
            notebook,
        )
        self.sc.remote_changes()
        self.assertEqual(
            self.sc.sq(models.Notebook).filter(
                models.Notebook.guid == notebook.guid, ).one().name,
            notebook.name, 'update local notebook')
        tag.name = str(datetime.now())
        self.sc.note_store.updateTag(
            self.sc.auth_token,
            tag,
        )
        self.sc.remote_changes()
        self.assertEqual(
            self.sc.sq(models.Tag).filter(
                models.Tag.guid == tag.guid, ).one().name, tag.name,
            'update local tag')
        note.tagGuids = []
        self.sc.note_store.updateNote(
            self.sc.auth_token,
            note,
        )
        self.sc.remote_changes()
        self.assertEqual(
            self.sc.sq(models.Note).filter(
                models.Note.guid == note.guid, ).one().tags, [],
            'update local note')
        self.sc.note_store.deleteNote(
            self.sc.auth_token,
            note.guid,
        )
        self.sc.remote_changes()
        # with self.assertRaises(NoResultFound):  fails via evernote issue =(
        #     sc.sq(models.Note).filter(
        #         models.Note.guid == note.guid,
        #     ).one().guid

    def test_service_get_note(self):
        note = models.Note(title='123', content='123', action=ACTION_CREATE)
        self.session.add(note)
        self.session.commit()
        self.assertEqual(
            self.serv.get_note(note.id),
            (note.id, '123', '123', None, None, None, []),
            'get note from service',
        )

    def test_service_find_notes(self):
        note = models.Note(title='q ab cd',
                           content='123',
                           action=ACTION_CREATE)
        tag = models.Tag(name='eee')
        notebook = models.Notebook(name='123')
        self.session.add_all([note, tag, notebook])
        note.tags = [tag]
        note.notebook = notebook
        self.session.commit()
        self.assertEqual(
            self.serv.find_notes('ab cd', [notebook.id], [tag.id]),
            [(note.id, u'q ab cd', u'123', None, None, notebook.id, [u'eee'])],
            'find notes via service',
        )

    def test_service_list_notebooks(self):
        notebook = models.Notebook(name='a123')
        self.session.add(notebook)
        self.session.commit()
        self.assertEqual(
            self.serv.list_notebooks(),
            [(notebook.id, u'a123', None)],
            'list notebooks',
        )

    def test_service_get_notebook(self):
        notebook = models.Notebook(name='a123', default=True)
        self.session.add(notebook)
        self.session.commit()
        self.assertEqual(
            self.serv.get_notebook(notebook.id),
            (notebook.id, u'a123', True),
            'get notebook',
        )

    def test_service_list_tags(self):
        tag = models.Tag(name='123')
        self.session.add(tag)
        self.session.commit()
        self.assertEqual(self.serv.list_tags(), [(tag.id, u'123')],
                         'list tags')

    def test_service_create_note(self):
        notebook = models.Notebook(name='a123', default=True)
        self.session.add(notebook)
        self.session.commit()
        note = (None, u'q ab cd', u'123', None, None, notebook.id, [u'eee'])
        self.assertEqual(
            self.serv.create_note(note),
            (1, u'q ab cd', u'123', None, None, notebook.id, [u'eee']),
            'create note via service',
        )

    def test_service_update_note(self):
        notebook = models.Notebook(name='a123', default=True)
        note = models.Note(
            title='123',
            content='123',
            notebook=notebook,
        )
        self.session.add_all([notebook, note])
        self.session.commit()
        result = self.serv.update_note(
            (note.id, u'q ab cd', u'123', None, None, notebook.id, [u'eee']), )
        self.assertEqual(
            result,
            (note.id, u'q ab cd', u'123', None, None, notebook.id, [u'eee']),
            'update note',
        )

    def test_service_delete_note(self):
        notebook = models.Notebook(name='a123', default=True)
        note = models.Note(
            title='123',
            content='123',
            notebook=notebook,
        )
        self.session.add_all([notebook, note])
        self.session.commit()
        self.serv.delete_note(note.id)
        self.assertEqual(note.action, ACTION_DELETE, 'delete note')

    def test_service_create_notebook(self):
        notebook = self.serv.create_notebook((None, u'a123', True), )
        self.assertEqual(
            notebook,
            (1, u'a123', True),
            'create notebook',
        )
Esempio n. 17
0
class EditorTestCase(unittest.TestCase):
    def setUp(self):
        self.service = ProviderService()
        self.service._session = get_db_session()
        models.Note.session = self.service._session
        self.app = app
        app.update(self.service)
        notebook = Notebook.from_tuple(
            self.service.create_notebook('test', None),
        )
        self.note = Note.from_tuple(self.service.create_note(Note(
            id=NONE_ID,
            title='New note',
            content="New note content",
            tags=[],
            notebook=notebook.id,
            created=NONE_VAL,
            updated=NONE_VAL,
            place='',
        ).struct))

    def test_content_nochange(self):
        """Test content nochange"""
        editor = Editor(self.note)
        self.assertEqual(
            editor.note_edit.content,
            "New note content",
        )
        for content in CONTENTS:
            editor.note_edit.content = content
            self.assertEqual(
                editor.note_edit.content,
                content,
            )

    def test_content_changing(self):
        """Test content changing"""
        editor = Editor(self.note)
        for prev, current in CHANGING_CONTENTS:
            editor.note_edit.content = prev
            self.assertEqual(
                editor.note_edit.content,
                current,
            )

    def test_title_nochange(self):
        """Test title nochange"""
        editor = Editor(self.note)
        self.assertEqual(
            editor.note_edit.title,
            "New note",
        )
        for title in TITLES:
            editor.note_edit.title = title
            self.assertEqual(
                editor.note_edit.title,
                title,
            )

    def test_set_links(self):
        """Test set links"""
        for orig, result in SET_LINKS:
            self.assertEqual(
                set_links(orig), result,
            )

    def test_not_broken_note_links(self):
        """Test content nochange"""
        content = '<a href="evernote:///view/123/123/123/">note link</a>'
        self.note.content = content
        editor = Editor(self.note)
        self.assertEqual(
            editor.note_edit.content,
            content,
        )

    def test_open_note_links(self):
        """Test open note links"""
        guid = 'guid'
        note = Note(
            id=123,
        )

        self.app.open = MagicMock()
        self.service.get_note_by_guid = MagicMock(
            return_value=note.struct,
        )

        link = "evernote:///view/123/123/{guid}/123/".format(
            guid=guid,
        )
        editor = Editor(self.note)
        editor.note_edit.link_clicked(QUrl(link))

        self.assertEqual(
            self.service.get_note_by_guid.call_args[0][0], guid,
        )
        self.assertEqual(
            self.app.open.call_args[0][0].id, note.id,
        )
Esempio n. 18
0
class MethodsCase(unittest.TestCase):
    """Case for dbus shortcuts"""
    def setUp(self):
        self.service = ProviderService()
        self.session = get_db_session()
        self.service._session = self.session
        models.Note.session = self.session
        self.service.qobject = MagicMock()
        self.service.app = MagicMock()
        self.service.sync = MagicMock()
        factories.invoke_session(self.session)

    def tearDown(self):
        self.session.flush()

    def _create_note(self, **kwargs):
        """Create note"""
        note = factories.NoteFactory.create(action=const.ACTION_NONE, **kwargs)
        self.session.commit()
        return note

    def test_get_note(self):
        """Test get note method"""
        note = self._create_note()
        remote_note = btype.Note << self.service.get_note(note.id)
        self.assertEqual(remote_note.title, note.title)

    def test_get_note_by_guid(self):
        """Test get note method"""
        note = self._create_note(guid='guid')
        remote_note = btype.Note << self.service.get_note_by_guid(note.guid)
        self.assertEqual(remote_note.title, note.title)

    def test_get_note_alternatives(self):
        """Test get note alternatives"""
        note = self._create_note(guid='guid')
        alternative = factories.NoteFactory.create(
            guid='guid',
            action=const.ACTION_CONFLICT,
            conflict_parent_id=note.id,
        )
        self.session.commit()
        remote_notes = btype.Note.list << self.service.get_note_alternatives(
            note.id, )
        self.assertEqual(remote_notes[0].id, alternative.id)

    def test_list_notebooks(self):
        """Test list notebooks method"""
        notebooks = factories.NotebookFactory.create_batch(
            10,
            action=const.ACTION_NONE,
        )
        self.session.commit()
        notebooks = [notebook.id for notebook in notebooks]

        remote_notebooks = btype.Notebook.list << self.service.list_notebooks()
        ids = [notebook.id for notebook in remote_notebooks]

        self.assertItemsEqual(notebooks, ids)

    def test_get_notebook(self):
        """Test get notebook method"""
        notebook = factories.NotebookFactory.create(action=const.ACTION_NONE, )
        deleted_notebook = factories.NotebookFactory(
            action=const.ACTION_DELETE, )
        self.session.commit()

        remote_notebook = btype.Notebook << self.service.get_notebook(
            notebook.id, )
        self.assertEqual(notebook.name, remote_notebook.name)

        with self.assertRaises(DBusException):
            self.service.get_notebook(deleted_notebook.id, )

    def test_get_notebook_notes_count(self):
        """Test get notebook notes count method"""
        notebook = factories.NotebookFactory.create(action=const.ACTION_NONE, )
        factories.NoteFactory.create_batch(
            10,
            action=const.ACTION_NONE,
            notebook=notebook,
        )
        self.session.commit()
        self.assertEqual(
            self.service.get_notebook_notes_count(notebook.id),
            10,
        )

    def test_update_notebook(self):
        """Test update notebook method"""
        notebook = factories.NotebookFactory.create(action=const.ACTION_NONE, )
        self.session.commit()
        new_name = 'name'
        notebook_btype = btype.Notebook.from_obj(notebook)
        notebook_btype.name = new_name
        notebook_btype = btype.Notebook << self.service.update_notebook(
            notebook_btype.struct, )
        self.assertEqual(notebook_btype.name, new_name)
        self.assertEqual(notebook.name, new_name)

    def test_delete_notebook(self):
        """Test delete notebook"""
        notebook = factories.NotebookFactory.create(
            name='notebook',
            action=const.ACTION_NONE,
        )
        self.session.commit()
        self.service.delete_notebook(notebook.id)
        self.assertEqual(notebook.action, const.ACTION_DELETE)

    def test_list_tags(self):
        """Test list tags"""
        tags = factories.TagFactory.create_batch(
            10,
            action=const.ACTION_NONE,
        )
        self.session.commit()
        tags = [tag.id for tag in tags]
        remote_tags = btype.Tag.list << self.service.list_tags()
        tags_ids = [tag.id for tag in remote_tags]
        self.assertItemsEqual(tags_ids, tags)

    def test_get_tag_notes_count(self):
        """Test get tag notes count method"""
        tag = factories.TagFactory.create(action=const.ACTION_NONE, )
        factories.NoteFactory.create_batch(
            10,
            action=const.ACTION_NONE,
            tags=[tag],
        )
        self.session.commit()
        self.assertEqual(
            self.service.get_tag_notes_count(tag.id),
            10,
        )

    def test_delete_tag(self):
        """Test delete tag"""
        tag = factories.TagFactory.create(action=const.ACTION_NONE, )
        self.session.commit()
        self.service.delete_tag(tag.id)
        self.assertEqual(tag.action, const.ACTION_DELETE)

    def test_update_tag(self):
        """Test update tag method"""
        tag = factories.TagFactory.create(action=const.ACTION_NONE, )
        self.session.commit()
        new_name = 'name'
        tag_btype = btype.Tag.from_obj(tag)
        tag_btype.name = new_name
        tag_btype = btype.Tag << self.service.update_tag(tag_btype.struct, )
        self.assertEqual(tag_btype.name, new_name)
        self.assertEqual(tag.name, new_name)

    def test_create_note(self):
        """Test create note"""
        notebook = factories.NotebookFactory.create(default=True)
        self.session.commit()

        title = 'note'
        note_btype = btype.Note(
            title=title,
            tags=[],
            id=const.NONE_ID,
            notebook=notebook.id,
        )

        note_btype = btype.Note << self.service.create_note(
            btype.Note >> note_btype, )
        note = self.session.query(models.Note).filter(
            models.Note.id == note_btype.id, ).one()

        self.assertEqual(note_btype.title, title)
        self.assertEqual(note.title, title)

    def test_update_note(self):
        """Test update note"""
        notebook = factories.NotebookFactory.create(default=True)
        self.session.commit()

        note = self._create_note(notebook_id=notebook.id, )

        new_title = 'title'

        note_btype = btype.Note.from_obj(note)
        note_btype.title = new_title

        note_btype = btype.Note << self.service.update_note(
            note_btype.struct, )
        note = self.session.query(models.Note).filter(
            models.Note.id == note_btype.id, ).one()

        self.assertEqual(note_btype.title, new_title)
        self.assertEqual(note.title, new_title)

    def test_get_note_resources(self):
        """Test get note resources"""
        note = self._create_note()

        resource = factories.ResourceFactory.create(
            file_name='name',
            action=const.ACTION_NONE,
            note_id=note.id,
        )
        self.session.commit()

        resources_btype = btype.Resource.list << self.service.get_note_resources(
            note.id, )

        self.assertEqual(resources_btype[0].file_name, resource.file_name)

    def test_update_note_resources(self):
        """Test update note resources"""
        note = self._create_note()

        resource = btype.Resource(file_name='test', )

        self.service.update_note_resources(
            note.id,
            btype.Resource.list >> [resource],
        )

        resource = self.session.query(models.Resource).one()
        self.assertEqual(resource.file_name, 'test')

    def test_delete_note(self):
        """Test delete note"""
        note = self._create_note()

        self.service.delete_note(note.id)

        self.assertEqual(note.action, const.ACTION_DELETE)

    def test_create_notebook(self):
        """Test create notebook"""
        notebook_btype =\
            btype.Notebook << self.service.create_notebook('test', 'test')

        notebook = self.session.query(models.Notebook).filter(
            models.Notebook.id == notebook_btype.id, ).one()
        self.assertEqual(notebook.name, 'test')

    def test_authenticate(self):
        """Test authenticate"""
        self.service.authenticate('test')
        self.service.qobject\
            .remove_authenticate_signal.emit.assert_called_once_with()
        self.service.qobject\
            .authenticate_signal.emit.assert_called_once_with('test')

    def test_remove_authentication(self):
        """Test remove authentication"""
        self.service.remove_authentication()
        self.service.qobject\
            .remove_authenticate_signal.emit.assert_called_once_with()

    def test_list_places(self):
        """Test list places"""
        places = factories.PlaceFactory.create_batch(10)
        self.session.commit()
        place_ids = [place.id for place in places]
        places_btype = btype.Place.list << self.service.list_places()
        for place_btype in places_btype:
            self.assertIn(place_btype.id, place_ids)

    def test_share_note(self):
        """Test share note"""
        note = self._create_note()

        self.service.share_note(note.id)
        self.assertEqual(note.share_status, const.SHARE_NEED_SHARE)
        self.service.sync.assert_called_once_with()

    def test_stop_sharing_note(self):
        """Test stop sharing note"""
        note = self._create_note(share_status=const.SHARE_SHARED)

        self.service.stop_sharing_note(note.id)
        self.assertEqual(note.share_status, const.SHARE_NEED_STOP)
        self.service.sync.assert_called_once_with()

    def test_is_first_synced(self):
        """Test is first synced"""
        self.assertFalse(self.service.is_first_synced())
Esempio n. 19
0
 def setUp(self):
     self.service = ProviderService()
     self.service._session = get_db_session()
     models.Note.session = self.service._session  # TODO: fix that shit
     self.notebook = Notebook.from_tuple(
         self.service.create_notebook('test', None),
     )
     self.notebook2 = Notebook.from_tuple(
         self.service.create_notebook('test2', None),
     )
     self.notebook3 = Notebook.from_tuple(
         self.service.create_notebook(u'Блокнот', None),
     )
     notes = [
         self.service.create_note(Note(
             id=NONE_ID,
             title='New note',
             content="New note content",
             tags=['ab', 'cd'],
             notebook=self.notebook.id,
             created=NONE_VAL,
             updated=NONE_VAL,
             place='first',
             pinnded=False,
         ).struct),
         self.service.create_note(Note(
             id=NONE_ID,
             title='Old note',
             content="Old note content",
             tags=['ef', 'gh'],
             notebook=self.notebook2.id,
             created=NONE_VAL,
             updated=NONE_VAL,
             place='second',
             pinnded=False,
         ).struct),
         self.service.create_note(Note(
             id=NONE_ID,
             title='not',
             content="oke",
             tags=['ab', 'gh'],
             notebook=self.notebook.id,
             created=NONE_VAL,
             updated=NONE_VAL,
             place='second',
             pinnded=True,
         ).struct),
         self.service.create_note(Note(
             id=NONE_ID,
             title=u'Заметка',
             content=u"Заметка",
             tags=[u'тэг'],
             notebook=self.notebook.id,
             created=NONE_VAL,
             updated=NONE_VAL,
             place=u'место',
             pinnded=False,
         ).struct),
         self.service.create_note(Note(
             id=NONE_ID,
             title=u'заметка',
             content=u"заметка",
             tags=[u'тэг'],
             notebook=self.notebook.id,
             created=NONE_VAL,
             updated=NONE_VAL,
             place=u'место',
             pinnded=False,
         ).struct),
     ]
     self.notes = map(lambda note:
         Note.from_tuple(self.service.update_note(note)),
     notes)
Esempio n. 20
0
class ServiceTestCase(unittest.TestCase):
    def setUp(self):
        self.service = ProviderService()
        self.service._session = get_db_session()
        models.Note.session = self.service._session  # TODO: fix that shit
        self.session = self.service._session

    def _to_ids(self, items):
        return set(map(lambda item: item.id, items))

    def test_notebooks(self):
        """Test notebooks"""
        notebooks = []
        for i in range(100):
            notebooks.append(Notebook.from_tuple(
                self.service.create_notebook(str(i), None),
            ))
            self.assertEqual(notebooks[-1].name, str(i))
        for num, notebook in enumerate(notebooks):
            if num % 2:
                self.service.delete_notebook(notebook.id)
                with self.assertRaises(DBusException):
                    self.service.get_notebook(notebook.id)
                notebooks.remove(notebook)
        self.assertEqual(
            self._to_ids(notebooks), self._to_ids(map(
                Notebook.from_tuple, self.service.list_notebooks(),
            )),
        )
        for num, notebook in enumerate(notebooks):
            notebook.name += '*'
            if num % 2:
                self.service.delete_notebook(notebook.id)
                with self.assertRaises(DBusException):
                    self.service.update_notebook(notebook.struct)
            else:
                updated = Notebook.from_tuple(
                    self.service.update_notebook(notebook.struct),
                )
                self.assertEqual(notebook.name, updated.name)

    def test_notes_with_notebook_and_places(self):
        """Test notes with notebook and places"""
        notebook = Notebook.from_tuple(
            self.service.create_notebook('test', None),
        )
        notes = []
        get_place = lambda num: '123' if num < 50 else '456'
        for i in range(100):
            notes.append(Note.from_tuple(self.service.create_note(Note(
                id=NONE_ID,
                title='New note',
                content="New note content",
                tags=['123', '345'],
                notebook=notebook.id,
                created=NONE_VAL,
                updated=NONE_VAL,
                place=get_place(i),
            ).struct)))
        filtered = []
        for num, note in enumerate(notes):
            if not num % 2:
                self.service.update_note(note.struct)  # mark note exist
                filtered.append(note)
        notes = filtered  # notes.remove(note) not work, wtf
        self.assertEqual(
            self._to_ids(notes), self._to_ids(map(
                Note.from_tuple, self.service.find_notes(
                    '', dbus.Array([], signature='i'),
                    dbus.Array([], signature='i'), 0,
                    100, Note.ORDER_UPDATED_DESC, -1,
                ),
            )),
        )
        filtered = []
        for num, note in enumerate(notes):
            note.title += '*'
            if num % 2:
                self.service.delete_note(note.id)
                with self.assertRaises(DBusException):
                    self.service.update_note(note.struct)
            else:
                updated = Note.from_tuple(
                    self.service.update_note(note.struct),
                )
                self.assertEqual(note.title, updated.title)
                filtered.append(updated)
        self.assertEqual(len(filtered),
            self.service.get_notebook_notes_count(notebook.id),
        )
        self.assertEqual(set(['123', '456']), set(map(
            lambda place: Place.from_tuple(place).name,
            self.service.list_places(),
        )))

    def test_tags(self):
        """Test tags"""
        tags = map(str, range(100))
        notebook = Notebook.from_tuple(
            self.service.create_notebook('test', None),
        )
        self.service.create_note(Note(
            id=NONE_ID,
            title='New note',
            content="New note content",
            tags=tags,
            notebook=notebook.id,
            created=NONE_VAL,
            updated=NONE_VAL,
            place='',
        ).struct)
        remote_tags = map(Tag.from_tuple, self.service.list_tags())
        self.assertEqual(set(tags), set(map(
            lambda tag: tag.name, remote_tags,
        )))
        filtered = []
        for num, tag in enumerate(remote_tags):
            if num % 2:
                filtered.append(tag)
            else:
                self.service.delete_tag(tag.id)
        tags = filtered  # tags.remove(tag) not work, wtf
        self.assertEqual(
            self._to_ids(tags), self._to_ids(map(
                Tag.from_tuple, self.service.list_tags(),
            )),
        )
        filtered = []
        for num, tag in enumerate(tags):
            tag.name += '*'
            if num % 2:
                self.service.delete_tag(tag.id)
                with self.assertRaises(DBusException):
                    self.service.update_tag(tag.struct)
            else:
                updated = Tag.from_tuple(
                    self.service.update_tag(tag.struct),
                )
                self.assertEqual(tag.name, updated.name)
                filtered.append(updated)

    def _file_names(self, items):
        return set(map(lambda item: item.file_name, items))

    def test_note_resources(self):
        """Test note resources"""
        notebook = Notebook.from_tuple(
            self.service.create_notebook('test', None),
        )
        struct = self.service.create_note(Note(
            id=NONE_ID,
            title='New note',
            content="New note content",
            tags=[],
            notebook=notebook.id,
            created=NONE_VAL,
            updated=NONE_VAL,
            place='',
        ).struct)
        note = Note.from_tuple(self.service.update_note(struct))
        resources = []
        for i in range(100):
            resources.append(Resource(
                id=NONE_ID,
                file_name="name/%d" % i,
                file_path="path/%d" % i,
                mime='image/png',
                hash='',
            ))
        self.service.update_note_resources(note.struct,
            map(lambda resource: resource.struct, resources),
        )
        received = map(Resource.from_tuple,
            self.service.get_note_resources(note.id))
        self.assertEqual(
            self._file_names(resources), self._file_names(received),
        )
        received = received[:50]
        self.service.update_note_resources(note.struct,
            map(lambda resource: resource.struct, received),
        )
        new_received = map(Resource.from_tuple,
            self.service.get_note_resources(note.id))
        self.assertEqual(
            self._file_names(new_received), self._file_names(received),
        )

    def test_note_conflicts_serialisation(self):
        """Test notes with conflict serialisztion"""
        parent = models.Note(
            title='123',
            content='456',
        )
        self.session.add(parent)
        self.session.commit()
        conflicts = []
        for i in range(10):
            conflict = models.Note(
                title='123',
                content='456',
                conflict_parent_id=parent.id,
            )
            self.session.add(conflict)
            conflicts.append(conflict)
        self.session.commit()
        self.assertEqual(
            set(parent.conflict_items_dbus),
            self._to_ids(conflicts),
        )
        for conflict in conflicts:
            self.assertEqual(parent.id,
                conflict.conflict_parent_dbus)

    def test_tag_delete(self):
        """Test deleting tags"""
        tag = models.Tag(
            name='okok',
            action=models.ACTION_NONE,
        )
        deleting_tag = models.Tag(
            name='deleted',
            action=models.ACTION_NONE,
        )
        self.session.add(tag)
        self.session.add(deleting_tag)
        note = models.Note(
            title='123',
            content='456',
            tags=[tag, deleting_tag],
        )
        self.session.add(note)
        self.session.commit()
        self.service.delete_tag(deleting_tag.id)
        self.assertItemsEqual(note.tags, [tag])
Esempio n. 21
0
class ServiceTestCase(unittest.TestCase):
    def setUp(self):
        self.service = ProviderService()
        self.service._session = get_db_session()
        models.Note.session = self.service._session  # TODO: fix that shit
        self.session = self.service._session

    def _to_ids(self, items):
        return set(map(lambda item: item.id, items))

    def test_notebooks(self):
        """Test notebooks"""
        notebooks = []
        for i in range(100):
            notebooks.append(
                Notebook.from_tuple(self.service.create_notebook(str(i),
                                                                 None), ))
            self.assertEqual(notebooks[-1].name, str(i))
        for num, notebook in enumerate(notebooks):
            if num % 2:
                self.service.delete_notebook(notebook.id)
                with self.assertRaises(DBusException):
                    self.service.get_notebook(notebook.id)
                notebooks.remove(notebook)
        self.assertEqual(
            self._to_ids(notebooks),
            self._to_ids(
                map(
                    Notebook.from_tuple,
                    self.service.list_notebooks(),
                )),
        )
        for num, notebook in enumerate(notebooks):
            notebook.name += '*'
            if num % 2:
                self.service.delete_notebook(notebook.id)
                with self.assertRaises(DBusException):
                    self.service.update_notebook(notebook.struct)
            else:
                updated = Notebook.from_tuple(
                    self.service.update_notebook(notebook.struct), )
                self.assertEqual(notebook.name, updated.name)

    def test_notes_with_notebook_and_places(self):
        """Test notes with notebook and places"""
        notebook = Notebook.from_tuple(
            self.service.create_notebook('test', None), )
        notes = []
        get_place = lambda num: '123' if num < 50 else '456'
        for i in range(100):
            notes.append(
                Note.from_tuple(
                    self.service.create_note(
                        Note(
                            id=NONE_ID,
                            title='New note',
                            content="New note content",
                            tags=['123', '345'],
                            notebook=notebook.id,
                            created=NONE_VAL,
                            updated=NONE_VAL,
                            place=get_place(i),
                        ).struct)))
        filtered = []
        for num, note in enumerate(notes):
            if not num % 2:
                self.service.update_note(note.struct)  # mark note exist
                filtered.append(note)
        notes = filtered  # notes.remove(note) not work, wtf
        self.assertEqual(
            self._to_ids(notes),
            self._to_ids(
                map(
                    Note.from_tuple,
                    self.service.find_notes(
                        '',
                        dbus.Array([], signature='i'),
                        dbus.Array([], signature='i'),
                        0,
                        100,
                        Note.ORDER_UPDATED_DESC,
                        -1,
                    ),
                )),
        )
        filtered = []
        for num, note in enumerate(notes):
            note.title += '*'
            if num % 2:
                self.service.delete_note(note.id)
                with self.assertRaises(DBusException):
                    self.service.update_note(note.struct)
            else:
                updated = Note.from_tuple(
                    self.service.update_note(note.struct), )
                self.assertEqual(note.title, updated.title)
                filtered.append(updated)
        self.assertEqual(
            len(filtered),
            self.service.get_notebook_notes_count(notebook.id),
        )
        self.assertEqual(
            set(['123', '456']),
            set(
                map(
                    lambda place: Place.from_tuple(place).name,
                    self.service.list_places(),
                )))

    def test_tags(self):
        """Test tags"""
        tags = map(str, range(100))
        notebook = Notebook.from_tuple(
            self.service.create_notebook('test', None), )
        self.service.create_note(
            Note(
                id=NONE_ID,
                title='New note',
                content="New note content",
                tags=tags,
                notebook=notebook.id,
                created=NONE_VAL,
                updated=NONE_VAL,
                place='',
            ).struct)
        remote_tags = map(Tag.from_tuple, self.service.list_tags())
        self.assertEqual(set(tags),
                         set(map(
                             lambda tag: tag.name,
                             remote_tags,
                         )))
        filtered = []
        for num, tag in enumerate(remote_tags):
            if num % 2:
                filtered.append(tag)
            else:
                self.service.delete_tag(tag.id)
        tags = filtered  # tags.remove(tag) not work, wtf
        self.assertEqual(
            self._to_ids(tags),
            self._to_ids(map(
                Tag.from_tuple,
                self.service.list_tags(),
            )),
        )
        filtered = []
        for num, tag in enumerate(tags):
            tag.name += '*'
            if num % 2:
                self.service.delete_tag(tag.id)
                with self.assertRaises(DBusException):
                    self.service.update_tag(tag.struct)
            else:
                updated = Tag.from_tuple(self.service.update_tag(tag.struct), )
                self.assertEqual(tag.name, updated.name)
                filtered.append(updated)

    def _file_names(self, items):
        return set(map(lambda item: item.file_name, items))

    def test_note_resources(self):
        """Test note resources"""
        notebook = Notebook.from_tuple(
            self.service.create_notebook('test', None), )
        struct = self.service.create_note(
            Note(
                id=NONE_ID,
                title='New note',
                content="New note content",
                tags=[],
                notebook=notebook.id,
                created=NONE_VAL,
                updated=NONE_VAL,
                place='',
            ).struct)
        note = Note.from_tuple(self.service.update_note(struct))
        resources = []
        for i in range(100):
            resources.append(
                Resource(
                    id=NONE_ID,
                    file_name="name/%d" % i,
                    file_path="path/%d" % i,
                    mime='image/png',
                    hash='',
                ))
        self.service.update_note_resources(
            note.struct,
            map(lambda resource: resource.struct, resources),
        )
        received = map(Resource.from_tuple,
                       self.service.get_note_resources(note.id))
        self.assertEqual(
            self._file_names(resources),
            self._file_names(received),
        )
        received = received[:50]
        self.service.update_note_resources(
            note.struct,
            map(lambda resource: resource.struct, received),
        )
        new_received = map(Resource.from_tuple,
                           self.service.get_note_resources(note.id))
        self.assertEqual(
            self._file_names(new_received),
            self._file_names(received),
        )

    def test_note_conflicts_serialisation(self):
        """Test notes with conflict serialisztion"""
        parent = models.Note(
            title='123',
            content='456',
        )
        self.session.add(parent)
        self.session.commit()
        conflicts = []
        for i in range(10):
            conflict = models.Note(
                title='123',
                content='456',
                conflict_parent_id=parent.id,
            )
            self.session.add(conflict)
            conflicts.append(conflict)
        self.session.commit()
        self.assertEqual(
            set(parent.conflict_items_dbus),
            self._to_ids(conflicts),
        )
        for conflict in conflicts:
            self.assertEqual(parent.id, conflict.conflict_parent_dbus)

    def test_tag_delete(self):
        """Test deleting tags"""
        tag = models.Tag(
            name='okok',
            action=models.ACTION_NONE,
        )
        deleting_tag = models.Tag(
            name='deleted',
            action=models.ACTION_NONE,
        )
        self.session.add(tag)
        self.session.add(deleting_tag)
        note = models.Note(
            title='123',
            content='456',
            tags=[tag, deleting_tag],
        )
        self.session.add(note)
        self.session.commit()
        self.service.delete_tag(deleting_tag.id)
        self.assertItemsEqual(note.tags, [tag])
Esempio n. 22
0
 def setUp(self):
     self.service = ProviderService()
     self.service._session = get_db_session()
     models.Note.session = self.service._session  # TODO: fix that shit
     self.notebook = Notebook.from_tuple(
         self.service.create_notebook('test', None), )
     self.notebook2 = Notebook.from_tuple(
         self.service.create_notebook('test2', None), )
     self.notebook3 = Notebook.from_tuple(
         self.service.create_notebook(u'Блокнот', None), )
     notes = [
         self.service.create_note(
             Note(
                 id=NONE_ID,
                 title='New note',
                 content="New note content",
                 tags=['ab', 'cd'],
                 notebook=self.notebook.id,
                 created=NONE_VAL,
                 updated=NONE_VAL,
                 place='first',
                 pinnded=False,
             ).struct),
         self.service.create_note(
             Note(
                 id=NONE_ID,
                 title='Old note',
                 content="Old note content",
                 tags=['ef', 'gh'],
                 notebook=self.notebook2.id,
                 created=NONE_VAL,
                 updated=NONE_VAL,
                 place='second',
                 pinnded=False,
             ).struct),
         self.service.create_note(
             Note(
                 id=NONE_ID,
                 title='not',
                 content="oke",
                 tags=['ab', 'gh'],
                 notebook=self.notebook.id,
                 created=NONE_VAL,
                 updated=NONE_VAL,
                 place='second',
                 pinnded=True,
             ).struct),
         self.service.create_note(
             Note(
                 id=NONE_ID,
                 title=u'Заметка',
                 content=u"Заметка",
                 tags=[u'тэг'],
                 notebook=self.notebook.id,
                 created=NONE_VAL,
                 updated=NONE_VAL,
                 place=u'место',
                 pinnded=False,
             ).struct),
         self.service.create_note(
             Note(
                 id=NONE_ID,
                 title=u'заметка',
                 content=u"заметка",
                 tags=[u'тэг'],
                 notebook=self.notebook.id,
                 created=NONE_VAL,
                 updated=NONE_VAL,
                 place=u'место',
                 pinnded=False,
             ).struct),
     ]
     self.notes = map(
         lambda note: Note.from_tuple(self.service.update_note(note)),
         notes)
Esempio n. 23
0
 def _create_service(self):
     """Create service"""
     self.service = ProviderService()
     self.service._session = get_db_session()
     models.Note.session = self.service._session  # TODO: fix that shit
Esempio n. 24
0
class EditorTestCase(unittest.TestCase):
    def setUp(self):
        self.service = ProviderService()
        self.service._session = get_db_session()
        models.Note.session = self.service._session
        self.app = app
        app.update(self.service)
        notebook = Notebook.from_tuple(
            self.service.create_notebook('test'),
        )
        self.note = Note.from_tuple(self.service.create_note(Note(
            id=NONE_ID,
            title='New note',
            content="New note content",
            tags=[],
            notebook=notebook.id,
            created=NONE_VAL,
            updated=NONE_VAL,
            place='',
        ).struct))

    def test_content_nochange(self):
        """Test content nochange"""
        editor = Editor(self.note)
        self.assertEqual(
            editor.note_edit.content,
            "New note content",
        )
        for content in CONTENTS:
            editor.note_edit.content = content
            self.assertEqual(
                editor.note_edit.content,
                content,
            )

    def test_content_changing(self):
        """Test content changing"""
        editor = Editor(self.note)
        for prev, current in CHANGING_CONTENTS:
            editor.note_edit.content = prev
            self.assertEqual(
                editor.note_edit.content,
                current,
            )

    def test_title_nochange(self):
        """Test title nochange"""
        editor = Editor(self.note)
        self.assertEqual(
            editor.note_edit.title,
            "New note",
        )
        for title in TITLES:
            editor.note_edit.title = title
            self.assertEqual(
                editor.note_edit.title,
                title,
            )

    def test_set_links(self):
        """Test set links"""
        for orig, result in SET_LINKS:
            self.assertEqual(
                set_links(orig), result,
            )
Esempio n. 25
0
 def _create_service(self):
     """Create service"""
     self.service = ProviderService()
     self.service._session = get_db_session()
     models.Note.session = self.service._session  # TODO: fix that shit
Esempio n. 26
0
    class EditorTestCase(unittest.TestCase):
        def setUp(self):
            self.service = ProviderService()
            self.service._session = get_db_session()
            models.Note.session = self.service._session
            self.app = app
            self.app.update(self.service)
            notebook = Notebook.from_tuple(
                self.service.create_notebook('test', None),
            )
            self.note = Note.from_tuple(self.service.create_note(Note(
                id=NONE_ID,
                title='New note',
                content="New note content",
                tags=[],
                notebook=notebook.id,
                created=NONE_VAL,
                updated=NONE_VAL,
                place='',
            ).struct))

        def tearDown(self):
            if hasattr(self, 'editor'):
                self.app.data_changed.disconnect(
                    self.editor.init_alternatives,
                )
            del self.app.provider
            del self.app

        def test_content_nochange(self):
            """Test content nochange"""
            self.editor = Editor(self.note)
            self.assertEqual(
                self.editor.note_edit.content,
                "New note content",
            )
            for content in CONTENTS:
                self.editor.note_edit.content = content
                self.assertEqual(
                    self.editor.note_edit.content,
                    content,
                )

        def test_content_changing(self):
            """Test content changing"""
            self.editor = Editor(self.note)
            for prev, current in CHANGING_CONTENTS:
                self.editor.note_edit.content = prev
                self.assertEqual(
                    self.editor.note_edit.content,
                    current,
                )

        def test_title_nochange(self):
            """Test title nochange"""
            self.editor = Editor(self.note)
            self.assertEqual(
                self.editor.note_edit.title,
                "New note",
            )
            for title in TITLES:
                self.editor.note_edit.title = title
                self.assertEqual(
                    self.editor.note_edit.title,
                    title,
                )

        def test_set_links(self):
            """Test set links"""
            for orig, result in SET_LINKS:
                self.assertEqual(
                    set_links(orig), result,
                )

        def test_not_broken_note_links(self):
            """Test content nochange"""
            content = '<a href="evernote:///view/123/123/123/">note link</a>'
            self.note.content = content
            self.editor = Editor(self.note)
            self.assertEqual(
                self.editor.note_edit.content,
                content,
            )

        def test_open_note_links(self):
            """Test open note links"""
            guid = 'guid'
            note = Note(
                id=123,
            )

            self.app.open = MagicMock()
            self.service.get_note_by_guid = MagicMock(
                return_value=note.struct,
            )

            link = "evernote:///view/123/123/{guid}/123/".format(
                guid=guid,
            )
            self.editor = Editor(self.note)
            self.editor.note_edit.link_clicked(QUrl(link))

            self.assertEqual(
                self.service.get_note_by_guid.call_args[0][0], guid,
            )
            self.assertEqual(
                self.app.open.call_args[0][0].id, note.id,
            )
            del self.app.open
Esempio n. 27
0
 def setUp(self):
     self.service = ProviderService()
     self.service._session = get_db_session()
     models.Note.session = self.service._session  # TODO: fix that shit
     self.session = self.service._session
Esempio n. 28
0
 def setUp(self):
     self.service = ProviderService()
     self.service._session = get_db_session()
     models.Note.session = self.service._session  # TODO: fix that shit
     self.session = self.service._session