diff UnitTests/CS/Data/EventsTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UnitTests/CS/Data/EventsTest.cs	Thu Mar 27 21:46:09 2014 +0400
@@ -0,0 +1,64 @@
+using System;
+using NUnit.Framework;
+using BLToolkit.Data;
+
+namespace Data
+{
+	[TestFixture]
+	public class EventsTest
+	{
+		[Test]
+		public void BeforeAfterInitTest()
+		{
+			using (DbManager db = new DbManager())
+			{
+				db.BeforeOperation += HandleBeforeOperation;
+				db.AfterOperation  += HandleAfterOperation;
+				db.InitCommand     += HandleInitCommand;
+
+				db
+					.SetCommand("SELECT * FROM Person")
+					.ExecuteDataSet();
+			}
+		}
+
+		private static void HandleBeforeOperation(object sender, OperationTypeEventArgs ea)
+		{
+			Console.WriteLine("Before operation: " + ea.Operation);
+		}
+
+		private static void HandleAfterOperation(object sender, OperationTypeEventArgs ea)
+		{
+			Console.WriteLine("After operation:  " + ea.Operation);
+		}
+
+		private static void HandleInitCommand(object sender, InitCommandEventArgs ea)
+		{
+			Console.WriteLine("Init command:  " + ea.Command.CommandText);
+		}
+
+		[Test, ExpectedException(typeof(DataException))]
+		public void ExceptionTest()
+		{
+			using (DbManager db = new DbManager())
+			{
+				db.OperationException += HandleOperationException;
+
+				db
+					.SetCommand("SELECT * FROM NoSuchTableEverExist")
+					.ExecuteDataSet();
+			}
+		}
+
+		private static void HandleOperationException(object sender, OperationExceptionEventArgs ea)
+		{
+			Assert.That(ea, Is.Not.Null);
+			Assert.That(ea.Exception, Is.Not.Null);
+			Assert.That(ea.Exception.Number, Is.Not.Null);
+
+			Console.WriteLine("Operation:    " + ea.Operation);
+			Console.WriteLine("Error number: " + ea.Exception.Number);
+			Console.WriteLine("Exception:    " + ea.Exception.GetBaseException().Message);
+		}
+	}
+}