public void ContactShortcut_CRUD_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { ContactShortcutService contactShortcutService = new ContactShortcutService(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] } ContactShortcut contactShortcut = GetFilledRandomContactShortcut(""); // ------------------------------- // ------------------------------- // CRUD testing // ------------------------------- // ------------------------------- count = contactShortcutService.GetContactShortcutList().Count(); Assert.AreEqual(count, (from c in dbTestDB.ContactShortcuts select c).Count()); contactShortcutService.Add(contactShortcut); if (contactShortcut.HasErrors) { Assert.AreEqual("", contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(true, contactShortcutService.GetContactShortcutList().Where(c => c == contactShortcut).Any()); contactShortcutService.Update(contactShortcut); if (contactShortcut.HasErrors) { Assert.AreEqual("", contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(count + 1, contactShortcutService.GetContactShortcutList().Count()); contactShortcutService.Delete(contactShortcut); if (contactShortcut.HasErrors) { Assert.AreEqual("", contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(count, contactShortcutService.GetContactShortcutList().Count()); } } }
public IHttpActionResult Post([FromBody] ContactShortcut contactShortcut, [FromUri] string lang = "en") { using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { ContactShortcutService contactShortcutService = new ContactShortcutService(new Query() { Language = (lang == "fr" ? LanguageEnum.fr : LanguageEnum.en) }, db, ContactID); if (!contactShortcutService.Add(contactShortcut)) { return(BadRequest(String.Join("|||", contactShortcut.ValidationResults))); } else { contactShortcut.ValidationResults = null; return(Created <ContactShortcut>(new Uri(Request.RequestUri, contactShortcut.ContactShortcutID.ToString()), contactShortcut)); } } }
public void ContactShortcut_Properties_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { ContactShortcutService contactShortcutService = new ContactShortcutService(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 = contactShortcutService.GetContactShortcutList().Count(); ContactShortcut contactShortcut = GetFilledRandomContactShortcut(""); // ------------------------------- // ------------------------------- // Properties testing // ------------------------------- // ------------------------------- // ----------------------------------- // [Key] // Is NOT Nullable // contactShortcut.ContactShortcutID (Int32) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.ContactShortcutID = 0; contactShortcutService.Update(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "ContactShortcutID"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.ContactShortcutID = 10000000; contactShortcutService.Update(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "ContactShortcut", "ContactShortcutID", contactShortcut.ContactShortcutID.ToString()), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPExist(ExistTypeName = "Contact", ExistPlurial = "s", ExistFieldID = "ContactID", AllowableTVtypeList = )] // contactShortcut.ContactID (Int32) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.ContactID = 0; contactShortcutService.Add(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "Contact", "ContactID", contactShortcut.ContactID.ToString()), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [StringLength(100))] // contactShortcut.ShortCutText (String) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut("ShortCutText"); Assert.AreEqual(false, contactShortcutService.Add(contactShortcut)); Assert.AreEqual(1, contactShortcut.ValidationResults.Count()); Assert.IsTrue(contactShortcut.ValidationResults.Where(c => c.ErrorMessage == string.Format(CSSPServicesRes._IsRequired, "ShortCutText")).Any()); Assert.AreEqual(null, contactShortcut.ShortCutText); Assert.AreEqual(count, contactShortcutService.GetContactShortcutList().Count()); contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.ShortCutText = GetRandomString("", 101); Assert.AreEqual(false, contactShortcutService.Add(contactShortcut)); Assert.AreEqual(string.Format(CSSPServicesRes._MaxLengthIs_, "ShortCutText", "100"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, contactShortcutService.GetContactShortcutList().Count()); // ----------------------------------- // Is NOT Nullable // [StringLength(200))] // contactShortcut.ShortCutAddress (String) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut("ShortCutAddress"); Assert.AreEqual(false, contactShortcutService.Add(contactShortcut)); Assert.AreEqual(1, contactShortcut.ValidationResults.Count()); Assert.IsTrue(contactShortcut.ValidationResults.Where(c => c.ErrorMessage == string.Format(CSSPServicesRes._IsRequired, "ShortCutAddress")).Any()); Assert.AreEqual(null, contactShortcut.ShortCutAddress); Assert.AreEqual(count, contactShortcutService.GetContactShortcutList().Count()); contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.ShortCutAddress = GetRandomString("", 201); Assert.AreEqual(false, contactShortcutService.Add(contactShortcut)); Assert.AreEqual(string.Format(CSSPServicesRes._MaxLengthIs_, "ShortCutAddress", "200"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, contactShortcutService.GetContactShortcutList().Count()); // ----------------------------------- // Is NOT Nullable // [CSSPAfter(Year = 1980)] // contactShortcut.LastUpdateDate_UTC (DateTime) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.LastUpdateDate_UTC = new DateTime(); contactShortcutService.Add(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "LastUpdateDate_UTC"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.LastUpdateDate_UTC = new DateTime(1979, 1, 1); contactShortcutService.Add(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes._YearShouldBeBiggerThan_, "LastUpdateDate_UTC", "1980"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPExist(ExistTypeName = "TVItem", ExistPlurial = "s", ExistFieldID = "TVItemID", AllowableTVtypeList = Contact)] // contactShortcut.LastUpdateContactTVItemID (Int32) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.LastUpdateContactTVItemID = 0; contactShortcutService.Add(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "LastUpdateContactTVItemID", contactShortcut.LastUpdateContactTVItemID.ToString()), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.LastUpdateContactTVItemID = 1; contactShortcutService.Add(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes._IsNotOfType_, "LastUpdateContactTVItemID", "Contact"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [NotMapped] // contactShortcut.HasErrors (Boolean) // ----------------------------------- // No testing requied // ----------------------------------- // Is NOT Nullable // [NotMapped] // contactShortcut.ValidationResults (IEnumerable`1) // ----------------------------------- // No testing requied } } }