public async Task AssertMockData()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                List <WeightLiftingSet> queryResult;
                using (var context = new FittifyContext(options))
                {
                    // Todo: Investigage... all children of children are loaded
                    queryResult = context
                                  .WeightLiftingSets
                                  .Include(i => i.ExerciseHistory)
                                  .ToList();
                }

                foreach (var entity in queryResult)
                {
                    Assert.AreNotEqual(entity.ExerciseHistory, null);
                }
                Assert.AreEqual(queryResult.Count, 6);
            }
            finally
            {
                connection.Close();
            }
        }
예제 #2
0
        ////public void ResetSeededTestDb()
        ////{
        ////    using (_fittifyContext = new FittifyContext(StaticFields.TestDbFittifyConnectionString))
        ////    {
        ////        DeleteTestDb();
        ////        _fittifyContext.Database.EnsureCreated();
        ////        EnsureFreshSeedDataForTestContext(_fittifyContext);
        ////    }
        ////}

        ////public void EnsureCreatedDbProductionContext(string dbConnectionString)
        ////{
        ////    using (_fittifyContext = new FittifyContext(dbConnectionString))
        ////    {
        ////        _fittifyContext.Database.EnsureCreated();
        ////    }
        ////}

        ////public static void DeleteTestDb()
        ////{
        ////    using (var master = new SqlConnection(StaticFields.DbMasterConnectionString))
        ////    {
        ////        string fittifyDbName;
        ////        using (var fittify = new SqlConnection(StaticFields.TestDbFittifyConnectionString))
        ////        {
        ////            fittifyDbName = fittify.Database;
        ////        }
        ////        master.Open();

        ////        using (var command = master.CreateCommand())
        ////        {
        ////            // SET SINGLE_USER will close any open connections that would prevent the drop
        ////            command.CommandText
        ////                = string.Format(@"IF EXISTS (SELECT * FROM sys.databases WHERE name = N'{0}')
        ////                                BEGIN
        ////                                    ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
        ////                                    DROP DATABASE [{0}];
        ////                                END", fittifyDbName);

        ////            command.ExecuteNonQuery();
        ////        }
        ////    }
        ////}

        public bool EnsureFreshSeedDataForProductionContext(string dbConnectionString)
        {
            using (_fittifyContext = new FittifyContext(dbConnectionString))
            {
                return(Seed(_fittifyContext));
            }
        }
예제 #3
0
        public async Task CreateOwnedEntity()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var newCategory = new Category()
                    {
                        Name = "NewOwnedCategory", OwnerGuid = _ownerGuid
                    };

                    var categoryRepository = new CategoryRepository(context);
                    var createdCategory    = await categoryRepository.Create(newCategory, _ownerGuid);

                    var serializedCategory        = JsonConvert.SerializeObject(newCategory);
                    var serializedCreatedCategory = JsonConvert.SerializeObject(createdCategory);

                    Assert.AreEqual(serializedCategory, serializedCreatedCategory);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #4
0
        public async Task ReturnCorrectlyQueriedCollection()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var exercisesFromContext = await context
                                               .Exercises
                                               .Where(w => w.OwnerGuid == _ownerGuid || w.OwnerGuid == null)
                                               .Where(w => w.Name.ToLower().Contains("thexercise"))
                                               .ToListAsync();

                    var serializedExercisesFromContext = JsonConvert.SerializeObject(exercisesFromContext);

                    var resourceParameters = new ExerciseResourceParameters()
                    {
                        OwnerGuid = _ownerGuid, SearchQuery = "thexercise"
                    };
                    var exerciseRepository = new ExerciseRepository(context);
                    var exerciseCollection = await exerciseRepository.GetPagedCollection(resourceParameters);

                    var serializedCategoriesFromRepo = JsonConvert.SerializeObject(exerciseCollection);
                    Assert.AreEqual(serializedExercisesFromContext, serializedCategoriesFromRepo);
                }
            }
            finally
            {
                connection.Close();
            }
        }
        public async Task ReturnCorrectQueryByIdResult()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var entityFromContext = await context
                                            .WeightLiftingSets
                                            .Include(i => i.ExerciseHistory)
                                            .FirstOrDefaultAsync(w => w.Id == 2);

                    var serializedEntitesFromContext = JsonConvert.SerializeObject(entityFromContext,
                                                                                   new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });

                    var repo   = new WeightLiftingSetRepository(context);
                    var entity = await repo.GetById(2);

                    var serializedEntityFromRepo = JsonConvert.SerializeObject(entity,
                                                                               new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });
                    Assert.AreEqual(serializedEntitesFromContext, serializedEntityFromRepo);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #6
