private TestResult Map(DbTestResult dbTestResult, int suiteId) { var testResult = new TestResult { TestResultID = dbTestResult.TestResultID, Test = new Test { TestID = dbTestResult.Test.TestID, Name = dbTestResult.Test.TestName, Suites = new List <Suite>(), Categories = new List <TestCategory>() }, SuiteResult = new SuiteResult { SuiteResultID = dbTestResult.SuiteResultId, Suite = new Suite { SuiteID = suiteId } }, ResultType = _testResultMapper.ToTestResultType(dbTestResult.TestResultTypeId), StartTime = dbTestResult.TestResultStartDateTime, EndTime = dbTestResult.TestResultEndDateTime }; if (testResult.ResultType == TestResultType.Fail) { testResult.TestFailureDetails = new TestFailureDetails { FailureDetailMessage = dbTestResult.TestFailureDetails.Count == 0 ? null : dbTestResult.TestFailureDetails.First().TestFailureDetail1, FailureType = _testResultMapper.ToTestFailureType(dbTestResult.TestFailureTypes.First().TestFailureTypeID) } } ; foreach (var dbSuite in dbTestResult.Test.Suites) { testResult.Test.Suites.Add(new Suite { SuiteID = dbSuite.SuiteID }); } foreach (var dbTestCategory in dbTestResult.Test.TestCategories) { testResult.Test.Categories.Add(new TestCategory { TestCategoryID = dbTestCategory.TestCategoryID, Name = dbTestCategory.CategoryName }); } return(testResult); } }
public void ThrowsIfSuiteResultDoesNotExist() { var mockTests = new List <DbTest>().ToDbSetMock(); mockTests.Setup(m => m.AsNoTracking()).Returns(mockTests.Object); mockTests.Setup(m => m.Include(It.IsAny <string>())).Returns(mockTests.Object); var mockTestResultMapper = new Mock <ITestResultMapper>(); mockTestResultMapper.Setup(t => t.ToDbTestResultTypeId(TestResultType.Inconclusive)).Returns(2); var mockTestCategories = new List <DbTestCategory>().ToDbSetMock(); var mockSuiteResults = new List <DbSuiteResult>().ToDbSetMock(); mockSuiteResults.Setup(m => m.AsNoTracking()).Returns(mockSuiteResults.Object); var mockContext = new Mock <ZigNetEntities>(); mockContext.Setup(m => m.Tests).Returns(mockTests.Object); mockContext.Setup(m => m.TestCategories).Returns(mockTestCategories.Object); mockContext.Setup(m => m.SuiteResults).Returns(mockSuiteResults.Object); var zignetEntitiesWrapperMock = new Mock <IDbContext>(); zignetEntitiesWrapperMock.Setup(z => z.Get()).Returns(mockContext.Object); var testResult = new TestResult { Test = new Test { Name = "test 1", Categories = new List <TestCategory>() }, SuiteResult = new SuiteResult { SuiteResultID = 3 } }; var testResultSaverService = new TestResultSaverService(zignetEntitiesWrapperMock.Object, new Mock <ILatestTestResultService>().Object, new Mock <ITemporaryTestResultService>().Object, new Mock <ITestFailureDurationService>().Object, new Mock <ITestStepService>().Object, mockTestResultMapper.Object); testResultSaverService.Save(testResult); }
public void SavesExistingTestFailedTestResult() { var mockTests = new List <DbTest> { new DbTest { TestName = "existing test", TestID = 1, TestCategories = new List <DbTestCategory>(), Suites = new List <Suite> { new Suite { SuiteID = 2 } } } }.ToDbSetMock(); mockTests.Setup(m => m.AsNoTracking()).Returns(mockTests.Object); mockTests.Setup(m => m.Include(It.IsAny <string>())).Returns(mockTests.Object); var mockTestResultMapper = new Mock <ITestResultMapper>(); mockTestResultMapper.Setup(t => t.ToDbTestResultTypeId(TestResultType.Fail)).Returns(1); mockTestResultMapper.Setup(t => t.ToTestResultType(1)).Returns(TestResultType.Fail); mockTestResultMapper.Setup(t => t.ToDbTestFailureTypeId(TestFailureType.Assertion)).Returns(1); mockTestResultMapper.Setup(t => t.ToTestFailureType(1)).Returns(TestFailureType.Assertion); var mockTestFailureTypes = new List <DbTestFailureType> { new DbTestFailureType { TestFailureTypeID = 1, TestFailureTypeName = "Assertion" } }.ToDbSetMock(); var mockTestCategories = new List <DbTestCategory>().ToDbSetMock(); var mockSuiteResults = new List <DbSuiteResult> { new DbSuiteResult { SuiteResultID = 3, SuiteId = 2 } }.ToDbSetMock(); mockSuiteResults.Setup(m => m.AsNoTracking()).Returns(mockSuiteResults.Object); var mockSuites = new List <Suite> { new Suite { SuiteID = 2 } }.ToDbSetMock(); mockSuites.Setup(m => m.AsNoTracking()).Returns(mockSuites.Object); mockSuites.Setup(m => m.Include(It.IsAny <string>())).Returns(mockSuites.Object); var mockTestResults = new List <DbTestResult>().ToDbSetMock(); var mockContext = new Mock <ZigNetEntities>(); mockContext.Setup(m => m.Tests).Returns(mockTests.Object); mockContext.Setup(m => m.TestFailureTypes).Returns(mockTestFailureTypes.Object); mockContext.Setup(m => m.TestCategories).Returns(mockTestCategories.Object); mockContext.Setup(m => m.SuiteResults).Returns(mockSuiteResults.Object); mockContext.Setup(m => m.Suites).Returns(mockSuites.Object); mockContext.Setup(m => m.TestResults).Returns(mockTestResults.Object); var zignetEntitiesWrapperMock = new Mock <IDbContext>(); zignetEntitiesWrapperMock.Setup(z => z.Get()).Returns(mockContext.Object); var now = DateTime.UtcNow; var testResult = new TestResult { Test = new Test { Name = "existing test", Categories = new List <TestCategory>() }, SuiteResult = new SuiteResult { SuiteResultID = 3 }, ResultType = TestResultType.Fail, TestFailureDetails = new TestFailureDetails { FailureType = TestFailureType.Assertion }, StartTime = now, EndTime = now.AddSeconds(1) }; var testResultSaverService = new TestResultSaverService(zignetEntitiesWrapperMock.Object, new Mock <ILatestTestResultService>().Object, new Mock <ITemporaryTestResultService>().Object, new Mock <ITestFailureDurationService>().Object, new Mock <ITestStepService>().Object, mockTestResultMapper.Object); var savedTestResult = testResultSaverService.Save(testResult); Assert.AreEqual(0, savedTestResult.TestResultID); Assert.AreEqual(1, savedTestResult.Test.TestID); Assert.AreEqual("existing test", savedTestResult.Test.Name); Assert.AreEqual(3, savedTestResult.SuiteResult.SuiteResultID); Assert.AreEqual(TestResultType.Fail, savedTestResult.ResultType); Assert.AreEqual(now, savedTestResult.StartTime); Assert.AreEqual(now.AddSeconds(1), savedTestResult.EndTime); Assert.AreEqual(TestFailureType.Assertion, savedTestResult.TestFailureDetails.FailureType); Assert.IsNull(savedTestResult.TestFailureDetails.FailureDetailMessage); Assert.AreEqual(1, savedTestResult.Test.Suites.Count); Assert.AreEqual(2, savedTestResult.Test.Suites.ToList()[0].SuiteID); Assert.AreEqual(0, savedTestResult.Test.Categories.Count); }
public void ClearsAllExistingTestCategories() { // bug here: there is no way for existing categories to be removed var mockTests = new List <DbTest> { new DbTest { TestName = "existing test", TestID = 1, TestCategories = new List <DbTestCategory> { new DbTestCategory { TestCategoryID = 4, CategoryName = "existing test category" } }, Suites = new List <Suite> { new Suite { SuiteID = 2 } } } }.ToDbSetMock(); mockTests.Setup(m => m.AsNoTracking()).Returns(mockTests.Object); mockTests.Setup(m => m.Include(It.IsAny <string>())).Returns(mockTests.Object); var mockTestResultMapper = new Mock <ITestResultMapper>(); mockTestResultMapper.Setup(t => t.ToDbTestResultTypeId(TestResultType.Inconclusive)).Returns(2); var mockTestCategories = new List <DbTestCategory> { new DbTestCategory { TestCategoryID = 4, CategoryName = "existing test category" } }.ToDbSetMock(); var mockSuiteResults = new List <DbSuiteResult> { new DbSuiteResult { SuiteResultID = 3, SuiteId = 2 } }.ToDbSetMock(); mockSuiteResults.Setup(m => m.AsNoTracking()).Returns(mockSuiteResults.Object); var mockSuites = new List <Suite> { new Suite { SuiteID = 2 } }.ToDbSetMock(); mockSuites.Setup(m => m.AsNoTracking()).Returns(mockSuites.Object); mockSuites.Setup(m => m.Include(It.IsAny <string>())).Returns(mockSuites.Object); var mockTestResults = new List <DbTestResult>().ToDbSetMock(); var mockContext = new Mock <ZigNetEntities>(); mockContext.Setup(m => m.Tests).Returns(mockTests.Object); mockContext.Setup(m => m.TestCategories).Returns(mockTestCategories.Object); mockContext.Setup(m => m.SuiteResults).Returns(mockSuiteResults.Object); mockContext.Setup(m => m.Suites).Returns(mockSuites.Object); mockContext.Setup(m => m.TestResults).Returns(mockTestResults.Object); var zignetEntitiesWrapperMock = new Mock <IDbContext>(); zignetEntitiesWrapperMock.Setup(z => z.Get()).Returns(mockContext.Object); var testResult = new TestResult { Test = new Test { Name = "existing test", Categories = new List <TestCategory>() }, SuiteResult = new SuiteResult { SuiteResultID = 3 } }; var testResultSaverService = new TestResultSaverService(zignetEntitiesWrapperMock.Object, new Mock <ILatestTestResultService>().Object, new Mock <ITemporaryTestResultService>().Object, new Mock <ITestFailureDurationService>().Object, new Mock <ITestStepService>().Object, mockTestResultMapper.Object); var savedTestResult = testResultSaverService.Save(testResult); Assert.AreEqual(0, savedTestResult.TestResultID); Assert.AreEqual(1, savedTestResult.Test.TestID); Assert.AreEqual("existing test", savedTestResult.Test.Name); Assert.AreEqual(3, savedTestResult.SuiteResult.SuiteResultID); Assert.AreEqual(TestResultType.Inconclusive, savedTestResult.ResultType); Assert.AreEqual(new DateTime(), savedTestResult.StartTime); Assert.AreEqual(new DateTime(), savedTestResult.EndTime); Assert.IsNull(savedTestResult.TestFailureDetails); Assert.AreEqual(1, savedTestResult.Test.Suites.Count); Assert.AreEqual(2, savedTestResult.Test.Suites.ToList()[0].SuiteID); // fails //Assert.AreEqual(0, testCategories.Count); }
public void DoesNotAssignTestIdWhenTestWithSameNameDoesNotExist() { var mockTests = new List <DbTest>().ToDbSetMock(); mockTests.Setup(m => m.AsNoTracking()).Returns(mockTests.Object); mockTests.Setup(m => m.Include(It.IsAny <string>())).Returns(mockTests.Object); var mockTestResultMapper = new Mock <ITestResultMapper>(); mockTestResultMapper.Setup(t => t.ToDbTestResultTypeId(TestResultType.Inconclusive)).Returns(2); var mockTestCategories = new List <DbTestCategory>().ToDbSetMock(); var mockSuiteResults = new List <DbSuiteResult> { new DbSuiteResult { SuiteResultID = 3, SuiteId = 2 } }.ToDbSetMock(); mockSuiteResults.Setup(m => m.AsNoTracking()).Returns(mockSuiteResults.Object); var mockSuites = new List <Suite> { new Suite { SuiteID = 2 } }.ToDbSetMock(); mockSuites.Setup(m => m.AsNoTracking()).Returns(mockSuites.Object); mockSuites.Setup(m => m.Include(It.IsAny <string>())).Returns(mockSuites.Object); var mockTestResults = new List <DbTestResult>().ToDbSetMock(); var mockContext = new Mock <ZigNetEntities>(); mockContext.Setup(m => m.Tests).Returns(mockTests.Object); mockContext.Setup(m => m.TestCategories).Returns(mockTestCategories.Object); mockContext.Setup(m => m.SuiteResults).Returns(mockSuiteResults.Object); mockContext.Setup(m => m.Suites).Returns(mockSuites.Object); mockContext.Setup(m => m.TestResults).Returns(mockTestResults.Object); var zignetEntitiesWrapperMock = new Mock <IDbContext>(); zignetEntitiesWrapperMock.Setup(z => z.Get()).Returns(mockContext.Object); var testResult = new TestResult { Test = new Test { Name = "new test", Categories = new List <TestCategory>() }, SuiteResult = new SuiteResult { SuiteResultID = 3 } }; var testResultSaverService = new TestResultSaverService(zignetEntitiesWrapperMock.Object, new Mock <ILatestTestResultService>().Object, new Mock <ITemporaryTestResultService>().Object, new Mock <ITestFailureDurationService>().Object, new Mock <ITestStepService>().Object, mockTestResultMapper.Object); var savedTestResult = testResultSaverService.Save(testResult); Assert.AreEqual(0, savedTestResult.TestResultID); Assert.AreEqual(0, savedTestResult.Test.TestID); Assert.AreEqual("new test", savedTestResult.Test.Name); Assert.AreEqual(3, savedTestResult.SuiteResult.SuiteResultID); Assert.AreEqual(TestResultType.Inconclusive, savedTestResult.ResultType); Assert.AreEqual(new DateTime(), savedTestResult.StartTime); Assert.AreEqual(new DateTime(), savedTestResult.EndTime); Assert.IsNull(savedTestResult.TestFailureDetails); Assert.AreEqual(1, savedTestResult.Test.Suites.Count); Assert.AreEqual(2, savedTestResult.Test.Suites.ToList()[0].SuiteID); Assert.AreEqual(0, savedTestResult.Test.Categories.Count); }
public TestResult Save(TestResult testResult) { var existingTest = _db.Tests .AsNoTracking() .Include(t => t.TestCategories) .Select(t => new Test { TestID = t.TestID, Name = t.TestName, Categories = t.TestCategories.Select(tc => new TestCategory { TestCategoryID = tc.TestCategoryID, Name = tc.CategoryName }).ToList() } ) .SingleOrDefault(t => t.Name == testResult.Test.Name); if (existingTest != null) { testResult.Test.TestID = existingTest.TestID; testResult.Test.Categories = testResult.Test.Categories.Concat(existingTest.Categories).ToList(); } var dbTestResult = new DbTestResult { SuiteResultId = testResult.SuiteResult.SuiteResultID, TestResultStartDateTime = testResult.StartTime, TestResultEndDateTime = testResult.EndTime, TestResultTypeId = _testResultMapper.ToDbTestResultTypeId(testResult.ResultType) }; if (testResult.ResultType == TestResultType.Fail) { var testFailureTypeId = _testResultMapper.ToDbTestFailureTypeId(testResult.TestFailureDetails.FailureType); var testFailureType = _db.TestFailureTypes.Single(t => t.TestFailureTypeID == testFailureTypeId); dbTestResult.TestFailureTypes.Add(testFailureType); if (!string.IsNullOrWhiteSpace(testResult.TestFailureDetails.FailureDetailMessage)) { dbTestResult.TestFailureDetails.Add( new DbTestFailureDetail { TestFailureDetail1 = testResult.TestFailureDetails.FailureDetailMessage }); } } if (testResult.Test.TestID != 0) { dbTestResult.Test = _db.Tests .Include(t => t.Suites) .Single(t => t.TestID == testResult.Test.TestID); } else { dbTestResult.Test = new DbTest { TestName = testResult.Test.Name, TestCategories = new List <DbTestCategory>() } }; dbTestResult.Test.TestCategories.Clear(); var dbTestCategories = _db.TestCategories.ToList(); foreach (var testCategory in testResult.Test.Categories) { // use FirstOrDefault instead of SingleOrDefault because first-run multi-threaded tests end up inserting duplicate categories // (before the check for duplicates happens) var existingDbTestCategory = dbTestCategories .FirstOrDefault(c => c.CategoryName == testCategory.Name); if (existingDbTestCategory != null) { dbTestResult.Test.TestCategories.Add(existingDbTestCategory); } else { dbTestResult.Test.TestCategories.Add(new DbTestCategory { CategoryName = testCategory.Name }); } } var suiteResult = _db.SuiteResults .AsNoTracking() .Single(sr => sr.SuiteResultID == testResult.SuiteResult.SuiteResultID); if (!dbTestResult.Test.Suites.Any(s => s.SuiteID == suiteResult.SuiteId)) { dbTestResult.Test.Suites.Add( _db.Suites .Single(s => s.SuiteID == suiteResult.SuiteId)); } _db.TestResults.Add(dbTestResult); _db.SaveChanges(); var savedTestResult = Map(dbTestResult, suiteResult.SuiteId); _testStepService.Save(savedTestResult.Test.TestID, savedTestResult.TestResultID, testResult.TestStepResults); _temporaryTestResultService.Save(_testResultMapper.ToTemporaryTestResult(savedTestResult)); var utcNow = DateTime.UtcNow; savedTestResult.SuiteResult.Suite.Name = _db.Suites .AsNoTracking() .Single(s => s.SuiteID == suiteResult.SuiteId) .SuiteName; _latestTestResultService.Save( _testResultMapper.ToLatestTestResult(savedTestResult), savedTestResult.ResultType, utcNow); _testFailureDurationService.Save( _testResultMapper.ToTestFailureDuration(savedTestResult), savedTestResult.ResultType, utcNow); return(savedTestResult); }