def test_multi_index(self): """ Test that documents can be stored in multiple indexes. """ self.index.delete_instance() indexes = [Index.create(name='idx-%s' % i) for i in range(3)] document = Document.create(content='hueybear') for index in indexes: index.index(document.content, document) self.assertEqual(Document.select().count(), 1) self.assertEqual(Index.select().count(), 3) self.assertEqual(IndexDocument.select().count(), 3) query = (IndexDocument.select( Index.name, IndexDocument.document).join(Index).order_by(Index.name).dicts()) idx_doc_data = [idx_doc for idx_doc in query] self.assertEqual(idx_doc_data, [ { 'document': document.get_id(), 'name': 'idx-0' }, { 'document': document.get_id(), 'name': 'idx-1' }, { 'document': document.get_id(), 'name': 'idx-2' }, ])
def test_reindex(self): """ Test that an existing document can be re-indexed, updating the content and metadata in the process. """ doc = self.index.index('test doc', foo='bar', baze='nug') doc_db = (Document.select(Document._meta.primary_key, Document.content).get()) self.assertTrue(doc_db.get_id() is not None) self.assertEqual(doc_db.content, 'test doc') self.assertEqual(doc_db.metadata, {'foo': 'bar', 'baze': 'nug'}) updated_doc = self.index.index('updated doc', document=doc, foo='bazz', nug='x') self.assertEqual(Document.select().count(), 1) self.assertEqual(updated_doc.metadata, {'foo': 'bazz', 'nug': 'x'}) u_doc_db = (Document.select(Document._meta.primary_key, Document.content).get()) self.assertEqual(u_doc_db.content, 'updated doc') self.assertEqual(u_doc_db.get_id(), doc_db.get_id()) self.assertEqual(u_doc_db.metadata, {'foo': 'bazz', 'nug': 'x'}) # Verify through relationship properties. self.assertEqual(IndexDocument.select().count(), 1) idx_doc = IndexDocument.get() self.assertEqual(idx_doc.__data__['document'], u_doc_db.get_id()) self.assertEqual(idx_doc.__data__['index'], self.index.id)
def _get_document(self, pk): if isinstance(pk, int) or (pk and pk.isdigit()): query = Document.all().where(Document._meta.primary_key == pk) try: return query.get() except Document.DoesNotExist: pass return get_object_or_404(Document.all(), Document.identifier == pk)
def test_document_detail_delete(self): idx = Index.create(name='idx') alt_idx = Index.create(name='alt-idx') d1 = idx.index('doc 1', k1='v1', k2='v2') d2 = idx.index('doc 2', k3='v3') d2.attach('foo.jpg', 'bar') alt_idx.add_to_index(d1) alt_idx.add_to_index(d2) self.assertEqual(Metadata.select().count(), 3) self.assertEqual(Attachment.select().count(), 1) response = self.app.delete('/documents/%s/' % d2.get_id()) data = json_load(response.data) self.assertEqual(data, {'success': True}) self.assertEqual(Metadata.select().count(), 2) self.assertEqual(Attachment.select().count(), 0) response = self.app.delete('/documents/%s/' % d2.get_id()) self.assertEqual(response.status_code, 404) self.assertEqual(Document.select().count(), 1) self.assertEqual(IndexDocument.select().count(), 2) self.assertEqual([d.get_id() for d in idx.documents], [d1.get_id()]) self.assertEqual([d.get_id() for d in alt_idx.documents], [d1.get_id()])
def test_index_document_validation(self): idx = Index.create(name='idx') response = self.post_json('/documents/', {'content': 'foo'}) self.assertEqual(response['error'], 'You must specify either an "index" or "indexes".') response = self.post_json('/documents/', {'content': 'x', 'index': ''}) self.assertEqual(response['error'], 'You must specify either an "index" or "indexes".') response = self.post_json('/documents/', { 'content': 'foo', 'index': 'missing' }) self.assertEqual(response['error'], 'The following indexes were not found: missing.') response = self.post_json('/documents/', { 'content': 'foo', 'indexes': ['missing', 'idx', 'blah'] }) self.assertEqual( response['error'], 'The following indexes were not found: missing, blah.') self.assertEqual(Document.select().count(), 0)
def create(self): data = validator.parse_post( ['content'], ['identifier', 'index', 'indexes', 'metadata']) indexes = validator.validate_indexes(data) if indexes is None: error('You must specify either an "index" or "indexes".') if data.get('identifier'): try: document = self._get_document(data['identifier']) except NotFound: pass else: return self.update(data['identifier']) document = Document.create(content=data['content'], identifier=data.get('identifier')) if data.get('metadata'): document.metadata = data['metadata'] logger.info('Created document with id=%s', document.get_id()) for index in indexes: index.add_to_index(document) logger.info('Added document %s to index %s', document.get_id(), index.name) if len(request.files): self.attach_files(document) return self.detail(document.get_id())
def list_view(self): # Allow filtering by index. idx_list = request.args.getlist('index') if idx_list: indexes = Index.select(Index.id).where(Index.name << idx_list) else: indexes = None document_count = Document.select().count() return jsonify(self._search_response(indexes, True, document_count))
def attachment_download(document_id, pk): document = get_object_or_404(Document.all(), Document._meta.primary_key == document_id) attachment = get_object_or_404(document.attachments, Attachment.filename == pk) response = make_response(attachment.blob.data) response.headers['Content-Type'] = attachment.mimetype response.headers['Content-Length'] = attachment.length response.headers['Content-Disposition'] = 'inline; filename=%s' % ( attachment.filename) return response
def test_document_detail_post(self): idx = Index.create(name='idx') alt_idx = Index.create(name='alt-idx') doc = idx.index('test doc', foo='bar', nug='baze') alt_doc = idx.index('alt doc') url = '/documents/%s/' % doc.get_id() def assertDoc(doc, content, metadata=None, indexes=None): doc_db = self.refresh_doc(doc) self.assertEqual(doc_db.content, content) self.assertEqual([idx.name for idx in doc_db.get_indexes()], indexes or []) self.assertEqual(doc_db.metadata, metadata or {}) # Update the content. response = self.post_json(url, {'content': 'updated'}) assertDoc(doc, 'updated', {'foo': 'bar', 'nug': 'baze'}, ['idx']) # Test updating metadata. response = self.post_json( url, {'metadata': dict(doc.metadata, nug='baz', herp='derp')}) assertDoc(doc, 'updated', { 'foo': 'bar', 'nug': 'baz', 'herp': 'derp' }, ['idx']) # Test clearing metadata. response = self.post_json(url, {'metadata': None}) assertDoc(doc, 'updated', {}, ['idx']) # Test updating indexes. response = self.post_json(url, {'indexes': ['idx', 'alt-idx']}) assertDoc(doc, 'updated', {}, ['alt-idx', 'idx']) # Test clearing indexes. response = self.post_json(url, {'indexes': []}) assertDoc(doc, 'updated', {}, []) # Ensure alt_doc has not been affected. assertDoc(alt_doc, 'alt doc', {}, ['idx']) # Sanity check. self.assertEqual(Document.select().count(), 2)
def test_index_update_delete(self): idx = Index.create(name='idx') alt_idx = Index.create(name='alt-idx') doc = idx.index(content='foo') alt_idx.index(doc.content, doc) idx.index('idx only') alt_idx.index('alt only') response = self.post_json('/idx/', {'name': 'idx-updated'}) self.assertEqual(response['id'], idx.id) self.assertEqual(response['name'], 'idx-updated') self.assertEqual([doc['content'] for doc in response['documents']], ['foo', 'idx only']) response = self.app.delete('/idx-updated/') data = json_load(response.data) self.assertEqual(data, {'success': True}) self.assertEqual(Document.select().count(), 3) self.assertEqual(IndexDocument.select().count(), 2) self.assertEqual(Index.select().count(), 1)
def refresh_doc(self, doc): return (Document.all().where( Document._meta.primary_key == doc.get_id()).get())