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_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 notes_remote(self): """Receive notes from server""" notes_ids = [] update_count = self.note_store.getSyncState( self.auth_token).updateCount if update_count == self.update_count: return self.update_count = update_count for note in self._iter_all_notes(): self.app.log('Note %s remote' % note.title) try: nt = self.sq(models.Note).filter( models.Note.guid == note.guid, ).one() notes_ids.append(nt.id) if nt.updated < note.updated: note = self.note_store.getNote( self.auth_token, note.guid, True, True, True, True, ) nt.from_api(note, self.session) self.note_resources_remote(note, nt) except NoResultFound: note = self.note_store.getNote( self.auth_token, note.guid, True, True, True, True, ) nt = models.Note(guid=note.guid) nt.from_api(note, self.session) self.session.add(nt) self.session.commit() notes_ids.append(nt.id) self.note_resources_remote(note, nt) if len(notes_ids): ids = filter( lambda id: id not in notes_ids, map( lambda note: note.id, self.sq(models.Note).all(), )) if len(ids): self.sq(models.Note).filter( and_( models.Note.id.in_(ids), models.Note.action != ACTION_NOEXSIST, )).delete(synchronize_session='fetch') self.session.commit()
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_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_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_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])
def test_note_content_formatting(self): """Test note content formatting. This tests that the content from the Note model object continues the same after converting it to a ttype. """ content = """Some foo bar content.""" guid = 'guid' note = models.Note( title='note', content=content, action=const.ACTION_CREATE, ) self.session.add(note) self.session.commit() note_ttype = self.sync._create_ttype(note) note.from_api(note_ttype, self.session) self.assertEqual(note.content, content)
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 notes_remote(self): """Receive notes from server""" notes_ids = [] for note in self.all_notes: self.app.log('Note %s remote' % note.title) try: nt = self.sq(models.Note).filter( models.Note.guid == note.guid, ).one() notes_ids.append(nt.id) conflict = nt.action == ACTION_CHANGE if nt.updated < note.updated: note = self.note_store.getNote( self.auth_token, note.guid, True, True, True, True, ) if conflict: parent = nt nt = models.Note() nt.from_api(note, self.session) if conflict: nt.guid = '' nt.action = ACTION_CONFLICT nt.conflict_parent_id = parent.id self.session.add(nt) self.session.commit() self.note_resources_remote(note, nt) except NoResultFound: note = self.note_store.getNote( self.auth_token, note.guid, True, True, True, True, ) nt = models.Note(guid=note.guid) nt.from_api(note, self.session) self.session.add(nt) self.session.commit() notes_ids.append(nt.id) self.note_resources_remote(note, nt) if not note.attributes.shareDate and nt.share_status not in ( models.SHARE_NONE, models.SHARE_NEED_SHARE, ): nt.share_status = models.SHARE_NONE nt.share_date = None nt.share_url = None self.session.commit() elif note.attributes.shareDate != nt.share_date and nt.share_status not in ( models.SHARE_NEED_SHARE, models.SHARE_NEED_STOP, ): self._single_note_share(nt, note.attributes.shareDate) self.session.commit() ids = filter(lambda id: id not in notes_ids, map( lambda note: note.id, self.sq(models.Note).all(), )) if len(ids): self.sq(models.Note).filter( and_( models.Note.id.in_(ids), models.Note.conflict_parent_id.in_(ids), models.Note.action != ACTION_NOEXSIST, models.Note.action != ACTION_CREATE, models.Note.action != ACTION_CHANGE, models.Note.action != ACTION_CONFLICT, )).delete(synchronize_session='fetch') self.session.commit()