Click here to Skip to main content
15,892,517 members
Articles / Programming Languages / C#

DataTable Synchronization Manager

Rate me:
Please Sign up or sign in to vote.
4.94/5 (12 votes)
4 Mar 20065 min read 73.3K   1.4K   69  
Adds synchronization to the DataTable Transaction Logger.
using System;
using System.Collections.Generic;
using System.Data;

using Vts.UnitTest;

using Clifton.Data;

namespace TransactionLoggerUnitTests
{
	[TestFixture]
	[ProcessTest]
	public class LoggerTests
	{
		protected DataTable dt;
		protected DataTableTransactionLog dttl;
		protected DataRow row;

		[TestFixtureSetUp]
		public void FixtureSetup()
		{
			dt = new DataTable();
			dt.Columns.Add(new DataColumn("LastName", typeof(string)));
			dt.Columns.Add(new DataColumn("FirstName", typeof(string)));

			dttl = new DataTableTransactionLog();
			dttl.SourceTable = dt;
		}

		[Test, Sequence(1)]
		public void NewRow()
		{
			row = dt.NewRow();
			Assertion.Assert(dttl.Log.Count == 1, "Expected one entry.");
			Assertion.Assert(dttl.Log[0].TransactionType == DataTableTransactionRecord.RecordType.NewRow, "Expected new row transaction.");
		}

		[Test, Sequence(2)]
		public void SetFields()
		{
			row["LastName"] = "Clifton";
			row["FirstName"] = "Marc";
			dt.Rows.Add(row);
			Assertion.Assert(dttl.Log.Count == 3, "Expected three entries.");
			Assertion.Assert(dttl.Log[1].TransactionType == DataTableTransactionRecord.RecordType.ChangeField, "Expected change field transaction.");
			Assertion.Assert(dttl.Log[2].TransactionType == DataTableTransactionRecord.RecordType.ChangeField, "Expected change field transaction.");
			Assertion.Assert(dttl.Log[1].NewValue.ToString()=="Clifton", "Incorrect new value.");
			Assertion.Assert(dttl.Log[2].NewValue.ToString() == "Marc", "Incorrect new value.");
		}

		[Test, Sequence(3)]
		public void CollectNothing()
		{
			dttl.CollectUncommittedRows();
			Assertion.Assert(dt.Rows.Count == 1, "Committed row was collected!");
		}

		[Test, Sequence(4)]
		public void CollectUncommitted()
		{
			dt.NewRow();
			dttl.CollectUncommittedRows();
			Assertion.Assert(dttl.Log.Count == 3, "Expected three entries.");
		}

		[Test, Sequence(5)]
		public void RevertFirstNameChange()
		{
			dttl.Revert(2);
			Assertion.Assert(dt.Rows[0]["LastName"].ToString() == "Clifton", "Incorrect value.");
			Assertion.Assert(dt.Rows[0]["FirstName"]==DBNull.Value, "Incorrect new value.");
		}

		[Test, Sequence(6)]
		public void RevertLastNameChange()
		{
			dttl.Revert(1);
			Assertion.Assert(dt.Rows[0]["LastName"] == DBNull.Value, "Incorrect value.");
			Assertion.Assert(dt.Rows[0]["FirstName"] == DBNull.Value, "Incorrect new value.");
		}

		[Test, Sequence(7)]
		public void RevertNewRowChange()
		{
			dttl.Revert(0);
			Assertion.Assert(dt.Rows.Count == 0, "Row should have been deleted.");
		}

		[Test, Sequence(8)]
		public void ApplyNewRow()
		{
			dttl.Apply(0);
			Assertion.Assert(dt.Rows.Count == 1, "Row was not added.");
			Assertion.Assert(dt.Rows[0]["LastName"] == DBNull.Value, "Incorrect value.");
			Assertion.Assert(dt.Rows[0]["FirstName"] == DBNull.Value, "Incorrect new value.");
		}

		[Test, Sequence(9)]
		public void ApplyLastName()
		{
			dttl.Apply(1);
			Assertion.Assert(dt.Rows.Count == 1, "Row was not added.");
			Assertion.Assert(dt.Rows[0]["LastName"].ToString() == "Clifton", "Incorrect value.");
			Assertion.Assert(dt.Rows[0]["FirstName"] == DBNull.Value, "Incorrect new value.");
		}

		[Test, Sequence(10)]
		public void ApplyFirstName()
		{
			dttl.Apply(2);
			Assertion.Assert(dt.Rows.Count == 1, "Row was not added.");
			Assertion.Assert(dt.Rows[0]["LastName"].ToString() == "Clifton", "Incorrect value.");
			Assertion.Assert(dt.Rows[0]["FirstName"].ToString() == "Marc", "Incorrect new value.");
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions