예제 #1
0
        public async Task GetWorkoutIdFromDateShouldReturnCorrectWorkoutId()
        {
            var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var dbContext = new ApplicationDbContext(optionsBuilder.Options);

            var workoutService = new WorkoutsService(dbContext);

            var workoutToAdd = new WorkoutInputModel
            {
                Date = DateTime.Now.Date,
            };

            await workoutService.CreateAsync(workoutToAdd, "Icaka99");

            var result = workoutService.GetWorkoutIdFromDate(DateTime.Now.Date.ToString("yy-MM-dd"));

            Assert.Equal(1, result);
        }
예제 #2
0
        public async Task CreateExerciseMethodShouldAddCorrectNewExerciseToDbAndWorkout()
        {
            var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var dbContext = new ApplicationDbContext(optionsBuilder.Options);

            var workoutService = new WorkoutsService(dbContext);

            var workoutToAdd = new WorkoutInputModel
            {
                Date     = DateTime.Now.Date,
                Duration = 180,
                TypeId   = 2,
            };

            await workoutService.CreateAsync(workoutToAdd, "Icaka99");

            var sets = new List <SetInputModel>
            {
                new SetInputModel {
                    ExerciseId = 2, Reps = 1,
                },
            };

            var exerciseToAdd = new ExerciseInputModel
            {
                MuscleGroupId = 1,
                Name          = "testName",
                WorkoutId     = 1,
                Sets          = sets,
            };

            await workoutService.CreateExerciseAsync(exerciseToAdd);

            Assert.True(dbContext.Exercises.Any());
            Assert.Equal(2, dbContext.Exercises.FirstOrDefault().Id);
            Assert.Equal(1, dbContext.Exercises.FirstAsync().Result.MuscleGroupId);
            Assert.Equal("testName", dbContext.Exercises.FirstAsync().Result.Name);
            Assert.True(dbContext.Exercises.FirstAsync().Result.Sets.Any());
            Assert.Equal(1, dbContext.Exercises.FirstAsync().Result.Sets.FirstOrDefault().Reps);
            Assert.Equal(2, dbContext.Exercises.FirstAsync().Result.Sets.FirstOrDefault().ExerciseId);
        }
예제 #3
0
        public async Task CreateMethodShouldAddCorrectNewWorkoutToDb()
        {
            var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var dbContext = new ApplicationDbContext(optionsBuilder.Options);

            var workoutService = new WorkoutsService(dbContext);

            var workoutToAdd = new WorkoutInputModel
            {
                Date     = DateTime.Now.Date,
                Duration = 180,
                TypeId   = 2,
            };

            await workoutService.CreateAsync(workoutToAdd, "Icaka99");

            Assert.True(dbContext.Workouts.Any());
            Assert.Equal(DateTime.Now.Date, dbContext.Workouts.FirstAsync().Result.Date);
            Assert.Equal(180, dbContext.Workouts.FirstAsync().Result.Duration.TotalMinutes);
            Assert.Equal(2, dbContext.Workouts.FirstAsync().Result.TypeId);
            Assert.Equal("Icaka99", dbContext.Workouts.FirstAsync().Result.UserId);
        }
        public async Task AddWorkoutShouldCreateWorkoutAndAddItToCoach()
        {
            var workouts = new List <Workout>
            {
                new Workout
                {
                    Id          = "workoutId123",
                    Name        = "Workout One",
                    Description = "Some kind of workout description",
                    Position    = new Position
                    {
                        Id          = "PositionOne",
                        Name        = PositionName.ShootingGuard,
                        Description = "Shooting guard position",
                        Playstyle   = "You play like a shooting guard",
                    },
                    VideoUrl   = "test youtube link",
                    PositionId = "PositionOne",
                    Picture    = new Picture {
                        Id = "pic", Url = "test url"
                    },
                    PictureId    = "pic",
                    ImageUrl     = "testimg",
                    AddedByCoach = new Coach
                    {
                        Id          = "c1",
                        Name        = "Coach1",
                        Description = "desc1",
                        Experience  = 2,
                        Phone       = "321312312",
                        Email       = "*****@*****.**",
                        User        = new ApplicationUser {
                            Id = "coachuser"
                        },
                        UserId  = "coachuser",
                        Picture = new Picture {
                            Id = "cpic", Url = "test xurl"
                        },
                        PictureId = "cpic",
                    },
                },
            };

            this.positionsRepository.Setup(r => r.AllAsNoTracking()).Returns(() => new List <Position>
            {
                new Position
                {
                    Id          = "PositionOne",
                    Name        = PositionName.ShootingGuard,
                    Description = "Shooting guard position",
                    Playstyle   = "You play like a shooting guard",
                },
            }.AsQueryable());
            var positionsService = new PositionsService(this.positionsRepository.Object);

            this.coachRepository.Setup(r => r.AllAsNoTracking()).Returns(() => new List <Coach>
            {
                new Coach
                {
                    Id          = "c1",
                    Name        = "Coach1",
                    Description = "desc1",
                    Experience  = 2,
                    Phone       = "321312312",
                    Email       = "*****@*****.**",
                    User        = new ApplicationUser {
                        Id = "coachuser"
                    },
                    UserId  = "coachuser",
                    Picture = new Picture {
                        Id = "cpic", Url = "test xurl"
                    },
                    PictureId = "cpic",
                },
            }.AsQueryable());

            var coachesService = new CoachesService(this.coachRepository.Object, this.moqCloudinaryService.Object);

            this.workoutsRepository.Setup(r => r.AllAsNoTracking()).Returns(() => workouts.AsQueryable());
            var service = new WorkoutsService(this.moqCloudinaryService.Object, this.workoutsRepository.Object, positionsService, coachesService);

            // Arrange
            var fileMock = new Mock <IFormFile>();

            // Setup mock file using a memory stream
            var content  = "Hello World from a Fake File";
            var fileName = "test.pdf";
            var ms       = new MemoryStream();
            var writer   = new StreamWriter(ms);

            writer.Write(content);
            writer.Flush();
            ms.Position = 0;
            fileMock.Setup(_ => _.OpenReadStream()).Returns(ms);
            fileMock.Setup(_ => _.FileName).Returns(fileName);
            fileMock.Setup(_ => _.Length).Returns(ms.Length);

            var file = fileMock.Object;

            this.workoutsRepository.Setup(r => r.AddAsync(It.IsAny <Workout>())).Callback((Workout workout) => workouts.Add(workout));

            var inputModel = new CreateWorkoutInputModel
            {
                Name         = "Workout for beginners",
                Description  = "This workout is for beginners and it will help them",
                PositionName = PositionName.ShootingGuard,
                VideoUrl     = "testvideourl",
                Image        = file,
            };

            await service.CreateAsync(inputModel, "coachuser");

            Assert.Contains(workouts, x => x.Name == "Workout for beginners");
            Assert.Equal(2, workouts.Count);
            this.workoutsRepository.Verify(x => x.AllAsNoTracking(), Times.Never);
        }