示例#1
0
        public async Task DeleteAsyncShouldWorkCorrectly()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            using var db = new ApplicationDbContext(options);
            var lakeRepository = new EfDeletableEntityRepository <Lake>(db);

            var service = new LakesService(null, lakeRepository);

            await lakeRepository.AddAsync(new Lake { Id = 1 });

            await lakeRepository.AddAsync(new Lake { Id = 2 });

            await lakeRepository.AddAsync(new Lake { Id = 3 });

            await lakeRepository.SaveChangesAsync();

            Assert.Equal(3, service.GetCount());

            await service.DeleteAsync(1);

            await lakeRepository.SaveChangesAsync();

            Assert.Equal(2, service.GetCount());

            await service.DeleteAsync(2);

            await lakeRepository.SaveChangesAsync();

            Assert.Equal(1, service.GetCount());
        }
示例#2
0
        public async Task AddAsyncShouldWorkCorrect()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            using var db = new ApplicationDbContext(options);
            var lakeRepository = new EfDeletableEntityRepository <Lake>(db);

            var clodinaryMock = new Mock <ICloudinaryService>();

            var service = new LakesService(clodinaryMock.Object, lakeRepository);

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

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

            using 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;

            var input = new AddLakeInputModel
            {
                Location = new LocationLakeInputModel
                {
                    Latitude  = 12.22,
                    Longitude = 13.33,
                },
                CountryId   = 2,
                Name        = "Test2",
                WebsiteUrl  = "www.test2.com",
                Area        = 22.22,
                IsFree      = true,
                MainImage   = file,
                OtherImages = new List <IFormFile> {
                    file
                },
            };

            Assert.Equal(0, service.GetCount());

            await service.AddAsync(input, Guid.NewGuid().ToString());

            await lakeRepository.SaveChangesAsync();

            Assert.Equal(1, service.GetCount());
        }
示例#3
0
        public void GetCountShouldWorkCorrect()
        {
            var lakesList = new List <Lake>
            {
                new Lake(),
                new Lake(),
                new Lake(),
                new Lake(),
            };

            var repository = new Mock <IDeletableEntityRepository <Lake> >();

            repository.Setup(r => r.All()).Returns(lakesList.AsQueryable());

            var service = new LakesService(null, repository.Object);

            Assert.Equal(4, service.GetCount());
        }