-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_ini.py
More file actions
150 lines (116 loc) · 4.61 KB
/
Copy pathtest_ini.py
File metadata and controls
150 lines (116 loc) · 4.61 KB
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
140
141
142
143
144
145
146
147
148
149
150
import os
from iterable.datatypes import INIIterable
class TestINI:
def test_id(self):
datatype_id = INIIterable.id()
assert datatype_id == "ini"
def test_flatonly(self):
flag = INIIterable.is_flatonly()
assert flag
def test_openclose(self):
# Create a simple INI file for testing
test_file = "testdata/test_ini.ini"
os.makedirs("testdata", exist_ok=True)
with open(test_file, "w") as f:
f.write("[section1]\nkey1=value1\nkey2=value2\n")
iterable = INIIterable(test_file)
iterable.close()
if os.path.exists(test_file):
os.unlink(test_file)
def test_read_ini_sections(self):
"""Test reading INI file with sections"""
test_file = "testdata/test_ini_sections.ini"
os.makedirs("testdata", exist_ok=True)
with open(test_file, "w") as f:
f.write("[section1]\nkey1=value1\nkey2=value2\n[section2]\nkey3=value3\n")
iterable = INIIterable(test_file)
records = list(iterable)
iterable.close()
assert len(records) >= 2
# Check that sections are present
sections = [r.get("_section") for r in records]
assert "section1" in sections or "section2" in sections
if os.path.exists(test_file):
os.unlink(test_file)
def test_read_ini_properties(self):
"""Test reading properties-style file"""
test_file = "testdata/test_ini_properties.ini"
os.makedirs("testdata", exist_ok=True)
with open(test_file, "w") as f:
f.write("key1=value1\nkey2=value2\n")
iterable = INIIterable(test_file)
records = list(iterable)
iterable.close()
assert len(records) > 0
if os.path.exists(test_file):
os.unlink(test_file)
def test_read_one(self):
"""Test reading single record"""
test_file = "testdata/test_ini_read.ini"
os.makedirs("testdata", exist_ok=True)
with open(test_file, "w") as f:
f.write("[section1]\nkey1=value1\n")
iterable = INIIterable(test_file)
record = iterable.read()
assert isinstance(record, dict)
assert "key1" in record or "_section" in record
iterable.close()
if os.path.exists(test_file):
os.unlink(test_file)
def test_read_bulk(self):
"""Test reading bulk records"""
test_file = "testdata/test_ini_bulk.ini"
os.makedirs("testdata", exist_ok=True)
with open(test_file, "w") as f:
f.write("[section1]\nkey1=value1\n[section2]\nkey2=value2\n")
iterable = INIIterable(test_file)
chunk = iterable.read_bulk(2)
assert isinstance(chunk, list)
assert len(chunk) > 0
iterable.close()
if os.path.exists(test_file):
os.unlink(test_file)
def test_reset(self):
"""Test reset functionality"""
test_file = "testdata/test_ini_reset.ini"
os.makedirs("testdata", exist_ok=True)
with open(test_file, "w") as f:
f.write("[section1]\nkey1=value1\n")
iterable = INIIterable(test_file)
record1 = iterable.read()
iterable.reset()
record2 = iterable.read()
# After reset, should read the first record again
assert record1.get("_section") == record2.get("_section")
iterable.close()
if os.path.exists(test_file):
os.unlink(test_file)
def test_write(self):
"""Test writing INI record"""
test_file = "testdata/test_ini_write.ini"
os.makedirs("testdata", exist_ok=True)
iterable = INIIterable(test_file, mode="w")
record = {"_section": "test_section", "key1": "value1", "key2": "value2"}
iterable.write(record)
iterable.close()
# Verify file was created
assert os.path.exists(test_file)
# Read it back
reader = INIIterable(test_file)
records = list(reader)
reader.close()
# Should be able to read what we wrote
assert len(records) > 0
if os.path.exists(test_file):
os.unlink(test_file)
def test_write_bulk(self):
"""Test writing bulk INI records"""
test_file = "testdata/test_ini_write_bulk.ini"
os.makedirs("testdata", exist_ok=True)
iterable = INIIterable(test_file, mode="w")
records = [{"_section": "section1", "key1": "value1"}, {"_section": "section2", "key2": "value2"}]
iterable.write_bulk(records)
iterable.close()
assert os.path.exists(test_file)
if os.path.exists(test_file):
os.unlink(test_file)