xtuml/consistency_check.py
def check_uniqueness_constraint(m, kind=None):
    '''
    Check the model for uniqueness constraint violations.
    '''
    if kind is None:
        metaclasses = m.metaclasses.values()
    else:
        metaclasses = [m.find_metaclass(kind)]
    
    res = 0
    for metaclass in metaclasses:
        id_map = dict()
        for identifier in metaclass.indices:
            id_map[identifier] = dict()
                
        for inst in metaclass.select_many():
            # Check for null-values
            for name, ty in metaclass.attributes:
                if name not in metaclass.identifying_attributes:
                    continue
                
                value = getattr(inst, name)
                isnull = value is None
                isnull |= (ty == 'UNIQUE_ID' and not value)
                if isnull:
                    res += 1 
                    logger.warning('%s.%s is part of an identifier and is null' 
                                   % (metaclass.kind, name))

            # Check uniqueness
            for identifier in metaclass.indices:
                kwargs = dict()
                for name in metaclass.indices[identifier]:
                    kwargs[name] = getattr(inst, name)

                index_key = frozenset(kwargs.items())
                if index_key in id_map[identifier]:
                    res += 1
                    id_string = pretty_unique_identifier(inst, identifier)
                    logger.warning('uniqueness constraint violation in %s, %s' 
                                   % (metaclass.kind, id_string))

                id_map[identifier][index_key] = inst

    return res
def check_uniqueness_constraint(m, kind=None):
    '''
    Check the model for uniqueness constraint violations.
    '''
    if kind is None:
        metaclasses = m.metaclasses.values()
    else:
        metaclasses = [m.find_metaclass(kind)]
    
    res = 0
    for metaclass in metaclasses:
        id_map = dict()
        for identifier in metaclass.indices:
            id_map[identifier] = dict()
                
        for inst in metaclass.select_many():
            # Check for null-values
            for name, ty in metaclass.attributes:
                if name not in metaclass.identifying_attributes:
                    continue
                
                value = getattr(inst, name)
                isnull = value is None
                isnull |= (ty == 'UNIQUE_ID' and not value)
                if isnull:
                    res += 1 
                    logger.warning('%s.%s is part of an identifier and is null' 
                                   % (metaclass.kind, name))

            # Check uniqueness
            for identifier in metaclass.indices:
                kwargs = dict()
                for name in metaclass.indices[identifier]:
                    kwargs[name] = getattr(inst, name)

                index_key = frozenset(kwargs.items())
                if index_key in id_map[identifier]:
                    res += 1
                    id_string = pretty_unique_identifier(inst, identifier)
                    logger.warning('uniqueness constraint violation in %s, %s' 
                                   % (metaclass.kind, id_string))

                id_map[identifier][index_key] = inst

    return res
Check the model for uniqueness constraint violations.