"""Test the cea.config.Configuration()"""

import unittest
import pickle
import os
import cea.config


class TestConfiguration(unittest.TestCase):
    def test_can_be_pickled(self):
        config = cea.config.Configuration()
        config = pickle.loads(pickle.dumps(config))
        self.assertIsNotNone(config)

    def test_changing_scenario(self):
        config = cea.config.Configuration()
        config.scenario = os.path.dirname(__file__)
        self.assertEquals(config.scenario, config.general.scenario)

    def test_pickling_parameters(self):
        config = cea.config.Configuration()
        config.scenario = os.path.dirname(__file__)
        config = pickle.loads(pickle.dumps(config))
        self.assertEquals(config.scenario, config.general.scenario)
        self.assertEquals(config.scenario, os.path.dirname(__file__))

    def test_update_parameter_value(self):
        config = cea.config.Configuration()
        config.general.parameters['scenario'].set('C:/test')
        self.assertEquals(config.scenario, r'C:\test')

    def test_update_parameter_values_after_pickling(self):
        config = cea.config.Configuration()
        config.general.parameters['scenario'].set('C:/test')
        config = pickle.loads(pickle.dumps(config))
        self.assertEquals(config.scenario, r'C:\test')

    def test_applying_parameters(self):
        config = cea.config.Configuration()
        config.apply_command_line_args(['--scenario', 'C:/test'], ['general'])
        self.assertEquals(config.scenario, r'C:\test')
        self.assertEquals(config.scenario, config.general.scenario)
        self.assertEquals(config.scenario, config.general.scenario)
        config = pickle.loads(pickle.dumps(config))
        config.scenario = 'foo'
        self.assertEquals(config.scenario, config.general.scenario)

    def test_setting_weather(self):
        config = cea.config.Configuration()
        self.assertEquals(config.weather, config.general.weather)
        config.weather = 'Brussels'
        self.assertEquals(config.weather, config.general.weather)
        self.assert_(config.weather.endswith('Brussels.epw'), config.weather)

    def test_setting_weather_pickling(self):
        config = cea.config.Configuration()
        self.assertEquals(config.weather, config.general.weather)
        config.weather = 'Brussels'
        config = pickle.loads(pickle.dumps(config))
        self.assertEquals(config.weather, config.general.weather)
        self.assert_(config.weather.endswith('Brussels.epw'))

    def test_weather_apply_parameters(self):
        config = cea.config.Configuration()
        self.assertEquals(config.weather, config.general.weather)
        config.apply_command_line_args(['--weather', 'Brussels'], ['general'])
        self.assertEquals(config.weather, config.general.weather)
        self.assert_(config.weather.endswith('Brussels.epw'))
        config = pickle.loads(pickle.dumps(config))
        self.assertEquals(config.weather, config.general.weather)

    def test_weather_apply_parameters_pickle_first(self):
        config = cea.config.Configuration()
        self.assertEquals(config.weather, config.general.weather)
        config = pickle.loads(pickle.dumps(config))
        config.apply_command_line_args(['--weather', 'Brussels'], ['general'])
        self.assertEquals(config.weather, config.general.weather)
        self.assert_(config.weather.endswith('Brussels.epw'))
        self.assertEquals(config.weather, config.general.weather)

if __name__ == "__main__":
    unittest.main()
@@ -76,16 +76,32 @@
 port os%0A
+import tempfile%0A
 import c
@@ -988,33 +988,64 @@
 nario'%5D.set(
-'C:/test'
+tempfile.gettempdir().replace('%5C%5C', '/')
 )%0A        se
@@ -1073,34 +1073,45 @@
 g.scenario, 
-r'C:%5Ctest'
+tempfile.gettempdir()
 )%0A%0A    def t
@@ -1254,25 +1254,56 @@
 o'%5D.set(
-'C:/test'
+tempfile.gettempdir().replace('%5C%5C', '/')
 )%0A      
@@ -1391,26 +1391,37 @@
 enario, 
-r'C:%5Ctest'
+tempfile.gettempdir()
 )%0A%0A    d
@@ -1557,17 +1557,48 @@
 o', 
-'C:/test'
+tempfile.gettempdir().replace('%5C%5C', '/')
 %5D, %5B
@@ -1656,18 +1656,29 @@
 io, 
-r'C:%5Ctest'
+tempfile.gettempdir()
 )%0A  

make unit tests portable