<gh_stars>10-100
import os
import pytest

try:
    from types import TypeType
except ImportError:
    TypeType = type

from amazonproduct import utils
from amazonproduct.processors import etree, minidom


def test_load_global_file_config(configfiles):
    configfiles.add_file('''
        [Credentials]
        access_key = <KEY>
        secret_key = <KEY>''', path='/etc/amazon-product-api.cfg')

    cfg = utils.load_file_config()
    assert cfg['access_key'] == '<KEY>'
    assert cfg['secret_key'] == '<KEY>'
    assert len(cfg) == 2


def test_load_local_file_config(configfiles):
    configfiles.add_file('''
        [Credentials]
        access_key = <KEY>
        secret_key = <KEY>''', path='~/.amazon-product-api')

    cfg = utils.load_file_config()
    assert cfg['access_key'] == '<KEY>'
    assert cfg['secret_key'] == '<KEY>'
    assert len(cfg) == 2


def test_load_environment_config(monkeypatch):
    monkeypatch.setenv('AWS_ACCESS_KEY', '<KEY>')
    monkeypatch.setenv('AWS_SECRET_ACCESS_KEY', '<KEY>')
    monkeypatch.setenv('AWS_LOCALE', 'uk')

    cfg = utils.load_environment_config()
    assert cfg['access_key'] == '<KEY>'
    assert cfg['secret_key'] == '<KEY>'
    assert cfg['locale'] == 'uk'
    assert 'associate_tag' not in cfg


DUMMY_CONFIG = """
# file: /etc/amazon-product-api.cfg
[Credentials]
access_key = global cfg value
secret_key = global cfg value

# file: ~/.amazon-product-api
[Credentials]
secret_key = local cfg value
locale = de

# file: ~/my-config
[Credentials]
secret_key = CUSTOM CONFIG OVERRIDES ALL!
"""


def test_load_config(configfiles, monkeypatch):
    configfiles.load_from_string(DUMMY_CONFIG)
    for key in ['AWS_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY', 'AWS_ASSOCIATE_TAG', 'AWS_LOCALE']:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.setenv('AWS_LOCALE', 'OS VARIABLE')

    cfg = utils.load_config()
    assert set(cfg.keys()) == set([
        'access_key', 'secret_key', 'associate_tag', 'locale'])

    assert cfg['access_key'] == 'global cfg value'
    assert cfg['secret_key'] == 'local cfg value'
    assert cfg['associate_tag'] is None
    assert cfg['locale'] == 'OS VARIABLE'


def test_specific_config_file_overrides_all_but_os_variables(configfiles, monkeypatch):
    configfiles.load_from_string(DUMMY_CONFIG)
    for key in ['AWS_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY', 'AWS_ASSOCIATE_TAG', 'AWS_LOCALE']:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.setenv('AWS_LOCALE', 'OS VARIABLE')

    path = configfiles.tmpdir.join(os.path.expanduser('~/my-config')).strpath
    cfg = utils.load_config(path)
    assert set(cfg.keys()) == set([
        'access_key', 'secret_key', 'associate_tag', 'locale'])

    assert cfg['secret_key'] == 'CUSTOM CONFIG OVERRIDES ALL!'
    assert cfg['access_key'] is None
    assert cfg['associate_tag'] is None
    assert cfg['locale'] == 'OS VARIABLE'


@pytest.mark.parametrize(('txt', 'cls'), [
    ('amazonproduct.processors.etree.Processor', etree.Processor),
    ('amazonproduct.processors.minidom.Processor', minidom.Processor),
])
def test_load_class(txt, cls):
    loaded = utils.load_class(txt)
    assert isinstance(loaded, TypeType)
    assert loaded == cls
import os
import pytest

try:
    from types import TypeType
except ImportError:
    TypeType = type

from amazonproduct import utils
from amazonproduct.processors import etree, minidom


