Ejemplo n.º 1
0
        public async Task CommitNullConnection()
        {
            // Setup: Create a basic session
            EditSession s = await GetBasicSession();

            // If: I attempt to commit with a null connection
            // Then: I should get an exception
            Assert.Throws <ArgumentNullException>(
                () => s.CommitEdits(null, () => Task.CompletedTask, e => Task.CompletedTask));
        }
Ejemplo n.º 2
0
        public void CommitEditsNotInitialized()
        {
            // Setup:
            // ... Create a session without initializing
            Mock <IEditMetadataFactory> emf = new Mock <IEditMetadataFactory>();
            EditSession s = new EditSession(emf.Object);

            // If: I ask to script edits without initializing
            // Then: I should get an exception
            Assert.Throws <InvalidOperationException>(() => s.CommitEdits(null, null, null));
        }
Ejemplo n.º 3
0
        public async Task CommitNullFailureHandler()
        {
            // Setup:
            // ... Create a basic session
            EditSession s = await GetBasicSession();

            // ... Mock db connection
            DbConnection conn = new TestSqlConnection(null);

            // If: I attempt to commit with a null success handler
            // Then: I should get an exception
            Assert.Throws <ArgumentNullException>(() => s.CommitEdits(conn, () => Task.CompletedTask, null));
        }
Ejemplo n.º 4
0
        public async Task CommitSuccess()
        {
            // Setup:
            // ... Basic session and db connection
            EditSession s = await GetBasicSession();

            DbConnection conn = new TestSqlConnection(null);

            // ... Add a mock commands for fun
            Mock <RowEditBase> edit = new Mock <RowEditBase>();

            edit.Setup(e => e.GetCommand(It.IsAny <DbConnection>())).Returns <DbConnection>(dbc => dbc.CreateCommand());
            edit.Setup(e => e.ApplyChanges(It.IsAny <DbDataReader>())).Returns(Task.FromResult(0));
            s.EditCache[0] = edit.Object;

            // If: I commit these changes (and await completion)
            bool successCalled = false;
            bool failureCalled = false;

            s.CommitEdits(conn,
                          () => {
                successCalled = true;
                return(Task.FromResult(0));
            },
                          e => {
                failureCalled = true;
                return(Task.FromResult(0));
            });
            await s.CommitTask;

            // Then:
            // ... The task should still exist
            Assert.NotNull(s.CommitTask);

            // ... The success handler should have been called (not failure)
            Assert.True(successCalled);
            Assert.False(failureCalled);

            // ... The mock edit should have generated a command and applied changes
            edit.Verify(e => e.GetCommand(conn), Times.Once);
            edit.Verify(e => e.ApplyChanges(It.IsAny <DbDataReader>()), Times.Once);

            // ... The edit cache should be empty
            Assert.Empty(s.EditCache);
        }
Ejemplo n.º 5
0
        public async Task CommitInProgress()
        {
            // Setup:
            // ... Basic session and db connection
            EditSession s = await GetBasicSession();

            DbConnection conn = new TestSqlConnection(null);

            // ... Mock a task that has not completed
            Task notCompleted = new Task(() => {});

            s.CommitTask = notCompleted;

            // If: I attempt to commit while a task is in progress
            // Then: I should get an exception
            Assert.Throws <InvalidOperationException>(
                () => s.CommitEdits(conn, () => Task.CompletedTask, e => Task.CompletedTask));
        }
Ejemplo n.º 6
0
        public async Task CommitFailure()
        {
            // Setup:
            // ... Basic session and db connection
            EditSession s = await GetBasicSession();

            DbConnection conn = new TestSqlConnection(null);

            // ... Add a mock edit that will explode on generating a command
            Mock <RowEditBase> edit = new Mock <RowEditBase>();

            edit.Setup(e => e.GetCommand(It.IsAny <DbConnection>())).Throws <Exception>();
            s.EditCache[0] = edit.Object;

            // If: I commit these changes (and await completion)
            bool successCalled = false;
            bool failureCalled = false;

            s.CommitEdits(conn,
                          () => {
                successCalled = true;
                return(Task.FromResult(0));
            },
                          e => {
                failureCalled = true;
                return(Task.FromResult(0));
            });
            await s.CommitTask;

            // Then:
            // ... The task should still exist
            Assert.NotNull(s.CommitTask);

            // ... The error handler should have been called (not success)
            Assert.False(successCalled);
            Assert.True(failureCalled);

            // ... The mock edit should have been asked to generate a command
            edit.Verify(e => e.GetCommand(conn), Times.Once);

            // ... The edit cache should not be empty
            Assert.NotEmpty(s.EditCache);
        }