2

I have a django project using multiple databases. I am using django-nose as the test runner. Test data is loaded to the default database using fixtures. I have a second database not managed by django models, where I want to create a structure and load some data before testing, using raw sql.

Is there any way to do this in a somehow clean way?

Thank you.

1 Answer 1

2

Ht Stathis,

Here was my scenario and how I solved it - YMMV..

1) I build up a testrunner and overriding the setup_databases method. I needed to do this because I wanted to externally create this database. We had custom tables, and functions. I literally used the same code with one small deviation

class IManageTestRunner(DjangoTestSuiteRunner):
    """This is a 3 line addition to the original method"""

    def setup_databases(self, **kwargs):
        from django.db import connections, DEFAULT_DB_ALIAS

        ### Skipped for brevity ###

        for signature, (db_name, aliases) in dependency_ordered(test_databases.items(), dependencies):
            # Actually create the database for the first connection
            connection = connections[aliases[0]]
            old_names.append((connection, db_name, True))
            test_db_name = connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive)

            # Our little hack...
            if db_name == "bugs":
                create_IManage_instance(django_created_sql = True, mysql_db = "test_bugs",
                                    arg = value, arg2 = value )
            # End of our little hack..

            for alias in aliases[1:]:
                connection = connections[alias]

                ### Skipped for brevity ###

2) Add this to your settings.py TEST_RUNNER = 'IManageTestRunner'

3) Once this was created then I knew my test db was created. I would then create tests which populated externally and test the results against it.

def test_add_property_value(self):
    """Test set / get a value"""
    # This will do some external process which occcurs to the db.
    pm = Pm(mysql_db='test_bugs', p4_port = settings.ICMSERVER_TEST_PORT)
    pmsite, pmproject, pmvariant, pmlibtype, pmlibrary, pmrelease = pm.add_release_tree()
    prop_type, pmvalue = ("string", "Funny Business")
    pmproperty = "%s_%s_basic" % (pmproject.name, prop_type)
    pm.add_property_definition(pmproperty, prop_type=prop_type)
    pm.add_propval(pmproperty, value=pmvalue, project=pmproject.name)

    # Now use Django to pull the value back out..
    project = Project.objects.get(name=pmproject.name)
    property = project.get_property(pmproperty)
    self.assertEqual(pmvalue, property.value)

Hope that helps took me awhile to figure it out. I still have one issue (doing repeat inserts/queries..)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.