class AbridgeDictTestCase(TestCase): """ Tests the abridge_dict function. """ schema = [ DataField(field_name='test1.foo', field_type='TextField'), DataField(field_name='test1.bar', field_type='TextField'), DataField(field_name='test3', field_type='TextField'), DataField(field_name='test4', field_type='TextField'), ] def test_abridge_dict(self): """ Tests method for getting a value from a nested dictionary. """ doc = { 'test1': { 'foo': 1, 'bar': 2, 'bogus': 3 }, 'test2': 4, # not in schema 'test3': 5, 'test4': None # null value } actual = parserutils.abridge_dict(self.schema, doc) expected = {'test1': {'foo': 1, 'bar': 2}, 'test3': 5} self.assertEqual(actual, expected)
def test_to_key_val_wo_target_type(self): """ Tests the to_key_val method for a field without a defined target_type. """ datafield = DataField( field_name='name', field_type='CharField', ) actual = datafield.to_key_val() expected = {'name': 'CharField'} self.assertEqual(actual, expected)
def test_find_fields(self): """ Tests that the find method only returns fields defined in the Engine's schema. """ doc_id = self.engine.insert({ 'content': { 'text': 'I like cats and dogs.', 'tags': ['cats', 'dogs'], }, 'user': { 'screen_name': 'Jill', 'email': '*****@*****.**', }, }) self.engine.schema = [ DataField( field_name='content.text', field_type='TextField', target_type='Keyword' ), DataField( field_name='user.screen_name', field_type='CharField', target_type='Account' ) ] fieldsets = [ QueryFieldset( field_name='user.screen_name', field_type='CharField', operator='eq', value='Jill' ) ] query = EngineQuery(fieldsets, 'AND') actual = self.engine.find(query) expected = { 'count': 1, 'results': [ { '_id': doc_id, 'content': { 'text': 'I like cats and dogs.' }, 'user': { 'screen_name': 'Jill' } } ] } self.assertEqual(actual, expected)
def test_no_query_and_type_nonmatch(self): """ Tests the _field_type_is_match method when the field_type doesn not match and no query is defined. """ query = None field = DataField(field_name='content', field_type='PointField') result = self.autocomplete._field_type_is_match(field, query) self.assertIs(result, False)
def test_no_query_and_type_match(self): """ Tests the _field_type_is_match method when both the field_type matches and no query is defined. """ query = None field = DataField(field_name='content', field_type='CharField') result = self.autocomplete._field_type_is_match(field, query) self.assertIs(result, True)
def test_no_match(self): """ Tests the _field_type_is_match method when neither the field_name nor the field_type matches. """ query = 'cont' field = DataField(field_name='location', field_type='PointField') result = self.autocomplete._field_type_is_match(field, query) self.assertIs(result, False)
def test_query_only_is_match(self): """ Tests the _field_type_is_match method when the field_name matches but not the field_type. """ query = 'cont' field = DataField(field_name='content', field_type='EmbeddedDocument') result = self.autocomplete._field_type_is_match(field, query) self.assertIs(result, False)
def test_field_type_only_is_match(self): """ Tests the _field_type_is_match method when the field_type matches but not the field_name. """ query = 'cont' field = DataField(field_name='text', field_type='CharField') result = self.autocomplete._field_type_is_match(field, query) self.assertIs(result, False)
def test_field_type_and_query_match(self): """ Tests the _field_type_is_match method when the field_name and field_type match. """ query = 'cont' field = DataField(field_name='content', field_type='CharField') result = self.autocomplete._field_type_is_match(field, query) self.assertIs(result, True)
def test_no_field_types_match(self): """ Tests the _field_type_is_match method when the field_type doesn not match and no qno field_types are defined. """ autocomplete = FilterFieldsAutocompleteBase() query = None field = DataField(field_name='content', field_type='CharField') result = autocomplete._field_type_is_match(field, query) self.assertIs(result, True)
def test_filter_ids_analyzed(self): """ Tests the filter_ids method. """ id_0 = self.engine.insert(self.test_docs[0]) id_1 = self.engine.insert(self.test_docs[1]) id_2 = self.engine.insert(self.test_docs[2]) ids = [id_0, id_1, id_2] actual = self.engine.filter_ids( doc_ids=ids, fields=[ DataField(field_name='content.text', field_type='TextField'), DataField(field_name='content.tags', field_type='ListField') ], value='CATS') expected = [id_0, id_2] actual.sort() expected.sort() self.assertEqual(actual, expected)
class DateFieldTestCase(TestCase): """ Tests the DateField class. """ datafield = DataField( field_name='title', field_type='CharField', target_type='Keyword' ) def test_to_dict_no_parent(self): """ Tests the to_dict method when no parent_name is provided. """ actual = self.datafield.to_dict() expected = { 'field_name': 'title', 'field_type': 'CharField', 'target_type': 'Keyword' } self.assertEqual(actual, expected) def test_to_dict_with_parent(self): """ Tests the to_dict method when no parent_name is provided. """ actual = self.datafield.to_dict('post') expected = { 'field_name': 'post.title', 'field_type': 'CharField', 'target_type': 'Keyword' } self.assertEqual(actual, expected) def test_to_key_val_w_target_type(self): """ Tests the to_key_val method for a field with a defined target_type. """ actual = self.datafield.to_key_val() expected = {'title': 'CharField (Keyword)'} self.assertEqual(actual, expected) def test_to_key_val_wo_target_type(self): """ Tests the to_key_val method for a field without a defined target_type. """ datafield = DataField( field_name='name', field_type='CharField', ) actual = datafield.to_key_val() expected = {'name': 'CharField'} self.assertEqual(actual, expected)
def test_filter_ids_not_analyzed(self): """ Tests the filter_ids method for a mixture of exact-text and full-text fields in ELasticsearch. """ id_0 = self.engine.insert(self.test_docs[0]) id_1 = self.engine.insert(self.test_docs[1]) id_2 = self.engine.insert(self.test_docs[2]) ids = [id_0, id_1, id_2] actual = self.engine.filter_ids(doc_ids=ids, fields=[ DataField(field_name='user.link', field_type='URLField'), DataField(field_name='user.email', field_type='EmailField') ], value='example') expected = [id_0, id_1, id_2] actual.sort() expected.sort() self.assertEqual(actual, expected)
def create_mapping(doc_type, fields): """Return a mappings parameter dict for an Elasticsearch index. Parameters ---------- doc_type : str An Elasticsearch doc_type. fields : |list| of |dict| List of dictionaries in the form of `{FIELD_NAME_KEY: <field name>, FIELD_TYPE_KEY: <field type>}`. Returns ------- dict Mappings parameter for a doc_type in an Elasticsearch index. """ default = {'type': 'text'} # add mapping for the _saved_date field added to every record fields.append(DataField(field_name=_DATE_KEY, field_type='DateTimeField')) properties = {'properties': {}} for field in fields: mapping = _MAPPINGS.get(field.field_type, default) prop = _map_field(mapping, field.field_name, properties) properties.update(prop) mappings = { 'settings': { 'index.mapping.ignore_malformed': True }, 'mappings': { doc_type: properties } } return mappings