|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="BusinessRuleTests.cs" company="Marimer LLC"> |
| 3 | +// Copyright (c) Marimer LLC. All rights reserved. |
| 4 | +// Website: https://cslanet.com |
| 5 | +// </copyright> |
| 6 | +// <summary>no summary</summary> |
| 7 | +//----------------------------------------------------------------------- |
| 8 | +using System.ComponentModel.DataAnnotations; |
| 9 | +using Csla.Configuration; |
| 10 | +using Csla.TestHelpers; |
| 11 | +using FluentAssertions; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 14 | + |
| 15 | +namespace Csla.Test.BizRules |
| 16 | +{ |
| 17 | + [TestClass] |
| 18 | + public class BusinessRuleTests |
| 19 | + { |
| 20 | + private static TestDIContext _testDIContext; |
| 21 | + |
| 22 | + [ClassInitialize] |
| 23 | + public static void ClassInitialize(TestContext context) |
| 24 | + { |
| 25 | + _testDIContext = TestDIContextFactory.CreateDefaultContext(); |
| 26 | + } |
| 27 | + |
| 28 | + [TestInitialize] |
| 29 | + public void Initialize() |
| 30 | + { |
| 31 | + TestResults.Reinitialise(); |
| 32 | + } |
| 33 | + |
| 34 | + [TestMethod] |
| 35 | + public void DefaultDataAnnotationsScan() |
| 36 | + { |
| 37 | + var portal = _testDIContext.ServiceProvider.GetRequiredService<IDataPortal<TestBusinessRule>>(); |
| 38 | + var obj = portal.Create(); |
| 39 | + obj.Name = ""; |
| 40 | + obj.IsValid.Should().BeFalse(); |
| 41 | + } |
| 42 | + |
| 43 | + [TestMethod] |
| 44 | + [TestCategory("SkipOnCIServer")] |
| 45 | + public void DisableDataAnnotationsScan() |
| 46 | + { |
| 47 | + var options = _testDIContext.ServiceProvider.GetRequiredService<CslaOptions>(); |
| 48 | + options.ScanForDataAnnotations(false); |
| 49 | + |
| 50 | + // use different type to avoid caching |
| 51 | + var portal = _testDIContext.ServiceProvider.GetRequiredService<IDataPortal<TestBusinessRule2>>(); |
| 52 | + var obj = portal.Create(); |
| 53 | + obj.Name = ""; |
| 54 | + obj.IsValid.Should().BeTrue(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public class TestBusinessRule : BusinessBase<TestBusinessRule> |
| 59 | + { |
| 60 | + public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name)); |
| 61 | + [Required] |
| 62 | + public string Name |
| 63 | + { |
| 64 | + get => GetProperty(NameProperty); |
| 65 | + set => SetProperty(NameProperty, value); |
| 66 | + } |
| 67 | + |
| 68 | + [Create] |
| 69 | + private void Create() |
| 70 | + { |
| 71 | + BusinessRules.CheckRules(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public class TestBusinessRule2 : BusinessBase<TestBusinessRule2> |
| 76 | + { |
| 77 | + public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name)); |
| 78 | + [Required] |
| 79 | + public string Name |
| 80 | + { |
| 81 | + get => GetProperty(NameProperty); |
| 82 | + set => SetProperty(NameProperty, value); |
| 83 | + } |
| 84 | + |
| 85 | + [Create] |
| 86 | + private void Create() |
| 87 | + { |
| 88 | + BusinessRules.CheckRules(); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments