<s> from nose . tools import eq_ <EOL> from elasticutils import get_es <EOL> from elasticutils import S , MappingType , Indexable <EOL> from elasticutils . tests import ESTestCase <EOL> class FakeModel ( object ) : <EOL> _cache = { } <EOL> @ classmethod <EOL> def reset ( cls ) : <EOL> cls . _cache = { } <EOL> def __init__ ( self , ** fields ) : <EOL> self . __dict__ . update ( fields ) <EOL> self . _cache [ fields [ '<STR_LIT:id>' ] ] = self <EOL> def get ( self , id ) : <EOL> return self . _cache [ id ] <EOL> @ classmethod <EOL> def get_objects ( self ) : <EOL> return self . _cache . values ( ) <EOL> class FakeMappingType ( MappingType , Indexable ) : <EOL> @ classmethod <EOL> def get_index ( cls ) : <EOL> return ESTestCase . index_name <EOL> @ classmethod <EOL> def get_mapping_type_name ( cls ) : <EOL> return ESTestCase . mapping_type_name <EOL> @ classmethod <EOL> def get_model ( cls ) : <EOL> return FakeModel <EOL> @ classmethod <EOL> def get_es ( cls ) : <EOL> return get_es ( ** ESTestCase . es_settings ) <EOL> @ classmethod <EOL> def get_mapping ( cls ) : <EOL> return { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : { '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> '<STR_LIT:title>' : { '<STR_LIT:type>' : '<STR_LIT:string>' } , <EOL> '<STR_LIT>' : { '<STR_LIT:type>' : '<STR_LIT:string>' } <EOL> } <EOL> } <EOL> @ classmethod <EOL> def extract_document ( cls , obj_id , obj = None ) : <EOL> if obj == None : <EOL> obj = cls . get_model ( ) . get ( id = obj_id ) <EOL> doc = { } <EOL> doc [ '<STR_LIT:id>' ] = obj . id <EOL> doc [ '<STR_LIT:title>' ] = obj . title <EOL> doc [ '<STR_LIT>' ] = obj . tags <EOL> return doc <EOL> @ classmethod <EOL> def get_indexable ( cls ) : <EOL> return cls . get_model ( ) . get_objects ( ) <EOL> class ModelTest ( ESTestCase ) : <EOL> mapping_type_name = FakeMappingType . get_mapping_type_name ( ) <EOL> mapping = { <EOL> FakeMappingType . get_mapping_type_name ( ) : FakeMappingType . get_mapping ( ) <EOL> } <EOL> def test_refresh ( self ) : <EOL> """<STR_LIT>""" <EOL> FakeMappingType . refresh_index ( ) <EOL> def setUp ( self ) : <EOL> super ( ESTestCase , self ) . setUp ( ) <EOL> ESTestCase . setup_class ( ) <EOL> def tearDown ( self ) : <EOL> super ( ESTestCase , self ) . tearDown ( ) <EOL> ESTestCase . teardown_class ( ) <EOL> FakeModel . reset ( ) <EOL> def test_index ( self ) : <EOL> obj1 = FakeModel ( id = <NUM_LIT:1> , title = '<STR_LIT>' , tags = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> FakeMappingType . index ( <EOL> FakeMappingType . extract_document ( obj_id = obj1 . id , obj = obj1 ) , <EOL> id_ = obj1 . id ) <EOL> FakeMappingType . refresh_index ( ) <EOL> s = S ( FakeMappingType ) <EOL> eq_ ( s . count ( ) , <NUM_LIT:1> ) <EOL> eq_ ( list ( s . execute ( ) ) [ <NUM_LIT:0> ] . title , obj1 . title ) <EOL> def test_bulk_index ( self ) : <EOL> FakeModel ( id = <NUM_LIT:1> , title = '<STR_LIT>' , tags = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> FakeModel ( id = <NUM_LIT:2> , title = '<STR_LIT>' , tags = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> documents = [ ] <EOL> for obj in FakeModel . get_objects ( ) : <EOL> documents . append ( <EOL> FakeMappingType . extract_document ( obj_id = obj . id , obj = obj ) ) <EOL> FakeMappingType . bulk_index ( documents , id_field = '<STR_LIT:id>' ) <EOL> FakeMappingType . refresh_index ( ) <EOL> s = S ( FakeMappingType ) <EOL> eq_ ( s . count ( ) , <NUM_LIT:2> ) <EOL> eq_ ( sorted ( [ res . title for res in s . execute ( ) ] ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_unindex ( self ) : <EOL> obj1 = FakeModel ( id = <NUM_LIT:1> , title = '<STR_LIT>' , tags = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> FakeMappingType . index ( <EOL> FakeMappingType . extract_document ( obj_id = obj1 . id , obj = obj1 ) , <EOL> id_ = obj1 . id ) <EOL> FakeMappingType . refresh_index ( ) <EOL> s = S ( FakeMappingType ) <EOL> eq_ ( s . count ( ) , <NUM_LIT:1> ) <EOL> eq_ ( list ( s . execute ( ) ) [ <NUM_LIT:0> ] . title , obj1 . title ) <EOL> FakeMappingType . unindex ( id_ = obj1 . id ) <EOL> FakeMappingType . refresh_index ( ) <EOL> s = S ( FakeMappingType ) <EOL> eq_ ( s . count ( ) , <NUM_LIT:0> ) </s>
