public void CreateCommandDefinition_creates_valid_CachingCommandDefinition_for_CachingCommand()
        {
            var command = Mock.Of <DbCommand>();
            var mockCommandDefinition = new Mock <DbCommandDefinition>();

            mockCommandDefinition.Setup(d => d.CreateCommand()).Returns(command);
            var commandTreeFacts   = new CommandTreeFacts(null, true, true);
            var transactionHandler = new Mock <CacheTransactionHandler>(Mock.Of <ICache>()).Object;
            var cachingPolicy      = Mock.Of <CachingPolicy>();

            var cachingCommand = new CachingCommand(command, commandTreeFacts, transactionHandler, cachingPolicy);

            var mockProviderServices = new Mock <DbProviderServices>();

            mockProviderServices.Setup(s => s.CreateCommandDefinition(command)).Returns(mockCommandDefinition.Object);

            var newCommandDefinition =
                new CachingProviderServices(
                    mockProviderServices.Object, new Mock <CacheTransactionHandler>(Mock.Of <ICache>()).Object)
                .CreateCommandDefinition(cachingCommand);

            Assert.IsType <CachingCommandDefinition>(newCommandDefinition);
            mockProviderServices.Verify(s => s.CreateCommandDefinition(command), Times.Once);

            var newCommand = (CachingCommand)newCommandDefinition.CreateCommand();

            Assert.Same(command, newCommand.WrappedCommand);
            Assert.Same(commandTreeFacts, newCommand.CommandTreeFacts);
            Assert.Same(transactionHandler, newCommand.CacheTransactionHandler);
            Assert.Same(cachingPolicy, newCommand.CachingPolicy);
        }
        public void CreateDbCommandDefinition_invokes_CreateCommandDefinition_on_wrapped_provider()
        {
            var mockProviderServices = new Mock <DbProviderServices>();

            var workspace = new MetadataWorkspace(
                () => new EdmItemCollection(
                    new[] {
                XmlReader.Create(
                    new StringReader(
                        @"<Schema xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"" Namespace=""ExampleModel"" />"))
            }), () => null, () => null);

            var commandDefinition =
                new CachingProviderServices(mockProviderServices.Object, new Mock <CacheTransactionHandler>(Mock.Of <ICache>()).Object)
                .CreateCommandDefinition(
                    new Mock <DbProviderManifest>().Object,
                    new DbQueryCommandTree(workspace, DataSpace.CSpace, DbExpressionBuilder.True));

            Assert.IsType <CachingCommandDefinition>(commandDefinition);
            Assert.True(((CachingCommandDefinition)commandDefinition).IsCacheable);
            Assert.True(((CachingCommandDefinition)commandDefinition).IsQuery);

            mockProviderServices
            .Protected()
            .Verify("CreateDbCommandDefinition",
                    Times.Once(),
                    ItExpr.IsAny <DbProviderManifest>(),
                    ItExpr.IsAny <DbCommandTree>());
        }
        public void CreateCommandDefinition_creates_valid_CachingCommandDefinition_for_CachingCommand()
        {
            var command = Mock.Of<DbCommand>();
            var mockCommandDefinition = new Mock<DbCommandDefinition>();
            mockCommandDefinition.Setup(d => d.CreateCommand()).Returns(command);
            var commandTreeFacts = new CommandTreeFacts(null, true, true);
            var transactionHandler = new Mock<CacheTransactionHandler>(Mock.Of<ICache>()).Object;
            var cachingPolicy = Mock.Of<CachingPolicy>();

            var cachingCommand = new CachingCommand(command, commandTreeFacts, transactionHandler, cachingPolicy);

            var mockProviderServices = new Mock<DbProviderServices>();
            mockProviderServices.Setup(s => s.CreateCommandDefinition(command)).Returns(mockCommandDefinition.Object);

            var newCommandDefinition =
                new CachingProviderServices(
                    mockProviderServices.Object, new Mock<CacheTransactionHandler>(Mock.Of<ICache>()).Object)
                        .CreateCommandDefinition(cachingCommand);

            Assert.IsType<CachingCommandDefinition>(newCommandDefinition);
            mockProviderServices.Verify(s => s.CreateCommandDefinition(command), Times.Once);

            var newCommand = (CachingCommand)newCommandDefinition.CreateCommand();
            Assert.Same(command, newCommand.WrappedCommand);
            Assert.Same(commandTreeFacts, newCommand.CommandTreeFacts);
            Assert.Same(transactionHandler, newCommand.CacheTransactionHandler);
            Assert.Same(cachingPolicy, newCommand.CachingPolicy);
        }
        public void CreateCommandDefinition_creates_valid_DbCommandDefinition_for_DbCommand()
        {
            var command = Mock.Of<DbCommand>();
            var mockCommandDefinition = new Mock<DbCommandDefinition>();
            mockCommandDefinition.Setup(d => d.CreateCommand()).Returns(command);

            var mockProviderServices = new Mock<DbProviderServices>();
            mockProviderServices.Setup(s => s.CreateCommandDefinition(command)).Returns(mockCommandDefinition.Object);

            var newCommandDefinition =
                new CachingProviderServices(
                    mockProviderServices.Object, new Mock<CacheTransactionHandler>(Mock.Of<ICache>()).Object)
                        .CreateCommandDefinition(command);

            Assert.IsNotType<CachingCommandDefinition>(newCommandDefinition);
            mockProviderServices.Verify(s => s.CreateCommandDefinition(command), Times.Once);

            var newCommand = newCommandDefinition.CreateCommand();
            Assert.Same(command, newCommand);
        }
        public void CreateCommandDefinition_creates_valid_DbCommandDefinition_for_DbCommand()
        {
            var command = Mock.Of <DbCommand>();
            var mockCommandDefinition = new Mock <DbCommandDefinition>();

            mockCommandDefinition.Setup(d => d.CreateCommand()).Returns(command);

            var mockProviderServices = new Mock <DbProviderServices>();

            mockProviderServices.Setup(s => s.CreateCommandDefinition(command)).Returns(mockCommandDefinition.Object);

            var newCommandDefinition =
                new CachingProviderServices(
                    mockProviderServices.Object, new Mock <CacheTransactionHandler>(Mock.Of <ICache>()).Object)
                .CreateCommandDefinition(command);

            Assert.IsNotType <CachingCommandDefinition>(newCommandDefinition);
            mockProviderServices.Verify(s => s.CreateCommandDefinition(command), Times.Once);

            var newCommand = newCommandDefinition.CreateCommand();

            Assert.Same(command, newCommand);
        }
        public void CreateDbCommandDefinition_invokes_CreateCommandDefinition_on_wrapped_provider()
        {
            var mockProviderServices = new Mock<DbProviderServices>();

            var workspace = new MetadataWorkspace(
                () => new EdmItemCollection(
                    new[] {
                    XmlReader.Create(
                        new StringReader(
                            @"<Schema xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"" Namespace=""ExampleModel"" />"))
                }), () => null, () => null);

            var commandDefinition =
                new CachingProviderServices(mockProviderServices.Object, new Mock<CacheTransactionHandler>(Mock.Of<ICache>()).Object)
                    .CreateCommandDefinition(
                        new Mock<DbProviderManifest>().Object,
                        new DbQueryCommandTree(workspace, DataSpace.CSpace, DbExpressionBuilder.True));

            Assert.IsType<CachingCommandDefinition>(commandDefinition);
            Assert.True(((CachingCommandDefinition)commandDefinition).IsCacheable);
            Assert.True(((CachingCommandDefinition)commandDefinition).IsQuery);

            mockProviderServices
                .Protected()
                .Verify("CreateDbCommandDefinition",
                    Times.Once(),
                    ItExpr.IsAny<DbProviderManifest>(),
                    ItExpr.IsAny<DbCommandTree>());
        }