This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_update.py
214 lines (170 loc) · 7.62 KB
/
test_update.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from update import MongoStorage, StashServer
import datetime
import pytest
#===================================================================================================
# storage
#===================================================================================================
@pytest.fixture
def storage(request):
'''
Initializes a MongoStorage() using a test-specific data-base, to avoid any conflicts between
tests and to avoid clashing with real databases.
'''
db_name = 'testing-{}'.format(request.node.name)
result = MongoStorage(default_db_name=db_name)
result.__TESTING__ = True
def finalizer():
result.get_connection().drop_database(db_name)
request.addfinalizer(finalizer)
return result
#===================================================================================================
# TestMongoStorage
#===================================================================================================
class TestMongoStorage(object):
'''
Tests for MongoStorage class. Requires a MongoDB running.
'''
def make_todo_dict(self, function_name, date, days, lineno):
return {
'function_name': function_name,
'date': date,
'days': days,
'lineno': lineno,
}
def discard_ids(self, entries):
entries = list(entries)
for entry in entries:
del entry['_id']
return list(entries)
def test_todos(self, storage):
assert list(storage.iter_all_todos()) == []
# push and check todos for 'foo'
foo_todos = [
self.make_todo_dict('test_foo1', datetime.datetime(2013, 9, 8), 5, 200),
self.make_todo_dict('test_foo2', datetime.datetime(2013, 9, 24), 10, 500),
]
storage.update_todos('proj/repo1', 'src/foo.py', foo_todos)
assert self.discard_ids(storage.iter_all_todos()) == [
{
'repo': 'proj/repo1',
'filename': 'src/foo.py',
'todos': foo_todos,
},
]
# change date and make sure it reflects back
foo_todos[0]['date'] = datetime.datetime(2014, 9, 8)
storage.update_todos('proj/repo1', 'src/foo.py', foo_todos)
assert self.discard_ids(storage.iter_all_todos()) == [
{
'repo': 'proj/repo1',
'filename': 'src/foo.py',
'todos': foo_todos,
},
]
# add todos for 'bar'
bar_todos = [
self.make_todo_dict('test_bar1', datetime.datetime(2013, 5, 8), 15, 600),
]
storage.update_todos('proj/repo2', 'src/bar.py', bar_todos)
assert self.discard_ids(storage.iter_all_todos()) == [
{
'repo': 'proj/repo1',
'filename': 'src/foo.py',
'todos': foo_todos,
},
{
'repo': 'proj/repo2',
'filename': 'src/bar.py',
'todos': bar_todos,
},
]
# remove todos for 'foo' and make sure 'bar' todos are still ok
storage.update_todos('proj/repo1', 'src/foo.py', [])
assert self.discard_ids(storage.iter_all_todos()) == [
{
'repo': 'proj/repo2',
'filename': 'src/bar.py',
'todos': bar_todos,
},
]
def test_last_hash(self, storage):
assert storage.get_last_hash('proj/repo1') is None
assert storage.get_last_hash('proj/repo2') is None
storage.set_last_hash('proj/repo1', '11111')
assert storage.get_last_hash('proj/repo1') == '11111'
assert storage.get_last_hash('proj/repo2') is None
storage.set_last_hash('proj/repo2', '22222')
assert storage.get_last_hash('proj/repo1') == '11111'
assert storage.get_last_hash('proj/repo2') == '22222'
def test_last_fetch_all_status(self, storage):
assert storage.get_last_fetch_all_status() == (None, None)
date = datetime.datetime(2013, 9, 8, 23, 30, 0)
storage.set_last_fetch_all_status(date, datetime.timedelta(seconds=40))
assert storage.get_last_fetch_all_status() == ('2013-09-08 23:30:00', '0:00:40')
#===================================================================================================
# stash_server
#===================================================================================================
@pytest.fixture
def stash_server(request, repo_name):
server = StashServer(request.config.option.stash_url, auth=None)
proj_name = repo_name.split('/')[0]
assert 'todo-dashboard-test' in set(server.iter_repos(proj_name))
return server
#===================================================================================================
# repo_name
#===================================================================================================
@pytest.fixture
def repo_name(request):
return request.config.option.stash_repo
#===================================================================================================
# TestStashServer
#===================================================================================================
@pytest.mark.skipif('not config.option.stash_url or not config.option.stash_repo')
class TestStashServer(object):
REPO_NAME = '~bruno/todo-dashboard-test'
def test_iter_file_names(self, stash_server, repo_name):
expected = set([
'.gitignore',
'README.md',
'foo/foo1.py',
'foo/foo2.py',
'main.py',
])
assert set(stash_server.iter_file_names(repo_name)) == expected
at = stash_server.get_branches(repo_name)['refs/heads/master']
assert set(stash_server.iter_file_names(repo_name, at=at)) == expected
at = stash_server.get_branches(repo_name)['refs/heads/bar_branch']
assert set(stash_server.iter_file_names(repo_name, at=at)) == set([
'.gitignore',
'README.md',
'bar/bar1.py',
'foo/foo1.py',
'foo/foo2.py',
'main.py',
])
def test_branches(self, stash_server, repo_name):
assert stash_server.get_branches(repo_name) == {
'refs/heads/master' : '43c918485bf46630d1d8d5ee65e19d0effb32f1f',
'refs/heads/bar_branch' : '3ee46863fdc113418f41019e868ed4597fb924b5',
}
def test_get_file_contents(self, stash_server, repo_name):
def split_contents(contents):
return [x.rstrip() for x in contents.splitlines() if x.strip()]
lines = split_contents(stash_server.get_file_contents(repo_name, 'foo/foo1.py'))
assert lines == [
'class Foo1:',
' def hello(self):',
" print 'foo1'",
]
at = stash_server.get_branches(repo_name)['refs/heads/bar_branch']
lines = split_contents(stash_server.get_file_contents(repo_name, 'bar/bar1.py', at=at))
assert lines == [
'class Bar1:',
' def hey(self):',
" print 'bar1'",
]
#===================================================================================================
# main
#===================================================================================================
if __name__ == '__main__':
pytest.main(['', '-s'])