33
44import django
55from django .db import models
6+
67if django .VERSION < (4 , 0 ):
78 from django .utils .translation import ugettext_lazy as _
89else :
@@ -366,7 +367,8 @@ class Django:
366367 3 , len (list (mock_bulk .call_args_list [0 ][1 ]['actions' ]))
367368 )
368369 self .assertEqual (mock_bulk .call_count , 1 , "bulk is called" )
369- self .assertEqual (mock_parallel_bulk .call_count , 0 , "parallel bulk is not called" )
370+ self .assertEqual (mock_parallel_bulk .call_count , 0 ,
371+ "parallel bulk is not called" )
370372
371373 def test_model_instance_iterable_update_with_parallel (self ):
372374 class CarDocument2 (DocType ):
@@ -405,7 +407,8 @@ def test_init_prepare_correct(self):
405407 e = expect [name ]
406408 self .assertEqual (str (type (field )), e [0 ], 'field type should be copied over' )
407409 self .assertTrue ('__call__' in dir (prep ), 'prep function should be callable' )
408- self .assertTrue (str (type (prep )) in e [1 ], 'prep function is correct partial or method' )
410+ self .assertTrue (str (type (prep )) in e [1 ],
411+ 'prep function is correct partial or method' )
409412
410413 def test_init_prepare_results (self ):
411414 """Are the results from init_prepare() actually used in prepare()?"""
@@ -418,7 +421,9 @@ def test_init_prepare_results(self):
418421 setattr (car , 'pk' , 4701 ) # Ignored, not in document
419422 setattr (car , 'type' , "imaginary" )
420423
421- self .assertEqual (d .prepare (car ), {'color' : 'blue' , 'type' : 'imaginary' , 'name' : 'Tusla' , 'price' : 340123.21 })
424+ self .assertEqual (d .prepare (car ),
425+ {'color' : 'blue' , 'type' : 'imaginary' , 'name' : 'Tusla' ,
426+ 'price' : 340123.21 })
422427
423428 m = Mock ()
424429 # This will blow up should we access _fields and try to iterate over it.
@@ -491,3 +496,45 @@ def generate_id(cls, article):
491496 # The generator get executed there.
492497 data = json .loads (mock_bulk .call_args [1 ]['body' ].split ("\n " )[0 ])
493498 assert data ["index" ]["_id" ] == article .slug
499+
500+ @patch ('elasticsearch_dsl.connections.Elasticsearch.bulk' )
501+ def test_should_index_object_is_called (self , mock_bulk ):
502+ doc = CarDocument ()
503+ car1 = Car ()
504+ car2 = Car ()
505+ car3 = Car ()
506+ should_index_object = ("django_elasticsearch_dsl.documents."
507+ "DocType.should_index_object" )
508+ with patch (should_index_object ) as mock_should_index_object :
509+ doc .update ([car1 , car2 , car3 ])
510+ # As we are indexing 3 object, it should be called 3 times
511+ self .assertEqual (mock_should_index_object .call_count , 3 ,
512+ "should_index_object is called" )
513+
514+ @patch ('elasticsearch_dsl.connections.Elasticsearch.bulk' )
515+ def test_should_index_object_working_perfectly (self , mock_bulk ):
516+ article1 = Article (slug = 'article1' )
517+ article2 = Article (slug = 'article2' )
518+
519+ @registry .register_document
520+ class ArticleDocument (DocType ):
521+ class Django :
522+ model = Article
523+ fields = [
524+ 'slug' ,
525+ ]
526+
527+ class Index :
528+ name = 'test_articles'
529+
530+ def should_index_object (self , obj ):
531+ # Article with slug article1 should not be indexed
532+ if obj .slug == "article2" :
533+ return False
534+ return True
535+
536+ d = ArticleDocument ()
537+ d .update ([article1 , article2 ])
538+ data_body = mock_bulk .call_args [1 ]['body' ]
539+ self .assertTrue (article1 .slug in data_body )
540+ self .assertTrue (article2 .slug not in data_body )
0 commit comments