0
        public async Task DeleteEntityById()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                int categoryId;
                using (var context = new FittifyContext(options))
                {
                    var category = new Category();
                    context.Categories.Add(category);
                    context.SaveChanges();
                    categoryId = category.Id;

                    var categoryRepository   = new CategoryRepository(context);
                    var entityDeletionResult = await categoryRepository.Delete(categoryId);

                    Assert.AreEqual(entityDeletionResult.DidEntityExist, true);
                    Assert.AreEqual(entityDeletionResult.IsDeleted, true);
                }

                using (var context = new FittifyContext(options))
                {
                    var category = context.Categories.FirstOrDefault(f => f.Id == categoryId);

                    Assert.AreEqual(null, category);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #7
0
        CreateUniqueMockDbConnectionForThisTest(List <WorkoutHistory> listWorkoutHistories)
        {
            SqliteConnection connection = null;
            DbContextOptions <FittifyContext> options = null;

            await Task.Run(() =>
            {
                connection = new SqliteConnection("DataSource=:memory:");
                connection.Open();

                options = new DbContextOptionsBuilder <FittifyContext>()
                          .UseSqlite(connection)
                          .Options;



                using (var context = new FittifyContext(options))
                {
                    context.Database.EnsureCreated();
                    if (listWorkoutHistories != null)
                    {
                        context.AddRange(listWorkoutHistories);
                        context.SaveChanges();
                    }
                }
            });

            return(connection, options);
        }
예제 #8
0
        public async Task ReturnSecondSequenceOfPagedCategoryListCollection()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var pagedCategoriesFromContext      = context.Categories.Skip(3).Take(3).ToList();
                    var serializedCategoriesFromContext = JsonConvert.SerializeObject(pagedCategoriesFromContext);

                    var resourceParameters = new CategoryResourceParameters()
                    {
                        PageNumber = 2, PageSize = 3, OwnerGuid = _ownerGuid
                    };
                    var categoryRepository = new CategoryRepository(context);
                    var categoryCollection = await categoryRepository.GetPagedCollection(resourceParameters);

                    var serializedCategoriesFromRepo = JsonConvert.SerializeObject(categoryCollection);
                    Assert.AreEqual(serializedCategoriesFromContext, serializedCategoriesFromRepo);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #9
0
        public async Task ReturnOrderByNameDescendingThenByIdDescendingCollection()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var resourceParameters = new CategoryResourceParameters()
                    {
                        OrderBy = new List <string>()
                        {
                            "name desc", " id desc"
                        }, OwnerGuid = _ownerGuid
                    };
                    var categoryRepository = new CategoryRepository(context);
                    var categoryCollection = await categoryRepository.GetPagedCollection(resourceParameters);

                    Assert.That(categoryCollection, Is.Ordered.By(nameof(Category.Name)).Descending.Then.By(nameof(Category.Id)).Descending);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #10
0
        public async Task ReturnCategoryCollection_ForQueriedFields()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var resourceParameters = new CategoryResourceParameters()
                    {
                        Fields = "Name", DoIncludeIdsWhenQueryingSelectedFields = false
                    };
                    var categoryRepository = new CategoryRepository(context);
                    var categoryCollection =
                        await categoryRepository.GetPagedCollection(resourceParameters);

                    Assert.That(categoryCollection.Select(s => s.Id), Is.All.EqualTo(0));
                    Assert.That(categoryCollection.Select(s => s.OwnerGuid), Is.All.EqualTo(null));
                }
            }
            catch (Exception e)
            {
                var msg = e.Message;
            }
            finally
            {
                connection.Close();
            }
        }
