<s> import uuid <EOL> from corehq . apps . case_search . exceptions import CaseSearchNotEnabledException <EOL> from corehq . apps . case_search . models import CaseSearchConfig <EOL> from corehq . apps . change_feed import topics <EOL> from corehq . apps . change_feed . consumer . feed import change_meta_from_kafka_message <EOL> from corehq . apps . change_feed . producer import producer <EOL> from corehq . apps . es import CaseSearchES <EOL> from corehq . apps . userreports . tests . utils import doc_to_change <EOL> from corehq . elastic import get_es_new <EOL> from corehq . form_processor . tests . utils import FormProcessorTestUtils <EOL> from corehq . pillows . case_search import delete_case_search_cases , get_case_search_to_elasticsearch_pillow , get_case_search_reindexer <EOL> from corehq . pillows . mappings . case_search_mapping import CASE_SEARCH_INDEX , CASE_SEARCH_INDEX_INFO <EOL> from corehq . util . elastic import ensure_index_deleted <EOL> from django . test import TestCase , override_settings <EOL> from mock import MagicMock , patch <EOL> from pillowtop . es_utils import initialize_index_and_mapping <EOL> from testapps . test_pillowtop . utils import get_current_kafka_seq , get_test_kafka_consumer <EOL> from corehq . util . test_utils import create_and_save_a_case <EOL> class CaseSearchPillowTest ( TestCase ) : <EOL> domain = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> FormProcessorTestUtils . delete_all_cases ( ) <EOL> self . elasticsearch = get_es_new ( ) <EOL> self . pillow = get_case_search_to_elasticsearch_pillow ( ) <EOL> ensure_index_deleted ( CASE_SEARCH_INDEX ) <EOL> initialize_index_and_mapping ( get_es_new ( ) , CASE_SEARCH_INDEX_INFO ) <EOL> def tearDown ( self ) : <EOL> ensure_index_deleted ( CASE_SEARCH_INDEX ) <EOL> CaseSearchConfig . objects . all ( ) . delete ( ) <EOL> def test_case_search_pillow ( self ) : <EOL> consumer = get_test_kafka_consumer ( topics . CASE ) <EOL> kafka_seq = self . _get_kafka_seq ( ) <EOL> case = self . _make_case ( case_properties = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> producer . send_change ( topics . CASE , doc_to_change ( case ) . metadata ) <EOL> message = consumer . next ( ) <EOL> change_meta = change_meta_from_kafka_message ( message . value ) <EOL> self . assertEqual ( case . case_id , change_meta . document_id ) <EOL> self . assertEqual ( self . domain , change_meta . domain ) <EOL> with patch ( '<STR_LIT>' , <EOL> new = MagicMock ( return_value = True ) ) as fake_case_search_enabled_for_domain : <EOL> self . pillow . process_changes ( since = kafka_seq , forever = False ) <EOL> fake_case_search_enabled_for_domain . assert_called_with ( self . domain ) <EOL> self . _assert_case_in_es ( self . domain , case ) <EOL> def test_case_search_reindex_by_domain ( self ) : <EOL> """<STR_LIT>""" <EOL> other_domain = "<STR_LIT>" <EOL> CaseSearchConfig . objects . get_or_create ( pk = other_domain , enabled = True ) <EOL> desired_case = self . _make_case ( domain = other_domain ) <EOL> undesired_case = self . _make_case ( domain = self . domain ) <EOL> with self . assertRaises ( CaseSearchNotEnabledException ) : <EOL> get_case_search_reindexer ( domain = self . domain ) . reindex ( ) <EOL> get_case_search_reindexer ( domain = other_domain ) . reindex ( ) <EOL> self . _assert_case_in_es ( other_domain , desired_case ) <EOL> def test_delete_case_search_cases ( self ) : <EOL> """<STR_LIT>""" <EOL> other_domain = "<STR_LIT>" <EOL> self . _bootstrap_cases_in_es_for_domain ( self . domain ) <EOL> case = self . _bootstrap_cases_in_es_for_domain ( other_domain ) <EOL> with self . assertRaises ( TypeError ) : <EOL> delete_case_search_cases ( None ) <EOL> with self . assertRaises ( TypeError ) : <EOL> delete_case_search_cases ( { } ) <EOL> delete_case_search_cases ( self . domain ) <EOL> self . _assert_case_in_es ( other_domain , case ) <EOL> delete_case_search_cases ( other_domain ) <EOL> self . _assert_index_empty ( ) <EOL> @ override_settings ( TESTS_SHOULD_USE_SQL_BACKEND = True ) <EOL> def test_sql_case_search_pillow ( self ) : <EOL> consumer = get_test_kafka_consumer ( topics . CASE_SQL ) <EOL> kafka_seq = self . _get_kafka_seq ( ) <EOL> case = self . _make_case ( case_properties = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> message = consumer . next ( ) <EOL> change_meta = change_meta_from_kafka_message ( message . value ) <EOL> self . assertEqual ( case . case_id , change_meta . document_id ) <EOL> self . assertEqual ( self . domain , change_meta . domain ) <EOL> with patch ( '<STR_LIT>' , <EOL> new = MagicMock ( return_value = True ) ) as fake_case_search_enabled_for_domain : <EOL> self . pillow . process_changes ( since = kafka_seq , forever = False ) <EOL> fake_case_search_enabled_for_domain . assert_called_with ( self . domain ) <EOL> self . _assert_case_in_es ( self . domain , case ) <EOL> def _get_kafka_seq ( self ) : <EOL> return { <EOL> topics . CASE_SQL : get_current_kafka_seq ( topics . CASE_SQL ) , <EOL> topics . CASE : get_current_kafka_seq ( topics . CASE ) <EOL> } <EOL> def _make_case ( self , domain = None , case_properties = None ) : <EOL> case_id = uuid . uuid4 ( ) . hex <EOL> case_name = '<STR_LIT>' . format ( uuid . uuid4 ( ) . hex ) <EOL> if domain is None : <EOL> domain = self . domain <EOL> case = create_and_save_a_case ( domain , case_id , case_name , case_properties ) <EOL> return case <EOL> def _assert_case_in_es ( self , domain , case ) : <EOL> self . elasticsearch . indices . refresh ( CASE_SEARCH_INDEX ) <EOL> results = CaseSearchES ( ) . run ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , results . total ) <EOL> case_doc = results . hits [ <NUM_LIT:0> ] <EOL> self . assertEqual ( domain , case_doc [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( case . case_id , case_doc [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( case . name , case_doc [ '<STR_LIT:name>' ] ) <EOL> self . assertItemsEqual ( case_doc [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . keys ( ) , [ '<STR_LIT:key>' , '<STR_LIT:value>' ] ) <EOL> for case_property in case_doc [ '<STR_LIT>' ] : <EOL> key = case_property [ '<STR_LIT:key>' ] <EOL> self . assertEqual ( case . get_case_property ( key ) , case_property [ '<STR_LIT:value>' ] ) <EOL> def _assert_index_empty ( self ) : <EOL> self . elasticsearch . indices . refresh ( CASE_SEARCH_INDEX ) <EOL> results = CaseSearchES ( ) . run ( ) <EOL> self . assertEqual ( <NUM_LIT:0> , results . total ) <EOL> def _bootstrap_cases_in_es_for_domain ( self , domain ) : <EOL> case = self . _make_case ( domain ) <EOL> CaseSearchConfig . objects . get_or_create ( pk = domain , enabled = True ) <EOL> get_case_search_reindexer ( domain ) . reindex ( ) <EOL> return case </s>
