PageRenderTime 65ms CodeModel.GetById 58ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/app/test_config.py

https://bitbucket.org/prologic/circuits/
Python | 110 lines | 106 code | 3 blank | 1 comment | 0 complexity | b9e9987d538954a6445776b44f72b7df MD5 | raw file
  1#!/usr/bin/env python
  2
  3import pytest
  4
  5
  6CONFIG = """\
  7[test]
  8foo = bar
  9int = 1
 10float = 1.0
 11bool = 1
 12"""
 13
 14
 15def pytest_funcarg__config(request):
 16    from circuits.app.config import Config, Load
 17
 18    tmpdir = request.getfuncargvalue("tmpdir")
 19    path = tmpdir.ensure("test.ini")
 20    path.write(CONFIG)
 21
 22    config = Config(str(path))
 23    config.start()
 24
 25    config.fire(Load())
 26
 27    assert pytest.wait_event(config, "load_success")
 28
 29    return config
 30
 31
 32def test_add_section(config):
 33    config.add_section("foo")
 34    assert config.has_section("foo")
 35
 36
 37def test_has_section(config):
 38    assert config.has_section("test")
 39
 40
 41def test_get(config):
 42    s = config.get("test", "foo")
 43    assert s == "bar"
 44
 45    s = config.get("test", "asdf", "foobar")
 46    assert s == "foobar"
 47
 48
 49def test_get_int(config):
 50    i = config.getint("test", "int")
 51    assert i == 1
 52
 53    i = config.getint("test", "asdf", 1234)
 54    assert i == 1234
 55
 56
 57def test_get_float(config):
 58    f = config.getfloat("test", "float")
 59    assert f == 1.0
 60
 61    f = config.getfloat("test", "asdf", 1234.1234)
 62    assert f == 1234.1234
 63
 64
 65def test_get_bool(config):
 66    b = config.getboolean("test", "bool")
 67    assert b
 68
 69    b = config.getboolean("test", "asdf", False)
 70    assert not b
 71
 72
 73def test_load(tmpdir):
 74    from circuits.app.config import Config, Load
 75
 76    path = tmpdir.ensure("test.ini")
 77    path.write(CONFIG)
 78
 79    config = Config(str(path))
 80    config.start()
 81
 82    config.fire(Load())
 83
 84    assert pytest.wait_event(config, "load_success")
 85
 86    s = config.get("test", "foo")
 87    assert s == "bar"
 88
 89    config.stop()
 90
 91
 92def test_save(tmpdir):
 93    from circuits.app.config import Config, Save
 94
 95    path = tmpdir.ensure("test.ini")
 96
 97    config = Config(str(path))
 98    config.start()
 99
100    config.add_section("test")
101    config.set("test", "foo", "bar")
102
103    config.fire(Save())
104
105    assert pytest.wait_event(config, "save_success")
106
107    s = config.get("test", "foo")
108    assert s == "bar"
109
110    config.stop()