class TestDatabaseInitialiser(unittest.TestCase):
    """
    In order to use these tests you need to have a config file named
    test_openhea.cfg in the directory you are running the tests from
    (i.e. the src directory).

        [database]
        superuser = root
        superuser_password = s00pers3cur3
        database = test_openhea
        user = openhea
        password = hea2012
        schema_script_path = tmp

    This should contain database credentials for a database that exists
    in mysql for testing.  This database will be toyed around with a lot.
    Obviously avoid pointing it at a database you care about...

    """

    def setUp(self):
        self.helper = DatabaseHelper()
        self.config = self.helper.config
        self.helper.start_tests()

    def tearDown(self):
        # drop the database
        self.helper.end_tests()

    def test___init__(self):
        database_initialiser = DatabaseInitialiser(self.config)
        # FIXME: this test is probably bogus
        # we're really just checking we manage to create the
        # object okay.
        expected = "latest update on 2012-05-16"
        self.assertEqual(expected, database_initialiser.latestupdatestring)

    def test_createDatabase(self):
        sql_filename = 'openheadb_mysql.sql'
        database_initialiser = DatabaseInitialiser(self.config)
        self.helper.setup_db_file(sql_filename)
        assert database_initialiser.createDatabase(sql_filename)
class TestSQLAlchemy(unittest.TestCase):
    """
    In order to use these tests you need to have a config file named
    test_openhea.cfg in the directory you are running the tests from
    (i.e. the src directory).

        [database]
        superuser = root
        superuser_password = s00pers3cur3
        database = test_openhea
        user = openhea
        password = hea2012
        schema_script_path = tmp

    This should contain database credentials for a database that exists
    in mysql for testing.  This database will be toyed around with a lot.
    Obviously avoid pointing it at a database you care about...

    """

    def setUp(self):
        self.helper = DatabaseHelper()
        self.helper.start_tests()

    def tearDown(self):
        # drop the database
        if hasattr(self, 'session') and self.session:
            self.session.rollback()
        self.helper.end_tests()

    def test_connection_string(self):
        session = self.get_session()

        self.assertEqual(session.query(Project).count(), 0)

        crop = Project(project="Testin' the alchemy")
        session.add(crop)

        self.assertEqual(session.query(Project).count(), 1)

        session.commit()

    def get_session(self):
        # NOTE: this isn't like a real session,
        # the class gets created fresh for every single
        # test so there is no point in caching the session
        # creator properly.
        #self.helper.setup_clean_db()
        cs = self.helper.config.sqlalchemy_connection_string()
        engine = create_engine(cs)
        metadata.bind = engine
        metadata.create_all()
        Session = sessionmaker(bind=engine)
        self.session = Session()
        return self.session

    def test_geo_type(self):
        session = self.get_session()
        wkt_subdiv = "POLYGON((-79.8 38.5, -80.03 38.2, -80.02 37.89, -79.92 37.75, -79.8 38.5))"
        subdivision = Subdivision(subdivision='newsub',
                coordinates=WKTSpatialElement(wkt_subdiv))
        session.add(subdivision)
        self.assertEqual(session.query(Subdivision).count(), 1)
        session.commit()

        # NOTE: one odd thing to note is that the session commit/rollback must
        # be the last thing you do with the session.

        # for more information on using the geo types take a look
        # here.
        # http://www.geoalchemy.org/tutorial.html#performing-spatial-queries

    def test_schema(self):
        cs = self.helper.config.sqlalchemy_connection_string()
        engine = create_engine(cs)
        metadata.bind = engine
        metadata.create_all()