-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_xml.py
183 lines (139 loc) · 5.38 KB
/
test_xml.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os
from datetime import datetime, date, timedelta
import pytest
from .fixtures import test_directory, initialized, one_watch_added
from squirrel import xml
from squirrel.vars import project_file_path, DIRECTORY_NAME, watch_file_path
from squirrel.squirrel import _main
from squirrel.exceptions import ProjectNotSetupCorrectlyError
def test_get_watches_data_before_init(test_directory):
with pytest.raises(FileNotFoundError) as e:
xml.get_watches_data()
assert e.type == FileNotFoundError
def test_get_watches_data_after_init(initialized):
watches = xml.get_watches_data()
assert len(watches) == 0
def test_get_watches_data_after_watch(one_watch_added):
watches = xml.get_watches_data()
assert len(watches) == 1
assert watches == [
(one_watch_added[1].strftime('%d/%m/%Y'), 0, one_watch_added[0])
]
def test_get_watches_entry_before_init(test_directory):
with pytest.raises(FileNotFoundError) as e:
xml.get_watches_entry(date.today())
assert e.type == FileNotFoundError
def test_get_watches_entry_after_init(initialized):
watches_tag, root = xml.get_watches_entry(date.today())
assert watches_tag is None
assert root is not None
def test_get_watches_entry_after_watch(one_watch_added):
date_of_watch = one_watch_added[1]
watches_tag, root = xml.get_watches_entry(date_of_watch)
assert len(watches_tag) == 1
assert watches_tag[0].text == str(one_watch_added[0])
unavailable_date = date_of_watch - timedelta(days=1)
watches_tag, root = xml.get_watches_entry(unavailable_date)
assert watches_tag is None
assert root is not None
def test_add_watch_entry_before_init(test_directory):
with pytest.raises(ProjectNotSetupCorrectlyError) as e:
xml.add_watch_entry(1, datetime.now())
assert e.type == ProjectNotSetupCorrectlyError
def test_add_watch_entry_after_init(initialized):
datetime_of_watch = datetime.now()
assert xml.add_watch_entry(1, datetime_of_watch)
watches_tag, root = xml.get_watches_entry(datetime_of_watch.date())
assert len(watches_tag) == 1
assert watches_tag[0].text == str(1)
def test_add_watch_entry_after_watch(one_watch_added):
datetime_of_watch = datetime.now()
assert not xml.add_watch_entry(1, datetime_of_watch)
assert xml.add_watch_entry(2, datetime_of_watch)
def test_get_data_from_project_file(test_directory):
with pytest.raises(FileNotFoundError) as e:
xml.get_data_from_project_file()
assert e.type == FileNotFoundError
return_code = _main(['init', '-n', 'xmltest', '--goal', '10000'])
assert return_code == 0
assert xml.get_data_from_project_file() == {
'name': 'xmltest',
'path': os.path.join(os.getcwd(), DIRECTORY_NAME),
'description': None,
'goal': str(10000),
'due-date': None,
'project-type': 'text'
}
# Reinitialize the project with no parameter
_main(['init', '-y'])
assert xml.get_data_from_project_file() == {
'name': None,
'path': os.path.join(os.getcwd(), DIRECTORY_NAME),
'description': None,
'goal': '0',
'due-date': None,
'project-type': 'text'
}
def test_update_project_file(initialized):
xml.update_project_file({
'name': 'test_update_project_file',
'goal': 666,
'description': None,
'due': None,
'project-type': None,
})
assert xml.get_data_from_project_file() == {
'name': 'test_update_project_file',
'path': os.path.join(os.getcwd(), DIRECTORY_NAME),
'goal': '666',
'description': None,
'due-date': None,
'project-type': 'text',
}
xml.update_project_file({
'name': 'test_when_not_all_args_are_passed',
'goal': 666,
})
assert xml.get_data_from_project_file() == {
'name': 'test_when_not_all_args_are_passed',
'path': os.path.join(os.getcwd(), DIRECTORY_NAME),
'goal': '666',
'description': None,
'due-date': None,
'project-type': 'text',
}
def test_build_project(test_directory):
path = os.path.join(os.getcwd(), DIRECTORY_NAME)
xml.build_project({
'name': 'test_building_project',
'description': None,
'goal': None,
'due': None,
'project_type': None,
'y': False,
}, path)
try:
with open(project_file_path, 'r') as f:
project = f.read()
with open(watch_file_path, 'r') as f:
watch = f.read()
except FileNotFoundError:
pytest.fail('file projects were not created')
# remove any whitespace to be able to create a
# test string without worrying about indentation and stuff
project = ''.join(project.split())
watch = ''.join(watch.split())
assert project == f'<?xmlversion=\'1.0\'encoding=\'utf-8\'?>' \
'<squirrelname="test_building_project">' \
f'<pathsrc="{os.getcwd()}/.squirrel"/>' \
'<description/>' \
'<due-date/>' \
'<goal>0</goal>' \
'<project-type>text</project-type>' \
'</squirrel>'
assert watch == '<?xmlversion=\'1.0\'encoding=\'utf-8\'?>'\
'<squirrel><!--Thisisafilegeneratedbysquirrel.Modifyitatyourownrisk.--></squirrel>'
def test_parse_before_init(test_directory):
with pytest.raises(FileNotFoundError) as e:
xml.parse(os.path.join(os.getcwd(), project_file_path))
assert e.type == FileNotFoundError