def test_should_build_query_with_start_and_end_date_for_mm_yyyy_format(self): mock_form_model = MagicMock(spec=FormModel) mock_form_model.get_field_by_code.return_value = DateField(name='q1', code='q1', label='q1', date_format='mm.yyyy') mock_form_model.id = 'form_id' actual_query = DateQuestionRangeFilter('11.2013-09.2013', mock_form_model, 'q1').build_filter_query() self.assertDictEqual(actual_query.to_dict(), {'range': {'form_id_q1_value': {'gte': u'11.2013', 'lte': u'09.2013'}}})
def test_should_call_query_match_for_current_day(self): mock_form_model = MagicMock(spec=FormModel) mock_form_model.get_field_by_code.return_value = DateField(name='q1', code='q1', label='q1', date_format='dd.mm.yyyy') mock_form_model.id = 'form_id' today = "26.11.2013" actual_query = DateQuestionRangeFilter(today, mock_form_model, 'q1').build_filter_query() self.assertDictEqual(actual_query.to_dict(), {'range': {'form_id_q1_value': {'gte': u'26.11.2013', 'lte': u'26.11.2013'}}})
def _create_search(dbm, form_model, local_time_delta, pagination_params, sort_params, search_parameters): es = Elasticsearch(hosts=[{ "host": ELASTIC_SEARCH_HOST, "port": ELASTIC_SEARCH_PORT }]) search = Search(using=es, index=dbm.database_name, doc_type=form_model.id) search = search.sort(sort_params) search = search.extra(**pagination_params) search = search.query('match', status='Success') search = search.query('term', void=False) if search_parameters.get('data_sender_filter'): search = search.query( "term", **{"datasender.id": search_parameters.get('data_sender_filter')}) if search_parameters.get('unique_id_filters'): search = _add_unique_id_filters( form_model, search_parameters.get('unique_id_filters'), search) if search_parameters.get('date_question_filters'): for key, values in search_parameters.get( 'date_question_filters').iteritems(): query = DateQuestionRangeFilter(values['dateRange'], form_model, key).build_filter_query() if query is not None: search = search.query(query) if search_parameters.get('search_text'): query_text_escaped = ElasticUtilsHelper().replace_special_chars( search_parameters.get('search_text')) search = search.query("query_string", query=query_text_escaped) submission_date_query = SubmissionDateRangeFilter( search_parameters.get('submission_date_range'), local_time_delta).build_filter_query() if submission_date_query: search = search.query(submission_date_query) return search
def _add_date_range_filters(self, query, search_filter_param): date_filters = search_filter_param.get("dateQuestionFilters") if date_filters: for question_code, date_range in date_filters.items(): if date_range: query = DateQuestionRangeFilter( date_range, self.form_model, question_code).build_filter_query(query) return query
def _add_date_range_filters(date_filters, form_model, search): if date_filters: for question_code, date_range in date_filters.items(): if date_range: date_query = DateQuestionRangeFilter( date_range, form_model, question_code).build_filter_query() if date_query: search = search.query(date_query) return search
def test_should_call_query_match_for_current_day(self): mock_query = Mock(spec=elasticutils.S) mock_form_model = MagicMock(spec=FormModel) mock_form_model.get_field_by_code.return_value = DateField( name='q1', code='q1', label='q1', date_format='dd.mm.yyyy') mock_form_model.id = 'form_id' today = "26.11.2013" DateQuestionRangeFilter(today, mock_form_model, 'q1').build_filter_query(mock_query) mock_query.filter.assert_called_with( form_id_q1_value__range=['26.11.2013', '26.11.2013'])
def test_should_build_query_with_start_and_end_date_for_mm_yyyy_format( self): mock_query = Mock(spec=elasticutils.S) mock_form_model = MagicMock(spec=FormModel) mock_form_model.get_field_by_code.return_value = DateField( name='q1', code='q1', label='q1', date_format='mm.yyyy') mock_form_model.id = 'form_id' DateQuestionRangeFilter('11.2013-09.2013', mock_form_model, 'q1').build_filter_query(mock_query) mock_query.filter.assert_called_with( form_id_q1_value__range=['11.2013', '09.2013'])