def test_pipe_not_indexed(self): """ The | symbols is used for TokenFieldIndex key generation so shouldn't be indexed... ever! """ class Doc(Document): name = fields.TextField() index = Index(name="test") index.add(Doc(name="|| Pipes")) self.assertEqual(index.document_count(), 1) self.assertEqual(TokenFieldIndex.objects.count(), 1) # Just "pipes"
def test_null_validation(self): """ If a field is marked as null=False, and someone tries to index None, then an IntegrityError should throw. None of the documents should be indexed if one of them is invalid. """ class Doc(Document): text = fields.TextField(null=False) index = Index("test") doc1 = Doc(text="test") doc2 = Doc(text=None) self.assertRaises(IntegrityError, index.add, [doc1, doc2]) self.assertEqual(index.document_count(), 0) # Nothing should've been indexed
def test_removing_document(self): class Doc(Document): text = fields.TextField() i0 = Index(name="index1") i1 = Index(name="index2") d0 = i0.add(Doc(text="One")) # One field, one token self.assertEqual( TokenFieldIndex.objects.count(), 1 ) self.assertEqual(i0.document_count(), 1) self.assertEqual(i1.document_count(), 0) d1 = i0.add(Doc(text="Two")) # Two fields, one token each self.assertEqual( TokenFieldIndex.objects.count(), 2 ) self.assertEqual(i0.document_count(), 2) self.assertEqual(i1.document_count(), 0) d2 = i1.add(Doc(text="Three 3")) # Three fields, one token each except last which has 2 self.assertEqual( TokenFieldIndex.objects.count(), 4 ) self.assertEqual(i0.document_count(), 2) self.assertEqual(i1.document_count(), 1) self.assertTrue(i0.remove(d0)) self.assertFalse(i0.remove(d0)) self.assertEqual(i0.document_count(), 1) self.assertEqual(i1.document_count(), 1) self.assertEqual( TokenFieldIndex.objects.count(), 3 ) self.assertFalse([x for x in i0.search("text:One", Doc)]) self.assertTrue(i0.remove(d1)) self.assertEqual(i0.document_count(), 0) self.assertEqual(i1.document_count(), 1) self.assertEqual( TokenFieldIndex.objects.count(), 2 ) self.assertFalse([x for x in i0.search("text:Two", Doc)]) self.assertTrue(i1.remove(d2)) self.assertEqual(i0.document_count(), 0) self.assertEqual(i1.document_count(), 0) self.assertEqual( TokenFieldIndex.objects.count(), 0 ) self.assertFalse([x for x in i1.search("text:Three", Doc)]) self.assertFalse([x for x in i1.search("text:3", Doc)])