views:

54

answers:

2

What's the best approach for testing multiple db's in a s#arparch project?

The current SetUp() code in MappingIntegrationTests tries to test each assembly against the first database.

string[] mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();

 configuration = NHibernateSession.Init(new SimpleSessionStorage(), mappingAssemblies,
                                   new AutoPersistenceModelGenerator().Generate(),
                                   "../../../../app/Humanities.IBusiness.Web/NHibernate.config");

Has anyone managed to correctly test each mapping against the appropriate database schema?

+1  A: 

Al, to be honest I do not know the extent that the users are using Multiple Databases. This is something I believe a few very vocal people lobbied for in the beginning of the projects lifecycle. This is something I was going to ask the community about, looks like the time has come for the question to be asked.

What you might need to do is move the set-up code into individual methods. While I am not crazy with breaking the DRY principal, it would seem in this case it is required.

Alec

Alec Whittington
Well, I can answer you that I use and really need Multiple Databases in 2 projects :)When a user creates a new account, we create it on production and test servers
Rafael Mueller
Rafael, Well that answers that one. Hit me up via email when you get a chance. Would like to pick your brain on multiple databases in general.Alec
Alec Whittington
@Alec, I use multiple databases in all my large projects (4+) as well, for what it's worth.
Martin Aatmaa
+1  A: 

Hey Alec, thanks for the reply. I've hacked a bit of a solution - it aint pretty but it does smoke test dodgy mappings across multiple db's

In the set up I add the following:

private List<string> sessionKeys;
        [SetUp]
        public virtual void SetUp()
        {
            string[] mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();
            configuration = NHibernateSession.Init(new SimpleSessionStorage(), mappingAssemblies,
                                   new AutoPersistenceModelGenerator().Generate(),
                                   "../../../../app/Humanities.IBusiness.Web/NHibernate.config");

            /*NEW CODE */
            var configuration2 = NHibernateSession.AddConfiguration(DataGlobals.ROLES_DB_FACTORY_KEY,
                mappingAssemblies,
                new AutoPersistenceModelGenerator().Generate(),
                "../../../../app/Humanities.IBusiness.Web/NHibernateForRolesDb.config",null,null, null);
            sessionKeys = new List<string>();
            sessionKeys.Add(DataGlobals.DEFAULT_DB_KEY);
            sessionKeys.Add(DataGlobals.ROLES_DB_FACTORY_KEY);

Then in the CanConfirmDatabaseMatchesMappings

foreach (var entry in allClassMetadata)
                {
                    bool found = false;
                    foreach (string key in sessionKeys)
                    {
                        ISession session = NHibernateSession.CurrentFor(key);
                        try
                        {
                            session.CreateCriteria(entry.Value.GetMappedClass(EntityMode.Poco))
                                .SetMaxResults(0).List();
                            found = true;
                        }
                        catch (Exception ex) { }

                    }
                    if (found == false)
                        throw new MappingException("Mapping not found for " + entry.Key.ToString());
                }

Not sure if it's a full answer but better than nothing :)

Any thoughts?

ActualAl
Al, I do not see a problem with that solution. While, as you pointed out, not elegant the big questions is does it fit your needs? I think the answer is a resounding yes!Would you mind posting your solution on the S# google group to show others? Might even want to make a wiki page for it. At the very minmum it will show others and promote some discussion on the topic.Alec
Alec Whittington
Will do Al. Thanks
ActualAl