예제 #11
0
        public static bool Seed(FittifyContext fittifyContext)
        {
            var listCategories = new List <Category>()
            {
                new Category()
                {
                    Name = "ChestSeed"
                },
                new Category()
                {
                    Name = "BackSeed"
                },
                new Category()
                {
                    Name = "LegsSeed"
                },
            };

            foreach (var category in listCategories)
            {
                if (fittifyContext.Categories.FirstOrDefault(f => f.Name == category.Name) == null)
                {
                    fittifyContext.Add(category);
                }
            }

            if (fittifyContext.SaveChanges() >= 0)
            {
                return(true);
            }

            return(false);
        }
예제 #12
0
        public async Task ReturnCollection_ForNullResourceParameters()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var defaultCategoryResourceParameters = new CategoryResourceParameters();
                    var pagedCategoriesFromContext        = await context
                                                            .Categories
                                                            .Take(defaultCategoryResourceParameters.PageSize)
                                                            .ToListAsync();

                    var serializedCategoriesFromContext = JsonConvert.SerializeObject(pagedCategoriesFromContext);

                    var categoryRepository = new CategoryRepository(context);
                    var categoryCollection = await categoryRepository.GetPagedCollection((CategoryResourceParameters)null);

                    var serializedCategoriesFromRepo = JsonConvert.SerializeObject(categoryCollection);

                    Assert.AreEqual(serializedCategoriesFromContext, serializedCategoriesFromRepo);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #13
0
        public static bool Seed(FittifyContext fittifyContext)
        {
            var exerciseHistories = fittifyContext.ExerciseHistories.Where(eH => eH.Exercise.Name == "SpinningBikeSeed").ToList();

            if (fittifyContext.CardioSets.Count() == 0)
            {
                foreach (var eH in exerciseHistories)
                {
                    fittifyContext.CardioSets.Add(new CardioSet()
                    {
                        DateTimeStart     = eH.ExecutedOnDateTime + TimeSpan.FromMinutes(0),
                        DateTimeEnd       = eH.ExecutedOnDateTime + TimeSpan.FromMinutes(5),
                        ExerciseHistoryId = eH.Id,
                        OwnerGuid         = StaticFields.TestOwnerGuid
                    });

                    fittifyContext.CardioSets.Add(new CardioSet()
                    {
                        DateTimeStart     = eH.ExecutedOnDateTime + TimeSpan.FromMinutes(60),
                        DateTimeEnd       = eH.ExecutedOnDateTime + TimeSpan.FromMinutes(80),
                        ExerciseHistoryId = eH.Id,
                        OwnerGuid         = StaticFields.TestOwnerGuid
                    });
                }
            }


            if (fittifyContext.SaveChanges() >= 0)
            {
                return(true);
            }

            return(false);
        }
        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();
            }
        }
예제 #15
0
        public async Task CorrectlyCascadeDeleteWorkoutHistories()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var repo = new WorkoutRepository(context);
                    var entityToBeDeleted            = context.Workouts.FirstOrDefault(f => f.Id == 1);
                    var exerciseHistoriesToBeChanged = context
                                                       .ExerciseHistories
                                                       .Where(w => w.PreviousExerciseHistory != null)
                                                       .Include(i => i.PreviousExerciseHistory)
                                                       .ToList();
                    var exerciseHistoryIds = exerciseHistoriesToBeChanged.Select(s => s.Id).ToList();

                    var relatedWorkoutHistory = context.WorkoutHistories.FirstOrDefault(f => f.Workout == entityToBeDeleted);

                    Assert.AreNotEqual(entityToBeDeleted, null);
                    Assert.AreNotEqual(relatedWorkoutHistory, null);
                    Assert.AreNotEqual(exerciseHistoriesToBeChanged.FirstOrDefault().PreviousExerciseHistory, null);
                    Assert.AreNotEqual(exerciseHistoriesToBeChanged.Skip(1).FirstOrDefault().PreviousExerciseHistory, null);

                    var deletionResult = await repo.Delete(entityToBeDeleted.Id);

                    if (deletionResult != null)
                    {
                        exerciseHistoriesToBeChanged = context
                                                       .ExerciseHistories
                                                       .Where(e => exerciseHistoryIds.Contains(e.Id))
                                                       .ToList();
                        entityToBeDeleted     = context.Workouts.FirstOrDefault(f => f.Id == 1);
                        relatedWorkoutHistory = context.WorkoutHistories.FirstOrDefault(f => f.Id == relatedWorkoutHistory.Id);

                        Assert.AreEqual(entityToBeDeleted, null);
                        Assert.AreEqual(relatedWorkoutHistory, null);
                        Assert.AreEqual(exerciseHistoriesToBeChanged.FirstOrDefault().PreviousExerciseHistory, null);
                        Assert.AreEqual(exerciseHistoriesToBeChanged.Skip(1).FirstOrDefault().PreviousExerciseHistory, null);
                    }
                    else
                    {
                        Assert.Fail();
                    }
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #16
0
 public static bool EnsureCreatedDbContext()
 {
     using (var fittifyContext = new FittifyContext(GetFittifyConnectionStringFromAppsettingsJson()))
     {
         ////try
         ////{
         return(fittifyContext.Database.EnsureCreated());
         ////}
         ////catch (Exception ex)
         ////{
         ////    var msg = ex.Message;
         ////}
     }
 }
