CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
DataAnnotations/DataAnnotationsTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DataAnnotationsTests.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//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12using Csla;
14using Csla.Rules;
16using UnitDriven;
17using System.ComponentModel.DataAnnotations;
18using System.Threading.Tasks;
19using Csla.TestHelpers;
20
21#if NUNIT
22using NUnit.Framework;
23using TestClass = NUnit.Framework.TestFixtureAttribute;
24using TestInitialize = NUnit.Framework.SetUpAttribute;
25using TestCleanup = NUnit.Framework.TearDownAttribute;
26using TestMethod = NUnit.Framework.TestAttribute;
27using TestSetup = NUnit.Framework.SetUpAttribute;
28#elif MSTEST
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30#endif
31
33{
34#if TESTING
35 [System.Diagnostics.DebuggerNonUserCode]
36#endif
37 [TestClass]
39 {
40 private static TestDIContext _testDIContext;
41
43 public static void ClassInitialize(TestContext context)
44 {
45 _testDIContext = TestDIContextFactory.CreateDefaultContext();
46 }
47
48 [TestMethod]
49 public async Task SingleAttribute()
50 {
51 var context = GetContext();
52
53 var dp = _testDIContext.CreateDataPortal<Single>();
54 var root = await dp.CreateAsync();
55 var rules = root.GetRules();
56
57 Assert.AreEqual(1, rules.Length, "Should be 1 rule");
58 Assert.IsFalse(root.IsValid, "Obj shouldn't be valid");
59 Assert.AreEqual(1, root.BrokenRulesCollection.Count, "Should be 1 broken rule");
60 Assert.AreEqual("Name value required", root.BrokenRulesCollection[0].Description, "Desc should match");
61 context.Assert.Success();
62
63 context.Complete();
64 }
65
66 [TestMethod]
67 public async Task MultipleAttributes()
68 {
69 var context = GetContext();
70
71 var dp = _testDIContext.CreateDataPortal<Multiple>();
72 var root = await dp.CreateAsync();
73 var rules = root.GetRules();
74
75 Assert.AreEqual(3, rules.Length, "Should be 3 rules");
76 Assert.IsFalse(root.IsValid, "Obj shouldn't be valid");
77 Assert.AreEqual(1, root.BrokenRulesCollection.Count, "Should be 1 broken rule");
78 root.Name = "xyz";
79 Assert.AreEqual(2, root.BrokenRulesCollection.Count, "Should be 2 broken rules after edit");
80 context.Assert.Success();
81
82 context.Complete();
83 }
84
85 [TestMethod]
86 public async Task CustomAttribute()
87 {
88 var context = GetContext();
89
90 var dp = _testDIContext.CreateDataPortal<Custom>();
91 var root = await dp.CreateAsync();
92 var rules = root.GetRules();
93
94 Assert.AreEqual(1, rules.Length, "Should be 1 rule");
95 Assert.IsFalse(root.IsValid, "Obj shouldn't be valid");
96 Assert.AreEqual(1, root.BrokenRulesCollection.Count, "Should be 1 broken rule");
97 Assert.AreEqual("Name must be abc", root.BrokenRulesCollection[0].Description, "Desc should match");
98 context.Assert.Success();
99
100 context.Complete();
101 }
102
103 [TestMethod]
104 public void MultipleMetaRules()
105 {
106 IDataPortal<MultipleMeta> dataPortal = _testDIContext.CreateDataPortal<MultipleMeta>();
107
108 var obj = dataPortal.Fetch();
109
110 var typeRules = obj.GetRegisteredRules();
111 // 4 rules from metadatacalss and 1 rule from autogenerated class.
112 Assert.AreEqual(5, typeRules.Rules.Count);
113 Assert.AreEqual(3, typeRules.Rules.Where(p => p.PrimaryProperty == MultipleMeta.AmountProperty).Count());
114 Assert.AreEqual(2, typeRules.Rules.Where(p => p.PrimaryProperty == MultipleMeta.QuantityProperty).Count());
115 }
116 }
117
118
120 public class Single : BusinessBase<Single>
121 {
122 private static PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
123 [Required(ErrorMessage = "Name value required")]
124 public string Name
125 {
126 get { return GetProperty(NameProperty); }
127 set { SetProperty(NameProperty, value); }
128 }
129
130 [Create]
131 private async Task Create()
132 {
134 }
135
136 public string[] GetRules()
137 {
139 }
140 }
141
143 public class Multiple : BusinessBase<Multiple>
144 {
145 private static PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
146 [Required(ErrorMessage = "Name value required")]
147 [RegularExpression("[0-9]")]
148 [System.ComponentModel.DataAnnotations.Range(typeof(string), "0", "9")]
149 public string Name
150 {
151 get { return GetProperty(NameProperty); }
152 set { SetProperty(NameProperty, value); }
153 }
154
155 [Create]
156 private async Task Create()
157 {
159 }
160
161 public string[] GetRules()
162 {
164 }
165 }
166
168 public class Custom : BusinessBase<Custom>
169 {
170 private static PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
171 [TestRule]
172 public string Name
173 {
174 get { return GetProperty(NameProperty); }
175 set { SetProperty(NameProperty, value); }
176 }
177
178 [Create]
179 private async Task Create()
180 {
182 }
183
184 public string[] GetRules()
185 {
187 }
188 }
189
190 public class TestRuleAttribute : ValidationAttribute
191 {
192 protected override ValidationResult IsValid(object value, ValidationContext validationContext)
193 {
194 if (validationContext.ObjectInstance == null)
195 return new ValidationResult("ObjectInstance is null");
196 var obj = validationContext.ObjectInstance as Custom;
197 if (obj == null)
198 return new ValidationResult("ObjectInstance is not the Custom type");
199 if (string.IsNullOrEmpty(obj.Name) || obj.Name != "abc")
200 return new ValidationResult("Name must be abc");
201 return null;
202 }
203 }
204
205 [Serializable()]
206 public partial class MultipleMeta : Csla.BusinessBase<MultipleMeta>
207 {
208 public static PropertyInfo<decimal> AmountProperty = RegisterProperty<decimal>(p => p.Amount);
209
210 [Required(ErrorMessage="Please enter an amount")]
211 public decimal Amount
212 {
213 get { return GetProperty(AmountProperty); }
214 set { SetProperty(AmountProperty, value); }
215 }
216
217
218 public static readonly PropertyInfo<int> QuantityProperty = RegisterProperty<int>(c => c.Quantity);
219 public int Quantity
220 {
221 get { return GetProperty(QuantityProperty); }
222 set { SetProperty(QuantityProperty, value); }
223 }
224
225 [Fetch]
226 private void Fetch()
227 {
228 }
229 }
230
232 {
233 [Required(ErrorMessage = "Amount is required")]
234 [Range(typeof(decimal), "1", "100", ErrorMessage = "Please enter a value between 1 and 100")]
235 public System.Decimal Amount { get; set; }
236
237 [Required(ErrorMessage = "Quantity is required")]
238 [Range(1, 100, ErrorMessage = "Please enter a value between 1 and 100")]
239 public System.Int32 Quantity { get; set; }
240
241 }
245 [MetadataType(typeof(MultipleMetaDataClass))]
246 public partial class MultipleMeta : Csla.BusinessBase<MultipleMeta>
247 {
249 {
250 return base.GetRegisteredRules();
251 }
252 }
253}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
Maintains metadata about a property.
Manages the list of rules for a business type.
Tracks the business rules for a business object.
async Task< List< string > > CheckRulesAsync(int timeout)
Invokes all rules for the business type.
string[] GetRuleDescriptions()
Gets a list of rule:// URI values for the rules defined in the object.
override ValidationResult IsValid(object value, ValidationContext validationContext)
Type to carry context information for DI in unit tests
UnitTestContext GetContext()
Definition: TestBase.cs:12
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
object Fetch(params object[] criteria)
Called by a factory method in a business class to retrieve an object, which is loaded with values fro...
@ Serializable
Prevents updating or inserting until the transaction is complete.