def test_set_parent(self): entity = Entity(name='test01', created_user=self._test_user) entity.save() attr_base = EntityAttr(name='hoge', created_user=self._test_user, parent_entity=entity) attr_base.save() self.assertEqual(attr_base.parent_entity, entity)
def test_make_attrbase(self): entity = Entity(name='test01', created_user=self._test_user) entity.save() attr_base = EntityAttr(name='hoge', created_user=self._test_user, parent_entity=entity) attr_base.save() self.assertEqual(attr_base.name, 'hoge') self.assertTrue(isinstance(attr_base.type, int)) self.assertEqual(attr_base.referral.count(), 0)
def test_set_parent(self): entity = Entity(name="test01", created_user=self._test_user) entity.save() attr_base = EntityAttr( name="hoge", type=AttrTypeValue["object"], created_user=self._test_user, parent_entity=entity, ) attr_base.save() self.assertEqual(attr_base.parent_entity, entity)
def test_make_attrbase(self): entity = Entity(name="test01", created_user=self._test_user) entity.save() attr_base = EntityAttr( name="hoge", type=AttrTypeValue["object"], created_user=self._test_user, parent_entity=entity, ) attr_base.save() self.assertEqual(attr_base.name, "hoge") self.assertTrue(isinstance(attr_base.type, int)) self.assertEqual(attr_base.referral.count(), 0)
class ElasticSearchTest(TestCase): def setUp(self): self._user = User(username='******') self._user.save() self._entity = Entity(name='entity', created_user=self._user) self._entity.save() self._entity_attr = EntityAttr(name='test', type=AttrTypeStr, is_mandatory=True, created_user=self._user, parent_entity=self._entity) self._entity_attr.save() def test_get_regex_pattern(self): # without escape character p1 = elasticsearch._get_regex_pattern('keyword') self.assertEqual(p1, '.*[kK][eE][yY][wW][oO][rR][dD].*') # with escape character p2 = elasticsearch._get_regex_pattern('key@@@word') self.assertEqual(p2, '.*[kK][eE][yY]\\@\\@\\@[wW][oO][rR][dD].*') # with anchor operators p2 = elasticsearch._get_regex_pattern('^keyword$') self.assertEqual(p2, '.*^?[kK][eE][yY][wW][oO][rR][dD]$?.*') def test_make_key_for_each_block_of_keywords(self): key1 = elasticsearch._make_key_for_each_block_of_keywords( {'name': 'name'}, 'keyword', True) self.assertEqual(key1, 'keyword') key2 = elasticsearch._make_key_for_each_block_of_keywords( {'name': 'name'}, 'keyword', False) self.assertEqual(key2, 'keyword_name') def test_is_matched_keyword(self): # if it has the same value with a hint self.assertTrue(elasticsearch._is_matched_entry( attrs=[{'name': 'attr', 'value': 'keyword', 'type': AttrTypeStr.TYPE}], hint_attrs=[{'name': 'attr', 'keyword': 'keyword'}] )) # if a hint has ^ and/or $, it matches with the keyword as a regexp self.assertTrue(elasticsearch._is_matched_entry( attrs=[{'name': 'attr', 'value': 'keyword', 'type': AttrTypeStr.TYPE}], hint_attrs=[{'name': 'attr', 'keyword': '^keyword'}] )) self.assertFalse(elasticsearch._is_matched_entry( attrs=[{'name': 'attr', 'value': '111keyword', 'type': AttrTypeStr.TYPE}], hint_attrs=[{'name': 'attr', 'keyword': '^keyword'}] )) self.assertTrue(elasticsearch._is_matched_entry( attrs=[{'name': 'attr', 'value': 'keyword', 'type': AttrTypeStr.TYPE}], hint_attrs=[{'name': 'attr', 'keyword': 'keyword$'}] )) self.assertFalse(elasticsearch._is_matched_entry( attrs=[{'name': 'attr', 'value': 'keyword111', 'type': AttrTypeStr.TYPE}], hint_attrs=[{'name': 'attr', 'keyword': 'keyword$'}] )) # if a hint is blank self.assertTrue(elasticsearch._is_matched_entry( attrs=[{'name': 'attr', 'value': 'keyword', 'type': AttrTypeStr.TYPE}], hint_attrs=[{'name': 'attr', 'keyword': ''}] )) def test_make_query(self): query = elasticsearch.make_query( hint_entity_ids=['1'], hint_attrs=[{'name': 'a1', 'keyword': 'a'}, {'name': 'a2', 'keyword': ''}], hint_attr_value=None, entry_name='entry1', or_match=False, ) self.assertEqual(query, { 'query': { 'bool': { 'filter': [ { 'nested': { 'path': 'entity', 'query': { 'bool': { 'should': [ {'term': {'entity.id': 1}} ] } } } }, { 'bool': { 'should': [ { 'bool': { 'filter': [ {'regexp': {'name': '.*[eE][nN][tT][rR][yY]1.*'}} ] } } ] } }, { 'nested': { 'path': 'attr', 'query': { 'bool': { 'should': [ {'term': {'attr.name': 'a1'}}, {'term': {'attr.name': 'a2'}} ] } } } }, { 'nested': { 'path': 'attr', 'query': { 'bool': { 'filter': [ {'term': {'attr.name': 'a1'}}, { 'bool': { 'should': [ {'match': {'attr.value': 'a'}}, {'regexp': {'attr.value': '.*[aA].*'}} ] } } ] } } } } ], 'should': [] } } }) def test_make_search_results(self): entry = Entry.objects.create(name='test_entry', schema=self._entity, created_user=self._user) attr = Attribute.objects.create(name='test_attr', schema=self._entity_attr, created_user=self._user, parent_entry=entry) attr_value = AttributeValue.objects.create(value='test_attr_value', created_user=self._user, parent_attr=attr) attr.values.add(attr_value) attr.save() res = { 'hits': { 'total': 1, 'hits': [ { '_type': 'entry', '_id': entry.id, '_source': { 'entity': { 'id': entry.id, 'name': entry.name }, 'name': entry.name, 'attr': [ { 'name': attr.name, 'type': attr.schema.type, 'key': '', 'value': attr_value.value, 'referral_id': '' } ] }, 'sort': [entry.name] } ] } } hint_attrs = [{'name': 'test_attr', 'keyword': ''}] results = elasticsearch.make_search_results(res, hint_attrs, 100, False) self.assertEqual(results['ret_count'], 1) self.assertEqual(results['ret_values'], [ { 'entity': { 'id': self._entity.id, 'name': self._entity.name, }, 'entry': { 'id': entry.id, 'name': entry.name }, 'attrs': { attr.name: { 'type': attr.schema.type, 'value': attr_value.value, } }, } ])
class ElasticSearchTest(TestCase): def setUp(self): self._user = User(username="******") self._user.save() self._entity = Entity(name="entity", created_user=self._user) self._entity.save() self._entity_attr = EntityAttr( name="test", type=AttrTypeStr, is_mandatory=True, created_user=self._user, parent_entity=self._entity, ) self._entity_attr.save() def test_get_regex_pattern(self): # without escape character p1 = elasticsearch._get_regex_pattern("keyword") self.assertEqual(p1, ".*[kK][eE][yY][wW][oO][rR][dD].*") # with escape character p2 = elasticsearch._get_regex_pattern("key@@@word") self.assertEqual(p2, ".*[kK][eE][yY]\\@\\@\\@[wW][oO][rR][dD].*") # with anchor operators p2 = elasticsearch._get_regex_pattern("^keyword$") self.assertEqual(p2, "[kK][eE][yY][wW][oO][rR][dD]") def test_make_query(self): query = elasticsearch.make_query( hint_entity=self._entity, hint_attrs=[ {"name": "a1", "keyword": "hoge|fu&ga"}, {"name": "a2", "keyword": ""}, ], entry_name="entry1", ) self.assertEqual( query, { "query": { "bool": { "filter": [ { "nested": { "path": "entity", "query": {"term": {"entity.id": self._entity.id}}, } }, { "bool": { "should": [ { "bool": { "must": [ { "regexp": { "name": ".*[eE][nN][tT][rR][yY]1.*" } } ] } } ] } }, { "nested": { "path": "attr", "query": { "bool": { "should": [ {"term": {"attr.name": "a1"}}, {"term": {"attr.name": "a2"}}, ] } }, } }, { "bool": { "should": [ { "nested": { "path": "attr", "query": { "bool": { "filter": [ {"term": {"attr.name": "a1"}}, { "bool": { "should": [ { "match": { "attr.value": "hoge" } }, { "regexp": { "attr.value": ".*[hH][oO][gG][eE].*" } }, ] } }, ] } }, } }, { "bool": { "filter": [ { "nested": { "path": "attr", "query": { "bool": { "filter": [ { "term": { "attr.name": "a1" } }, { "bool": { "should": [ { "match": { "attr.value": "fu" } }, { "regexp": { "attr.value": ".*[fF][uU].*" } }, ] } }, ] } }, } }, { "nested": { "path": "attr", "query": { "bool": { "filter": [ { "term": { "attr.name": "a1" } }, { "bool": { "should": [ { "match": { "attr.value": "ga" } }, { "regexp": { "attr.value": ".*[gG][aA].*" } }, ] } }, ] } }, } }, ] } }, ] } }, ], "should": [], } } }, ) def test_make_query_for_simple(self): query = elasticsearch.make_query_for_simple("hoge|fuga&1", None, [], 0) self.assertEqual( query, { "query": { "bool": { "must": [ { "bool": { "should": [ { "bool": { "should": [ { "bool": { "must": [ { "regexp": { "name": ".*[hH][oO][gG][eE].*" } } ] } }, { "bool": { "must": [ { "regexp": { "name": ".*[fF][uU][gG][aA].*" } }, {"regexp": {"name": ".*1.*"}}, ] } }, ] } }, { "bool": { "filter": { "nested": { "path": "attr", "query": { "bool": { "should": [ { "bool": { "filter": [ { "regexp": { "attr.value": ".*[hH][oO][gG][eE].*" } } ] } }, { "bool": { "filter": [ { "regexp": { "attr.value": ".*[fF][uU][gG][aA].*" } }, { "regexp": { "attr.value": ".*1.*" } }, ] } }, ] } }, "inner_hits": {"_source": ["attr.name"]}, } } } }, ] } } ] } }, "_source": ["name", "entity"], "sort": [{"_score": {"order": "desc"}, "name.keyword": {"order": "asc"}}], "from": 0, }, ) # set hint_entity_name query = elasticsearch.make_query_for_simple("hoge", "fuga", [], 0) self.assertEqual( query["query"]["bool"]["must"][1], {"nested": {"path": "entity", "query": {"term": {"entity.name": "fuga"}}}}, ) # set exclude_entity_names query = elasticsearch.make_query_for_simple("hoge", None, ["fuga"], 0) self.assertEqual( query["query"]["bool"]["must_not"][0], {"nested": {"path": "entity", "query": {"term": {"entity.name": "fuga"}}}}, ) # set offset query = elasticsearch.make_query_for_simple("hoge", "fuga", [], 100) self.assertEqual(query["from"], 100) def test_make_search_results(self): entry = Entry.objects.create( name="test_entry", schema=self._entity, created_user=self._user ) attr = Attribute.objects.create( name="test_attr", schema=self._entity_attr, created_user=self._user, parent_entry=entry, ) attr_value = AttributeValue.objects.create( value="test_attr_value", created_user=self._user, parent_attr=attr ) attr.values.add(attr_value) attr.save() res = { "hits": { "total": 1, "hits": [ { "_type": "entry", "_id": entry.id, "_source": { "entity": {"id": entry.id, "name": entry.name}, "name": entry.name, "attr": [ { "name": attr.name, "type": attr.schema.type, "key": "", "value": attr_value.value, "referral_id": "", "is_readble": True, } ], "is_readble": True, }, "sort": [entry.name], } ], } } hint_attrs = [{"name": "test_attr", "keyword": "", "is_readble": True}] results = elasticsearch.make_search_results(self._user, res, hint_attrs, 100, False) self.assertEqual(results["ret_count"], 1) self.assertEqual( results["ret_values"], [ { "entity": { "id": self._entity.id, "name": self._entity.name, }, "entry": {"id": entry.id, "name": entry.name}, "attrs": { attr.name: { "type": attr.schema.type, "value": attr_value.value, "is_readble": True, } }, "is_readble": True, } ], ) def test_make_search_results_for_simple(self): entry = Entry.objects.create( name="test_entry", schema=self._entity, created_user=self._user ) attr = Attribute.objects.create( name="test", schema=self._entity_attr, created_user=self._user, parent_entry=entry, ) attr_value = AttributeValue.objects.create( value="test_attr_value", created_user=self._user, parent_attr=attr ) entry.attrs.add(attr) attr.values.add(attr_value) attr.save() res = { "hits": { "total": 1, "hits": [ { "_type": "entry", "_id": entry.id, "_source": { "name": entry.name, "entity": { "id": entry.schema.id, "name": entry.schema.name, }, }, "inner_hits": { "attr": { "hits": { "total": 1, "hits": [ { "_type": "entry", "_id": entry.id, "_source": {"name": attr.name}, } ], } } }, } ], } } results = elasticsearch.make_search_results_for_simple(res) self.assertEqual(results["ret_count"], 1) self.assertEqual( results["ret_values"], [ { "id": entry.id, "name": entry.name, "schema": { "id": entry.schema.id, "name": entry.schema.name, }, "attr": attr.name, } ], )
class ElasticSearchTest(TestCase): def setUp(self): self._user = User(username='******') self._user.save() self._entity = Entity(name='entity', created_user=self._user) self._entity.save() self._entity_attr = EntityAttr(name='test', type=AttrTypeStr, is_mandatory=True, created_user=self._user, parent_entity=self._entity) self._entity_attr.save() def test_get_regex_pattern(self): # without escape character p1 = elasticsearch._get_regex_pattern('keyword') self.assertEqual(p1, '.*[kK][eE][yY][wW][oO][rR][dD].*') # with escape character p2 = elasticsearch._get_regex_pattern('key@@@word') self.assertEqual(p2, '.*[kK][eE][yY]\\@\\@\\@[wW][oO][rR][dD].*') # with anchor operators p2 = elasticsearch._get_regex_pattern('^keyword$') self.assertEqual(p2, '[kK][eE][yY][wW][oO][rR][dD]') def test_make_query(self): query = elasticsearch.make_query( hint_entity=self._entity, hint_attrs=[{ 'name': 'a1', 'keyword': 'hoge|fu&ga' }, { 'name': 'a2', 'keyword': '' }], entry_name='entry1', ) self.assertEqual( query, { 'query': { 'bool': { 'filter': [{ 'nested': { 'path': 'entity', 'query': { 'term': { 'entity.id': self._entity.id } } } }, { 'bool': { 'should': [{ 'bool': { 'must': [{ 'regexp': { 'name': '.*[eE][nN][tT][rR][yY]1.*' } }] } }] } }, { 'nested': { 'path': 'attr', 'query': { 'bool': { 'should': [{ 'term': { 'attr.name': 'a1' } }, { 'term': { 'attr.name': 'a2' } }] } } } }, { 'bool': { 'should': [{ 'nested': { 'path': 'attr', 'query': { 'bool': { 'filter': [{ 'term': { 'attr.name': 'a1' } }, { 'bool': { 'should': [{ 'match': { 'attr.value': 'hoge' } }, { 'regexp': { 'attr.value': '.*[hH][oO][gG][eE].*' } }] } }] } } } }, { 'bool': { 'filter': [{ 'nested': { 'path': 'attr', 'query': { 'bool': { 'filter': [{ 'term': { 'attr.name': 'a1' } }, { 'bool': { 'should': [{ 'match': { 'attr.value': 'fu' } }, { 'regexp': { 'attr.value': '.*[fF][uU].*' } }] } }] } } } }, { 'nested': { 'path': 'attr', 'query': { 'bool': { 'filter': [{ 'term': { 'attr.name': 'a1' } }, { 'bool': { 'should': [{ 'match': { 'attr.value': 'ga' } }, { 'regexp': { 'attr.value': '.*[gG][aA].*' } }] } }] } } } }] } }] } }], 'should': [] } } }) def test_make_query_for_simple(self): query = elasticsearch.make_query_for_simple('hoge|fuga&1', None, 0) self.assertEqual( query, { 'query': { 'bool': { 'must': [{ 'bool': { 'should': [{ 'bool': { 'should': [{ 'bool': { 'must': [{ 'regexp': { 'name': '.*[hH][oO][gG][eE].*' } }] } }, { 'bool': { 'must': [{ 'regexp': { 'name': '.*[fF][uU][gG][aA].*' } }, { 'regexp': { 'name': '.*1.*' } }] } }] } }, { 'bool': { 'filter': { 'nested': { 'path': 'attr', 'query': { 'bool': { 'should': [{ 'bool': { 'filter': [{ 'regexp': { 'attr.value': '.*[hH][oO][gG][eE].*' } }] } }, { 'bool': { 'filter': [{ 'regexp': { 'attr.value': '.*[fF][uU][gG][aA].*' } }, { 'regexp': { 'attr.value': '.*1.*' } }] } }] } }, 'inner_hits': { '_source': ['attr.name'] } } } } }] } }] } }, '_source': ['name'], 'sort': [{ '_score': { 'order': 'desc' }, 'name.keyword': { 'order': 'asc' } }], 'from': 0, }) # set hint_entity_name query = elasticsearch.make_query_for_simple('hoge', 'fuga', 0) self.assertEqual( query['query']['bool']['must'][1], { 'nested': { 'path': 'entity', 'query': { 'term': { 'entity.name': 'fuga' } } } }) # set offset query = elasticsearch.make_query_for_simple('hoge', 'fuga', 100) self.assertEqual(query['from'], 100) def test_make_search_results(self): entry = Entry.objects.create(name='test_entry', schema=self._entity, created_user=self._user) attr = Attribute.objects.create(name='test_attr', schema=self._entity_attr, created_user=self._user, parent_entry=entry) attr_value = AttributeValue.objects.create(value='test_attr_value', created_user=self._user, parent_attr=attr) attr.values.add(attr_value) attr.save() res = { 'hits': { 'total': 1, 'hits': [{ '_type': 'entry', '_id': entry.id, '_source': { 'entity': { 'id': entry.id, 'name': entry.name }, 'name': entry.name, 'attr': [{ 'name': attr.name, 'type': attr.schema.type, 'key': '', 'value': attr_value.value, 'referral_id': '', 'is_readble': True }], 'is_readble': True }, 'sort': [entry.name] }] } } hint_attrs = [{'name': 'test_attr', 'keyword': '', 'is_readble': True}] results = elasticsearch.make_search_results(self._user, res, hint_attrs, 100, False) self.assertEqual(results['ret_count'], 1) self.assertEqual(results['ret_values'], [{ 'entity': { 'id': self._entity.id, 'name': self._entity.name, }, 'entry': { 'id': entry.id, 'name': entry.name }, 'attrs': { attr.name: { 'type': attr.schema.type, 'value': attr_value.value, 'is_readble': True } }, 'is_readble': True }]) def test_make_search_results_for_simple(self): entry = Entry.objects.create(name='test_entry', schema=self._entity, created_user=self._user) attr = Attribute.objects.create(name='test', schema=self._entity_attr, created_user=self._user, parent_entry=entry) attr_value = AttributeValue.objects.create(value='test_attr_value', created_user=self._user, parent_attr=attr) entry.attrs.add(attr) attr.values.add(attr_value) attr.save() res = { 'hits': { 'total': 1, 'hits': [{ '_type': 'entry', '_id': entry.id, '_source': { 'name': entry.name }, 'inner_hits': { 'attr': { 'hits': { 'total': 1, 'hits': [{ '_type': 'entry', '_id': entry.id, '_source': { 'name': attr.name }, }] } } } }] } } results = elasticsearch.make_search_results_for_simple(res) self.assertEqual(results['ret_count'], 1) self.assertEqual(results['ret_values'], [{ 'id': entry.id, 'name': entry.name, 'attr': attr.name, }])