Esempio n. 1
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. 2
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. 3
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. 4
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())