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.rowid, Document.content).get() self.assertTrue(doc_db.rowid 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.rowid, Document.content).get() self.assertEqual(u_doc_db.content, 'updated doc') self.assertEqual(u_doc_db.rowid, doc_db.rowid) 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.rowid) self.assertEqual(idx_doc._data['index'], self.index.id)
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.rowid, 'name': 'idx-0'}, {'document': document.rowid, 'name': 'idx-1'}, {'document': document.rowid, 'name': 'idx-2'}, ])
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') alt_idx.add_to_index(d1) alt_idx.add_to_index(d2) self.assertEqual(Metadata.select().count(), 3) response = self.app.delete('/documents/%s/' % d2.get_id()) data = json.loads(response.data) self.assertEqual(data, {'success': True}) self.assertEqual(Metadata.select().count(), 2) 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_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.loads(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_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 test_index_document(self): """ Basic test case ensuring that content can be indexed and that the many-to-many relationship between documents and indexes is set up correctly. """ content = 'huey is a sweet little kitty' doc = self.index.index(content=content) # Verify document properties. self.assertEqual(doc.rowid, 1) self.assertEqual(doc.content, content) self.assertEqual(doc.metadata, {}) # Verify through relationship properties. self.assertEqual(IndexDocument.select().count(), 1) idx_doc = IndexDocument.get() self.assertEqual(idx_doc._data['document'], doc.rowid) self.assertEqual(idx_doc._data['index'], self.index.id)
def test_index_document(self): """ Basic test case ensuring that content can be indexed and that the many-to-many relationship between documents and indexes is set up correctly. """ content = 'huey is a sweet little kitty' doc = self.index.index(content=content) # Verify document properties. self.assertEqual(doc.get_id(), 1) self.assertEqual(doc.content, content) self.assertEqual(doc.metadata, {}) # Verify through relationship properties. self.assertEqual(IndexDocument.select().count(), 1) idx_doc = IndexDocument.get() self.assertEqual(idx_doc.__data__['document'], doc.get_id()) self.assertEqual(idx_doc.__data__['index'], self.index.id)
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.loads(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 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.loads(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)