def test_load_global_file_config(configfiles):
    configfiles.add_file('''
        [Credentials]
        access_key = <KEY>
        secret_key = <KEY>''', path='/etc/amazon-product-api.cfg')

    cfg = utils.load_file_config()
    assert cfg['access_key'] == '<KEY>'
    assert cfg['secret_key'] == '<KEY>'
    assert len(cfg) == 2


def test_load_local_file_config(configfiles):
    configfiles.add_file('''
        [Credentials]
        access_key = <KEY>
        secret_key = <KEY>''', path='~/.amazon-product-api')

    cfg = utils.load_file_config()
    assert cfg['access_key'] == '<KEY>'
    assert cfg['secret_key'] == '<KEY>'
    assert len(cfg) == 2


def test_load_environment_config(monkeypatch):
    monkeypatch.setenv('AWS_ACCESS_KEY', '<KEY>')
    monkeypatch.setenv('AWS_SECRET_ACCESS_KEY', '<KEY>')
    monkeypatch.setenv('AWS_LOCALE', 'uk')

    cfg = utils.load_environment_config()
    assert cfg['access_key'] == '<KEY>'
    assert cfg['secret_key'] == '<KEY>'
    assert cfg['locale'] == 'uk'
    assert 'associate_tag' not in cfg


DUMMY_CONFIG = """
# file: /etc/amazon-product-api.cfg
[Credentials]
access_key = global cfg value
secret_key = global cfg value

# file: ~/.amazon-product-api
[Credentials]
secret_key = local cfg value
locale = de

# file: ~/my-config
[Credentials]
secret_key = CUSTOM CONFIG OVERRIDES ALL!
"""


def test_load_config(configfiles, monkeypatch):
    configfiles.load_from_string(DUMMY_CONFIG)
    for key in ['AWS_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY', 'AWS_ASSOCIATE_TAG', 'AWS_LOCALE']:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.setenv('AWS_LOCALE', 'OS VARIABLE')

    cfg = utils.load_config()
    assert set(cfg.keys()) == set([
        'access_key', 'secret_key', 'associate_tag', 'locale'])

    assert cfg['access_key'] == 'global cfg value'
    assert cfg['secret_key'] == 'local cfg value'
    assert cfg['associate_tag'] is None
    assert cfg['locale'] == 'OS VARIABLE'


def test_specific_config_file_overrides_all_but_os_variables(configfiles, monkeypatch):
    configfiles.load_from_string(DUMMY_CONFIG)
    for key in ['AWS_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY', 'AWS_ASSOCIATE_TAG', 'AWS_LOCALE']:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.setenv('AWS_LOCALE', 'OS VARIABLE')

    path = configfiles.tmpdir.join(os.path.expanduser('~/my-config')).strpath
    cfg = utils.load_config(path)
    assert set(cfg.keys()) == set([
        'access_key', 'secret_key', 'associate_tag', 'locale'])

    assert cfg['secret_key'] == 'CUSTOM CONFIG OVERRIDES ALL!'
    assert cfg['access_key'] is None
    assert cfg['associate_tag'] is None
    assert cfg['locale'] == 'OS VARIABLE'


@pytest.mark.parametrize(('txt', 'cls'), [
    ('amazonproduct.processors.etree.Processor', etree.Processor),
    ('amazonproduct.processors.minidom.Processor', minidom.Processor),
])
def test_load_class(txt, cls):
    loaded = utils.load_class(txt)
    assert isinstance(loaded, TypeType)
    assert loaded == cls
[Credentials]         access_key = <KEY>         secret_key = <KEY> [Credentials]         access_key = <KEY>         secret_key = <KEY> # file: /etc/amazon-product-api.cfg [Credentials] access_key = global cfg value secret_key = global cfg value  # file: ~/.amazon-product-api [Credentials] secret_key = local cfg value locale = de  # file: ~/my-config [Credentials] secret_key = CUSTOM CONFIG OVERRIDES ALL!