private void VerifyMethodInitialization(Action <BridgeDataReader> methodToInvoke, bool async = false) { Assert.NotNull(methodToInvoke); var sourceEnumerable = new[] { new object[] { 1 } }; var rootCoordinatorFactory = MockHelper.CreateCoordinatorFactory <int, RecordState>( depth: 0, stateSlot: 0, ordinal: 0, nestedCoordinators: new CoordinatorFactory[0], producedValues: null); var shaperMock = new Mock <Shaper <RecordState> >( Common.Internal.Materialization.MockHelper.CreateDbDataReader(sourceEnumerable), /*context*/ null, /*workspace*/ null, MergeOption.AppendOnly, /*stateCount*/ 2, rootCoordinatorFactory, /*readerOwned*/ false, /*useSpatialReader*/ false, /*shouldReleaseConnection*/ true) { CallBase = true }; var bridgeDataReaderMock = new Mock <BridgeDataReader>(shaperMock.Object, rootCoordinatorFactory, 0, null) { CallBase = true }; bridgeDataReaderMock.Protected().Setup("EnsureInitialized").Verifiable(); #if !NET40 bridgeDataReaderMock.Protected().Setup <Task>("EnsureInitializedAsync", ItExpr.IsAny <CancellationToken>()).Verifiable(); #endif try { methodToInvoke(bridgeDataReaderMock.Object); } catch (Exception) { } if (async) { bridgeDataReaderMock.Protected().Verify <Task>( "EnsureInitializedAsync", Times.AtLeastOnce(), ItExpr.IsAny <CancellationToken>()); } else { bridgeDataReaderMock.Protected().Verify("EnsureInitialized", Times.AtLeastOnce()); } }
private BridgeDataRecord CreateBridgeDataRecord() { var dbDataReaderMock = new Mock <DbDataReader>(); var coordinatorFactory = MockHelper.CreateCoordinatorFactory <RecordState>(shaper => null); var shaperMock = new Mock <Shaper <RecordState> >( dbDataReaderMock.Object, /*context*/ null, /*workspace*/ null, MergeOption.AppendOnly, /*stateCount*/ 1, coordinatorFactory, /*checkPermissions*/ null, /*readerOwned*/ false) { CallBase = true }; return(new BridgeDataRecord(shaperMock.Object, 0)); }
public void Constructors_dont_advance_the_underlying_shaper() { var sourceEnumerable = new[] { new object[] { 1 } }; var rootCoordinatorFactory = MockHelper.CreateCoordinatorFactory <int, RecordState>( depth: 0, stateSlot: 0, ordinal: 0, nestedCoordinators: new CoordinatorFactory[0], producedValues: null); var shaperMock = new Mock <Shaper <RecordState> >( Common.Internal.Materialization.MockHelper.CreateDbDataReader(sourceEnumerable), /*context*/ null, /*workspace*/ null, MergeOption.AppendOnly, /*stateCount*/ 2, rootCoordinatorFactory, /*readerOwned*/ false, /*useSpatialReader*/ false, /*shouldReleaseConnection*/ true) { CallBase = true }; shaperMock.Setup(m => m.GetEnumerator()).Returns( () => { Assert.True(false); return(null); }); // Verify these methods don't cause initialization var bridgeDataReader = new BridgeDataReader(shaperMock.Object, rootCoordinatorFactory, 0, null); bridgeDataReader.GetEnumerator(); }
private BridgeDataReader CreateNestedBridgeDataReader(DbDataReader dataReader = null) { var sourceEnumerable1 = new[] { new object[] { 1 }, new object[] { null }, new object[] { 2 }, new object[] { 2 } }; var sourceEnumerable2 = new[] { new object[] { 1 }, new object[] { 3 }, }; dataReader = dataReader ?? Common.Internal.Materialization.MockHelper.CreateDbDataReader(sourceEnumerable1, sourceEnumerable2); var rootCoordinatorFactory = MockHelper.CreateCoordinatorFactory <int, RecordState>( depth: 0, stateSlot: 0, ordinal: 0, nestedCoordinators: new CoordinatorFactory[0], producedValues: null); var shaperMock = new Mock <Shaper <RecordState> >( dataReader, /*context*/ null, /*workspace*/ null, MergeOption.AppendOnly, /*stateCount*/ 2, rootCoordinatorFactory, /*checkPermissions*/ null, /*readerOwned*/ false) { CallBase = true }; var rootCoordinatorFactory2 = MockHelper.CreateCoordinatorFactory <int, RecordState>( depth: 0, stateSlot: 0, ordinal: 0, nestedCoordinators: new CoordinatorFactory[0], producedValues: null); var shaperMock2 = new Mock <Shaper <RecordState> >( dataReader, /*context*/ null, /*workspace*/ null, MergeOption.AppendOnly, /*stateCount*/ 2, rootCoordinatorFactory2, /*checkPermissions*/ null, /*readerOwned*/ false) { CallBase = true }; var nextResultInfo = new[] { new KeyValuePair <Shaper <RecordState>, CoordinatorFactory <RecordState> >( shaperMock2.Object, rootCoordinatorFactory2) }.ToList(); return(new BridgeDataReader(shaperMock.Object, rootCoordinatorFactory, 0, nextResultInfo.GetEnumerator())); }
private void Execute_calls_ObjectQueryExecutionPlan_Execute_in_a_transaction_using_ExecutionStrategy_implementation( Func <ObjectQuery <object>, ObjectResult> execute, bool async) { var objectQuery = MockHelper.CreateMockObjectQuery((object)null).Object; var executionPlanMock = Mock.Get(objectQuery.QueryState.GetExecutionPlan(MergeOption.AppendOnly)); var executionStrategyMock = new Mock <IDbExecutionStrategy>(); var objectContextMock = Mock.Get(objectQuery.QueryState.ObjectContext); // Verify that ExecuteInTransaction calls ObjectQueryExecutionPlan.Execute if (async) { #if !NET40 objectContextMock.Setup( m => m.ExecuteInTransactionAsync( It.IsAny <Func <Task <ObjectResult <object> > > >(), It.IsAny <IDbExecutionStrategy>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())) .Returns <Func <Task <ObjectResult <object> > >, IDbExecutionStrategy, bool, bool, CancellationToken>( (f, t, s, r, c) => { executionPlanMock.Verify( m => m.ExecuteAsync <object>( It.IsAny <ObjectContext>(), It.IsAny <ObjectParameterCollection>(), It.IsAny <CancellationToken>()), Times.Never()); var result = f().Result; executionPlanMock.Verify( m => m.ExecuteAsync <object>( It.IsAny <ObjectContext>(), It.IsAny <ObjectParameterCollection>(), It.IsAny <CancellationToken>()), Times.Once()); return(Task.FromResult(result)); }); #endif } else { objectContextMock.Setup( m => m.ExecuteInTransaction( It.IsAny <Func <ObjectResult <object> > >(), It.IsAny <IDbExecutionStrategy>(), It.IsAny <bool>(), It.IsAny <bool>())) .Returns <Func <ObjectResult <object> >, IDbExecutionStrategy, bool, bool>( (f, t, s, r) => { executionPlanMock.Verify( m => m.Execute <object>(It.IsAny <ObjectContext>(), It.IsAny <ObjectParameterCollection>()), Times.Never()); var result = f(); executionPlanMock.Verify( m => m.Execute <object>(It.IsAny <ObjectContext>(), It.IsAny <ObjectParameterCollection>()), Times.Once()); return(result); }); } // Verify that ExecutionStrategy.Execute calls ExecuteInTransaction if (async) { #if !NET40 executionStrategyMock.Setup( m => m.ExecuteAsync(It.IsAny <Func <Task <ObjectResult <object> > > >(), It.IsAny <CancellationToken>())) .Returns <Func <Task <ObjectResult <object> > >, CancellationToken>( (f, c) => { objectContextMock.Verify( m => m.ExecuteInTransactionAsync( It.IsAny <Func <Task <ObjectResult <object> > > >(), It.IsAny <IDbExecutionStrategy>(), false, It.IsAny <bool>(), It.IsAny <CancellationToken>()), Times.Never()); var result = f().Result; objectContextMock.Verify( m => m.ExecuteInTransactionAsync( It.IsAny <Func <Task <ObjectResult <object> > > >(), It.IsAny <IDbExecutionStrategy>(), false, It.IsAny <bool>(), It.IsAny <CancellationToken>()), Times.Once()); return(Task.FromResult(result)); }); #endif } else { executionStrategyMock.Setup(m => m.Execute(It.IsAny <Func <ObjectResult <object> > >())) .Returns <Func <ObjectResult <object> > >( f => { objectContextMock.Verify( m => m.ExecuteInTransaction( It.IsAny <Func <ObjectResult <object> > >(), It.IsAny <IDbExecutionStrategy>(), false, It.IsAny <bool>()), Times.Never()); var result = f(); objectContextMock.Verify( m => m.ExecuteInTransaction( It.IsAny <Func <ObjectResult <object> > >(), It.IsAny <IDbExecutionStrategy>(), false, It.IsAny <bool>()), Times.Once()); return(result); }); } MutableResolver.AddResolver <Func <IDbExecutionStrategy> >(key => (Func <IDbExecutionStrategy>)(() => executionStrategyMock.Object)); try { execute(objectQuery); } finally { MutableResolver.ClearResolvers(); } // Finally verify that ExecutionStrategy.Execute was called if (async) { #if !NET40 executionStrategyMock.Verify( m => m.ExecuteAsync(It.IsAny <Func <Task <ObjectResult <object> > > >(), It.IsAny <CancellationToken>()), Times.Once()); #endif } else { executionStrategyMock.Verify(m => m.Execute(It.IsAny <Func <ObjectResult <object> > >()), Times.Once()); } }
public void Is_streaming_by_default() { var objectQuery = MockHelper.CreateMockObjectQuery((object)null).Object; Assert.True(objectQuery.Streaming); }