public void ModelBase_MultiFieldValidation() { var person = new SimplePerson(); person.FirstName = "Tom"; person.LastName = "Tom"; IList <ValidationResult> errors = person.GetErrors("FirstName"); Assert.AreEqual(1, errors.Count); Assert.IsTrue(errors[0].MemberNames.Contains("FirstName")); Assert.IsFalse(string.IsNullOrEmpty(errors[0].ErrorMessage)); errors = person.GetErrors("LastName"); Assert.AreEqual(1, errors.Count); Assert.IsTrue(errors[0].MemberNames.Contains("LastName")); Assert.IsFalse(string.IsNullOrEmpty(errors[0].ErrorMessage)); errors = ((INotifyDataErrorInfo)person).GetErrors("LastName").Cast <ValidationResult>().ToList(); Assert.AreEqual(1, errors.Count); Assert.IsTrue(errors[0].MemberNames.Contains("LastName")); Assert.IsFalse(string.IsNullOrEmpty(errors[0].ErrorMessage)); errors = person.GetErrors(); Assert.AreEqual(1, errors.Count); Assert.IsTrue(errors[0].MemberNames.Contains("FirstName")); Assert.IsTrue(errors[0].MemberNames.Contains("LastName")); Assert.IsFalse(string.IsNullOrEmpty(errors[0].ErrorMessage)); var interfacePerson = (IDataErrorInfo)person; Assert.IsTrue(!string.IsNullOrEmpty(interfacePerson.Error)); Assert.IsTrue(!string.IsNullOrEmpty(interfacePerson["FirstName"])); Assert.IsTrue(!string.IsNullOrEmpty(interfacePerson["LastName"])); }
public IActionResult AddPersonValidate(SimplePerson person) { var errors = new List <string>(); if (string.IsNullOrEmpty(person.FirstName)) { errors.Add("Förnamnet får inte vara tomt"); } if (person.FirstName?.Length > 20) { errors.Add("Förnamnet får inte innehålla fler än 20 tecken"); } if (person.Age == null) { errors.Add("Ålder måste anges"); } if (person.Age < 0 || person.Age > 120) { errors.Add("Ålder måste vara mellan 0 och 120"); } if (errors.Count > 0) { return(BadRequest(string.Join(". ", errors))); } else { return(Ok($"Du har angett {person.FirstName} som är {person.Age} år")); } }
public void TestConfiguredTriggerListener() { IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory; IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-extend-maptrigger-cache-config.xml"); ccf.Config = config; INamedCache cache = CacheFactory.GetCache(CACHE_NAME); cache.Clear(); SimplePerson pIn = new SimplePerson("123-45-6789", "Eddie", "Van Halen", 1955, "987-65-4321", new String[] { "456-78-9123" }); try { cache.Insert(1, pIn); Object[] keys = cache.GetKeys(AlwaysFilter.Instance); Assert.AreEqual(keys.Length, 1); SimplePerson pOut = (SimplePerson)cache[1]; Assert.AreEqual(pIn.LastName.ToUpper(), pOut.LastName); } finally { CacheFactory.Shutdown(); } }
public IActionResult AddPerson(SimplePerson person) { bool validName = true; bool validAge = true; if (person.Firstname == null || !(person.Firstname.Length <= 20 && person.Firstname.Length >= 1)) { validName = false; } if (person.Age == null || !(person.Age.Length <= 120 && person.Age.Length > 0)) { validAge = false; } if (!validName || !validAge) { return(BadRequest(ModelState)); } else if (!validName) { return(BadRequest("Name is incorrect")); } if (!validAge) { return(BadRequest("Age is incorrect")); } return(Ok($"Du har angett {person.Firstname} som är {person.Age}")); }
public void InvokeActionConstantIsSupportedSimple() { var refValueArg = Parameter(typeof(SimplePerson).MakeByRefType(), "value"); void AssigningRefs(ref SimplePerson value) => value.Health = 5; var lambda = Lambda <DeserializeDelegateSimple <SimplePerson> >( Assign(PropertyOrField(refValueArg, nameof(SimplePerson.Health)), Constant(5)), refValueArg); void LocalAssert(DeserializeDelegateSimple <SimplePerson> invoke) { var person = new SimplePerson { Health = 1 }; invoke(ref person); Assert.AreEqual(5, person.Health); } LocalAssert(AssigningRefs); var func = lambda.CompileSys(); LocalAssert(func); var funcFast = lambda.CompileFast(true); LocalAssert(funcFast); }
public void ModelBase_ValueChangedTest() { var person = new SimplePerson(); var firstDate = new DateTime(1980, 1, 1); var secondDate = new DateTime(2000, 1, 1); person.DateOfBirth = firstDate; person.DateOfBirth = secondDate; Assert.AreEqual(firstDate, person.PreviousDateOfBirth); }
public void SerializeSimplePersonResultsInProperOutput() { var guy = new SimplePerson { Name = "Joe Average" }; var serializer = new Serializer(); string xml = serializer.SerializeObject(guy); Approvals.VerifyXml(xml); }
public void PropertyBag_FixCasingTest() { var person = new SimplePerson(); var bag = person.GetPropertyBag(); person.FirstName = "Frank"; person.FirstName = "Frank"; bag.Set("Tom", PropertySetModes.FixCasing, "firstname"); Assert.AreEqual("Tom", person.FirstName); }
public void ModelBase_ChildPropertyChangedTest() { var person = new SimplePerson(); Assert.AreEqual(0, person.SecretaryChangeCounter); person.Secretary = new SimplePerson(); Assert.AreEqual(0, person.SecretaryChangeCounter); person.Secretary.FirstName = "Tom"; Assert.AreEqual(2, person.SecretaryChangeCounter, "FirstName and FullName"); }
public IActionResult AddPersonValidateAttribute(SimplePerson person) { if (ModelState.IsValid) { return(Ok($"Bra gjort! {person.Name} är {person.Age} och lades till i databasen")); } else { return(BadRequest(ModelState)); } }
public void DeserializeSimplePersonSucceeds() { var guy = new SimplePerson { Name = "Joe Average" }; var serializer = new Serializer(); string xml = serializer.SerializeObject(guy); var guy2 = serializer.DeserializeObject <SimplePerson>(xml); Assert.AreEqual(guy, guy2); }
public IActionResult ValidateWithAttributes(SimplePerson person) { if (ModelState.IsValid) { return(Ok($"Du har angett {person.Name} som är {person.Age}.")); } else { return(BadRequest(ModelState)); } }
public void Process(ICacheTriggerEntry entry) { SimplePerson person = (SimplePerson)entry.Extract(IdentityExtractor.Instance); String sName = person.LastName; String sNameUC = sName.ToUpper(); if (!sNameUC.Equals(sName)) { person.LastName = sNameUC; entry.SetValue(person, false); } }
public void TestDefaultValues() { var person = new SimplePerson(); var answer1 = "{\"Age\":0,\"Name\":null,\"IsHuman\":false,\"Probability\":0,\"Size\":0,\"Color\":\"None\"}"; var answer2 = "{\"Age\":0,\"IsHuman\":false,\"Probability\":0,\"Size\":0,\"Color\":\"None\"}"; var answerDefault = "{}"; JsonSerializer.ToJsonString(person, Utf8Json.Resolvers.StandardResolver.Default).Is(answer1); JsonSerializer.ToJsonString(person, Utf8Json.Resolvers.StandardResolver.ExcludeNull).Is(answer2); JsonSerializer.ToJsonString(person, Utf8Json.Resolvers.StandardResolver.AllowPrivate).Is(answer1); JsonSerializer.ToJsonString(person, Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNullExcludeDefault).Is(answerDefault); }
public Dictionary <string, FaceRecognitionTrainingStatus> ReadImagesWithPersonsFromDirectory(string directoryWithImages) { var faceRecognitionTrainingStatuses = new Dictionary <string, FaceRecognitionTrainingStatus>(); if (!Directory.Exists(directoryWithImages)) { faceRecognitionTrainingStatuses.Add("failure", FaceRecognitionTrainingStatus.TrainingFailure); return(faceRecognitionTrainingStatuses); } var directoryWithImagesInfo = new DirectoryInfo(directoryWithImages); foreach (var personDirectoryInfo in directoryWithImagesInfo.GetDirectories()) { var personDirectoryInfoSplitted = personDirectoryInfo.Name.Split(' '); if (personDirectoryInfoSplitted.Length != 2) { faceRecognitionTrainingStatuses.Add(personDirectoryInfo.Name, FaceRecognitionTrainingStatus.TrainingFailure); return(faceRecognitionTrainingStatuses); } var simplePersonFromDirectoryInfo = new SimplePerson { FirstName = personDirectoryInfoSplitted.First(), LastName = personDirectoryInfoSplitted.Last() }; foreach (var imageFileInfo in personDirectoryInfo.GetFiles()) { try { using (var imageWithFace = new Bitmap(imageFileInfo.FullName)) { var faceRecognitionTrainingStatus = _faceRecognitionAlgorithm.AddImageToTrainingSet(imageWithFace, simplePersonFromDirectoryInfo); faceRecognitionTrainingStatuses.Add(imageFileInfo.Name, faceRecognitionTrainingStatus); } } catch (Exception e) { faceRecognitionTrainingStatuses.Add(imageFileInfo.Name, FaceRecognitionTrainingStatus.TrainingFailure); throw e; } } } return(faceRecognitionTrainingStatuses); }
public void ModelBase_GetNewTest() { var person = new SimplePerson(); var a = person.Boss; Assert.AreEqual("Da", a.FirstName); Assert.AreEqual("Boss", a.LastName); Assert.AreSame(a, person.Boss); var b = person.Partner; Assert.AreSame(b, person.Partner); }
public void ModelBase_GetNewFailedTest3() { var person = new SimplePerson(); try { person.BadGetNew3(); Assert.Fail("Expected an exception"); } catch (ArgumentException ex) { Assert.AreEqual("creationFunction", ex.ParamName); } }
public void ModelBase_SetFailedTest2() { var person = new SimplePerson(); try { person.BadSet2(); Assert.Fail("Expected an exception"); } catch (ArgumentException ex) { Assert.AreEqual("propertyName", ex.ParamName); } }
public void PropertyBag_IsDefinedTest() { var person = new SimplePerson(); var bag = person.GetPropertyBag(); Assert.IsFalse(bag.IsDefined("FirstName")); Assert.IsFalse(bag.IsDefined("FullName")); Assert.IsFalse(bag.IsDefined("Frank")); person.FirstName = "Tom"; Assert.IsTrue(bag.IsDefined("FirstName")); Assert.IsFalse(bag.IsDefined("FullName")); Assert.IsFalse(bag.IsDefined("Frank")); }
public void ModelBase_RemoveHandlerNullTest() { var person = new SimplePerson(); try { person.RemoveHandler(null); Assert.Fail("Excepted an ArgumentNullException"); } catch (ArgumentNullException ex) { Assert.AreEqual("eventHandler", ex.ParamName); } }
public void ModelBase_PropertyChangedTest() { var person = new SimplePerson(); try { person.InvokeBadPropertyMessage(); Assert.Fail("Expected an exception"); } catch (ArgumentOutOfRangeException ex) { Assert.AreEqual("propertyName", ex.ParamName); Assert.AreEqual("Boom", ex.ActualValue); } }
public void ModelBase_AddRemoveHandlerTest() { var fired = false; var person = new SimplePerson(); var listener = new Listener <PropertyChangedEventArgs>((sender, e) => { fired = true; }); person.ErrorsChanged += (sender, e) => { }; person.AddHandler(listener); person.FirstName = "Tom"; Assert.IsTrue(fired); fired = false; person.RemoveHandler(listener); person.FirstName = "Sam"; Assert.IsFalse(fired); }
public IActionResult AddPersonValidate(SimplePerson person) { if (person.Firstname == null || person.Age == null) { return(BadRequest("Du måste ange både förnamn och ålder")); } if (person.Firstname.Length > 20) { return(BadRequest("För långt förnamn")); } if (person.Age < 0 || person.Age > 120) { return(BadRequest("Fyll i rätt ålder")); } return(Ok($"{person.Firstname} som är {person.Age} år lades till i databasen")); }
public IActionResult AddPersonValidate(SimplePerson person) { if (person.Name == null || person?.Age == null) { return(BadRequest("Förnamn och ålder måste anges")); } if (person.Age < 1 || person.Age > 119) { return(BadRequest("Nu har du varit orimlig, så gammal eller ung kan du inte vara")); } if (person.Name.Length > 20) { return(BadRequest("Så långt namn kan du inte ha")); } return(Ok($"Bra gjort! {person.Name} är {person.Age} och lades till i databasen")); }
public void AutoloadOffSuccessScenario() { String data = @" SimplePerson@autoload = no SimplePerson.Id = 15 SimplePerson.Age = 200 " ; args = DataBinderTestCase.ParseNameValueString(@data); instance = binder.BindObject(typeof(SimplePerson), "SimplePerson", args); Assert.IsNotNull(instance); person = instance as SimplePerson; Assert.IsNull(person.Name); Assert.IsTrue(person.Age == 200); }
public void ModelBase_SerializationTest1() { var person = new SimplePerson(); person.FirstName = "Tom"; person.LastName = "Jones"; var stream = new MemoryStream(); var serializer = new DataContractSerializer(typeof(SimplePerson)); serializer.WriteObject(stream, person); stream.Position = 0; var newPerson = (SimplePerson)serializer.ReadObject(stream); Assert.AreEqual(person.FirstName, newPerson.FirstName); Assert.AreEqual(person.LastName, newPerson.LastName); Assert.AreEqual(person.FullName, newPerson.FullName); }
public static void CreateAndPopulatePeopleTable() { ActiveRecordStarter.Initialize( ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource, typeof(SimplePerson)); ActiveRecordStarter.CreateSchema(); using (new SessionScope()) { for (int i = 1; i <= 15; i++) { SimplePerson person = new SimplePerson(); person.Name = "Name " + i.ToString(); person.Age = i; person.Save(); } } }
public IActionResult ValidatePerson(SimplePerson person) { if (person.Age < 0 && person.Name.Length > 20 || person.Age > 120 && person.Name.Length > 20) { return(BadRequest("The name can only be 20 characters long and the age can only be between 0 and 120")); } else if (person.Age < 0 || person.Age > 120) { return(BadRequest("The age can only be between 0 and 120")); } else if (person.Name.Length > 20) { return(BadRequest("The name can only be 20 characters long.")); } else { return(Ok($"Du har angett {person.Name} som är {person.Age}.")); } }
public void TestFilledValues() { var person = new SimplePerson() { Age = 18, Name = "Bob", IsHuman = true, Probability = 0.7891m, Size = 178, Color = Color.Blue }; var answer = "{\"Age\":18,\"Name\":\"Bob\",\"IsHuman\":true,\"Probability\":0.7891,\"Size\":178,\"Color\":\"Blue\"}"; JsonSerializer.ToJsonString(person, Utf8Json.Resolvers.StandardResolver.Default).Is(answer); JsonSerializer.ToJsonString(person, Utf8Json.Resolvers.StandardResolver.ExcludeNull).Is(answer); JsonSerializer.ToJsonString(person, Utf8Json.Resolvers.StandardResolver.AllowPrivate).Is(answer); JsonSerializer.ToJsonString(person, Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNullExcludeDefault).Is(answer); }
public void ModelBase_ValidationTest2() { using (var verify = new Verify()) { var person = new SimplePerson(); var eventAssert = new PropertyChangedEventTest(verify, person); person.Validate(); Assert.IsTrue(person.HasErrors); eventAssert.ExpectEvent("HasErrors"); var errors = person.GetErrors("FirstName"); Assert.AreEqual(1, errors.Count); person.ClearErrors(); Assert.IsFalse(person.HasErrors); var errors2 = person.GetErrors("FirstName"); Assert.AreEqual(0, errors2.Count); } }