public async Task CorrectlyCascadeDeleteExerciseHistoryHistories_WhenNoPreviousAndNextEntityExist() { var(connection, options) = await CreateUniqueMockDbConnectionForThisTest(); try { using (var context = new FittifyContext(options)) { var repo = new ExerciseHistoryRepository(context); var entityToBeDeleted = context.ExerciseHistories.FirstOrDefault(f => f.Id == 1); var weightLiftingSetsToBeCascadeDeleted = context.WeightLiftingSets.Where(w => w.ExerciseHistoryId == entityToBeDeleted.Id).ToList(); var cardioSetsToBeCascadeDeleted = context.WeightLiftingSets.Where(w => w.ExerciseHistoryId == entityToBeDeleted.Id).ToList(); var exerciseHistoryToBeChanged = context .ExerciseHistories .Include(i => i.PreviousExerciseHistory) .FirstOrDefault(w => w.PreviousExerciseHistory == entityToBeDeleted); Assert.AreNotEqual(entityToBeDeleted, null); Assert.AreNotEqual(exerciseHistoryToBeChanged, null); Assert.AreEqual(weightLiftingSetsToBeCascadeDeleted.Count(), 2); Assert.AreEqual(cardioSetsToBeCascadeDeleted.Count(), 2); var deletionResult = await repo.Delete(entityToBeDeleted.Id); if (deletionResult != null) { weightLiftingSetsToBeCascadeDeleted = context.WeightLiftingSets.Where(w => w.ExerciseHistoryId == entityToBeDeleted.Id).ToList(); cardioSetsToBeCascadeDeleted = context.WeightLiftingSets.Where(w => w.ExerciseHistoryId == entityToBeDeleted.Id).ToList(); exerciseHistoryToBeChanged = context .ExerciseHistories .FirstOrDefault(f => f.PreviousExerciseHistory == null); entityToBeDeleted = context.ExerciseHistories.FirstOrDefault(f => f.Id == 1); Assert.AreEqual(entityToBeDeleted, null); Assert.AreEqual(weightLiftingSetsToBeCascadeDeleted.Count, 0); Assert.AreEqual(cardioSetsToBeCascadeDeleted.Count, 0); Assert.AreNotEqual(exerciseHistoryToBeChanged, null); Assert.AreEqual(exerciseHistoryToBeChanged.PreviousExerciseHistory, null); } else { Assert.Fail(); } } } finally { connection.Close(); } }
public async Task FailDeleteForNonExistingEntity() { var(connection, options) = await CreateUniqueMockDbConnectionForThisTest(); try { using (var context = new FittifyContext(options)) { var sut = new ExerciseHistoryRepository(context); var deletionResult = await sut.Delete(0); Assert.AreEqual(deletionResult.DidEntityExist, false); Assert.AreEqual(deletionResult.IsDeleted, false); } } finally { connection.Close(); } }
public async Task CorrectlyCascadeDeleteExerciseHistoryHistories_WhenPreviousAndNextEntityExist() { var(connection, options) = await CreateUniqueMockDbConnectionForThisTest(); try { using (var context = new FittifyContext(options)) { var repo = new ExerciseHistoryRepository(context); var entityToBeDeleted = context.ExerciseHistories.FirstOrDefault(f => f.Id == 2); var weightLiftingSetsToBeCascadeDeleted = context.WeightLiftingSets.Where(w => w.ExerciseHistoryId == entityToBeDeleted.Id).ToList(); var cardioSetsToBeCascadeDeleted = context.WeightLiftingSets.Where(w => w.ExerciseHistoryId == entityToBeDeleted.Id).ToList(); var exerciseHistoryToBeChanged = context .ExerciseHistories .Include(i => i.PreviousExerciseHistory) .FirstOrDefault(w => w.PreviousExerciseHistory == entityToBeDeleted); var newPreviousExerciseHistory = context.ExerciseHistories.FirstOrDefault(f => f.Id == 1); var formerPreviousExerciseHistory = JsonConvert.SerializeObject(exerciseHistoryToBeChanged.PreviousExerciseHistory, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); Assert.AreNotEqual(entityToBeDeleted, null); Assert.AreNotEqual(exerciseHistoryToBeChanged, null); Assert.AreEqual(weightLiftingSetsToBeCascadeDeleted.Count(), 2); Assert.AreEqual(cardioSetsToBeCascadeDeleted.Count(), 2); var deletionResult = await repo.Delete(entityToBeDeleted.Id); if (deletionResult != null && deletionResult.DidEntityExist == true && deletionResult.IsDeleted) { weightLiftingSetsToBeCascadeDeleted = context.WeightLiftingSets.Where(w => w.ExerciseHistoryId == entityToBeDeleted.Id).ToList(); cardioSetsToBeCascadeDeleted = context.WeightLiftingSets.Where(w => w.ExerciseHistoryId == entityToBeDeleted.Id).ToList(); exerciseHistoryToBeChanged = context .ExerciseHistories .FirstOrDefault(f => f.PreviousExerciseHistory == newPreviousExerciseHistory); entityToBeDeleted = context.ExerciseHistories.FirstOrDefault(f => f.Id == 2); var latterPreviousExerciseHistory = JsonConvert.SerializeObject(exerciseHistoryToBeChanged.PreviousExerciseHistory, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); Assert.AreEqual(entityToBeDeleted, null); Assert.AreEqual(weightLiftingSetsToBeCascadeDeleted.Count, 0); Assert.AreEqual(cardioSetsToBeCascadeDeleted.Count, 0); Assert.AreNotEqual(exerciseHistoryToBeChanged, null); Assert.AreNotEqual(formerPreviousExerciseHistory, latterPreviousExerciseHistory); } else { Assert.Fail(); } } } finally { connection.Close(); } }