public async Task SaveChangesAsync()
        {
            // Arrange
            var entity       = new FakeEntity("Name");
            var databaseName = $"{DatabaseNamePrefix}.{nameof(SaveChangesAsync)}";
            var options      = new DbContextOptionsBuilder <FakeContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;

            using (var context = new FakeContext(options))
            {
                context.Add(entity);
                await context.SaveChangesAsync(true);
            }

            // Act
            using (var context = new FakeContext(options))
            {
                context.Entry(entity).State = EntityState.Modified;
                await context.SaveChangesAsync(true);
            }

            // Assert
            using (var context = new FakeContext(options))
            {
                entity = await context.FindAsync <FakeEntity>(entity.Id);
            }

            Assert.NotNull(entity?.Updated);
        }
        public async Task Delete()
        {
            // Arrange
            var entity       = new FakeEntity("Name");
            var databaseName = $"{DatabaseNamePrefix}.{nameof(Delete)}";
            var options      = new DbContextOptionsBuilder <FakeContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;

            using (var context = new FakeContext(options))
            {
                context.Add(entity);
                await context.SaveChangesAsync();
            }

            var keyValues = new object[] { entity.Id };
            var request   = new Mock <DeleteRequest>(new object[] { keyValues });

            // Act
            using (var context = new FakeContext(options))
            {
                var requestHandler = new FakeDeleteRequestHandler(context);
                await requestHandler.Handle(request.Object, CancellationToken.None);
            }

            // Assert
            using (var context = new FakeContext(options))
            {
                entity = await context.FindAsync <FakeEntity>(keyValues);

                Assert.Null(entity);
            }
        }
        public async Task Read()
        {
            // Arrange
            var entity       = new FakeEntity("Name");
            var model        = new object();
            var databaseName = $"{DatabaseNamePrefix}.{nameof(Read)}";
            var options      = new DbContextOptionsBuilder <FakeContext>()
                               .UseInMemoryDatabase(databaseName)
                               .Options;

            using (var context = new FakeContext(options))
            {
                context.Add(entity);
                await context.SaveChangesAsync();
            }

            var keyValues = new object[] { entity.Id };
            var mapper    = new Mock <IMapper>();

            mapper.Setup(x => x.Map <object>(It.Is <FakeEntity>(y => y.Name == entity.Name))).Returns(model);
            var    request = new Mock <ReadRequest <FakeEntity, object> >(new object[] { keyValues });
            object read;

            // Act
            using (var context = new FakeContext(options))
            {
                var requestHandler = new FakeReadRequestHandler(context, mapper.Object);
                read = await requestHandler.Handle(request.Object, CancellationToken.None);
            }

            // Assert
            Assert.Equal(model, read);
        }
예제 #4
0
        public void QueryCache()
        {
            var optionsBuilder = new DbContextOptionsBuilder <FakeContext>();

            optionsBuilder.UseInMemoryDatabase("testName")
            .ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning));
            var fakeContext = new FakeContext(optionsBuilder.Options);

            QueryCacheManager.UseFirstTagAsCacheKey = true;


            fakeContext.FakeModels.RemoveRange(fakeContext.FakeModels.ToList());
            fakeContext.SaveChanges();

            fakeContext.Add(new FakeModel {
                FakeId = Guid.NewGuid(), FakeString = "fakeString"
            });
            QueryCacheManager.IsEnabled = false;


            fakeContext.SaveChanges();
            try
            {
                var fakeModels = fakeContext
                                 .FakeModels
                                 .FromCache(typeof(FakeModel).Name)
                                 .ToList();


                var fakeModels2 = fakeContext
                                  .FakeModels
                                  .FromCache(typeof(FakeModel).Name)
                                  .ToList();



                Assert.AreEqual(1, fakeModels.Count);
                Assert.AreEqual(1, fakeModels2.Count);
                Assert.AreEqual(1, fakeContext
                                .FakeModels
                                .DeferredCount()
                                .FromCache(typeof(FakeModel).Name));
            }
            finally
            {
                QueryCacheManager.UseFirstTagAsCacheKey = false;
                QueryCacheManager.IsEnabled             = true;
            }
        }
        public void MyTestMethod()
        {
            var optionsBuilder = new DbContextOptionsBuilder <FakeContext>();

            optionsBuilder.UseInMemoryDatabase("testName")
            .ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning));
            var fakeContext = new FakeContext(optionsBuilder.Options);

            QueryCacheManager.UseFirstTagAsCacheKey = true;

            fakeContext.Add(new FakeModel {
                FakeId = Guid.NewGuid(), FakeString = "fakeString"
            });

            var fakeModels = fakeContext
                             .FakeModels
                             .FromCache(typeof(FakeModel).Name)
                             .ToList();
        }
        public async Task Update()
        {
            // Arrange
            const string newName      = "New Name";
            var          entity       = new FakeEntity("Name");
            var          databaseName = $"{DatabaseNamePrefix}.{nameof(Update)}";
            var          options      = new DbContextOptionsBuilder <FakeContext>()
                                        .UseInMemoryDatabase(databaseName)
                                        .Options;

            using (var context = new FakeContext(options))
            {
                context.Add(entity);
                await context.SaveChangesAsync();
            }

            entity.Name = newName;
            var keyValues = new object[] { entity.Id };
            var mapper    = new Mock <IMapper>();

            mapper.Setup(x => x.Map <FakeEntity>(It.IsAny <object>())).Returns(entity);
            var request = new Mock <UpdateRequest <FakeEntity, object> >(new object());

            // Act
            using (var context = new FakeContext(options))
            {
                var requestHandler = new FakeUpdateRequestHandler(context, mapper.Object);
                await requestHandler.Handle(request.Object, CancellationToken.None);
            }

            // Assert
            using (var context = new FakeContext(options))
            {
                entity = await context.FindAsync <FakeEntity>(keyValues);

                Assert.Equal(newName, entity.Name);
            }
        }