Пример #1
0
        public void EditShouldWorkProperly(string specialityName, bool isDeleted)
        {
            //// Arange
            AutoMapperConfig.RegisterMappings(typeof(SpecialityViewModel).GetTypeInfo().Assembly);

            var options = new DbContextOptionsBuilder <S2DentDbContext>().UseInMemoryDatabase(nameof(EditShouldWorkProperly)).Options;

            using var context = new S2DentDbContext(options);
            var service = new SpecialitiesService(context);

            var speciality = new Speciality {
                Id = 1, Name = "Dentist", IsDeleted = false
            };

            context.Specialities.Add(speciality);
            context.SaveChangesAsync().GetAwaiter().GetResult();

            var editSpeciality = new Speciality {
                Id = 1, Name = specialityName, IsDeleted = isDeleted
            };

            //// Act
            service.Edit(editSpeciality).GetAwaiter().GetResult();

            //// Assert
            var specialityModel = context.Specialities.FirstOrDefault(x => x.Id == 1);

            Assert.AreEqual(specialityName, specialityModel.Name);
            Assert.AreEqual(isDeleted, specialityModel.IsDeleted);
        }
Пример #2
0
        public void GetOneShouldReturnTheRightData(int id, string name)
        {
            //// Arrange
            AutoMapperConfig.RegisterMappings(
                typeof(SpecialityViewModel).GetTypeInfo().Assembly);

            var options = new DbContextOptionsBuilder <S2DentDbContext>()
                          .UseInMemoryDatabase(nameof(GetOneShouldReturnTheRightData))
                          .Options;

            var speciality = new Speciality
            {
                Id   = id,
                Name = name,
            };

            using (var context = new S2DentDbContext(options))
            {
                context.Specialities.Add(speciality);
                context.SaveChanges();
            }

            using (var context = new S2DentDbContext(options))
            {
                //// Act
                var service     = new SpecialitiesService(context);
                var resultModel = service.GetOne <SpecialityViewModel>(id).GetAwaiter().GetResult();

                //// Assert
                Assert.AreEqual(speciality.Id, resultModel.Id);
                Assert.AreEqual(speciality.Name, resultModel.Name);
                Assert.AreEqual(true, resultModel is SpecialityViewModel);
            }
        }
Пример #3
0
        public void EditShouldThrowError()
        {
            //// Arange
            AutoMapperConfig.RegisterMappings(typeof(SpecialityViewModel).GetTypeInfo().Assembly);

            var options = new DbContextOptionsBuilder <S2DentDbContext>().UseInMemoryDatabase(nameof(EditShouldThrowError)).Options;

            using var context = new S2DentDbContext(options);
            var service = new SpecialitiesService(context);

            var speciality = new Speciality {
                Id = 1, Name = "Dentist", IsDeleted = false
            };

            context.Specialities.Add(speciality);
            context.SaveChangesAsync().GetAwaiter().GetResult();

            var editSpeciality = new Speciality {
                Id = 2, Name = "Anesthesiologis", IsDeleted = false
            };

            //// Assert
            Assert.That(() =>
                        service.Edit(editSpeciality).GetAwaiter().GetResult(),
                        Throws.TypeOf <ArgumentException>().With.Message.EqualTo("Speciality does not exist."));
        }
Пример #4
0
        public void GetAllShouldReturnIcollectionInputModel()
        {
            //// Arange
            AutoMapperConfig.RegisterMappings(
                typeof(SpecialityViewModel).GetTypeInfo().Assembly);

            var options = new DbContextOptionsBuilder <S2DentDbContext>()
                          .UseInMemoryDatabase(nameof(GetAllShouldReturnIcollectionInputModel))
                          .Options;

            using var context = new S2DentDbContext(options);
            var service = new SpecialitiesService(context);

            var specialityModels = new List <Speciality>
            {
                new Speciality {
                    Id = 1, Name = "Dentist"
                },
                new Speciality {
                    Id = 2, Name = "Anesthesiologist"
                },
            };

            context.Specialities.AddRange(specialityModels);
            context.SaveChanges();

            //// Act
            var resultSpecialities = service.GetAll <SpecialityInputModel>().GetAwaiter().GetResult();

            //// Assert
            Assert.AreEqual(true, resultSpecialities is ICollection <SpecialityInputModel>);
        }
Пример #5
0
        public void CreateShouldThrowErrorWithSameId()
        {
            //// Arange
            AutoMapperConfig.RegisterMappings(
                typeof(SpecialityViewModel).GetTypeInfo().Assembly);

            var options = new DbContextOptionsBuilder <S2DentDbContext>()
                          .UseInMemoryDatabase(nameof(CreateShouldThrowErrorWithSameName))
                          .Options;

            using var context = new S2DentDbContext(options);
            var service    = new SpecialitiesService(context);
            var speciality = new Speciality {
                Id = 1, Name = "TestName"
            };

            context.Specialities.Add(speciality);
            context.SaveChanges();

            //// Act
            var testSpeciality = new Speciality {
                Id = 1, Name = "TestSpeciality"
            };


            //// Assert
            Assert.That(() =>
                        service.Create(testSpeciality).GetAwaiter().GetResult(),
                        Throws.TypeOf <ArgumentException>().With.Message.EqualTo("Speciality already exists."));
        }
