Here's something below that I did in a later presentation of that
material that used the DB. As to errors, in that TestOperations method
I'm rethrowing exceptions so there shouldn't be any checking needed in
the test, if there is an exception it will fail the test.

    [TestClass]
    public class UserWriteToDBTest : BaseTestClass
    {
        public class GenerateTestData : AbstractOperation
        {
            public override IEnumerable<Row> Execute(IEnumerable<Row>
rows)
            {
                yield return Row.FromObject(new UserFullRecord { Id =
1, Name = "Jim1", Address = "1215 Smith St." });
                yield return Row.FromObject(new UserFullRecord { Id =
2, Name = "Jim2", Address = "1216 Smith St." });
            }
        }

        [TestMethod]
        public void CanWriteToDB()
        {
            var op = new UserWriteToDB();

            TruncateTable(op.ConnectionStringName, op.TargetTable);

            var userRecords = TestOperation(
                new GenerateTestData(),
                op
            );

            Assert.AreEqual(2, CheckRowCount(op.ConnectionStringName,
op.TargetTable));
        }

        private void TruncateTable(string connectionStringName, string
targetTable)
        {
            using (var con = new
SqlConnection(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString))
            using (var cmd = new SqlCommand("TRUNCATE TABLE " +
targetTable, con))
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }

        private int CheckRowCount(string connectionStringName, string
targetTable)
        {
            using(var con = new
SqlConnection(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString))
            using (var cmd = new SqlCommand("SELECT COUNT(*) FROM " +
targetTable, con))
            {
                con.Open();
                return (int)cmd.ExecuteScalar();
            }
        }
    }


On Apr 6, 12:21 pm, Jason Meckley <[email protected]> wrote:
> you may also want to redesign your test to query the database after
> the records are created to prove the database matches the input file.
> testing the records were iterated over doesn't prove the records were
> successfully written.
>
> On Apr 6, 12:37 pm, Chris Hoffman <[email protected]> wrote:
>
>
>
> > Additionally, a slight variation on the test process below *does*
> > succeed.  Is there something stupid I'm missing with respect to making
> > database connections within a test assembly?  I'm kinda learning this
> > test framework at the same time I'm learning Rhino.ETL.
>
> > Thanks.
>
> > On Tue, Apr 6, 2010 at 12:13 PM, khaavren <[email protected]> wrote:
> > > I can't for the life of me adapt
>
> > >http://www.codeproject.com/KB/cs/ETLWithCSharp.aspx
>
> > > to work with a database.  Here is the code (it is short enough that I
> > > don't feel it cumbersome to just post it).  The Assert fails, and nor
> > > do I receive an exception about database access, nor do I see any
> > > activity in SQL Profiler.  It just doesn't work.  Could someone tell
> > > me if I'm doing something blatantly wrong here?
>
> > > Thanks.
>
> > > the test:
>
> > >    [TestClass]
> > >    [DeploymentItem(@".\SampleData\names.txt", "SampleData")]
> > >    public class UserNameWriteDBTest : BaseTestClass
> > >    {
> > >        [TestMethod]
> > >        public void CanWriteDB()
> > >        {
> > >            var numprocessed = 0;
> > >            var unr = new UserNameWriteToDB("uvmcar");
> > >            unr.OnRowProcessed += delegate { numprocessed++ };
>
> > >            var userRecords = new TestProcess(
> > >                new UserNameRead(@".\SampleData\names.txt"),
> > >                unr,
> > >                new GetInputFromDB("uvmcar")
> > >            );
>
> > >            Assert.IsTrue(numprocessed > 0);
> > >        }
> > >    }
>
> > > UserNameWriteToDB:
>
> > >    public class UserNameWriteToDB : ConventionOutputCommandOperation
> > >    {
> > >        public UserNameWriteToDB(string connectionString) :
> > >            base (connectionString){
> > >            Command = "INSERT INTO Users (Id,Name) VALUES(@Id,@Name)";
> > >        }
> > >    }
>
> > > GetInputFromDB:
>
> > >    public class GetInputFromDB : ConventionInputCommandOperation
> > >    {
> > >        public GetInputFromDB(string connectionStringName)
> > >            : base(connectionStringName){
> > >            Command = "SELECT * FROM Users";
> > >            }
> > >    }- Hide quoted text -
>
> - Show quoted text -

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino Tools Dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhino-tools-dev?hl=en.

Reply via email to