예제 #17
0
        public async Task ReturnCorrectlyQueriedCollection()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var entitiesFromContext = await context
                                              .CardioSets
                                              .Where(w =>
                                                     w.OwnerGuid == _ownerGuid &&
                                                     w.ExerciseHistoryId == 2 &&
                                                     w.DateTimeStart == new DateTime(1989, 11, 01, 14, 00, 00) &&
                                                     w.DateTimeEnd == new DateTime(1989, 11, 01, 16, 00, 00))
                                              .Include(i => i.ExerciseHistory)
                                              .ToListAsync();

                    var serializedEntitesFromContext = JsonConvert.SerializeObject(entitiesFromContext,
                                                                                   new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });

                    var resourceParameters =
                        new CardioSetResourceParameters()
                    {
                        OwnerGuid         = _ownerGuid,
                        ExerciseHistoryId = 2,
                        FromDateTimeStart = new DateTime(1989, 11, 01, 14, 00, 00),
                        UntilDateTimeEnd  = new DateTime(1989, 11, 01, 16, 00, 00)
                    };
                    var repo       = new CardioSetRepository(context);
                    var collection = await repo.GetPagedCollection(resourceParameters);

                    var serializedEntitiesFromRepo = JsonConvert.SerializeObject(collection,
                                                                                 new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });

                    Assert.AreEqual(serializedEntitesFromContext, serializedEntitiesFromRepo);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #18
0
        public override Task <EntityDeletionResult <int> > Delete(int id)
        {
            var mapExerciseWorkouts = FittifyContext.MapExerciseWorkout.Where(w => w.ExerciseId == id).ToList();

            FittifyContext.RemoveRange(mapExerciseWorkouts);

            var exerciseHistories = FittifyContext.ExerciseHistories.Where(w => w.ExerciseId == id).ToList();

            foreach (var exerciseHistory in exerciseHistories)
            {
                exerciseHistory.ExerciseId = null;
                exerciseHistory.Exercise   = null;
            }

            return(base.Delete(id));
        }
