def create_db(): """Create the ElasticSearch index for Annotations and Documents""" try: es.conn.indices.create(es.index) except elasticsearch_exceptions.RequestError as e: if not (e.error.startswith('IndexAlreadyExistsException') or e.error.startswith('InvalidIndexNameException')): raise except elasticsearch_exceptions.ConnectionError as e: msg = ('Can not access ElasticSearch at {0}! ' 'Check to ensure it is running.').format(es.host) raise elasticsearch_exceptions.ConnectionError('N/A', msg, e) # Pylint issue #258: https://bitbucket.org/logilab/pylint/issue/258 # # pylint: disable=unexpected-keyword-arg es.conn.cluster.health(wait_for_status='yellow') # pylint: enable=unexpected-keyword-arg try: Annotation.update_settings() Annotation.create_all() Document.create_all() except elasticsearch_exceptions.RequestError as e: if e.error.startswith('MergeMappingException'): date = time.strftime('%Y-%m-%d') message = ("Elasticsearch index mapping is incorrect! Please " "reindex it. For example, run: " "./bin/hypothesis reindex {0} {1} {1}-{2}" .format('yourconfig.ini', es.index, date) ) log.critical(message) raise RuntimeError(message) raise
def session(self, db_session, added_ann_id, changed_ann_id, deleted_ann_id): # Populate the DB session with different types of change relative to the # last-committed state. We could use any model object for this purpose # but annotations are the primary object in the system. doc = Document(web_uri="https://example.org") changed = Annotation(id=changed_ann_id, userid="foo", groupid="wibble", document=doc) deleted = Annotation(id=deleted_ann_id, userid="foo", groupid="wibble", document=doc) db_session.add(changed) db_session.add(deleted) db_session.commit() changed.text = "changed text" db_session.delete(deleted) added = Annotation(id=added_ann_id, userid="foo", groupid="wibble", document=doc) db_session.add(added) return db_session
def create_db(): """Create the ElasticSearch index for Annotations and Documents""" try: es.conn.indices.create(es.index) except elasticsearch_exceptions.RequestError as e: if not (e.error.startswith('IndexAlreadyExistsException') or e.error.startswith('InvalidIndexNameException')): raise except elasticsearch_exceptions.ConnectionError as e: msg = ('Can not access ElasticSearch at {0}! ' 'Check to ensure it is running.').format(es.host) raise elasticsearch_exceptions.ConnectionError('N/A', msg, e) es.conn.cluster.health(wait_for_status='yellow') Annotation.update_settings() Annotation.create_all() Document.create_all()
def document(self, storage_driver): title = 'My fascinating page' if storage_driver == 'elastic': return elastic.Document(title=[title]) else: doc = Document() doc.meta.append(DocumentMeta(type='title', value=[title])) return doc
def meta_attrs(self): return { "claimant": "http://example.com/claimant", "type": "title", "value": "the title", "document": Document(), "created": datetime.now() - timedelta(days=1), "updated": datetime.now(), }
def document(self, db_session): doc = Document() doc.meta.append( DocumentMeta(type='title', value=['My fascinating page'], claimant='http://example.org')) db_session.add(doc) db_session.flush() return doc
def create_db(): """Create the ElasticSearch index for Annotations and Documents""" try: es.conn.indices.create(es.index) except elasticsearch_exceptions.RequestError as e: if not (e.error.startswith('IndexAlreadyExistsException') or e.error.startswith('InvalidIndexNameException')): raise except elasticsearch_exceptions.ConnectionError as e: msg = ('Can not access ElasticSearch at {0}! ' 'Check to ensure it is running.').format(es.host) raise elasticsearch_exceptions.ConnectionError('N/A', msg, e) # Pylint issue #258: https://bitbucket.org/logilab/pylint/issue/258 # # pylint: disable=unexpected-keyword-arg es.conn.cluster.health(wait_for_status='yellow') # pylint: enable=unexpected-keyword-arg Annotation.update_settings() Annotation.create_all() Document.create_all()
def test_it_denormalizes_title_to_document_when_falsy( self, db_session, meta_attrs, doc_title, final_title ): meta_attrs["value"] = ["attr_title"] meta_attrs["document"] = document = Document(title=doc_title) db_session.add(document) create_or_update_document_meta(session=db_session, **meta_attrs) document = db_session.query(Document).get(document.id) assert document.title == final_title
def reply(self, annotations, db_session, parent): # We need to create a document object to provide the title, and # ensure it is associated with the annotation through the # annotation's `target_uri` doc = Document.find_or_create_by_uris(db_session, claimant_uri='http://example.net/foo', uris=[]).one() doc.meta.append(DocumentMeta(type='title', value=['Some document'], claimant='http://example.com/foo')) reply = Annotation(**FIXTURE_DATA['reply']) reply.target_uri = 'http://example.net/foo' reply.references = [parent.id] db_session.add(reply) db_session.flush() annotations[reply.id] = reply return reply
def reply(self, annotations, parent): # We need to create a document object to provide the title, and # ensure it is associated with the annotation through the # annotation's `target_uri` doc = Document.find_or_create_by_uris(db.Session, claimant_uri='http://example.net/foo', uris=[]).one() doc.meta.append(DocumentMeta(type='title', value=['Some document'], claimant='http://example.com/foo')) reply = Annotation(**FIXTURE_DATA['reply']) reply.target_uri = 'http://example.net/foo' reply.references = [parent.id] db.Session.add(reply) db.Session.flush() annotations[reply.id] = reply return reply
def group_with_two_users(db_session, factories): """ Create a group with two members and an annotation created by each. """ creator = factories.User() member = factories.User() group = Group(authority=creator.authority, creator=creator, members=[creator, member], name='test', pubid='group_with_two_users') db_session.add(group) doc = Document(web_uri='https://example.org') creator_ann = Annotation(userid=creator.userid, groupid=group.pubid, document=doc) member_ann = Annotation(userid=member.userid, groupid=group.pubid, document=doc) db_session.add(creator_ann) db_session.add(member_ann) db_session.flush() return (group, creator, member, creator_ann, member_ann)
def reply(self, annotations, storage_driver, parent): if storage_driver == 'elastic': reply = elastic.Annotation(**FIXTURE_DATA['reply_elastic']) reply['document'] = {'title': ['Some document']} reply['references'] = [parent.id] else: # We need to create a document object to provide the title, and # ensure it is associated with the annotation through the # annotation's `target_uri` doc = Document.find_or_create_by_uris(db.Session, claimant_uri='http://example.net/foo', uris=[]).one() doc.meta.append(DocumentMeta(type='title', value=['Some document'], claimant='http://example.com/foo')) reply = Annotation(**FIXTURE_DATA['reply_postgres']) reply.target_uri = 'http://example.net/foo' reply.references = [parent.id] db.Session.add(reply) db.Session.flush() annotations[reply.id] = reply return reply
def test_it_updates_an_existing_DocumentMeta_if_there_is_one( self, db_session, meta_attrs, correct_document ): original_attrs = meta_attrs updated_attrs = dict( original_attrs, value="new value", # This should be ignored either way. document=meta_attrs["document"] if correct_document else Document(), created=datetime.now(), # This should be ignored. updated=datetime.now(), ) document_meta = DocumentMeta(**original_attrs) db_session.add(document_meta) create_or_update_document_meta(session=db_session, **updated_attrs) assert document_meta.value == updated_attrs["value"] assert document_meta.updated == updated_attrs["updated"] assert document_meta.created == original_attrs["created"] assert document_meta.document == original_attrs["document"] assert ( len(db_session.query(DocumentMeta).all()) == 1 ), "It shouldn't have added any new objects to the db"
def document(self, db_session): doc = Document(title="My fascinating page") db_session.add(doc) db_session.flush() return doc
def delete_db(): Annotation.drop_all() Document.drop_all()