using System; using System.Collections; using NUnit.Framework; using IBatisNet.DataMapper.Test.NUnit; using IBatisNet.DataMapper.Test.Domain; using NUnit.Framework.SyntaxHelpers; namespace IBatisNet.DataMapper.Test.NUnit.SqlMapTests.MSSQL { /// /// Summary description for StatementTest. /// [TestFixture] [Category("MSSQL")] public class StatementTest : BaseTest { #region SetUp & TearDown /// /// SetUp /// [SetUp] public void Init() { InitScript( sqlMap.DataSource, ScriptDirectory + "account-init.sql" ); InitScript( sqlMap.DataSource, ScriptDirectory + "account-procedure.sql", false ); InitScript( sqlMap.DataSource, ScriptDirectory + "ps_SelectAccount.sql", false ); InitScript( sqlMap.DataSource, ScriptDirectory + "category-init.sql" ); InitScript( sqlMap.DataSource, ScriptDirectory + "order-init.sql" ); } /// /// TearDown /// [TearDown] public void Dispose() { /* ... */ } #endregion #region Specific statement test for sql server /// /// Test Insert Account via store procedure /// [Test] public void TestInsertAccountViaStoreProcedure() { Account account = new Account(); account.Id = 99; account.FirstName = "Achille"; account.LastName = "Talon"; account.EmailAddress = "Achille.Talon@somewhere.com"; sqlMap.Insert("InsertAccountViaStoreProcedure", account); Account testAccount = sqlMap.QueryForObject("GetAccountViaColumnName", 99) as Account; Assert.IsNotNull(testAccount); Assert.AreEqual(99, testAccount.Id); } /// /// Test an insert with identity key. /// [Test] public void TestInsertIdentityViaInsertQuery() { Category category = new Category(); category.Name = "toto"; category.Guid = Guid.NewGuid(); int key = (int)sqlMap.Insert("InsertCategory", category); Assert.AreEqual(1, key); } /// /// Test an insert using SCOPE_IDENTITY. /// [Test] public void TestInsertCategoryScope() { Category category = new Category(); category.Name = "toto"; category.Guid = Guid.NewGuid(); sqlMap.QueryForObject("InsertCategoryScope", category, category); Assert.That(category.Id, Is.EqualTo(1)); } /// /// Test Insert Via Insert Statement. /// (Test for IBATISNET-21 : Property substitutions do not occur inside selectKey statement) /// [Test] public void TestInsertViaInsertStatement() { Category category = new Category(); category.Name = "toto"; category.Guid = Guid.NewGuid(); int key = (int)sqlMap.Insert("InsertCategoryViaInsertStatement", category); Assert.AreEqual(1, key); } /// /// Test statement with properties subtitutions /// (Test for IBATISNET-21 : Property substitutions do not occur inside selectKey statement) /// [Test] public void TestInsertCategoryWithProperties() { Category category = new Category(); category.Guid = Guid.NewGuid(); int key = (int)sqlMap.Insert("InsertCategoryWithProperties", category); Category categoryTest = sqlMap.QueryForObject("GetCategory", key) as Category; Assert.AreEqual(key, categoryTest.Id); Assert.AreEqual("Film", categoryTest.Name); Assert.AreEqual(category.Guid, categoryTest.Guid); } /// /// Test guid column/field. /// [Test] public void TestGuidColumn() { Category category = new Category(); category.Name = "toto"; category.Guid = Guid.NewGuid(); int key = (int)sqlMap.Insert("InsertCategory", category); Category categoryTest = (Category)sqlMap.QueryForObject("GetCategory", key); Assert.AreEqual(key, categoryTest.Id); Assert.AreEqual(category.Name, categoryTest.Name); Assert.AreEqual(category.Guid, categoryTest.Guid); } /// /// Test guid column/field through parameterClass. /// [Test] public void TestGuidColumnParameterClass() { Guid newGuid = Guid.NewGuid(); int key = (int)sqlMap.Insert("InsertCategoryGuidParameterClass", newGuid); Category categoryTest = (Category)sqlMap.QueryForObject("GetCategory", key); Assert.AreEqual(key, categoryTest.Id); Assert.AreEqual("toto", categoryTest.Name); Assert.AreEqual(newGuid, categoryTest.Guid); } /// /// Test guid column/field through parameterClass without specifiyng dbType /// [Test] public void TestGuidColumnParameterClassJIRA20() { Guid newGuid = Guid.NewGuid(); int key = (int)sqlMap.Insert("InsertCategoryGuidParameterClassJIRA20", newGuid); Category categoryTest = (Category)sqlMap.QueryForObject("GetCategory", key); Assert.AreEqual(key, categoryTest.Id); Assert.AreEqual("toto", categoryTest.Name); Assert.AreEqual(newGuid, categoryTest.Guid); } /// /// Test Insert Category Via ParameterMap. /// [Test] public void TestInsertCategoryViaParameterMap() { Category category = new Category(); category.Name = "Cat"; category.Guid = Guid.NewGuid(); int key = (int)sqlMap.Insert("InsertCategoryViaParameterMap", category); Assert.AreEqual(1, key); } /// /// Test Update Category with Extended ParameterMap /// [Test] public void TestUpdateCategoryWithExtendParameterMap() { Category category = new Category(); category.Name = "Cat"; category.Guid = Guid.NewGuid(); int key = (int)sqlMap.Insert("InsertCategoryViaParameterMap", category); category.Id = key; category.Name = "Dog"; category.Guid = Guid.NewGuid(); sqlMap.Update("UpdateCategoryViaParameterMap", category); Category categoryRead = null; categoryRead = (Category) sqlMap.QueryForObject("GetCategory", key); Assert.AreEqual(category.Id, categoryRead.Id); Assert.AreEqual(category.Name, categoryRead.Name); Assert.AreEqual(category.Guid.ToString(), categoryRead.Guid.ToString()); } /// /// Test select via store procedure /// [Test] public void TestSelect() { Order order = (Order) sqlMap.QueryForObject("GetOrderWithAccountViaSP", 1); AssertOrder1(order); AssertAccount1(order.Account); } #endregion } }