def test_init_default(self): """ Testing GetQuery instanciation arguments default value """ tclass_list = [ dyncode.Object, dyncode.Entry, dyncode.Person, dyncode.Text, dyncode.Section, dyncode.Publication, dyncode.Text_Person ] for tclass in tclass_list: get_q = LeGetQuery(tclass, []) qinfos = get_q.dump_infos() self.assertEqual( set(qinfos['field_list']), set(tclass.fieldnames(True))) self.assertEqual( qinfos['limit'], None) self.assertEqual( qinfos['offset'], 0) self.assertEqual( qinfos['group'], None) self.assertEqual( qinfos['order'], None) self.assertEqual( qinfos['query_filter'], ([],[])) self.assertEqual( qinfos['target_class'], tclass)
def test_implicit_uid(self): """ Testing query with an UID as implicit referenced field for a relationnal filter """ getq = LeGetQuery(dyncode.Indextheme, "texts in 13,1337,357,80") qinfos = getq.dump_infos() #No subqueries expected self.assertEqual(qinfos['subqueries'], []) self.assertEqual(qinfos['query_filter'], ([('texts', 'in', '13,1337,357,80')], []))
def test_same_datasource(self): """ Testing a query with relationnal filters concerning only one datasource to check for 'false positive' in subqueries creation """ getq = LeGetQuery(dyncode.Text, "linked_persons.fullname = John Doe") qinfos = getq.dump_infos() self.assertEqual(qinfos['subqueries'], []) self.assertEqual(qinfos['query_filter'], ([], [(('linked_persons', { dyncode.Person: 'fullname' }), '=', 'John Doe')]))
def test_uid_as_ref_field(self): """ Testing basic query optimisation with a relationnal filter with lodel_id as referenced field""" getq = LeGetQuery(dyncode.Indextheme, "texts.lodel_id in 1,2,3,42") qinfos = getq.dump_infos() #No subqueries should be created because referenced field is #the referenced class UID self.assertEqual(qinfos['subqueries'], []) self.assertEqual(qinfos['query_filter'], ([('texts', 'in', '1,2,3,42')], []))
def test_field_list_duplicated(self): """ Testing GetQuery field list argument deduplication """ tclass_list = [ dyncode.Object, dyncode.Text, dyncode.Section, dyncode.Publication, dyncode.Text_Person ] for tclass in tclass_list: fl = [ 'lodel_id', 'lodel_id', 'help_text', 'help_text', 'help_text'] get_q = LeGetQuery(tclass, [], field_list = fl) self.assertEqual( sorted(list(set(fl))), sorted(get_q.dump_infos()['field_list']))
def test_rel_filters(self): """ Testing relational filters recognition """ test_datas = [ (dyncode.Subsection, 'parent.title = 42', ([], [(('parent', { dyncode.Section: 'title' }), '=', '42')])), (dyncode.Section, 'childs.title = 42', ([], [(('childs', { dyncode.Subsection: 'title' }), '=', '42')])) ] for le_class, q_filter_arg, e_qfilter in test_datas: get_q = LeGetQuery(le_class, q_filter_arg) qinfos = get_q.dump_infos() self.assertEqual(qinfos['query_filter'], e_qfilter)
def test_basic(self): """ Testing a LeGetQuery with a relationnal field implying another datasource """ getq = LeGetQuery(dyncode.Indextheme, "texts.title = super titre !") qinfos = getq.dump_infos() # The single query filter should be in subquery self.assertEqual(qinfos['query_filter'], ([], [])) self.assertEqual(len(qinfos['subqueries']), 1) rfield, subq = qinfos['subqueries'][0] # Checking subquery self.assertEqual(rfield, 'texts') # The reference field of the subquery qinfos = subq.dump_infos() self.assertEqual(qinfos['target_class'], dyncode.Text) self.assertEqual(qinfos['query_filter'], ([('title', '=', 'super titre !')], [])) self.assertEqual(qinfos['field_list'], ['title'])
def test_field_list(self): """ Testing GetQuery field list argument processing """ tclass_list = [ dyncode.Object, dyncode.Entry, dyncode.Person, dyncode.Text, dyncode.Section, dyncode.Publication, dyncode.Text_Person ] for tclass in tclass_list: # testing all field list possible combinations field_list = tclass.fieldnames(True) for r in range(1, len(field_list) + 1): combinations = [ list(c) for c in itertools.combinations(field_list, r)] for test_flist in combinations: expected = set(test_flist) get_q = LeGetQuery(tclass, [], field_list = test_flist) qinfos = get_q.dump_infos() self.assertEqual( sorted(qinfos['field_list']), sorted(test_flist))
def test_field_list_invalid(self): """ Testing GetQuery invalid field name detection in field list """ bad_field_lists = ( ('non-existing',), (1,), (True,), (None,), ('lodel_id', 'non-existing',), ('lodel_id', 1,), ('lodel_id', True,), ('lodel_id', None,) ) for bad_field_list in bad_field_lists: with self.assertRaises(LeApiQueryError): LeGetQuery(dyncode.Object, [], field_list = bad_field_list)