File: test_config.py

package info (click to toggle)
python-easydev 0.13.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: python: 1,904; makefile: 116; javascript: 49
file content (139 lines) | stat: -rw-r--r-- 2,988 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os

from easydev import CustomConfig
from easydev.config_tools import DynamicConfigParser

from . import test_dir


def test_config_custom():
    c = CustomConfig("dummy")
    c.init()
    c.user_config_dir
    c.remove()


def test_configExample():

    c = DynamicConfigParser(f"{test_dir}/data/config.ini")
    assert "General" in c.sections()
    assert "GA" in c.sections()
    print(c)
    c.remove_section("General")


def test_ordered_dict_attribute():
    # disabled because incompatible with python 3
    c = DynamicConfigParser()
    c.add_section("GA")
    c.GA.test = 2  # this is an attribute only, not  yet a key
    c.add_option("GA", "test", 1)
    c.GA.test = 2
    c["GA"]["test"] = 4
    del c["GA"]["test"]


def test_DynamicConfig(tmpdir):

    outname = tmpdir.join("test.ini")

    # constructor with filename
    dc = DynamicConfigParser(f"{test_dir}/data/config.ini")

    # constructor with existing constructor
    dc = DynamicConfigParser(dc)

    dc.GA
    dc.add_option("GA", "bool", "True")
    dc.get_options("GA")

    # save file and read back
    outname = tmpdir.join("test.ini")
    dc.save(outname)
    dc.save(outname)  # called twice on purpose

    dc.remove_section("GA")
    assert "GA" not in dc.sections()
    print(dc)

    # try something stupid
    try:
        dc = DynamicConfigParser(234)
        assert False
    except TypeError:
        assert True

    dc = DynamicConfigParser()
    try:
        dc.read("test_dummy")
        assert False
    except:
        assert True


def test_DynamicConfigDelete():

    cfg = DynamicConfigParser(f"{test_dir}/data/config.ini")

    dcp = DynamicConfigParser(cfg)
    try:
        del dcp["GA"]
        assert dcp.sections() == ["General"]
    except:
        pass


def test_DynamicConfig_setter(tmpdir):
    outname = tmpdir.join("test.ini")

    dc = DynamicConfigParser()
    dc.add_section("GA")
    dc.add_option("GA", "popsize", 1)
    dc.add_section("General")
    dc.add_option("General", "verbose", True)
    dc.add_option("General", "tag", "test")
    dc.save(outname)

    dc2 = DynamicConfigParser(f"{test_dir}/data/config.ini")
    assert dc == dc2

    dc.GA.test = 10


def test_section2dict():
    dc = DynamicConfigParser()
    dc.add_section("GA")
    dc.add_option("GA", "test", 1)


def test_compare():

    dc = DynamicConfigParser()
    dc.add_section("GA")
    dc.add_option("GA", "test", 1)

    dc2 = DynamicConfigParser()
    dc2.add_section("GA2")
    dc2.add_option("GA2", "test", 1)

    assert (dc == dc2) == False

    dc = DynamicConfigParser()
    dc.add_section("GA")
    dc.add_option("GA", "test", 1)

    dc2 = DynamicConfigParser()
    dc2.add_section("GA")
    dc2.add_option("GA", "test", 2)

    assert (dc == dc2) == False

    dc = DynamicConfigParser()
    dc.add_section("GA")
    dc.add_option("GA", "test", 1)

    dc2 = DynamicConfigParser()
    dc2.add_section("GA")
    dc2.add_option("GA", "test", 1)

    assert (dc == dc2) == True