예제 #19
0
        public async Task <WorkoutHistory> CreateIncludingExerciseHistories(WorkoutHistory newWorkoutHistory, Guid ownerGuid)
        {
            var workoutBluePrint = FittifyContext.Workouts.FirstOrDefault(w => w.Id == newWorkoutHistory.WorkoutId);

            if (workoutBluePrint == null && newWorkoutHistory.Workout != null)
            {
                workoutBluePrint = FittifyContext.Workouts.FirstOrDefault(w => w.Id == newWorkoutHistory.Workout.Id);
            }

            newWorkoutHistory.Workout   = workoutBluePrint;
            newWorkoutHistory.OwnerGuid = ownerGuid;
            await FittifyContext.AddAsync(newWorkoutHistory);

            await FittifyContext.SaveChangesAsync();

            var listExerciseHistories = new List <ExerciseHistory>();

            foreach (var map in FittifyContext.MapExerciseWorkout
                     .Where(map => map.WorkoutId == workoutBluePrint.Id)
                     .Include(i => i.Exercise)
                     .ToList())
            {
                var exerciseHistory = new ExerciseHistory();
                exerciseHistory.Exercise           = map.Exercise;
                exerciseHistory.WorkoutHistory     = newWorkoutHistory;
                exerciseHistory.WorkoutHistoryId   = newWorkoutHistory.Id;
                exerciseHistory.ExecutedOnDateTime = DateTime.Now;
                exerciseHistory.OwnerGuid          = ownerGuid;

                // Finding the latest non null and non-empty previous exerciseHistory
                exerciseHistory.PreviousExerciseHistory =
                    FittifyContext
                    .ExerciseHistories
                    .OrderByDescending(o => o.Id)
                    .FirstOrDefault(eH => eH.Exercise == map.Exercise &&
                                    (FittifyContext.WeightLiftingSets.OrderByDescending(o => o.Id).FirstOrDefault(wls => wls.ExerciseHistoryId == eH.Id && wls.RepetitionsFull != null) != null ||
                                     FittifyContext.CardioSets.OrderByDescending(o => o.Id).FirstOrDefault(cds => cds.ExerciseHistoryId == eH.Id && cds.DateTimeStart != null && cds.DateTimeEnd != null) != null));

                listExerciseHistories.Add(exerciseHistory);
            }

            newWorkoutHistory.ExerciseHistories = listExerciseHistories;

            await FittifyContext.SaveChangesAsync();

            return(newWorkoutHistory);
        }
        public async Task ReturnCorrectlyQueriedCollection()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var entitiesFromContext = await context
                                              .ExerciseHistories
                                              .Include(i => i.Exercise)
                                              .Where(w => w.OwnerGuid == _ownerGuid)
                                              .Where(w => w.ExerciseId == context.Exercises.FirstOrDefault(f => f.OwnerGuid != null).Id)
                                              .Where(w => w.WorkoutHistoryId ==
                                                     context.WorkoutHistories.FirstOrDefault(f => f.OwnerGuid != null).Id)
                                              .ToListAsync();

                    var serializedEntitesFromContext = JsonConvert.SerializeObject(entitiesFromContext,
                                                                                   new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });

                    var resourceParameters =
                        new ExerciseHistoryResourceParameters()
                    {
                        OwnerGuid        = _ownerGuid,
                        ExerciseId       = context.Exercises.FirstOrDefault(f => f.OwnerGuid != null).Id,
                        WorkoutHistoryId = context.WorkoutHistories.FirstOrDefault(f => f.OwnerGuid != null).Id
                    };
                    var repo       = new ExerciseHistoryRepository(context);
                    var collection = await repo.GetPagedCollection(resourceParameters);

                    var serializedEntitiesFromRepo = JsonConvert.SerializeObject(collection,
                                                                                 new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });
                    Assert.AreEqual(serializedEntitesFromContext, serializedEntitiesFromRepo);
                }
            }
            finally
            {
                connection.Close();
            }
        }
        public async Task CodeCoverage100()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var sut = new ExerciseHistoryRepository(context);
                    sut.FixRelationOfNextExerciseHistory(id: 0);
                    sut.FixRelationOfNextExerciseHistory(entity: null);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #22
0
        public async Task AssertMockData()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var queryResult = context.Exercises.ToList();

                    Assert.AreEqual(queryResult.Count, 9);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #23
