<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="TestFu DatabasePopulator Crud Harness" %> <%@ Property Name="Namespace" Type="System.String" Default="Populators" Category="Output" Description="Namespace of the populator." %> <%@ Property Name="DataSetNameFormat" Type="System.String" Default="{0}DataSet" Category="Output" Description="Format string to create the DatabaseSet class name." %> <%@ Property Name="DalNamespace" Type="System.String" Default="Northwind" Category="Output" Description="Namespace of the Dal" %> <%@ Property Name="DalFormatName" Type="System.String" Default="{0}" Category="Output" Description="Format string to create the Dal class name" %> <%@ Property Name="CrudPopulatorNameFormat" Type="System.String" Default="{0}CrudPopulator" Category="Output" Description="Format string to create the CrudPopulator class name." %> <%@ Property Name="DatabasePopulatorNameFormat" Type="System.String" Default="{0}DatabasePopulator" Category="Output" Description="Format string to create the DatabasePopulator class name." %> <%@ Property Name="TableNamePrefix" Type="System.String" Default="" Category="Output" Optional="true" Description="Prefix to be removed." %> <%@ Property Name="TablePopulatorNameFormat" Type="System.String" Default="{0}TablePopulator" Category="Output" Description="Format string to create the TablePopulator class names." %> <%@ Property Name="ColumnDataGeneratorNameFormat" Type="System.String" Default="{0}DataGenerator" Category="Output" Description="Format string to create the ColumnGenenerator properties." %> <%@ Property Name="UniqueValidatorNameFormat" Type="System.String" Default="{0}Validator" Category="Output" Description="Format string to create the UniqueValidator properties." %> <%@ Property Name="ForeignKeyProviderNameFormat" Type="System.String" Default="{0}Provider" Category="Output" Description="Format string to create the ForeignKeyProviderNameFormat properties." %> <%@ Property Name="Database" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="Target database for the IDatabasePopulator." %> <%@ Assembly Name="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer" %> ////////////////////////////////////////////////////////////////// // // Strongly typed CRUD populator for the <%= DatabaseName %> // // This file was generated by CodeSmith. Do not edit it! Modify // the template if you need to tweak it for you. // ////////////////////////////////////////////////////////////////// using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using MbUnit.Core.Framework; using MbUnit.Framework; using TestFu.Data; using TestFu.Data.SqlClient; using TestFu.Data.Graph; using <%= DalNamespace %>; namespace <%= Namespace %> { /// /// A CRUD harness for the <%= DatabaseName %> database. /// /// /// Note to implementors: /// /// Implementors should modify the CodeSmith template that generated /// this class to inject their Create,Update,Read and Delete methods. /// Each generated method contains a /// that should be replaced with the proper DAL code. /// /// /// The Create* methods create a new row and verify that it is /// present in the database using the Read* methods. /// The Update* methods apply an update between two rows and /// verify that the update was succesful using Read* methods. /// The Delete* remove a row from the tables and verify /// that it has been removed using Read* methods. /// /// public class <%= CrudPopulatorName %> : <%= DatabasePopulatorName %> { #region Fields private <%= DalNamespace %>.<%= Format(DalFormatName, DatabaseName) %> dal = new <%= DalNamespace %>.<%= Format(DalFormatName, DatabaseName) %>(); #endregion #region Constructors /// /// Initializes a new with the /// connection string of the database. /// /// /// A valid conneciton string to the target database. /// /// /// The is a /// null reference (Nothing in Visual Basic). /// /// /// The is empty. /// public <%= CrudPopulatorName %>(string connectionString) :this(new <%= DataSetName %>(), connectionString) {} /// /// Initializes a new with the /// connection string of the database. /// /// /// A representing the structure of the /// <%= DatabaseName %> database. /// /// /// A valid conneciton string to the target database. /// /// /// The or the is a /// null reference (Nothing in Visual Basic). /// /// /// The is empty. /// public <%= CrudPopulatorName %>( DataSet dataSet, string connectionString ) :base(dataSet,connectionString) {} #endregion <% foreach(TableSchema table in this.Database.Tables) {%> #region <%= table.Name %> /// /// Creates a new in the /// <%= table.Name %> table. /// /// /// The created instance /// public virtual DataRow Create<%= Format(table.Name) %>() { // generate row DataRow row = this.<%=GetTablePopulatorPropertyName(table)%>.Generate(); // Add the row to the <%= table.Name %> table here <%= TableRow(table) %> irow = new <%= TableRow(table) %>(); <% foreach(ColumnSchema column in table.Columns) {%> <% if (column.AllowDBNull) {%> if (row["<%= column.Name%>"] is DBNull) <% if (column.SystemType.IsValueType) {%> irow.Is<%= Format(column.Name) %>Null = true; <% } else { %> irow.<%= Format(column.Name) %> = null; <%}%> else irow.<%= Format(column.Name) %> = (<%= column.SystemType %>)row["<%= column.Name %>"]; <% } else {%> irow.<%= Format(column.Name) %> = (<%= column.SystemType %>)row["<%= column.Name %>"]; <% } %> <% } // foreach(ColumnSchema column in table.Columns) %> try { this.dal.<%= TableCollection(table) %>.Insert(irow); } catch(Exception ex) { Console.WriteLine("Inserted row:"); DisplayRow(row); throw new ApplicationException("Insertion in <%= table.Name %> failed",ex); } // RapTier does not return the new identity when the primary key is // identity. Hence, we cannot verify anything! return row; } <% if (table.PrimaryKey.MemberColumns.Count != table.Columns.Count) {%> /// /// Updates a row (chosen randomly) with a new generated row in the /// <%= table.Name %> table. /// /// /// The update instance /// public virtual DataRow Update<%= Format(table.Name) %>() { // generate row DataRow row = this.<%=GetTablePopulatorPropertyName(table)%>.GetRandomRow(); // update random return Update<%= Format(table.Name) %>(row); } /// /// Updates with in the /// <%= table.Name %> table. /// /// /// The update instance /// public virtual DataRow Update<%= Format(table.Name) %>(DataRow row) { if (row==null) throw new ArgumentNullException("row"); // changes values this.<%= GetTablePopulatorPropertyName(table)%>.ChangeRowValues(row); // Update the rowToUpdate with row in the <%= table.Name %> table <%= TableRow(table) %> urow = new <%= TableRow(table) %>(); <% foreach(ColumnSchema column in table.Columns) {%> <% if (column.AllowDBNull) {%> if (row["<%= column.Name%>"] is DBNull) <% if (column.SystemType.IsValueType) {%> urow.Is<%= Format(column.Name) %>Null = true; <% } else { %> urow.<%= Format(column.Name) %> = null; <%}%> else urow.<%= Format(column.Name) %> = (<%= column.SystemType %>)row["<%= column.Name %>"]; <% } else {%> urow.<%= Format(column.Name) %> = (<%= column.SystemType %>)row["<%= column.Name %>"]; <% } %> <% } // foreach(ColumnSchema column in table.Columns) %> try { this.dal.<%= TableCollection(table) %>.Update(urow); } catch(Exception ex) { Console.WriteLine("Updated row:"); DisplayRow(row); throw new ApplicationException("Update in <%= table.Name %> failed",ex); } return row; } <%}// if (table.PrimaryKey.MemberColumns.Count != table.Columns.COunt) {%> /// /// Deletes a random row /// from the <%= table.Name %> table. /// public virtual void Delete<%= Format(table.Name) %>() { // choose a row DataRow row = this.<%=GetTablePopulatorPropertyName(table)%>.GetRandomRow(); // delete this.Delete<%= Format(table.Name) %>(row); } /// /// Deletes the row /// from the <%= table.Name %> table. /// public virtual void Delete<%= Format(table.Name) %>(DataRow row) { if (row==null) throw new ArgumentNullException("row"); // Delete the row from the <%= table.Name %> table try { this.dal.<%= TableCollection(table) %>.DeleteByPrimaryKey( <% for(int i = 0;i <%= (i!=0) ? "," : "" %>(<%= column.SystemType %>)row["<%= column.Name %>"] <% } // foreach(ColumnSchema column in table.Columns) %> ); } catch(Exception ex) { Console.WriteLine("Deleted row:"); DisplayRow(row); throw new ApplicationException("Delete in <%= table.Name %> failed",ex); } // check it is not in the table DataRow readRow = Read<%= Format(table.Name) %>(row); if (readRow!=null) { Console.WriteLine("Row to delete"); DisplayRow(row); Console.WriteLine("Not deleted row"); DisplayRow(readRow); Assert.Fail("Row delete failed"); } } /// /// Reads the <%= table.Name %> table for a row matching /// and returns it. /// /// /// Read row if foud; otherwize /// a null reference. /// public virtual DataRow Read<%= Format(table.Name) %>(DataRow row) { if (row==null) throw new ArgumentNullException("row"); <%= TableRow(table) %> readRow = null; try { readRow = this.dal.<%= TableCollection(table) %>.GetByPrimaryKey( <% for(int i = 0;i <%= (i!=0) ? "," : "" %>(<%= column.SystemType %>)row["<%= column.Name %>"] <% } // foreach(ColumnSchema column in table.Columns) %> ); } catch(Exception ex) { Console.WriteLine("Read row:"); DisplayRow(row); throw new ApplicationException("Read in <%= table.Name %> failed",ex); } if (readRow==null) return null; DataRow rrow = row.Table.NewRow(); <% foreach(ColumnSchema column in table.Columns) {%> <% if (column.SystemType.IsValueType && column.AllowDBNull) {%> if (readRow.Is<%= Format(column.Name) %>Null) rrow["<%=column.Name %>"] = DBNull.Value; else rrow["<%=column.Name %>"] = readRow.<%= Format(column.Name) %>; <%} else { %> rrow["<%=column.Name %>"] = readRow.<%= Format(column.Name) %>; <%}%> <% } // foreach(ColumnSchema column in table.Columns) %> return rrow; } #endregion <%} // foreach(TableSchema table in this.Database.Tables) %> #region Misc protected virtual void DisplayRow(DataRow row) { if (row==null) throw new ArgumentNullException("row"); foreach(DataColumn column in row.Table.Columns) { Console.WriteLine("\t{0}: {1}", column.ColumnName, row[column].ToString()); } } #endregion } }