<%@ 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="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" 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 MbUnit.Core.Framework; using MbUnit.Framework; using TestFu.Data; using TestFu.Data.SqlClient; using TestFu.Data.Graph; 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 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 <%= DatabasePopulatorName %>(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(); // extract info <% foreach(ColumnSchema column in table.Columns) {%> <%= FetchColumnValue(column) %> <% } // foreach(ColumnSchema column in table.Columns) %> // TODO: add the row to the <%= table.Name %> table here <%= DalTableName(table) %>DB db = new <%= DalTableName(table) %>DB(); row["<%= table.PrimaryKey.MemberColumns[0].Name%>"] = db.Add<%= DalTableName(table) %>( <% for(int i = 0;i <%= FormatLower(column.Name) %> <% } else { %> ,<%= FormatLower(column.Name) %> <% } %> <% } // foreach(ColumnSchema column in table.Columns) %> ); // check Create has been successfull DataRow readRow = Read<%= Format(table.Name)%>(row); if (readRow==null) { DisplayRow(row); Assert.Fail("Row creation failed"); } else { // verify that data are equal DataAssert.AreEqual(row, readRow); } return row; } /// /// 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)%>.Generate(); // update random return Update<%= Format(table.Name) %>(row); } /// /// Updates a row (chosen randomly) with a in the /// <%= table.Name %> table. /// /// /// The update instance /// public virtual DataRow Update<%= Format(table.Name) %>(DataRow row) { if (row==null) throw new ArgumentNullException("row"); // choose a row DataRow rowToUpdate = this.<%=GetTablePopulatorPropertyName(table)%>.GetRandomRow(); return Update<%= Format(table.Name) %>(row, rowToUpdate); } /// /// Updates with in the /// <%= table.Name %> table. /// /// /// The update instance /// public virtual DataRow Update<%= Format(table.Name) %>(DataRow row, DataRow rowToUpdate) { if (row==null) throw new ArgumentNullException("row"); if (rowToUpdate==null) throw new ArgumentNullException("rowToUpdate"); // extract info <% foreach(ColumnSchema column in table.Columns) {%> <%= FetchColumnValue(column) %> <% } // foreach(ColumnSchema column in table.Columns) %> // TODO: update the rowToUpdate with row in the <%= table.Name %> table <%= DalTableName(table) %>DB db = new <%= DalTableName(table) %>DB(); db.Update<%= DalTableName(table) %>( <% for(int i = 0;i <%= FormatLower(column.Name) %> <% } else { %> ,<%= FormatLower(column.Name) %> <% } %> <% } // foreach(ColumnSchema column in table.Columns) %> ); // check Update has been successfull DataRow readRow = Read<%= Format(table.Name)%>(rowToUpdate); if (readRow==null) { Console.WriteLine("New row:"); DisplayRow(row); Console.WriteLine("Row to update:"); DisplayRow(rowToUpdate); Assert.Fail("Row update failed"); } else { // verify that data are equal DataAssert.AreEqual(rowToUpdate, readRow); } return rowToUpdate; } /// /// 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"); // extract info <% foreach(ColumnSchema column in table.PrimaryKey.MemberColumns) {%> <%= FetchColumnValue(column) %> <% } // foreach(ColumnSchema column in table.Columns) %> // TODO: delete the row from the <%= table.Name %> table <%= DalTableName(table) %>DB db = new <%= DalTableName(table) %>DB(); db.Delete<%= DalTableName(table) %>( <% for(int i = 0;i <%= FormatLower(column.Name) %> <% } else { %> ,<%= FormatLower(column.Name) %> <% } %> <% } // foreach(ColumnSchema column in table.Columns) %> ); // 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"); // extract info <% foreach(ColumnSchema column in table.Columns) {%> <%= FetchColumnValue(column) %> <% } // foreach(ColumnSchema column in table.Columns) %> // TODO: read the data from the db and return the row, // if not found, return null <%= DalTableName(table) %>DB db = new <%= DalTableName(table) %>DB(); using(IDataReader dr = db.Get<%= DalTableName(table) %>DataReader( <% for(int i = 0;i <%= FormatLower(column.Name) %> <% } else { %> ,<%= FormatLower(column.Name) %> <% } %> <% } // foreach(ColumnSchema column in table.Columns) %> )) { if (!dr.Read()) return null; DataRow readRow = row.Table.NewRow(); for (int i = 0; i < dr.FieldCount; ++i) readRow[i] = dr[i]; return readRow; } } #endregion <%} // foreach(TableSchema table in this.Database.Tables) %> 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()); } } } }