public async Task Save_changes_removes_deleted_objects_from_store()
        {
            var model         = CreateModel();
            var configuration = CreateConfiguration();
            var entityType    = model.GetEntityType(typeof(Customer));

            var customer = new Customer {
                Id = 42, Name = "Unikorn"
            };
            var entityEntry = new ClrStateEntry(configuration, entityType, customer);
            await entityEntry.SetEntityStateAsync(EntityState.Added);

            var inMemoryDataStore = new InMemoryDataStore(configuration, new InMemoryDatabase(new[] { new NullLoggerFactory() }));

            await inMemoryDataStore.SaveChangesAsync(new[] { entityEntry });

            // Because the data store is being used directly the entity state must be manually changed after saving.
            await entityEntry.SetEntityStateAsync(EntityState.Unchanged);

            customer.Name = "Unikorn, The Return";
            await entityEntry.SetEntityStateAsync(EntityState.Deleted);

            await inMemoryDataStore.SaveChangesAsync(new[] { entityEntry });

            Assert.Equal(0, inMemoryDataStore.Database.SelectMany(t => t).Count());
        }
        public async Task Should_log_writes()
        {
            var model         = CreateModel();
            var configuration = CreateConfiguration();
            var entityType    = model.GetEntityType(typeof(Customer));

            var customer = new Customer {
                Id = 42, Name = "Unikorn"
            };
            var entityEntry = new ClrStateEntry(configuration, entityType, customer);
            await entityEntry.SetEntityStateAsync(EntityState.Added);

            var mockLogger  = new Mock <ILogger>();
            var mockFactory = new Mock <ILoggerFactory>();

            mockFactory.Setup(m => m.Create(It.IsAny <string>())).Returns(mockLogger.Object);

            var inMemoryDataStore = new InMemoryDataStore(configuration, new InMemoryDatabase(new[] { mockFactory.Object }));

            await inMemoryDataStore.SaveChangesAsync(new[] { entityEntry });

            mockLogger.Verify(
                l => l.WriteCore(
                    TraceType.Information,
                    0,
                    It.IsAny <string>(),
                    null,
                    It.IsAny <Func <object, Exception, string> >()),
                Times.Once);
        }
        public async Task Save_changes_updates_changed_objects_in_store()
        {
            var model         = CreateModel();
            var configuration = CreateConfiguration();
            var entityType    = model.GetEntityType(typeof(Customer));

            var customer = new Customer {
                Id = 42, Name = "Unikorn"
            };
            var entityEntry = new ClrStateEntry(configuration, entityType, customer);
            await entityEntry.SetEntityStateAsync(EntityState.Added);

            var inMemoryDataStore = new InMemoryDataStore(configuration, new InMemoryDatabase(new[] { new NullLoggerFactory() }));

            await inMemoryDataStore.SaveChangesAsync(new[] { entityEntry });

            customer.Name = "Unikorn, The Return";
            await entityEntry.SetEntityStateAsync(EntityState.Modified);

            await inMemoryDataStore.SaveChangesAsync(new[] { entityEntry });

            Assert.Equal(1, inMemoryDataStore.Database.SelectMany(t => t).Count());
            Assert.Equal(new object[] { 42, "Unikorn, The Return" }, inMemoryDataStore.Database.Single().Single());
        }