public static async Task <EditSession> GetCustomSession(Query q, EditTableMetadata etm) { // Step 1) Create the Session object // Mock metadata factory Mock <IEditMetadataFactory> metaFactory = new Mock <IEditMetadataFactory>(); metaFactory .Setup(f => f.GetObjectMetadata(It.IsAny <DbConnection>(), It.IsAny <string[]>(), It.IsAny <string>())) .Returns(etm); EditSession session = new EditSession(metaFactory.Object); // Step 2) Initialize the Session // Mock connector that does nothing EditSession.Connector connector = () => Task.FromResult <DbConnection>(null); // Mock query runner that returns the query we were provided EditSession.QueryRunner queryRunner = (s) => Task.FromResult(new EditSession.EditSessionQueryExecutionState(q)); // Initialize session.Initialize(BasicInitializeParameters, connector, queryRunner, () => Task.FromResult(0), (e) => Task.FromResult(0)); await session.InitializeTask; return(session); }
public async Task InitializeMetadataFails() { // Setup: // ... Create a metadata factory that throws Mock <IEditMetadataFactory> emf = new Mock <IEditMetadataFactory>(); emf.Setup(f => f.GetObjectMetadata(It.IsAny <DbConnection>(), It.IsAny <string[]>(), It.IsAny <string>())) .Throws <Exception>(); // ... Create a session that hasn't been initialized EditSession s = new EditSession(emf.Object); // ... Create a mock for verifying the failure handler will be called var successHandler = DoNothingSuccessMock; var failureHandler = DoNothingFailureMock; // If: I initalize the session with a metadata factory that will fail s.Initialize(Common.BasicInitializeParameters, DoNothingConnector, DoNothingQueryRunner, successHandler.Object, failureHandler.Object); await s.InitializeTask; // Then: // ... The session should not be initialized Assert.False(s.IsInitialized); // ... The failure handler should have been called once failureHandler.Verify(f => f(It.IsAny <Exception>()), Times.Once); // ... The success handler should not have been called at all successHandler.Verify(f => f(), Times.Never); }
public void InitializeNullParams(EditInitializeParams initParams, EditSession.Connector c, EditSession.QueryRunner qr, Func <Task> sh, Func <Exception, Task> fh) { // Setup: // ... Create a session that hasn't been initialized Mock <IEditMetadataFactory> emf = new Mock <IEditMetadataFactory>(); EditSession s = new EditSession(emf.Object); // If: I initialize it with a missing parameter // Then: It should throw an exception Assert.ThrowsAny <ArgumentException>(() => s.Initialize(initParams, c, qr, sh, fh)); }
public void InitializeAlreadyInitializing() { // Setup: // ... Create a session and fake that it is in progress of initializing Mock <IEditMetadataFactory> emf = new Mock <IEditMetadataFactory>(); EditSession s = new EditSession(emf.Object) { InitializeTask = new Task(() => {}) }; // If: I initialize it // Then: I should get an exception Assert.Throws <InvalidOperationException>(() => s.Initialize(null, null, null, null, null)); }
public void InitializeAlreadyInitialized() { // Setup: // ... Create a session and fake that it has been initialized Mock <IEditMetadataFactory> emf = new Mock <IEditMetadataFactory>(); EditSession s = new EditSession(emf.Object) { IsInitialized = true }; // If: I initialize it // Then: I should get an exception Assert.Throws <InvalidOperationException>(() => s.Initialize(null, null, null, null, null)); }
public async Task InitializeSuccess() { // Setup: // ... Create a metadata factory that will return some generic column information var q = QueryExecution.Common.GetBasicExecutedQuery(); var rs = q.Batches[0].ResultSets[0]; var etm = Common.GetCustomEditTableMetadata(rs.Columns.Cast <DbColumn>().ToArray()); Mock <IEditMetadataFactory> emf = new Mock <IEditMetadataFactory>(); emf.Setup(f => f.GetObjectMetadata(It.IsAny <DbConnection>(), It.IsAny <string[]>(), It.IsAny <string>())) .Returns(etm); // ... Create a session that hasn't been initialized EditSession s = new EditSession(emf.Object); // ... Create a query runner that will return a successful query Mock <EditSession.QueryRunner> qr = new Mock <EditSession.QueryRunner>(); qr.Setup(r => r(It.IsAny <string>())) .Returns(Task.FromResult(new EditSession.EditSessionQueryExecutionState(q))); // ... Create a mock for verifying the failure handler will be called var successHandler = DoNothingSuccessMock; var failureHandler = DoNothingFailureMock; // If: I initialize the session with a query runner that will fail s.Initialize(Common.BasicInitializeParameters, DoNothingConnector, qr.Object, successHandler.Object, failureHandler.Object); await s.InitializeTask; // Then: // ... The failure handler should not have been called failureHandler.Verify(f => f(It.IsAny <Exception>()), Times.Never); // ... The success handler should have been called successHandler.Verify(f => f(), Times.Once); // ... The session should have been initialized Assert.True(s.IsInitialized); Assert.Equal(rs.RowCount, s.NextRowId); Assert.NotNull(s.EditCache); Assert.Empty(s.EditCache); }
public async Task InitializeQueryFailReturnNull(string message) { // Setup: // ... Create a metadata factory that will return some generic column information var b = QueryExecution.Common.GetBasicExecutedBatch(); var etm = Common.GetStandardMetadata(b.ResultSets[0].Columns); Mock <IEditMetadataFactory> emf = new Mock <IEditMetadataFactory>(); emf.Setup(f => f.GetObjectMetadata(It.IsAny <DbConnection>(), It.IsAny <string[]>(), It.IsAny <string>())) .Returns(etm); // ... Create a session that hasn't been initialized EditSession s = new EditSession(emf.Object); // ... Create a query runner that will fail via returning a null query Mock <EditSession.QueryRunner> qr = new Mock <EditSession.QueryRunner>(); qr.Setup(r => r(It.IsAny <string>())) .Returns(Task.FromResult(new EditSession.EditSessionQueryExecutionState(null, message))); // ... Create a mock for verifying the failure handler will be called var successHandler = DoNothingSuccessMock; var failureHandler = DoNothingFailureMock; // If: I initialize the session with a query runner that will fail s.Initialize(Common.BasicInitializeParameters, DoNothingConnector, qr.Object, successHandler.Object, failureHandler.Object); await s.InitializeTask; // Then: // ... The session should not be initialized Assert.False(s.IsInitialized); // ... The failure handler should have been called once failureHandler.Verify(f => f(It.IsAny <Exception>()), Times.Once); // ... The success handler should not have been called at all successHandler.Verify(f => f(), Times.Never); }