-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: FetchOrCreate: An idea for automating object construction
PostPosted: Fri Feb 29, 2008 3:06 pm 
Beginner
Beginner

Joined: Tue Sep 04, 2007 12:36 pm
Posts: 23
Hibernate version: 1.2

One unit test phenomenon I continually revisit:

On TestFixtureSetup, I use SchemaExport (a very nice feature) to give me a fresh database.

On Test Setup, I use a test data "factory" to populate the database with some meaningful data. The testdatafactory is some very common code that varies by domain.


On TearDown, I clean all of the data from all the tables... something like this

Code:
  using (SqlCommand cmd = new SqlCommand(null, session.Connection as SqlConnection))
                {
                    cmd.CommandText =
                        @"EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL';
                        EXEC sp_MSForEachTable 'delete from ?';
                        EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL';";
                    cmd.ExecuteNonQuery();
            }


On TestFixutreTearDown, I use schemaexport to drop the schema

I am interested in automating my test data factory code, to remove some of the more mundane work involved with building a graph of dummy domain objects.
I was wondering if this syntax was attractive to you:

Code:
ICriteria criteria = session.CreateCriteria(typeof(Cat)).Add(Expression.Eq("Name","Sam")).Add(Expression.Eq("Owner",ownerObj));
Cat cat = criteria.FetchOrCreate<Cat>();


If the cat exists, return it, otherwise, construct it and add it to the session.

_________________
^^If this post helped you, make sure to rate it Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.