def OrganizationsResource(OrganizationResource): @CollectionResource.define(item_class=OrganizationResource, primary_index=SimpleIndex(('id', int), )) class OrganizationsRes(CollectionResource): def __init__(self, parent, name): CollectionResource.__init__(self, parent, name, 'memory') return OrganizationsRes
def test_simple_index_custom_delimiter(User): index = SimpleIndex(('organization_id', int), ('id', int), '/') user = User(organization_id=0, id=1, name='test') expected_key = '0/1' expected_dict = {'organization_id': 0, 'id': 1} assert index.make_key_from_dict(user._asdict()) == expected_key assert index.make_key_from_values(0, 1) == expected_key assert index.make_key_from_data(user) == expected_key assert index.make_key_dict(expected_key) == expected_dict assert index.make_key_dict_from_values(0, 1) == expected_dict assert index.make_key_dict_from_dict(user._asdict()) == expected_dict assert index.make_key_dict_from_data(user) == expected_dict
def UsersResource(UserResource): @CollectionResource.define( item_class=UserResource, primary_index=SimpleIndex(('organization_id', int), ('id', int)), ) class UsersRes(CollectionResource): def __init__(self, parent, name): CollectionResource.__init__(self, parent, name, 'memory') return UsersRes
def test_simple_index(User): index = SimpleIndex(('organization_id', int), ('id', int)) user = User(organization_id=0, id=1, name='test') expected_key = '0_1' expected_dict = {'organization_id': 0, 'id': 1} assert index.make_key_from_dict(user._asdict()) == expected_key assert index.make_key_from_values(0, 1) == expected_key assert index.make_key_from_data(user) == expected_key assert index.make_key_dict(expected_key) == expected_dict assert index.make_key_dict_from_values(0, 1) == expected_dict assert index.make_key_dict_from_dict(user._asdict()) == expected_dict assert index.make_key_dict_from_data(user) == expected_dict assert index.get_first_value_from_dict(user._asdict()) == 0 assert index.get_first_value_from_data(user) == 0 assert index.get_second_value_from_dict(user._asdict()) == 1 assert index.get_second_value_from_data(user) == 1