public void Save_creates_table_if_needed_and_save_views()
        {
            var mockViewCacheFactory =
                new Mock <DatabaseViewCacheFactoryProtectedInvoker>()
            {
                CallBase = true
            };

            mockViewCacheFactory
            .Protected()
            .Setup <bool>("ViewTableExists", ItExpr.IsAny <DatabaseViewCacheContext>())
            .Returns(false);

            var mockFactoryContext =
                CreateMockDatabaseViewCacheContext(new DatabaseViewCacheEntry[0]);

            var resultViewCacheEntry = new DatabaseViewCacheEntry
            {
                ConceptualModelContainerName = "SimpleModel",
                StoreModelContainerName      = "CodeFirstDatabase"
            };

            Mock.Get(mockFactoryContext.ViewCache)
            .Setup(s => s.Add(It.IsAny <DatabaseViewCacheEntry>()))
            .Callback <DatabaseViewCacheEntry>(
                e =>
            {
                Assert.Equal("SimpleModel", e.ConceptualModelContainerName);
                Assert.Equal("CodeFirstDatabase", e.StoreModelContainerName);
            })
            .Returns(resultViewCacheEntry);

            mockViewCacheFactory
            .Setup(f => f.CreateDatabaseViewCacheContext())
            .Returns(mockFactoryContext);

            mockViewCacheFactory.Object
            .InvokeSave(XDocument.Parse(SimpleModel.Views));

            mockViewCacheFactory
            .Protected()
            .Verify("CreateViewTable", Times.Once(), mockFactoryContext);

            Assert.True(
                XNode.DeepEquals(
                    XDocument.Parse(SimpleModel.Views),
                    XDocument.Parse(resultViewCacheEntry.ViewDefinitions)));

            Assert.True(resultViewCacheEntry.LastUpdated >
                        resultViewCacheEntry.LastUpdated.AddSeconds(-10));

            Mock.Get(mockFactoryContext).Verify(c => c.SaveChanges(), Times.Once);
        }
        public void Save_updates_existing_views_and_save()
        {
            var mockViewCacheFactory =
                new Mock <DatabaseViewCacheFactoryProtectedInvoker>()
            {
                CallBase = true
            };

            mockViewCacheFactory
            .Protected()
            .Setup <bool>("ViewTableExists", ItExpr.IsAny <DatabaseViewCacheContext>())
            .Returns(true);

            var resultViewCacheEntry = new DatabaseViewCacheEntry
            {
                ConceptualModelContainerName = "SimpleModel",
                StoreModelContainerName      = "CodeFirstDatabase",
                ViewDefinitions = "<views />",
                LastUpdated     = new DateTimeOffset(0, TimeSpan.Zero)
            };

            var mockFactoryContext =
                CreateMockDatabaseViewCacheContext(new[] { resultViewCacheEntry });

            mockViewCacheFactory
            .Setup(f => f.CreateDatabaseViewCacheContext())
            .Returns(mockFactoryContext);

            mockViewCacheFactory.Object
            .InvokeSave(XDocument.Parse(SimpleModel.Views));

            mockViewCacheFactory
            .Protected()
            .Verify("CreateViewTable", Times.Never(), mockFactoryContext);

            Assert.True(
                XNode.DeepEquals(
                    XDocument.Parse(SimpleModel.Views),
                    XDocument.Parse(resultViewCacheEntry.ViewDefinitions)));

            Assert.True(resultViewCacheEntry.LastUpdated >
                        resultViewCacheEntry.LastUpdated.AddSeconds(-10));
        }