public void TVItem_CRUD_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { TVItemService tvItemService = new TVItemService(new Query() { Lang = culture.TwoLetterISOLanguageName }, dbTestDB, ContactID); int count = 0; if (count == 1) { // just so we don't get a warning during compile [The variable 'count' is assigned but its value is never used] } TVItem tvItem = GetFilledRandomTVItem(""); // ------------------------------- // ------------------------------- // CRUD testing // ------------------------------- // ------------------------------- count = tvItemService.GetTVItemList().Count(); Assert.AreEqual(count, (from c in dbTestDB.TVItems select c).Count()); tvItemService.Add(tvItem); if (tvItem.HasErrors) { Assert.AreEqual("", tvItem.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(true, tvItemService.GetTVItemList().Where(c => c == tvItem).Any()); tvItemService.Update(tvItem); if (tvItem.HasErrors) { Assert.AreEqual("", tvItem.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(count + 1, tvItemService.GetTVItemList().Count()); tvItemService.Delete(tvItem); if (tvItem.HasErrors) { Assert.AreEqual("", tvItem.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(count, tvItemService.GetTVItemList().Count()); } } }
public IHttpActionResult Put([FromBody] TVItem tvItem, [FromUri] string lang = "en") { using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { TVItemService tvItemService = new TVItemService(new Query() { Language = (lang == "fr" ? LanguageEnum.fr : LanguageEnum.en) }, db, ContactID); if (!tvItemService.Update(tvItem)) { return(BadRequest(String.Join("|||", tvItem.ValidationResults))); } else { tvItem.ValidationResults = null; return(Ok(tvItem)); } } }
public void TVItem_Properties_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { TVItemService tvItemService = new TVItemService(new Query() { Lang = culture.TwoLetterISOLanguageName }, dbTestDB, ContactID); int count = 0; if (count == 1) { // just so we don't get a warning during compile [The variable 'count' is assigned but its value is never used] } count = tvItemService.GetTVItemList().Count(); TVItem tvItem = GetFilledRandomTVItem(""); // ------------------------------- // ------------------------------- // Properties testing // ------------------------------- // ------------------------------- // ----------------------------------- // [Key] // Is NOT Nullable // tvItem.TVItemID (Int32) // ----------------------------------- tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.TVItemID = 0; tvItemService.Update(tvItem); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "TVItemID"), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.TVItemID = 10000000; tvItemService.Update(tvItem); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "TVItemID", tvItem.TVItemID.ToString()), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [Range(0, 100)] // tvItem.TVLevel (Int32) // ----------------------------------- tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.TVLevel = -1; Assert.AreEqual(false, tvItemService.Add(tvItem)); Assert.AreEqual(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "TVLevel", "0", "100"), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, tvItemService.GetTVItemList().Count()); tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.TVLevel = 101; Assert.AreEqual(false, tvItemService.Add(tvItem)); Assert.AreEqual(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "TVLevel", "0", "100"), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, tvItemService.GetTVItemList().Count()); // ----------------------------------- // Is NOT Nullable // [StringLength(250))] // tvItem.TVPath (String) // ----------------------------------- tvItem = null; tvItem = GetFilledRandomTVItem("TVPath"); Assert.AreEqual(false, tvItemService.Add(tvItem)); Assert.AreEqual(1, tvItem.ValidationResults.Count()); Assert.IsTrue(tvItem.ValidationResults.Where(c => c.ErrorMessage == string.Format(CSSPServicesRes._IsRequired, "TVPath")).Any()); Assert.AreEqual(null, tvItem.TVPath); Assert.AreEqual(count, tvItemService.GetTVItemList().Count()); tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.TVPath = GetRandomString("", 251); Assert.AreEqual(false, tvItemService.Add(tvItem)); Assert.AreEqual(string.Format(CSSPServicesRes._MaxLengthIs_, "TVPath", "250"), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, tvItemService.GetTVItemList().Count()); // ----------------------------------- // Is NOT Nullable // [CSSPEnumType] // tvItem.TVType (TVTypeEnum) // ----------------------------------- tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.TVType = (TVTypeEnum)1000000; tvItemService.Add(tvItem); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "TVType"), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPExist(ExistTypeName = "TVItem", ExistPlurial = "s", ExistFieldID = "TVItemID", AllowableTVtypeList = Root,Address,Area,ClimateSite,Contact,Country,Email,HydrometricSite,Infrastructure,MikeBoundaryConditionWebTide,MikeBoundaryConditionMesh,MikeScenario,MikeSource,Municipality,MWQMSite,PolSourceSite,Province,Sector,Subsector,Tel,MWQMRun,RainExceedance,Classification)] // tvItem.ParentID (Int32) // ----------------------------------- tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.ParentID = 0; tvItemService.Add(tvItem); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "ParentID", tvItem.ParentID.ToString()), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.ParentID = 38; tvItemService.Add(tvItem); Assert.AreEqual(string.Format(CSSPServicesRes._IsNotOfType_, "ParentID", "Root,Address,Area,ClimateSite,Contact,Country,Email,HydrometricSite,Infrastructure,MikeBoundaryConditionWebTide,MikeBoundaryConditionMesh,MikeScenario,MikeSource,Municipality,MWQMSite,PolSourceSite,Province,Sector,Subsector,Tel,MWQMRun,RainExceedance,Classification"), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // tvItem.IsActive (Boolean) // ----------------------------------- // ----------------------------------- // Is NOT Nullable // [CSSPAfter(Year = 1980)] // tvItem.LastUpdateDate_UTC (DateTime) // ----------------------------------- tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.LastUpdateDate_UTC = new DateTime(); tvItemService.Add(tvItem); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "LastUpdateDate_UTC"), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.LastUpdateDate_UTC = new DateTime(1979, 1, 1); tvItemService.Add(tvItem); Assert.AreEqual(string.Format(CSSPServicesRes._YearShouldBeBiggerThan_, "LastUpdateDate_UTC", "1980"), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPExist(ExistTypeName = "TVItem", ExistPlurial = "s", ExistFieldID = "TVItemID", AllowableTVtypeList = Contact)] // tvItem.LastUpdateContactTVItemID (Int32) // ----------------------------------- tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.LastUpdateContactTVItemID = 0; tvItemService.Add(tvItem); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "LastUpdateContactTVItemID", tvItem.LastUpdateContactTVItemID.ToString()), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); tvItem = null; tvItem = GetFilledRandomTVItem(""); tvItem.LastUpdateContactTVItemID = 1; tvItemService.Add(tvItem); Assert.AreEqual(string.Format(CSSPServicesRes._IsNotOfType_, "LastUpdateContactTVItemID", "Contact"), tvItem.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [NotMapped] // tvItem.HasErrors (Boolean) // ----------------------------------- // No testing requied // ----------------------------------- // Is NOT Nullable // [NotMapped] // tvItem.ValidationResults (IEnumerable`1) // ----------------------------------- // No testing requied } } }