def test_single_argument(): settings = Settings({'a': 1}) assert not settings settings = Settings({'A': 1}) assert settings assert settings == {'A': 1} assert settings.A == 1
def test_empty_arguments(): settings1 = Settings() assert not settings1 settings2 = Settings({}) assert not settings2 settings3 = Settings({}, {}, {}) assert not settings3
def test_copying(): settings1 = Settings(A=1) settings2 = settings1.copy() assert not settings1 is settings2 assert settings1 == settings2 assert settings2.A == 1 settings2['A'] = 2 assert settings1.A != 2 assert settings2.A == 2
def test_few_arguments(): settings1 = Settings(A=1, b=2) assert settings1 assert settings1 == {'A': 1} assert settings1.A == 1 assert not hasattr(settings1, 'b') settings2 = Settings({}, A=1) assert settings2 assert settings2 == {'A': 1} assert settings2.A == 1 assert settings1 == settings2
def test_comparing_for_equality(): settings1 = Settings() settings2 = Settings({}) settings3 = Settings({}, {}) assert settings1 == settings2 assert settings2 == settings3 assert settings1 != {'a': 1} settings4 = Settings(A=1, B=2) settings5 = Settings({'b': 1}, {'A': 1}, B=2) assert settings4 == {'A': 1, 'B': 2} assert settings4 == settings5
def test_path_to_python_module_argument(tmpdir, check_settings): test_dir = tmpdir.mkdir('test_path_to_python_module_argument') # Nonexisting file. pytest.raises(IOError, lambda: Settings(str(test_dir.join('settings')))) # Empty file. empty_module = test_dir.join('config') empty_module.write('') empty_settings = Settings(str(empty_module)) assert not empty_settings assert empty_settings == {} # Non-empty .py file. modules_data = { 'LETTERS': { 'a': 1, 'b': 2 }, 'NUMBERS': [1, 2, 3], 'STRING': 'one.two.three', } module_contents = '''# -*- coding: utf-8 -*- LETTERS = {'a': 1, 'b': 2} NUMBERS = [1, 2, 3] # another comment. # Some comment. STRING = 'one.two.three' ''' module_py = test_dir.join('settings.py') module_py.write(module_contents) settings_py = check_settings(str(module_py), modules_data) # Non-empty .py.local file. module_py_local = test_dir.join('settings.py.local') module_py_local.write(module_contents) settings_py_local = check_settings(str(module_py_local), modules_data) # .py and .py.local files have equal contents. assert settings_py == settings_py_local assert 'settings' not in sys.modules
def test_setting(): settings = Settings() settings['foo'] = 'bar' assert not settings assert settings == {} assert not hasattr(settings, 'foo') settings['BAR'] = 'baz' assert settings assert settings == {'BAR': 'baz'} assert settings.BAR == 'baz' settings.BAR = 'bar' assert settings == {'BAR': 'bar'} assert settings['BAR'] == 'bar'
def test_module_and_object_path_notation_argument(tmpdir, monkeypatch, check_settings): pytest.raises(ImportError, lambda: Settings('a.b.c')) pytest.raises(ImportError, lambda: Settings('bar:foo')) pytest.raises(ImportError, lambda: Settings('bar::foo')) pkg_name = 'test_module_name_argument' pkg_dir = tmpdir.mkdir(pkg_name) monkeypatch.syspath_prepend(str(tmpdir)) pkg_variable_name = 'some_configuration' pkg_variable_data = {'FOO': 'bar'} pkg_dir.join('__init__.py').write('%s = %r' % (pkg_variable_name, pkg_variable_data)) module_one_data = {'ONE': 1, 'TWO': 2, 'THREE': 3, 'NOT_THREE': 4} module_one_name = 'testsettingsmodule' module_one = pkg_dir.join(py(module_one_name)) module_one.write('''ONE = 1 TWO = 2 THREE = 3 # Some comment. NOT_THREE = 4 ''') module_two_name = 'othertestsettingsmodule' module_two = pkg_dir.join(py(module_two_name)) module_two_variable_name = 'conf' module_two_variable_data = {'HERE': '1', 'FOO': 'Bar'} module_two.write('%s = %r' % (module_two_variable_name, module_two_variable_data)) check_settings(':'.join((pkg_name, pkg_variable_name)), pkg_variable_data) check_settings('.'.join((pkg_name, module_one_name)), module_one_data) check_settings( '%s.%s:%s' % (pkg_name, module_two_name, module_two_variable_name), module_two_variable_data) assert 'settings' not in sys.modules assert pkg_name not in sys.modules assert module_one_name not in sys.modules assert module_two_name not in sys.modules
def test_updating(check_settings): expected_data = {'A': 1, 'B': 2, 'C': 3, 'D': 4} settings = Settings() settings.update({'A': 1, 'foo': 'bar'}, B=2, C=3) settings.update(D=4, bar='baz') settings = check_settings(settings, expected_data) settings.update() # we can do this on dict
def test_class_and_instance_arguments(): class MyObj(object): a = 1 B = 2 settings1 = Settings(MyObj) assert settings1 assert settings1 == {'B': 2} assert settings1.B == 2 assert not hasattr(settings1, 'a') settings2 = Settings(MyObj()) assert settings2 assert settings2 == {'B': 2} assert settings2.B == 2 assert not hasattr(settings2, 'a') assert settings1 == settings2
def test_generator_argument(): def settings_generator(): for k, v in (('a', 1), ('B', 2)): yield k, v settings = Settings(settings_generator()) assert settings assert settings == {'B': 2} assert settings.B == 2 assert not hasattr(settings, 'a')
def check_settings(arg, expected_data): if not isinstance(arg, Settings): settings = Settings(arg) else: settings = arg # reuse passed instance assert settings == expected_data for k, v in expected_data.items(): assert getattr(settings, k) == v return settings
def test_adding(check_settings): expected_data = {'A': 26, 'B': 2, 'C': 24} settings1 = Settings(A=1, B=2) settings2 = Settings(C=24, A=26) combined_settings = settings1 + settings2 assert combined_settings != settings1 assert combined_settings != settings2 combined_settings = check_settings(combined_settings, expected_data) expected_data['A'] = 1 combined_settings += dict(A=1) combined_settings = check_settings(combined_settings, expected_data) expected_data['A'] = -3 combined_settings = combined_settings + dict(A=-3) check_settings(combined_settings, expected_data)
def test_module_argument(): module_name = 'testmodule' module = types.ModuleType(module_name) module.B = 2 module.a = 1 settings = Settings(module) assert settings assert settings == {'B': 2} assert settings.B == 2 assert not hasattr(settings, 'a') assert 'settings' not in sys.modules assert module_name not in sys.modules
def test_getting(tmpdir, monkeypatch): settings1 = Settings(A=1, B=2) assert settings1.A == 1 pytest.raises(AttributeError, lambda: settings1.a) assert settings1.get('a') == None assert settings1.get('a', 42) == 42 assert settings1['A'] == 1 pytest.raises(KeyError, lambda: settings1['a']) module_name, module_dir = 'somemodule', tmpdir.mkdir('test_getting') module = module_dir.join(module_name + '.py') module.write('obj = {\'foo\': \'bar\'}') monkeypatch.syspath_prepend(str(module_dir)) setting_name, setting_value = 'OBJ_FROM_OTHER_MODULE', module_name + ':obj' settings2 = Settings({setting_name: setting_value}) assert settings2.get(setting_name, imp=True) == {'foo': 'bar'} assert settings2.get(setting_name, 'smth', imp=True) == {'foo': 'bar'} assert settings2.get(setting_name) == setting_value assert settings2.get('bbb', imp=True) is None assert settings2.get('aaa', 123, imp=True) == 123
def test_deleting(): settings = Settings(A=1, B=2, C=3) delattr(settings, 'A') assert settings == {'B': 2, 'C': 3} pytest.raises(KeyError, lambda: settings['A']) pytest.raises(AttributeError, lambda: settings.A) del settings['C'] assert settings == {'B': 2} pytest.raises(AttributeError, lambda: settings.C) pytest.raises(KeyError, lambda: settings['C']) def test_del(): del settings['123'] pytest.raises(KeyError, test_del) pytest.raises(AttributeError, lambda: delattr(settings, '123')) assert settings == {'B': 2} assert settings.__dict__ == {'B': 2}
def test_module_name_argument(tmpdir, monkeypatch, check_settings): pytest.raises(ImportError, lambda: Settings('settings')) module_data = {'ONE': 1, 'TWO': 2, 'THREE': 3, 'NOT_THREE': 4} module_name = 'testsettingsmodule' module_dir = tmpdir.mkdir('test_module_name_argument') module = module_dir.join(py(module_name)) module.write('''ONE = 1 TWO = 2 THREE = 3 # Some comment. NOT_THREE = 4 ''') monkeypatch.syspath_prepend(str(module_dir)) check_settings(module_name, module_data) assert 'settings' not in sys.modules assert module_name not in sys.modules