public ICollection <TestStepResult> Save(int testId, int testResultId, IEnumerable <TestStepResult> testStepResults) { var dbTestSteps = _db.TestSteps.AsNoTracking().ToList(); var dbTest = _db.Tests .Include(t => t.TestSteps) .Single(t => t.TestID == testId); var savedTestStepResults = new List <TestStepResult>(); foreach (var testStepResult in testStepResults) { var dbTestStepResult = new DbTestStepResult { TestResultId = testResultId, TestStepResultStartDateTime = testStepResult.StartTime, TestStepResultEndDateTime = testStepResult.EndTime, TestStepResultTypeId = Map(testStepResult.ResultType) }; // use FirstOrDefault instead of SingleOrDefault because first-run multi-threaded tests can end up inserting duplicate step names // (before the check for duplicates happens) var existingTestStep = dbTestSteps .FirstOrDefault(t => t.TestStepName == testStepResult.TestStep.Name); if (existingTestStep != null) { dbTestStepResult.TestStepId = existingTestStep.TestStepID; } else { dbTestStepResult.TestStep = new DbTestStep { TestStepName = testStepResult.TestStep.Name } }; _db.TestStepResults.Add(dbTestStepResult); _db.SaveChanges(); if (!dbTest.TestSteps.Any(t => t.TestStepID == dbTestStepResult.TestStepId)) { dbTest.TestSteps.Add( _db.TestSteps .Single(ts => ts.TestStepID == dbTestStepResult.TestStepId)); } savedTestStepResults.Add(Map(dbTestStepResult, dbTest)); } _db.SaveChanges(); return(savedTestStepResults); }
private TestStepResult Map(DbTestStepResult dbTestStepResult, DbTest dbTest) { var testStepResult = new TestStepResult { TestStep = new TestStep { TestStepID = dbTestStepResult.TestStepId, Tests = new List <Test>() }, TestResult = new TestResult { TestResultID = dbTestStepResult.TestResultId }, StartTime = dbTestStepResult.TestStepResultStartDateTime, EndTime = dbTestStepResult.TestStepResultEndDateTime, ResultType = Map(dbTestStepResult.TestStepResultTypeId) }; testStepResult.TestStep.Tests.Add( new Test { TestID = dbTest.TestID, Name = dbTest.TestName, TestSteps = new List <TestStep>() }); foreach (var testStep in dbTest.TestSteps) { testStepResult.TestStep.Tests.First().TestSteps.Add( new TestStep { TestStepID = testStep.TestStepID, Name = testStep.TestStepName }); } if (dbTestStepResult.TestStep != null) { testStepResult.TestStep.Name = dbTestStepResult.TestStep.TestStepName; } return(testStepResult); }