Update of /cvsroot/springnet/Spring.Net/examples/Spring/Spring.DataQuickStart/test/Spring/Spring.DataQuickStart.Tests/DataQuickStart/Template
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv18095/Template
Modified Files:
ExampleTests.cs ExampleTests.xml
Log Message:
add dataset and generic stored proc example to data access quickstart
Index: ExampleTests.xml
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/examples/Spring/Spring.DataQuickStart/test/Spring/Spring.DataQuickStart.Tests/DataQuickStart/Template/ExampleTests.xml,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** ExampleTests.xml 8 Aug 2007 23:05:48 -0000 1.3
--- ExampleTests.xml 4 Dec 2007 08:25:22 -0000 1.4
***************
*** 4,9 ****
<db:provider id="dbProvider"
! provider="SqlServer-1.1"
! connectionString="Data Source=(local);Database=Northwind;User ID=springqa;Password=springqa;Trusted_Connection=False"/>
<object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
--- 4,9 ----
<db:provider id="dbProvider"
! provider="System.Data.SqlClient"
! connectionString="Data Source=.\SQL2005;Initial Catalog=Northwind;Persist Security Info=True;User ID=springqa;Password=springqa;Trusted_Connection=False"/>
<object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
***************
*** 35,38 ****
--- 35,42 ----
<constructor-arg index="0" ref="dbProvider"/>
</object>
+
+ <object id="customerDataSetDao" type="Spring.DataQuickStart.Dao.Template.CustomerDataSetDao, Spring.DataQuickStart">
+ <property name="AdoTemplate" ref="adoTemplate"/>
+ </object>
</objects>
\ No newline at end of file
Index: ExampleTests.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/examples/Spring/Spring.DataQuickStart/test/Spring/Spring.DataQuickStart.Tests/DataQuickStart/Template/ExampleTests.cs,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** ExampleTests.cs 9 Aug 2007 01:52:56 -0000 1.5
--- ExampleTests.cs 4 Dec 2007 08:25:22 -0000 1.6
***************
*** 1,11 ****
using System.Collections;
!
using NUnit.Framework;
using Spring.Context;
using Spring.Context.Support;
using Spring.Data.Config;
using Spring.Objects.Factory.Xml;
- using Spring.Data;
- using Spring.Data.Config;
using Spring.DataQuickStart.Dao.Template;
--- 1,11 ----
using System.Collections;
! using System.Data;
! using System.Globalization;
using NUnit.Framework;
using Spring.Context;
using Spring.Context.Support;
using Spring.Data.Config;
+ using Spring.Data.Core;
using Spring.Objects.Factory.Xml;
using Spring.DataQuickStart.Dao.Template;
***************
*** 18,21 ****
--- 18,22 ----
{
private IApplicationContext ctx;
+ private AdoTemplate adoTemplate;
[SetUp]
***************
*** 26,29 ****
--- 27,31 ----
ctx = new XmlApplicationContext(
"assembly://Spring.DataQuickStart.Tests/Spring.DataQuickStart.Template/ExampleTests.xml");
+ adoTemplate = ctx["adoTemplate"] as AdoTemplate;
}
***************
*** 96,99 ****
--- 98,198 ----
}
+ [Test]
+ public void DataSetDemo()
+ {
+ CustomerDataSetDao customerDataSetDao = ctx["customerDataSetDao"] as CustomerDataSetDao;
+ DataSet dataSet = customerDataSetDao.GetCustomers();
+ Assert.AreEqual(91, dataSet.Tables["Table"].Rows.Count);
+
+ dataSet = customerDataSetDao.GetCustomers("Customers");
+ Assert.AreEqual(91, dataSet.Tables["Customers"].Rows.Count);
+
+ dataSet = customerDataSetDao.GetCustomersLike("M%");
+ Assert.AreEqual(12, dataSet.Tables["Table"].Rows.Count);
+
+ dataSet = new DataSet();
+ dataSet.Locale = CultureInfo.InvariantCulture;
+
+
+ customerDataSetDao.FillCustomers(dataSet);
+ Assert.AreEqual(91, dataSet.Tables["Customers"].Rows.Count);
+
+
+ AddAndEditRow(dataSet);
+
+
+ try
+ {
+ customerDataSetDao.UpdateWithCommandBuilder(dataSet);
+
+ int count =
+ (int)adoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers where CustomerId = 'NewID'");
+ Assert.AreEqual(1, count);
+
+ count = (int)adoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers where CustomerId = 'ALFKI' and Phone = '030-0074322'");
+ Assert.AreEqual(1, count);
+
+ }
+ finally
+ {
+ //revert changes
+ adoTemplate.ExecuteNonQuery(CommandType.Text, "delete from Customers where CustomerId = 'NewID'");
+ adoTemplate.ExecuteNonQuery(CommandType.Text,
+ "update Customers SET Phone='030-0074321' where CustomerId = 'ALFKI'");
+ }
+
+
+ }
+
+ private static void AddAndEditRow(DataSet dataSet)
+ {
+ DataRow dataRow = dataSet.Tables["Customers"].NewRow();
+ dataRow["CustomerId"] = "NewID";
+ dataRow["CompanyName"] = "New Company Name";
+ dataRow["ContactName"] = "New Name";
+ dataRow["ContactTitle"] = "New Contact Title";
+ dataRow["Address"] = "New Address";
+ dataRow["City"] = "New City";
+ dataRow["Region"] = "NR";
+ dataRow["PostalCode"] = "New Code";
+ dataRow["Country"] = "New Country";
+ dataRow["Phone"] = "New Phone";
+ dataRow["Fax"] = "New Fax";
+ dataSet.Tables["Customers"].Rows.Add(dataRow);
+
+ DataRow alfkiDataRow = dataSet.Tables["Customers"].Rows[0];
+ alfkiDataRow["Phone"] = "030-0074322"; // simple change, last digit changed from 1 to 2.
+ }
+
+ [Test]
+ public void UpdateWithoutCommandBuilder()
+ {
+ CustomerDataSetDao customerDataSetDao = ctx["customerDataSetDao"] as CustomerDataSetDao;
+ try
+ {
+ DataSet dataSet = new DataSet();
+ dataSet.Locale = CultureInfo.InvariantCulture;
+ customerDataSetDao.FillCustomers(dataSet);
+ Assert.AreEqual(91, dataSet.Tables["Customers"].Rows.Count);
+
+ AddAndEditRow(dataSet);
+
+ customerDataSetDao.Update(dataSet);
+
+ int count = (int)adoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers where CustomerId = 'NewID'");
+ Assert.AreEqual(1, count);
+
+ count = (int)adoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers where CustomerId = 'ALFKI' and Phone = '030-0074322'");
+ Assert.AreEqual(1, count);
+
+ } finally
+ {
+ //revert changes
+ adoTemplate.ExecuteNonQuery(CommandType.Text, "delete from Customers where CustomerId = 'NewID'");
+ adoTemplate.ExecuteNonQuery(CommandType.Text,
+ "update Customers SET Phone='030-0074321' where CustomerId = 'ALFKI'");
+ }
+ }
+
private static void AssertsOnCustomerList(IList customerList)
{
|