import autopath

#from py.test import raises
from tiramisu.config import Config, mandatory_warnings
from tiramisu.option import StrOption, UnicodeOption, OptionDescription
from tiramisu.error import PropertiesOptionError


def make_description():
    stroption = StrOption('str', 'Test string option', default="abc",
                          properties=('mandatory', ))
    stroption1 = StrOption('str1', 'Test string option',
                           properties=('mandatory', ))
    stroption2 = UnicodeOption('unicode2', 'Test string option',
                               properties=('mandatory', ))
    stroption3 = StrOption('str3', 'Test string option', multi=True,
                           properties=('mandatory', ))
    descr = OptionDescription('tiram', '', [stroption, stroption1, stroption2, stroption3])
    return descr


def test_mandatory_ro():
    descr = make_description()
    config = Config(descr)
    config.read_only()
    prop = []
    try:
        config.str1
    except PropertiesOptionError as err:
        prop = err.proptype
    assert 'mandatory' in prop
    config.read_write()
    config.str1 = 'yes'
    config.read_only()
    assert config.str1 == 'yes'


def test_mandatory_rw():
    descr = make_description()
    config = Config(descr)
    config.read_write()
    #not mandatory in rw
    config.str1
    config.str1 = 'yes'
    assert config.str1 == 'yes'


def test_mandatory_default():
    descr = make_description()
    config = Config(descr)
    config.read_only()
    #not mandatory in rw
    config.str
    config.read_write()
    config.str = 'yes'
    config.read_only()
    config.str
    config.read_write()
    config.str = None
    config.read_only()
    prop = []
    try:
        config.str
    except PropertiesOptionError as err:
        prop = err.proptype
    assert 'mandatory' in prop


#valeur vide : None, '', u'', ...
def test_mandatory_none():
    descr = make_description()
    config = Config(descr)
    config.str1 = None
    assert config.getowner(config.unwrap_from_path('str1')) == 'user'
    config.read_only()
    prop = []
    try:
        config.str1
    except PropertiesOptionError as err:
        prop = err.proptype
    assert 'mandatory' in prop


def test_mandatory_empty():
    descr = make_description()
    config = Config(descr)
    config.str1 = ''
    assert config.getowner(config.unwrap_from_path('str1')) == 'user'
    config.read_only()
    prop = []
    try:
        config.str1
    except PropertiesOptionError as err:
        prop = err.proptype
    assert 'mandatory' in prop


def test_mandatory_multi_none():
    descr = make_description()
    config = Config(descr)
    config.str3 = [None]
    config.read_only()
    assert config.getowner(config.unwrap_from_path('str3')) == 'user'
    prop = []
    try:
        config.str3
    except PropertiesOptionError as err:
        prop = err.proptype
    assert 'mandatory' in prop
    config.read_write()
    config.str3 = ['yes', None]
    config.read_only()
    assert config.getowner(config.unwrap_from_path('str3')) == 'user'
    prop = []
    try:
        config.str3
    except PropertiesOptionError as err:
        prop = err.proptype
    assert 'mandatory' in prop


def test_mandatory_multi_empty():
    descr = make_description()
    config = Config(descr)
    config.str3 = ['']
    config.read_only()
    assert config.getowner(config.unwrap_from_path('str3')) == 'user'
    prop = []
    try:
        config.str3
    except PropertiesOptionError as err:
        prop = err.proptype
    assert 'mandatory' in prop
    config.read_write()
    config.str3 = ['yes', '']
    config.read_only()
    assert config.getowner(config.unwrap_from_path('str3')) == 'user'
    prop = []
    try:
        config.str3
    except PropertiesOptionError as err:
        prop = err.proptype
    assert 'mandatory' in prop


def test_mandatory_multi_append():
    descr = make_description()
    config = Config(descr)
    config.str3 = ['yes']
    config.read_write()
    config.str3.append(None)


def test_mandatory_disabled():
    descr = make_description()
    config = Config(descr)
    setting = config.cfgimpl_get_settings()
    config.str1
    config.read_only()
    prop = []
    try:
        config.str1
    except PropertiesOptionError as err:
        prop = err.proptype
    assert prop == ['mandatory']
    setting[descr.str1].append('disabled')
    prop = []
    try:
        config.str1
    except PropertiesOptionError as err:
        prop = err.proptype
    assert set(prop) == set(['disabled', 'mandatory'])


def test_mandatory_unicode():
    descr = make_description()
    config = Config(descr)
    config.unicode2
    config.read_only()
    prop = []
    try:
        config.unicode2
    except PropertiesOptionError as err:
        prop = err.proptype
    assert prop == ['mandatory']
    config.read_write()
    config.unicode2 = u''
    config.read_only()
    prop = []
    try:
        config.unicode2
    except PropertiesOptionError as err:
        prop = err.proptype
    assert prop == ['mandatory']


def test_mandatory_warnings_ro():
    descr = make_description()
    config = Config(descr)
    config.str = ''
    config.read_only()
    proc = []
    try:
        config.str
    except PropertiesOptionError as err:
        proc = err.proptype
    assert proc == ['mandatory']
    assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3']
    config.read_write()
    config.str = 'a'
    config.read_only()
    assert list(mandatory_warnings(config)) == ['str1', 'unicode2', 'str3']


def test_mandatory_warnings_rw():
    descr = make_description()
    config = Config(descr)
    config.str = ''
    config.read_write()
    config.str
    assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3']
    config.str = 'a'
    assert list(mandatory_warnings(config)) == ['str1', 'unicode2', 'str3']


def test_mandatory_warnings_disabled():
    descr = make_description()
    config = Config(descr)
    config.str = ''
    setting = config.cfgimpl_get_settings()
    config.read_write()
    config.str
    assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3']
    setting[descr.str].append('disabled')
    assert list(mandatory_warnings(config)) == ['str1', 'unicode2', 'str3']


def test_mandatory_warnings_frozen():
    descr = make_description()
    config = Config(descr)
    config.str = ''
    setting = config.cfgimpl_get_settings()
    config.read_write()
    config.str
    assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3']
    setting[descr.str].append('frozen')
    config.read_only()
    assert list(mandatory_warnings(config)) == ['str', 'str1', 'unicode2', 'str3']