0
        public async Task CorrectlyCascadeDeleteExerciseHistoriesForDeletingWorkoutHistoryHistory()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest(_listWorkoutHistories);

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var repo = new WorkoutHistoryRepository(context);
                    var entityToBeDeleted = context.WorkoutHistories.FirstOrDefault(f => f.Id == 1);
                    var exerciseHistoriesToBeCascadeDeleted = context
                                                              .ExerciseHistories
                                                              .Where(w => w.WorkoutHistoryId == entityToBeDeleted.Id)
                                                              .ToList();
                    var exerciseHistoryIds = exerciseHistoriesToBeCascadeDeleted.Select(s => s.Id).ToList();


                    Assert.AreNotEqual(entityToBeDeleted, null);
                    Assert.AreEqual(exerciseHistoriesToBeCascadeDeleted.Count, 3);

                    var deletionResult = await repo.Delete(entityToBeDeleted.Id);

                    if (deletionResult != null)
                    {
                        exerciseHistoriesToBeCascadeDeleted = context
                                                              .ExerciseHistories
                                                              .Where(e => exerciseHistoryIds.Contains(e.Id))
                                                              .ToList();
                        entityToBeDeleted = context.WorkoutHistories.FirstOrDefault(f => f.Id == 1);

                        Assert.AreEqual(entityToBeDeleted, null);
                        Assert.AreEqual(exerciseHistoriesToBeCascadeDeleted.Count, 0);
                    }
                    else
                    {
                        Assert.Fail();
                    }
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #24
0
 public async Task CreateDatabase_ForSimpleConnectenString()
 {
     await Task.Run(() =>
     {
         using (var context = new FittifyContext(
                    "Server=.\\SQLEXPRESS2016S1;Database=FittifyTestCreation;User Id=seifert-1;Password=merlin;"))
         {
             try
             {
                 Assert.That(() => context.Database.EnsureDeletedAsync(), Throws.Nothing);
                 Assert.That(() => context.Database.EnsureCreatedAsync(), Throws.Nothing);
             }
             finally
             {
                 Assert.That(() => context.Database.EnsureDeletedAsync(), Throws.Nothing);
             }
         }
     });
 }
예제 #25
0
        public async Task ReturnFalseUsingDoesEntityExist()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var categoryRepository = new CategoryRepository(context);
                    var doesEntityExist    = await categoryRepository.DoesEntityExist(42);

                    Assert.AreEqual(false, doesEntityExist);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #26
0
        public async Task ReturnSingleCategoryById()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var categoryRepository = new CategoryRepository(context);
                    var singleCategory     = await categoryRepository.GetById(1);

                    Assert.AreEqual(true, singleCategory != null);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #27
0
        public async Task ReturnFalseUsingIsEntityOwner()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var categoryRepository = new CategoryRepository(context);
                    var isEntityOwner      = await categoryRepository.IsEntityOwner(1, _ownerGuid);

                    Assert.AreEqual(false, isEntityOwner);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #28
0
        public async Task ReturnCorrectlyQueriedCollection()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var entitiesFromContext = await context
                                              .Workouts
                                              .Where(w => w.OwnerGuid == _ownerGuid)
                                              .Where(w => w.Name == "WorkoutA")
                                              .ToListAsync();

                    var serializedEntitesFromContext = JsonConvert.SerializeObject(entitiesFromContext,
                                                                                   new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });

                    var resourceParameters =
                        new WorkoutResourceParameters()
                    {
                        OwnerGuid   = _ownerGuid,
                        SearchQuery = "WorkoutA"
                    };
                    var repo       = new WorkoutRepository(context);
                    var collection = await repo.GetPagedCollection(resourceParameters);

                    var serializedEntitiesFromRepo = JsonConvert.SerializeObject(collection,
                                                                                 new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });
                    Assert.AreEqual(serializedEntitesFromContext, serializedEntitiesFromRepo);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #29
0
        public async Task NotDeleteWhenEntityDoesNotExist()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var repo           = new WorkoutRepository(context);
                    var deletionResult = await repo.Delete(0);

                    Assert.AreEqual(deletionResult.DidEntityExist, false);
                    Assert.AreEqual(deletionResult.IsDeleted, false);
                }
            }
            finally
            {
                connection.Close();
            }
        }
예제 #30
0
        public async Task FailDeletingEntityByNullEntity()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest();

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var categoryRepository   = new CategoryRepository(context);
                    var entityDeletionResult = await categoryRepository.Delete(null);

                    Assert.AreEqual(entityDeletionResult.DidEntityExist, false);
                    Assert.AreEqual(entityDeletionResult.IsDeleted, false);
                }
            }
            finally
            {
                connection.Close();
            }
        }