/// <summary> /// Provides a basic sanity check that a change item is well formed. /// </summary> /// <param name="item">The item.</param> /// <param name="context">The context.</param> /// <returns> /// <see cref="P:System.ComponentModel.DataAnnotations.ValidationResult.Success"/> /// if the change is valid; otherwise, a ValidationResult /// denoting the reason the change failed validation. /// </returns> public static ValidationResult SanityCheck(ChangeItem item, ValidationContext context) { switch (item.Action) { case ChangeAction.Create: if (item.GetBFIM() != null) return new ValidationResult("BFIM not permitted for new entries.", new[] { "BFIM" }); if (item.GetAFIM() == null) return new ValidationResult("AFIM required for new entries.", new[] { "AFIM" }); break; case ChangeAction.Update: if (item.GetBFIM() == null) return new ValidationResult("BFIM required for update entries.", new[] { "BFIM" }); if (item.GetAFIM() == null) return new ValidationResult("AFIM required for update entries.", new[] { "AFIM" }); break; case ChangeAction.Delete: if (item.GetBFIM() == null) return new ValidationResult("BFIM required for new entries.", new[] { "BFIM" }); if (item.GetAFIM() != null) return new ValidationResult("AFIM not permitted for new entries.", new[] { "AFIM" }); break; default: return new ValidationResult("Invalid change action", new[] { "Action" }); } return ValidationResult.Success; }
public void Invalid_update_without_afim_fails_validation() { var ctx = A.Dummy<ValidationContext>(); var item = new ChangeItem<object>(ChangeAction.Update, A.Dummy<object>(), null); var result = ChangeValidator.SanityCheck(item, ctx); Assert.AreNotEqual(ValidationResult.Success, result); Assert.IsTrue(result.MemberNames.Contains("AFIM")); }
public void Invalid_action_fails_validation() { var ctx = A.Dummy<ValidationContext>(); var item = new ChangeItem<object>((ChangeAction)(42), A.Dummy<object>(), A.Dummy<object>()); var result = ChangeValidator.SanityCheck(item, ctx); Assert.AreNotEqual(ValidationResult.Success, result); Assert.IsTrue(result.MemberNames.Contains("Action")); }
public void Valid_create_passes_validation() { var ctx = A.Dummy<ValidationContext>(); var item = new ChangeItem<object>(ChangeAction.Create, null, A.Dummy<object>()); var result = ChangeValidator.SanityCheck(item, ctx); Assert.AreEqual(ValidationResult.Success, result); }
private ChangeItem<Asset> ModifyAsset(Asset asset) { var choice = s_rng.NextDouble(); ChangeItem<Asset> change; if (s_rng.NextDouble() < s_chanceDelete) { change = new ChangeItem<Asset>(ChangeAction.Delete, asset, null); } else { change = new ChangeItem<Asset>(ChangeAction.Update, asset, Dirty(asset)); } return change; }