#
#    OSIS stands for Open Student Information System. It's an application
#    designed to manage the core business of higher education institutions,
#    such as universities, faculties, institutes and professional schools.
#    The core business involves the administration of students, teachers,
#    courses, programs and so on.
#
#    Copyright (C) 2015-2021 Université catholique de Louvain (http://www.uclouvain.be)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU General Public License for more details.
#
#    A copy of this license - GNU General Public License - is available
#    at the root of the source code of this program.  If not,
#    see http://www.gnu.org/licenses/.
#
##############################################################################
import attr


def assert_attrs_instances_are_equal(inst1, inst2, exclude=None):
    """
    Assert that two instances of a same class have same values for their attributes

    :param inst1:  Instance of an attrs-decoracted class
    :param inst2: Instance of an attrs-decoracted class
    """
    exclude = exclude or []
    attr_fields = attr.fields(type(inst1))
    fields_to_exclude = [getattr(attr_fields, field) for field in exclude if getattr(attr_fields, field, None)]
    filter_exclude = attr.filters.exclude(*fields_to_exclude)
    assert attr.astuple(inst1, filter=filter_exclude, retain_collection_types=True) ==\
        attr.astuple(inst2, filter=filter_exclude, retain_collection_types=True)