Пример #6
0
        public void GetAllShouldWorkProperly()
        {
            //// Arrange
            AutoMapperConfig.RegisterMappings(
                typeof(SpecialityViewModel).GetTypeInfo().Assembly);

            var options = new DbContextOptionsBuilder <S2DentDbContext>()
                          .UseInMemoryDatabase(nameof(GetAllShouldWorkProperly))
                          .Options;
            var specialityModels = new List <Speciality>()
            {
                new Speciality {
                    Id = 1, Name = "Anesthesiologist", IsDeleted = false
                },
                new Speciality {
                    Id = 2, Name = "Orthodontologist", IsDeleted = false
                },
                new Speciality {
                    Id = 3, Name = "Dentists", IsDeleted = true
                },
            };

            using (var context = new S2DentDbContext(options))
            {
                context.Specialities.AddRange(specialityModels);
                context.SaveChanges();
            }

            using (var context = new S2DentDbContext(options))
            {
                //// Assert
                var service      = new SpecialitiesService(context);
                var specialities = service.GetAll <SpecialityViewModel>().GetAwaiter().GetResult();

                Assert.AreEqual(specialityModels[0].Name, specialities.FirstOrDefault(x => x.Id == 1).Name);
                Assert.AreEqual(specialityModels[1].Name, specialities.FirstOrDefault(x => x.Id == 2).Name);
                Assert.AreEqual(2, specialities.Count());
            }
        }
Пример #7
0
        public void CreateShouldCreateOject(string name)
        {
            //// Arange
            AutoMapperConfig.RegisterMappings(
                typeof(SpecialityViewModel).GetTypeInfo().Assembly);

            var options = new DbContextOptionsBuilder <S2DentDbContext>()
                          .UseInMemoryDatabase(nameof(CreateShouldCreateOject))
                          .Options;

            using var context = new S2DentDbContext(options);
            var service    = new SpecialitiesService(context);
            var speciality = new Speciality {
                Name = name
            };

            //// Act
            service.Create(speciality).GetAwaiter().GetResult();

            //// Assert
            Assert.AreEqual(true, context.Specialities.Any(x => x.Name == speciality.Name));
        }
Пример #8
0
        public void GetOneShouldReturnTheRightType()
        {
            //// Arrange
            AutoMapperConfig.RegisterMappings(
                typeof(SpecialityViewModel).GetTypeInfo().Assembly);

            var options = new DbContextOptionsBuilder <S2DentDbContext>()
                          .UseInMemoryDatabase(nameof(GetOneShouldReturnTheRightType))
                          .Options;

            using (var context = new S2DentDbContext(options))
            {
                var speciality = new Speciality
                {
                    Id   = 2,
                    Name = "Dentist",
                };

                if (!context.Specialities.Any(x => x.Name == speciality.Name))
                {
                    context.Specialities.Add(speciality);
                    context.SaveChanges();
                }
            }

            using (var context = new S2DentDbContext(options))
            {
                //// Act
                var service    = new SpecialitiesService(context);
                var viewModel  = service.GetOne <SpecialityViewModel>(2).GetAwaiter().GetResult();
                var inputModel = service.GetOne <SpecialityInputModel>(2).GetAwaiter().GetResult();

                //// Assert
                Assert.AreEqual(true, viewModel is SpecialityViewModel);
                Assert.AreEqual(true, inputModel is SpecialityInputModel);
            }
        }
Пример #9
0
        public void GetOneShouldThrowException(
            [Range(-1, 1)] int intId)
        {
            //// Arrange
            AutoMapperConfig.RegisterMappings(
                typeof(SpecialityViewModel).GetTypeInfo().Assembly);

            var options = new DbContextOptionsBuilder <S2DentDbContext>()
                          .UseInMemoryDatabase(nameof(GetOneShouldThrowException))
                          .Options;

            using (var context = new S2DentDbContext(options))
            {
                var speciality = new Speciality
                {
                    Id   = 2,
                    Name = "Dentist",
                };

                if (!context.Specialities.Any(x => x.Name == speciality.Name))
                {
                    context.Specialities.Add(speciality);
                    context.SaveChanges();
                }
            }

            using (var context = new S2DentDbContext(options))
            {
                var service = new SpecialitiesService(context);

                //// Assert
                Assert.That(
                    () => service.GetOne <SpecialityViewModel>(intId).GetAwaiter().GetResult(),
                    Throws.TypeOf <ArgumentException>().With.Message.EqualTo("Speciality does not exist."));
            }
        }
Пример #10
0
 public SpecialitiesController(IRepository <Speciality> repository, SpecialitiesService service)
 {
     _repository = repository ?? throw new ArgumentNullException(nameof(repository));
     _service    = service ?? throw new ArgumentNullException(nameof(service));
 }