"""
This file is part of open-hea.

open-hea is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

open-hea is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with open-hea.  If not, see <http://www.gnu.org/licenses/>.
"""


__author__ = 'Colin Newell <colin.newell@gmail.com>'
__date__ = 'September 2012'
__credits__ = ''

import io
import unittest

from openhea.model.config_parser import OpenHEAConfig


class TestOpenHEAConfig(unittest.TestCase):
    def test___init__(self):
        open_ihm_config = OpenHEAConfig()

    def test_database_config(self):
        open_ihm_config = OpenHEAConfig()

        expected = {
            'host': 'localhost',
            'database': 'openheadb',
            'user': 'openhea',
            'password': 'hea2012',
            'port': 3306,
            'superuser': 'root',
            'superuser_password': '',
        }
        self.assertEqual(expected, open_ihm_config.database_config())

    def test_read_config_file(self):
        open_ihm_config = OpenHEAConfig()
        config_file = """
[database]
superuser = master
superuser_password = password
port = 3307
        """
        open_ihm_config.readfp(io.BytesIO(config_file))
        expected = {
            'host': 'localhost',
            'database': 'openheadb',
            'user': 'openhea',
            'password': 'hea2012',
            'port': 3307,
            'superuser': 'master',
            'superuser_password': 'password',
        }
        self.assertEqual(expected, open_ihm_config.database_config())

    def test_read_config_file_port_default(self):
        open_ihm_config = OpenHEAConfig()
        config_file = """
[database]
superuser = master
superuser_password = password
        """
        open_ihm_config.readfp(io.BytesIO(config_file))
        expected = {
            'host': 'localhost',
            'database': 'openheadb',
            'user': 'openhea',
            'password': 'hea2012',
            'port': 3306,
            'superuser': 'master',
            'superuser_password': 'password',
        }
        self.assertEqual(expected, open_ihm_config.database_config())

    def test_database_config_dbinfo(self):
        open_ihm_config = OpenHEAConfig()
        expected = {
            'host': 'localhost',
            'database': 'openheadb',
            'user': 'openhea',
            'password': 'hea2012',
            'charset': 'utf8',
            'use_unicode': True,
            'get_warnings': True,
        }
        self.assertEqual(expected, open_ihm_config.dbinfo())

    def test_database_config_superuser_dbinfo(self):
        open_ihm_config = OpenHEAConfig()
        expected = {
            'host': 'localhost',
            'database': 'openheadb',
            'user': 'root',
            'password': '',
            'charset': 'utf8',
            'use_unicode': True,
            'get_warnings': True,
        }
        self.assertEqual(expected, open_ihm_config.superuser_dbinfo())

    def test_database_config_superuser_dbinfo_no_file(self):
        open_ihm_config = OpenHEAConfig()
        open_ihm_config.read('not_there.cfg')
        expected = {
            'host': 'localhost',
            'database': 'openheadb',
            'user': 'root',
            'password': '',
            'charset': 'utf8',
            'use_unicode': True,
            'get_warnings': True,
        }
        self.assertEqual(expected, open_ihm_config.superuser_dbinfo())

    def test_database_config_issue_38(self):
        # this issue was caused by not protecting the 
        # default values dictionary so it got broken
        # after using it for one purpose.
        open_ihm_config = OpenHEAConfig()
        expected = {
            'host': 'localhost',
            'database': 'openheadb',
            'user': 'openhea',
            'password': 'hea2012',
            'charset': 'utf8',
            'use_unicode': True,
            'get_warnings': True,
        }
        self.assertEqual(expected, open_ihm_config.dbinfo())

        expected = {
            'host': 'localhost',
            'database': 'openheadb',
            'user': 'root',
            'password': '',
            'charset': 'utf8',
            'use_unicode': True,
            'get_warnings': True,
        }
        self.assertEqual(expected, open_ihm_config.superuser_dbinfo())

    def test_sqlalchemy_config(self):
        open_ihm_config = OpenHEAConfig()
        expected = 'mysql+mysqldb://openhea:hea2012@localhost/openheadb'
        self.assertEqual(expected,
                        open_ihm_config.sqlalchemy_connection_string())

    def test_sqlalchemy_config_file(self):
        open_ihm_config = OpenHEAConfig()
        config_file = """
[database]
superuser = master
superuser_password = password
driver = mysql+mysqlconnector
        """
        open_ihm_config.readfp(io.BytesIO(config_file))
        expected = 'mysql+mysqlconnector://master:password@localhost/openheadb'
        self.assertEqual(expected,
                    open_ihm_config.sqlalchemy_superuser_connection_string())

    def test_sqlalchemy_config_socket(self):
        open_ihm_config = OpenHEAConfig()
        config_file = """
[database]
superuser = master
superuser_password = password
driver = mysql+mysqlconnector
unix_socket = /home/jenkins/mysql/mysql.sock
        """
        open_ihm_config.readfp(io.BytesIO(config_file))
        expected = 'mysql+mysqlconnector://master:password@localhost/openheadb?unix_socket=/home/jenkins/mysql/mysql.sock'
        self.assertEqual(expected,
                    open_ihm_config.sqlalchemy_superuser_connection_string())

    def test_schema_script_path(self):
        open_ihm_config = OpenHEAConfig()
        # FIXME: might need to use a regex to make this pass
        # on windows?
        expected = 'model/scripts'
        script_path = open_ihm_config.schema_script_path()
        self.assertEqual(expected, script_path[0 - len(expected):])

    def test_old_connection_unix_port(self):
        open_ihm_config = OpenHEAConfig()
        config_file = """
[database]
superuser = master
superuser_password = password
port = 3307
unix_socket = /home/jenkins/mysql/mysql.sock
        """
        open_ihm_config.readfp(io.BytesIO(config_file))
        sexpected = {
            'host': 'localhost',
            'database': 'openheadb',
            'user': 'master',
            'password': 'password',
            'charset': 'utf8',
            'use_unicode': True,
            'get_warnings': True,
            'unix_socket': '/home/jenkins/mysql/mysql.sock',
        }
        self.assertEqual(sexpected, open_ihm_config.superuser_dbinfo())
        expected = {
            'host': 'localhost',
            'database': 'openheadb',
            'user': 'openhea',
            'password': 'hea2012',
            'charset': 'utf8',
            'use_unicode': True,
            'get_warnings': True,
            'unix_socket': '/home/jenkins/mysql/mysql.sock',
        }
        self.assertEqual(expected, open_ihm_config.dbinfo())

if __name__ == '__main__':
    unittest.main()
