public async Task CorrectlyCreateWorkoutHistoryWithExerciseHistories() { var(connection, options) = await CreateUniqueMockDbConnectionForThisTest(null); try { using (var context = new FittifyContext(options)) { var listWorkouts = new List <Workout>() { new Workout() { Name = "WorkoutA", OwnerGuid = _ownerGuid }, new Workout() { Name = "WorkoutB", OwnerGuid = _ownerGuid }, new Workout() { Name = "WorkoutC", OwnerGuid = null }, new Workout() { Name = "WorkoutD", OwnerGuid = null } }; var listExercises = new List <Exercise>() { new Exercise() { Name = "ExerciseA", OwnerGuid = _ownerGuid }, new Exercise() { Name = "ExerciseB", OwnerGuid = _ownerGuid }, new Exercise() { Name = "ExerciseC", OwnerGuid = null }, new Exercise() { Name = "ExerciseD", OwnerGuid = null }, }; var listMapExerciseWorkouts = new List <MapExerciseWorkout>(); foreach (var workout in listWorkouts) { foreach (var exercise in listExercises) { if (workout.OwnerGuid != null && exercise.OwnerGuid != null && workout.OwnerGuid == exercise.OwnerGuid) { listMapExerciseWorkouts.Add(new MapExerciseWorkout() { OwnerGuid = _ownerGuid, Workout = workout, Exercise = exercise }); } else if (workout.OwnerGuid == null && exercise.OwnerGuid == null && workout.OwnerGuid == exercise.OwnerGuid) { listMapExerciseWorkouts.Add(new MapExerciseWorkout() { OwnerGuid = null, Workout = workout, Exercise = exercise }); } // we don't want Guid && null or null && Guid } } context.MapExerciseWorkout.AddRange(listMapExerciseWorkouts); context.SaveChanges(); } using (var context = new FittifyContext(options)) { //var Workouts = context.Workouts.ToList(); var newWorkoutHistory = new WorkoutHistory() { Workout = context.Workouts.ToList().FirstOrDefault(f => f.OwnerGuid != null) }; var repo = new WorkoutHistoryRepository(context); var newlyCreatedWorkoutHistory = await repo.CreateIncludingExerciseHistories(newWorkoutHistory, _ownerGuid); var serializedNewlyCreatedWorkoutHistoryReturnedFromRepo = JsonConvert.SerializeObject(newlyCreatedWorkoutHistory, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); var newlyCreatedWorkoutHistoryFromContext = context .WorkoutHistories .Include(i => i.ExerciseHistories) .Include(i => i.Workout) .FirstOrDefault(f => f.OwnerGuid != null); var serializedNewlyCreatedWorkoutHistoryReturnedFromContext = JsonConvert.SerializeObject(newlyCreatedWorkoutHistory, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); Assert.AreEqual(serializedNewlyCreatedWorkoutHistoryReturnedFromRepo, serializedNewlyCreatedWorkoutHistoryReturnedFromContext); Assert.AreEqual(newlyCreatedWorkoutHistory.ExerciseHistories.Count(), 2); Assert.AreEqual(newlyCreatedWorkoutHistoryFromContext.ExerciseHistories.Count(), 2); foreach (var exerciseHistory in newlyCreatedWorkoutHistory.ExerciseHistories) { Assert.AreEqual(exerciseHistory.PreviousExerciseHistory, null); } } using (var context = new FittifyContext(options)) { var existingExerciseHistories = context.ExerciseHistories.Where(w => w.OwnerGuid == _ownerGuid).ToList(); existingExerciseHistories.FirstOrDefault().WeightLiftingSets = new List <WeightLiftingSet>() { new WeightLiftingSet() { RepetitionsFull = 30 } }; existingExerciseHistories.Skip(1).FirstOrDefault().CardioSets = new List <CardioSet>() { new CardioSet() { DateTimeStart = new DateTime(1989, 11, 01, 13, 10, 00), DateTimeEnd = new DateTime(1989, 11, 01, 13, 50, 00) } }; //var Workouts = context.Workouts.ToList(); var newWorkoutHistory = new WorkoutHistory() { Workout = context.Workouts.ToList().FirstOrDefault(f => f.OwnerGuid != null) }; var repo = new WorkoutHistoryRepository(context); var newlyCreatedWorkoutHistory = await repo.CreateIncludingExerciseHistories(newWorkoutHistory, _ownerGuid); foreach (var exerciseHistory in newlyCreatedWorkoutHistory.ExerciseHistories) { Assert.AreNotEqual(exerciseHistory.PreviousExerciseHistory, null); } } } finally { connection.Close(); } }