public void MinLengthTest() { var obj1 = new MinLengthTestClass() { MinLengthOf3 = "1234", anotherStr = "123" }; Assert.IsTrue(SwagValidator.Validate(obj1)); try { var obj2 = new MinLengthTestClass() { MinLengthOf3 = "12" //not long enough }; SwagValidator.Validate(obj2); //should throw exception Assert.Fail(); // should not reach here because empty string threw exception } catch (Exception ex) { Assert.IsTrue(ex.Message.Contains("has length 2. Minlength is 3")); } }
public void UpdateInUse() { SvnSandBox sbox = new SvnSandBox(this); Uri CollabReposUri = sbox.CreateRepository(SandBoxRepository.MergeScenario); string dir = sbox.Wc; Client.CheckOut(new SvnUriTarget(new Uri(CollabReposUri, "trunk"), 1), dir); using (File.OpenRead(Path.Combine(dir, "index.html"))) using (new Implementation.SvnFsOperationRetryOverride(0)) { SvnSystemException se = null; try { Client.Update(Path.Combine(dir, "index.html")); Assert.Fail("Should have failed"); } catch (SvnWorkingCopyException e) { Assert.That(e.Message, Is.StringContaining("Failed to run the WC DB")); se = e.GetCause <SvnSystemException>(); } catch (SvnSystemException e) { se = e; } Assert.That(se, Is.Not.Null, "Have system exception"); Assert.That(se.Message, Is.StringContaining("Can't move")); } }
public void EditSubmissionThesisTest() { IDao dao = new DatabaseManager(); SubmissionThesis submission = new SubmissionThesis(); submission.SubmissionId = 1; submission.ThesisTopic = "Tresc"; submission.TopicNumber = 2; submission.ThesisObjectives = "Cele pracy"; submission.ThesisScope = "Cele pracy"; submission.Status = ThesisStatus.APPROVED; FinalThesis finalThesis = new FinalThesis(); finalThesis.FinalThesisId = 1; submission.FinalThesis = finalThesis; Edition edition = new Edition(); edition.Number = 1; submission.Edition = edition; Assert.IsTrue(dao.EditSubmissionThesis(submission)); submission.SubmissionId = 100000; Assert.IsFalse(dao.EditSubmissionThesis(submission)); try { SubmissionThesis incorrectSubmission = new SubmissionThesis(); incorrectSubmission.SubmissionId = -1; dao.EditSubmissionThesis(incorrectSubmission); Assert.Fail(); } catch (ArgumentException) { } }
public void DeleteInfringingFK() { // Creates a category Category cat = new Category(); cat.Name = "cat"; cat.Save(false); Item item = new Item(); item.Description = "an item"; item.CategoryId = cat.CategoryId; item.Save(false); try { cat.Delete(); } catch (SQLiteException ex) { if (ex.ErrorCode == SQLiteErrorCode.Constraint) { return; } } Assert.Fail(); }
public void TestRegExFilePath() { try { //Valid value var obj = new RegExFilePathTestClass() { Text = @"D:\server\tshare\folder\myfile.txt" }; Assert.IsTrue(SwagValidator.Validate(obj)); obj.Text = @"\\server1\Folder\File.txt"; Assert.IsTrue(SwagValidator.Validate(obj)); obj.Text = "\\\\server1\\Folder\\File.txt"; Assert.IsTrue(SwagValidator.Validate(obj)); obj.Text = "D:\\server\\tshare\\folder\\myfile.txt"; Assert.IsTrue(SwagValidator.Validate(obj)); //Invalid Value obj.Text = "1098.3456.33.44"; SwagValidator.Validate(obj); Assert.Fail(); } catch (Exception ex) { Assert.IsTrue(ex.Message.Contains("is invalid. Received value")); } }
public void Lock_CommitTest() { SvnSandBox sbox = new SvnSandBox(this); Uri CollabReposUri = sbox.CreateRepository(SandBoxRepository.MergeScenario); string wc1 = sbox.Wc; string wc2 = sbox.GetTempDir("WC2"); Uri repos = new Uri(CollabReposUri, "trunk/"); Client.CheckOut(repos, wc1); Client.CheckOut(repos, wc2); string index1 = Path.Combine(wc1, "index.html"); string index2 = Path.Combine(wc2, "index.html"); Client.Lock(index1, "q!"); File.WriteAllText(index2, "QQQQQ"); try { Client.Commit(index2); Assert.Fail("Commit should have failed"); } catch (SvnException e) { Assert.That(e.GetCause <SvnFileSystemLockException>(), Is.Not.Null, "Caused by lock error"); } }
public IEnumerator GuardedMethodThrowsIfNotLoggedIn() { yield return(OnFacet <AuthenticationFacet> .Call( nameof(AuthenticationFacet.GuardedMethod) ).Then(() => { Assert.Fail("Exception wasn't thrown."); }).Catch(e => { Assert.IsInstanceOf <AuthException>(e); }).AsCoroutine()); }
public static void DoesNotThrow(MyTestDelegate d) { try { d(); } catch (Exception e) { Assert.Fail(e.ToString()); } }
public void SaveNewObjectWithValidate() { var person = CreateNewTransientPerson(); person.ChangeName("fail"); try { Save(person); Assert.Fail(); } // ReSharper disable once EmptyGeneralCatchClause catch (Exception /*expected*/) {} }
public void ChangeObjectWithValidate() { var person = CreateNewTransientPerson(); person.Name = Guid.NewGuid().ToString(); person = Save(person).GetDomainObject<Person>(); try { TransactionManager.StartTransaction(); person.Name = "fail"; TransactionManager.EndTransaction(); Assert.Fail(); } catch (PersistFailedException /*expected*/) {} }
public void SaveNewObjectCallsPersistingPersistedRecursivelyExceedsMax() { var order = CreateNewTransientOrderFail(); order.Name = Guid.NewGuid().ToString(); try { Save(order); Assert.Fail("Expect exception"); } catch (NakedObjectDomainException e) { // expected Assert.AreEqual("Max number of commit cycles exceeded. Either increase MaxCommitCycles on installer or identify Updated/Persisted loop. Possible types : TestData.OrderFail", e.Message); } }
public void EditReviewStatusTest() { IDao manager = new DatabaseManager(); try { manager.EditReviewStatus(-1, ThesisStatus.APPROVED); Assert.Fail(); } catch { } try { manager.EditReviewStatus(100000, ThesisStatus.APPROVED); Assert.Fail(); } catch { } try { manager.EditReviewStatus(1, ThesisStatus.DISCARD); FinalThesisReview review = manager.GetReview(1); Assert.IsTrue(review.FormStatus == ThesisStatus.DISCARD); } catch { Assert.Fail(); } try { manager.EditReviewStatus(1, ThesisStatus.WAITING); FinalThesisReview review = manager.GetReview(1); Assert.IsTrue(review.FormStatus == ThesisStatus.WAITING); } catch { Assert.Fail(); } try { manager.EditReviewStatus(1, ThesisStatus.APPROVED); FinalThesisReview review = manager.GetReview(1); Assert.IsTrue(review.FormStatus == ThesisStatus.APPROVED); } catch { Assert.Fail(); } }
public void RequiredObjectListWithOutValueTest() { try { var obj8 = new ComplexTypeWithRequiredList(); //empty list test SwagValidator.Validate(obj8); //should throw exception Assert.Fail(); // should not reach here because empty string threw exception } catch (Exception ex) { Assert.AreEqual("Value cannot be null. Parameter name: RequiredObject", ex.Message); } }
public void SaveManyToManyList() { //Loads a person and adds 3 departments to its N:N relation. Person p = new Person(2); p.MaritalStatus = (MaritalStatus) new Random().Next(0, 8); p.DepartmentList.Clear(); int[] departments = new int[] { 1, 3, 4 }; foreach (int id in departments) { Department d = new Department(); d.DepartmentId = id; p.DepartmentList.Add(d); } Transaction t = new Transaction(); try { t.Include(p); p.Save(true); p.SaveList("DepartmentList"); t.Commit(); } catch (Exception ex) { t.Rollback(); Assert.Fail("Cannot save person. " + ex.Message); } //Check if person 2 have the departments 1, 3 and 4 p = new Person(2); foreach (int id in departments) { Department d = new Department(); d.DepartmentId = id; if (!p.DepartmentList.Contains(d)) { Assert.Fail(string.Format("Department {0} was not associated with person 2. ", id)); } } }
public void RegexTestNotValid() { try { var obj = new RegularExpressionAttrTest() { Country = "uX" }; SwagValidator.Validate(obj); //should throw exception Assert.Fail(); // should not reach here because empty string threw exception } catch (Exception ex) { Assert.IsTrue(ex.Message.Contains("Country is invalid")); } }
public void SaveNewObjectCallsPersistingPersistedRecursivelyFails() { Assert.AreEqual(0, Persistor.Instances<OrderFail>().Count()); var order = CreateNewTransientOrderFail(); order.Name = Guid.NewGuid().ToString(); try { Save(order); Assert.Fail("Expect exception"); } // ReSharper disable once EmptyGeneralCatchClause catch (Exception) { // expected } Assert.AreEqual(0, Persistor.Instances<OrderFail>().Count()); }
public void ObjectWithNestedClass() { var obj1 = new SomeObjectWithNestedClass() { SomethingElse = "something", ThisIsARequiredObj = null }; try { SwagValidator.Validate(obj1); Assert.Fail(); // should not reach here } catch (Exception ex) { Assert.AreEqual("Value cannot be null. Parameter name: ThisIsARequiredObj", ex.Message); } }
public static Exception Throws <T>(Action action) where T : Exception { Exception targetException = null; try { action(); } catch (T ex) { // Test pass return(ex); } #if PORTABLE catch (System.Reflection.TargetInvocationException ex) { var inner = ex.InnerException; if (inner is T) { return(inner); } else { FrameworkAssert.Fail(String.Format("Wrong exception type thrown. Expected {0}, got {1}.", typeof(T), inner.GetType())); } } #endif catch (Exception ex) { #if XUNIT FrameworkAssert.True(false, String.Format("Wrong exception type thrown. Expected {0}, got {1}.", typeof(T), ex.GetType())); #else FrameworkAssert.Fail(String.Format("Wrong exception type thrown. Expected {0}, got {1}.", typeof(T), ex.GetType())); #endif } #if XUNIT FrameworkAssert.True(false, String.Format("No Expected {0} was thrown", typeof(T).FullName)); #else FrameworkAssert.Fail(String.Format("No Expected {0} was thrown", typeof(T).FullName)); #endif throw new Exception(); }
public void SaveNewObjectWithTransientReferenceInvalid() { var person = CreateNewTransientPerson(); var product = CreateNewTransientProduct(); person.Name = Guid.NewGuid().ToString(); try { // should fail as product name is not set person.FavouriteProduct = product; // for EF var adapter = AdapterFor(person); if (adapter.ValidToPersist() != null) { throw new PersistFailedException(""); } Assert.Fail(); } catch (PersistFailedException /*expected*/) {} }
public void TestRegExCountry() { try { //Valid value var obj = new RegExCountyTestClass() { Text = "US" }; Assert.IsTrue(SwagValidator.Validate(obj)); //Invalid Value obj.Text = "PPP"; SwagValidator.Validate(obj); //should throw exception Assert.Fail(); // should not reach here because empty string threw exception } catch (Exception ex) { Assert.IsTrue(ex.Message.Contains("is invalid. Received value")); } }
public void TestRegExEmail() { try { //Valid value var obj = new RegExEmailTestClass() { Text = "*****@*****.**" }; Assert.IsTrue(SwagValidator.Validate(obj)); //Invalid Value obj.Text = "test.com"; SwagValidator.Validate(obj); Assert.Fail(); } catch (Exception ex) { Assert.IsTrue(ex.Message.Contains("is invalid. Received value")); } }
protected void AssertBinaryFileContent(string actualFile, string expectedFile) { var actual = File.ReadAllBytes(actualFile); var expected = File.ReadAllBytes(expectedFile); Assert.AreEqual(expected.Length, actual.Length); for (int i = 0; i < actual.Length; i++) { if (actual[i] != expected[i]) { StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); for (int j = 0; j < 8 && i + j < expected.Length; j++) { sb1.AppendFormat("{0:X2} ", expected[i + j]); sb2.AppendFormat("{0:X2} ", actual[i + j]); } Assert.Fail("Files differ at index {0}: expected <{1}> but found <{2}>", i, sb1, sb2); } } }
public void TestRegExIPAddress() { try { //Valid value var obj = new RegExIPAddressTestClass() { Text = "168.0.2.456" }; Assert.IsTrue(SwagValidator.Validate(obj)); //Invalid Value obj.Text = "1098.3456.33.44"; SwagValidator.Validate(obj); Assert.Fail(); } catch (Exception ex) { Assert.IsTrue(ex.Message.Contains("is invalid. Received value")); } }
public void StrLengthTest2() { var obj1 = new StrLengthTestClass2() { SomeString = "as44" }; Assert.IsTrue(SwagValidator.Validate(obj1)); try { var obj2 = new StrLengthTestClass2() { SomeString = "1" //too short }; SwagValidator.Validate(obj2); //should throw exception Assert.Fail(); // should not reach here because empty string threw exception } catch (Exception ex) { Assert.IsTrue(ex.Message.Contains("SomeString has length 1. Minlength is 2")); } try { var obj2 = new StrLengthTestClass2() { SomeString = "1324234234234234" //too long }; SwagValidator.Validate(obj2); //should throw exception Assert.Fail(); // should not reach here because empty string threw exception } catch (Exception ex) { Assert.IsTrue(ex.Message.Contains("SomeString has length 16. Maxlength is 5")); } }
public void CanPrependCustomMessageToDescriptionOfFailure() { object expected = new NamedObject("expected"); object actual = new NamedObject("actual"); Matcher matcher = Is.EqualTo(expected); try { Verify.That(actual, matcher, "message {0} {1}", "0", 1); } catch (ExpectationException e) { Assert.AreEqual( "message 0 1" + Environment.NewLine + "Expected: " + matcher + Environment.NewLine + "Actual: <" + actual + ">(NMockTests.NamedObject)", e.Message); return; } Assert.Fail("Verify.That should have failed"); }
public void RangeTest() { var obj1 = new RangeTestClass() { SomeInt = 5 }; Assert.IsTrue(SwagValidator.Validate(obj1)); try { var obj2 = new RangeTestClass() { SomeInt = 1 }; SwagValidator.Validate(obj2); //should throw exception Assert.Fail(); // should not reach here because empty string threw exception } catch (Exception ex) { Assert.IsTrue(ex.Message.Contains("SomeInt is invalid. Received value: 1")); } try { var obj3 = new RangeTestClass() { SomeInt = 1123123 }; SwagValidator.Validate(obj3); //should throw exception Assert.Fail(); // should not reach here because empty string threw exception } catch (Exception ex) { Assert.IsTrue(ex.Message.Contains("SomeInt is invalid. Received value: 1123123, accepted values: 3 to 8000")); } }
public void SaveNewObjectWithTransientReferenceValidateAssocInvalid() { var person = CreateNewTransientPerson(); var pet = CreateNewTransientPet(); pet.Name = Guid.NewGuid().ToString(); person.Name = "Cruella"; try { pet.Owner = person; person.Pet = pet; // for EF var adapter = AdapterFor(person); if (adapter.ValidToPersist() != null) { throw new PersistFailedException(""); } Assert.Fail(); } catch (PersistFailedException /*expected*/) {} }
public void VerifyThatFailsIfMatcherDoesNotMatchValue() { object expected = new NamedObject("expected"); object actual = new NamedObject("actual"); Matcher matcher = Is.EqualTo(expected); try { Verify.That(actual, matcher); } catch (ExpectationException e) { Assert.AreEqual( Environment.NewLine + "Expected: " + matcher + Environment.NewLine + "Actual: <" + actual + ">(NMockTests.NamedObject)", e.Message); return; } Assert.Fail("Verify.That should have failed"); }
public void AddedFeedAppearsinGetFeeds() { FeedManager.ClearFeeds(); Feed feed = new Feed(); feed.Name = "sample feed"; feed.Url = "https://example.com"; FeedManager.Add(feed); var expected = FeedManager.GetFeeds(); foreach (var item in expected) { if (item.Name.Equals(feed.Name, StringComparison.OrdinalIgnoreCase) && item.Url.Equals(feed.Url, StringComparison.OrdinalIgnoreCase)) { return; } } Assert.Fail("The feed was not returned by GetFeeds."); }
public void EditQuestionTest() { IDao manager = new DatabaseManager(); Question question = new Question(); question.QuestionId = 1; question.Content = "Tresc"; question.Answer = "Odp"; Assert.IsTrue(manager.EditQuestion(question)); Question incorrectQuestion = new Question(); Assert.IsFalse(manager.EditQuestion(incorrectQuestion)); incorrectQuestion.QuestionId = -1; try { manager.EditQuestion(incorrectQuestion); Assert.Fail(); } catch (ArgumentException) { } }