def test_edit_revert__edit_deletion(parent_model_name, model_name, create_user,
                                    create_minimal_obj, ahj_obj):
    user = create_user()
    parent_obj = create_minimal_obj(parent_model_name)
    obj = create_minimal_obj(model_name)
    relation = obj.create_relation_to(parent_obj)
    set_obj_field(relation, relation.get_relation_status_field(), False)
    edit_dict = {
        'ChangedBy': user,
        'ApprovedBy': user,
        'SourceTable': relation.__class__.__name__,
        'SourceRow': relation.pk,
        'SourceColumn': relation.get_relation_status_field(),
        'OldValue': True,
        'NewValue': False,
        'DateRequested': timezone.now(),
        'DateEffective': timezone.now(),
        'ReviewStatus': 'A',
        'EditType': 'D',
        'AHJPK': ahj_obj
    }
    edit = Edit.objects.create(**edit_dict)
    views_edits.revert_edit(user, edit)
    edit_dict['OldValue'], edit_dict['NewValue'] = edit_dict[
        'NewValue'], edit_dict['OldValue']
    assert check_edit_exists(edit_dict) is True
    assert get_obj_field(
        relation,
        relation.get_relation_status_field()) is edit_dict['NewValue']
def test_edit_revert__current_value_is_old_value_do_nothing(
        create_user, ahj_obj):
    user = create_user()
    old_value = 'oldname'
    new_value = 'newname'
    set_obj_field(ahj_obj, 'AHJName', old_value)
    edit_dict = {
        'ChangedBy': user,
        'ApprovedBy': user,
        'SourceTable': 'AHJ',
        'SourceRow': ahj_obj.pk,
        'SourceColumn': 'AHJName',
        'OldValue': old_value,
        'NewValue': new_value,
        'DateRequested': timezone.now(),
        'DateEffective': timezone.now(),
        'ReviewStatus': 'A',
        'EditType': 'U',
        'AHJPK': ahj_obj
    }
    edit = Edit.objects.create(**edit_dict)
    views_edits.revert_edit(user, edit)
    edit_dict['OldValue'], edit_dict['NewValue'] = old_value, edit_dict[
        'OldValue']
    assert not check_edit_exists(edit_dict)
    assert Edit.objects.all().count() == 1
def test_edit_revert__revert_edit_old_value_uses_current_row_value(
        create_user, ahj_obj):
    user = create_user()
    old_value = 'oldname'
    middle_value = 'newername'
    new_value = 'newestname'
    edit_dict = {
        'ChangedBy': user,
        'ApprovedBy': user,
        'SourceTable': 'AHJ',
        'SourceRow': ahj_obj.pk,
        'SourceColumn': 'AHJName',
        'OldValue': old_value,
        'NewValue': middle_value,
        'DateRequested': timezone.now(),
        'DateEffective': timezone.now(),
        'ReviewStatus': 'A',
        'EditType': 'U',
        'AHJPK': ahj_obj
    }
    edit = Edit.objects.create(**edit_dict)
    edit_dict['OldValue'], edit_dict['NewValue'] = edit_dict[
        'NewValue'], new_value
    setattr(ahj_obj, 'AHJName', new_value)
    ahj_obj.save()
    newer_edit = Edit.objects.create(**edit_dict)
    views_edits.revert_edit(user, edit)
    edit_dict['OldValue'], edit_dict['NewValue'] = edit_dict[
        'NewValue'], old_value
    reverting_edit = filter_to_edit(edit_dict)
    assert reverting_edit.exists() is True
    assert reverting_edit.first().OldValue == new_value
    assert get_obj_field(ahj_obj, 'AHJName')
def test_edit_revert__edit_update(model_name, field_name, old_value, new_value,
                                  create_user, ahj_obj, expected_value,
                                  create_minimal_obj, add_enums):
    user = create_user()
    obj = create_minimal_obj(model_name)
    set_obj_field(obj, field_name, new_value)
    edit_dict = {
        'ChangedBy': user,
        'ApprovedBy': user,
        'SourceTable': model_name,
        'SourceRow': obj.pk,
        'SourceColumn': field_name,
        'OldValue': old_value,
        'NewValue': new_value,
        'DateRequested': timezone.now(),
        'DateEffective': timezone.now(),
        'ReviewStatus': 'A',
        'EditType': 'U',
        'AHJPK': ahj_obj
    }
    edit = Edit.objects.create(**edit_dict)
    views_edits.revert_edit(user, edit)
    edit_dict['OldValue'], edit_dict['NewValue'] = edit.NewValue, edit.OldValue
    if expected_value:
        expected_value = get_value_or_enum_row(field_name, old_value)
    assert get_obj_field(obj, field_name) == expected_value
    assert check_edit_exists(edit_dict) is True