Ejemplo n.º 1
0
        public async Task CreateShouldSucceed()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "CreatePropertiesTestDb").Options;

            using var dbContext          = new ApplicationDbContext(options);
            using var propertyRepository = new EfDeletableEntityRepository <Property>(dbContext);
            var propertiesService = new PropertiesService(propertyRepository);

            var model = new CreatePropertiesViewModel()
            {
                Address = "33300 Main st",
                Name    = "Peter",
                Owner   = "Ivan Ivanov",
                Size    = 110,
                Type    = PMStudio.Data.Models.Enum.PropertyType.Residential,
                Images  = new List <IFormFile>(),
            };

            await propertiesService.CreateAsync(model, @"C:\PM.Studio.Images");

            var createdModel = dbContext.Properties.FirstOrDefault(p => p.Name == "Peter");

            Assert.NotNull(createdModel);
        }
        public async Task CreateAsync_SouldReturn_True_IfInputDataIsCorrectFromDb()
        {
            var db     = this.GetDatabase();
            var mapper = this.GetMapper();


            var firstProp = new Property
            {
                Id       = 3,
                Name     = "Голям склад",
                Area     = 400,
                IsActual = true
            };
            var companyOne = new Company
            {
                Id         = 1,
                Name       = "First",
                Properties = new List <Property>()
                {
                    firstProp
                }
            };
            await db.Companies.AddRangeAsync(companyOne);

            await db.SaveChangesAsync();

            var propertyService = new PropertiesService(mapper, db);
            var model           = new CreatePropertyModel
            {
                CompanyId       = 1,
                PropertyName    = "Office",
                Description     = "No Descriprion",
                Area            = 80,
                Type            = PropertyType.Office,
                PropertyAddress = "Mars"
            };

            //Act
            var result = await propertyService.CreateAsync(model);

            var addedProperty = await db.Properties.FirstOrDefaultAsync(x => x.Name == "Office");

            //Assert
            result
            .Should()
            .BeTrue();

            addedProperty
            .Should()
            .NotBeNull()
            .And
            .BeOfType <Property>()
            .And
            .Match <Property>(p => p.Type == PropertyType.Office &&
                              p.Address == "Mars" &&
                              p.Area == 80 &&
                              p.CompanyId == 1);
        }
        public async Task CreateAsync_SouldReturn_False_IfCompanyDoNotExistsDb()
        {
            var db     = this.GetDatabase();
            var mapper = this.GetMapper();

            var firstProp = new Property
            {
                Id       = 3,
                Name     = "Голям склад",
                Area     = 400,
                IsActual = true
            };
            var companyOne = new Company
            {
                Id         = 1,
                Name       = "First",
                Properties = new List <Property>()
                {
                    firstProp
                }
            };
            await db.Companies.AddRangeAsync(companyOne);

            await db.SaveChangesAsync();

            var propertyService = new PropertiesService(mapper, db);

            var model = new CreatePropertyModel
            {
                CompanyId       = 2, // ID do not exsist
                PropertyName    = "Office",
                Description     = "No Descriprion",
                Area            = 80,
                Type            = PropertyType.Office,
                PropertyAddress = "Mars"
            };

            //Act
            var result = await propertyService.CreateAsync(model);

            var addedProperty = await db.Properties.FirstOrDefaultAsync(x => x.Name == "Office");

            //Assert
            result
            .Should()
            .BeFalse();

            addedProperty
            .Should()
            .BeNull();
        }