def get_query_for_ids(modelquery, model, ids): """ Return a query object filtered by primary key values passed in `ids` argument. Unfortunately, it is not possible to use `in_` filter if model has more than one primary key. """ if has_multiple_pks(model): # Decode keys to tuples decoded_ids = [iterdecode(v) for v in ids] # Get model primary key property references model_pk = [getattr(model, name) for name in get_primary_key(model)] try: query = modelquery.filter(tuple_(*model_pk).in_(decoded_ids)) # Only the execution of the query will tell us, if the tuple_ # operator really works query.all() except DBAPIError: query = modelquery.filter(tuple_operator_in(model_pk, decoded_ids)) else: model_pk = getattr(model, get_primary_key(model)) query = modelquery.filter(model_pk.in_(ids)) return query
def test_encode_decode(): eq_(tools.iterdecode(tools.iterencode([1, 2, 3])), (u'1', u'2', u'3')) eq_(tools.iterdecode(tools.iterencode([',', ',', ','])), (u',', u',', u',')) eq_(tools.iterdecode(tools.iterencode(['.hello.,', ',', ','])), (u'.hello.,', u',', u',')) eq_(tools.iterdecode(tools.iterencode(['.....,,,.,,..,.,,.,'])), (u'.....,,,.,,..,.,,.,',)) eq_(tools.iterdecode(tools.iterencode([])), tuple()) # Malformed inputs should not crash ok_(tools.iterdecode('.')) eq_(tools.iterdecode(','), (u'', u''))
def test_encode_decode(): assert tools.iterdecode(tools.iterencode([1, 2, 3])) == (u'1', u'2', u'3') assert tools.iterdecode(tools.iterencode([',', ',', ','])) == (u',', u',', u',') assert tools.iterdecode(tools.iterencode(['.hello.,', ',', ',' ])) == (u'.hello.,', u',', u',') assert tools.iterdecode(tools.iterencode(['.....,,,.,,..,.,,.,' ])) == (u'.....,,,.,,..,.,,.,', ) assert tools.iterdecode(tools.iterencode([])) == tuple() # Malformed inputs should not crash assert tools.iterdecode('.') assert tools.iterdecode(',') == (u'', u'')
def test_encode_decode(): eq_(tools.iterdecode(tools.iterencode([1, 2, 3])), (u'1', u'2', u'3')) eq_(tools.iterdecode(tools.iterencode([',', ',', ','])), (u',', u',', u',')) eq_(tools.iterdecode(tools.iterencode(['.hello.,', ',', ','])), (u'.hello.,', u',', u',')) eq_(tools.iterdecode(tools.iterencode(['.....,,,.,,..,.,,.,'])), (u'.....,,,.,,..,.,,.,', )) eq_(tools.iterdecode(tools.iterencode([])), tuple()) # Malformed inputs should not crash ok_(tools.iterdecode('.')) eq_(tools.iterdecode(','), (u'', u''))