public void TideSite_CRUD_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { TideSiteService tideSiteService = new TideSiteService(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] } TideSite tideSite = GetFilledRandomTideSite(""); // ------------------------------- // ------------------------------- // CRUD testing // ------------------------------- // ------------------------------- count = tideSiteService.GetTideSiteList().Count(); Assert.AreEqual(count, (from c in dbTestDB.TideSites select c).Count()); tideSiteService.Add(tideSite); if (tideSite.HasErrors) { Assert.AreEqual("", tideSite.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(true, tideSiteService.GetTideSiteList().Where(c => c == tideSite).Any()); tideSiteService.Update(tideSite); if (tideSite.HasErrors) { Assert.AreEqual("", tideSite.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(count + 1, tideSiteService.GetTideSiteList().Count()); tideSiteService.Delete(tideSite); if (tideSite.HasErrors) { Assert.AreEqual("", tideSite.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(count, tideSiteService.GetTideSiteList().Count()); } } }
public IHttpActionResult Post([FromBody] TideSite tideSite, [FromUri] string lang = "en") { using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { TideSiteService tideSiteService = new TideSiteService(new Query() { Language = (lang == "fr" ? LanguageEnum.fr : LanguageEnum.en) }, db, ContactID); if (!tideSiteService.Add(tideSite)) { return(BadRequest(String.Join("|||", tideSite.ValidationResults))); } else { tideSite.ValidationResults = null; return(Created <TideSite>(new Uri(Request.RequestUri, tideSite.TideSiteID.ToString()), tideSite)); } } }
public void TideSite_Properties_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { TideSiteService tideSiteService = new TideSiteService(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 = tideSiteService.GetTideSiteList().Count(); TideSite tideSite = GetFilledRandomTideSite(""); // ------------------------------- // ------------------------------- // Properties testing // ------------------------------- // ------------------------------- // ----------------------------------- // [Key] // Is NOT Nullable // tideSite.TideSiteID (Int32) // ----------------------------------- tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.TideSiteID = 0; tideSiteService.Update(tideSite); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "TideSiteID"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.TideSiteID = 10000000; tideSiteService.Update(tideSite); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TideSite", "TideSiteID", tideSite.TideSiteID.ToString()), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPExist(ExistTypeName = "TVItem", ExistPlurial = "s", ExistFieldID = "TVItemID", AllowableTVtypeList = TideSite)] // tideSite.TideSiteTVItemID (Int32) // ----------------------------------- tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.TideSiteTVItemID = 0; tideSiteService.Add(tideSite); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "TideSiteTVItemID", tideSite.TideSiteTVItemID.ToString()), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.TideSiteTVItemID = 1; tideSiteService.Add(tideSite); Assert.AreEqual(string.Format(CSSPServicesRes._IsNotOfType_, "TideSiteTVItemID", "TideSite"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [StringLength(100))] // tideSite.TideSiteName (String) // ----------------------------------- tideSite = null; tideSite = GetFilledRandomTideSite("TideSiteName"); Assert.AreEqual(false, tideSiteService.Add(tideSite)); Assert.AreEqual(1, tideSite.ValidationResults.Count()); Assert.IsTrue(tideSite.ValidationResults.Where(c => c.ErrorMessage == string.Format(CSSPServicesRes._IsRequired, "TideSiteName")).Any()); Assert.AreEqual(null, tideSite.TideSiteName); Assert.AreEqual(count, tideSiteService.GetTideSiteList().Count()); tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.TideSiteName = GetRandomString("", 101); Assert.AreEqual(false, tideSiteService.Add(tideSite)); Assert.AreEqual(string.Format(CSSPServicesRes._MaxLengthIs_, "TideSiteName", "100"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, tideSiteService.GetTideSiteList().Count()); // ----------------------------------- // Is NOT Nullable // [StringLength(2, MinimumLength = 2)] // tideSite.Province (String) // ----------------------------------- tideSite = null; tideSite = GetFilledRandomTideSite("Province"); Assert.AreEqual(false, tideSiteService.Add(tideSite)); Assert.AreEqual(1, tideSite.ValidationResults.Count()); Assert.IsTrue(tideSite.ValidationResults.Where(c => c.ErrorMessage == string.Format(CSSPServicesRes._IsRequired, "Province")).Any()); Assert.AreEqual(null, tideSite.Province); Assert.AreEqual(count, tideSiteService.GetTideSiteList().Count()); tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.Province = GetRandomString("", 1); Assert.AreEqual(false, tideSiteService.Add(tideSite)); Assert.AreEqual(string.Format(CSSPServicesRes._LengthShouldBeBetween_And_, "Province", "2", "2"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, tideSiteService.GetTideSiteList().Count()); tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.Province = GetRandomString("", 3); Assert.AreEqual(false, tideSiteService.Add(tideSite)); Assert.AreEqual(string.Format(CSSPServicesRes._LengthShouldBeBetween_And_, "Province", "2", "2"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, tideSiteService.GetTideSiteList().Count()); // ----------------------------------- // Is NOT Nullable // [Range(0, 10000)] // tideSite.sid (Int32) // ----------------------------------- tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.sid = -1; Assert.AreEqual(false, tideSiteService.Add(tideSite)); Assert.AreEqual(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "sid", "0", "10000"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, tideSiteService.GetTideSiteList().Count()); tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.sid = 10001; Assert.AreEqual(false, tideSiteService.Add(tideSite)); Assert.AreEqual(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "sid", "0", "10000"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, tideSiteService.GetTideSiteList().Count()); // ----------------------------------- // Is NOT Nullable // [Range(0, 10000)] // tideSite.Zone (Int32) // ----------------------------------- tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.Zone = -1; Assert.AreEqual(false, tideSiteService.Add(tideSite)); Assert.AreEqual(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "Zone", "0", "10000"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, tideSiteService.GetTideSiteList().Count()); tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.Zone = 10001; Assert.AreEqual(false, tideSiteService.Add(tideSite)); Assert.AreEqual(string.Format(CSSPServicesRes._ValueShouldBeBetween_And_, "Zone", "0", "10000"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, tideSiteService.GetTideSiteList().Count()); // ----------------------------------- // Is NOT Nullable // [CSSPAfter(Year = 1980)] // tideSite.LastUpdateDate_UTC (DateTime) // ----------------------------------- tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.LastUpdateDate_UTC = new DateTime(); tideSiteService.Add(tideSite); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "LastUpdateDate_UTC"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.LastUpdateDate_UTC = new DateTime(1979, 1, 1); tideSiteService.Add(tideSite); Assert.AreEqual(string.Format(CSSPServicesRes._YearShouldBeBiggerThan_, "LastUpdateDate_UTC", "1980"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPExist(ExistTypeName = "TVItem", ExistPlurial = "s", ExistFieldID = "TVItemID", AllowableTVtypeList = Contact)] // tideSite.LastUpdateContactTVItemID (Int32) // ----------------------------------- tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.LastUpdateContactTVItemID = 0; tideSiteService.Add(tideSite); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "LastUpdateContactTVItemID", tideSite.LastUpdateContactTVItemID.ToString()), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); tideSite = null; tideSite = GetFilledRandomTideSite(""); tideSite.LastUpdateContactTVItemID = 1; tideSiteService.Add(tideSite); Assert.AreEqual(string.Format(CSSPServicesRes._IsNotOfType_, "LastUpdateContactTVItemID", "Contact"), tideSite.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [NotMapped] // tideSite.HasErrors (Boolean) // ----------------------------------- // No testing requied // ----------------------------------- // Is NOT Nullable // [NotMapped] // tideSite.ValidationResults (IEnumerable`1) // ----------------------------------- // No testing requied } } }