Beispiel #1
0
def compare_obj(test,
                obj,
                db_obj,
                subs=None,
                allow_missing=None,
                comparators=None):
    """Compare a CinderObject and a dict-like database object.

    This automatically converts TZ-aware datetimes and iterates over
    the fields of the object.

    :param:test: The TestCase doing the comparison
    :param:obj: The CinderObject to examine
    :param:db_obj: The dict-like database object to use as reference
    :param:subs: A dict of objkey=dbkey field substitutions
    :param:allow_missing: A list of fields that may not be in db_obj
    :param:comparators: Map of comparator functions to use for certain fields
    """

    if subs is None:
        subs = {}
    if allow_missing is None:
        allow_missing = []
    if comparators is None:
        comparators = {}

    for key in obj.fields:
        if key in allow_missing and not obj.obj_attr_is_set(key):
            continue
        obj_val = getattr(obj, key)
        db_key = subs.get(key, key)
        db_val = db_obj[db_key]
        if isinstance(obj_val, datetime.datetime):
            obj_val = obj_val.replace(tzinfo=None)

        if key in comparators:
            comparator = comparators[key]
            comparator(db_val, obj_val)
        else:
            test.assertEqual(db_val, obj_val)
Beispiel #2
0
def compare_obj(test, obj, db_obj, subs=None, allow_missing=None,
                comparators=None):
    """Compare a CinderObject and a dict-like database object.

    This automatically converts TZ-aware datetimes and iterates over
    the fields of the object.

    :param:test: The TestCase doing the comparison
    :param:obj: The CinderObject to examine
    :param:db_obj: The dict-like database object to use as reference
    :param:subs: A dict of objkey=dbkey field substitutions
    :param:allow_missing: A list of fields that may not be in db_obj
    :param:comparators: Map of comparator functions to use for certain fields
    """

    if subs is None:
        subs = {}
    if allow_missing is None:
        allow_missing = []
    if comparators is None:
        comparators = {}

    for key in obj.fields:
        if key in allow_missing and not obj.obj_attr_is_set(key):
            continue
        obj_val = getattr(obj, key)
        db_key = subs.get(key, key)
        db_val = db_obj[db_key]
        if isinstance(obj_val, datetime.datetime):
            obj_val = obj_val.replace(tzinfo=None)

        if key in comparators:
            comparator = comparators[key]
            comparator(db_val, obj_val)
        else:
            test.assertEqual(db_val, obj_val)
Beispiel #3
0
    def _compare(test, db, obj):
        for field, value in db.items():
            if not hasattr(obj, field):
                continue

            if field in ('modified_at', 'created_at', 'updated_at',
                         'deleted_at') and db[field]:
                test.assertEqual(db[field],
                                 timeutils.normalize_time(obj[field]))
            elif isinstance(obj[field], obj_base.ObjectListBase):
                test.assertEqual(db[field], obj[field].objects)
            else:
                test.assertEqual(db[field], obj[field])
    def _compare(test, db, obj):
        for field, value in db.items():
            if not hasattr(obj, field):
                continue

            if field in ('modified_at', 'created_at',
                         'updated_at', 'deleted_at') and db[field]:
                test.assertEqual(db[field],
                                 timeutils.normalize_time(obj[field]))
            elif isinstance(obj[field], obj_base.ObjectListBase):
                test.assertEqual(db[field], obj[field].objects)
            else:
                test.assertEqual(db[field], obj[field])
Beispiel #5
0
    def _compare(test, db, obj):
        for field, value in db.items():
            try:
                getattr(obj, field)
            except (AttributeError, exception.CinderException,
                    NotImplementedError):
                # NotImplementedError: ignore "Cannot load 'projects' in the
                # base class" error
                continue

            if field in ('modified_at', 'created_at',
                         'updated_at', 'deleted_at') and db[field]:
                test.assertEqual(db[field],
                                 timeutils.normalize_time(obj[field]))
            elif isinstance(obj[field], obj_base.ObjectListBase):
                test.assertEqual(db[field], obj[field].objects)
            else:
                test.assertEqual(db[field], obj[field])