Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Side by Side Diff: tools/telemetry/third_party/rope/ropetest/objectdbtest.py

Issue 1132103009: Example of refactoring using rope library. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 import unittest
2
3 from rope.base.oi import objectdb, memorydb
4 from ropetest import testutils
5
6
7 def _do_for_all_dbs(function):
8 def called(self):
9 for db in self.dbs:
10 function(self, db)
11 return called
12
13
14 class _MockValidation(object):
15
16 def is_value_valid(self, value):
17 return value != -1
18
19 def is_more_valid(self, new, old):
20 return new != -1
21
22 def is_file_valid(self, path):
23 return path != 'invalid'
24
25 def is_scope_valid(self, path, key):
26 return path != 'invalid' and key != 'invalid'
27
28
29 class _MockFileListObserver(object):
30
31 log = ''
32
33 def added(self, path):
34 self.log += 'added %s ' % path
35
36 def removed(self, path):
37 self.log += 'removed %s ' % path
38
39
40 class ObjectDBTest(unittest.TestCase):
41
42 def setUp(self):
43 super(ObjectDBTest, self).setUp()
44 self.project = testutils.sample_project()
45 validation = _MockValidation()
46 self.dbs = [
47 objectdb.ObjectDB(memorydb.MemoryDB(self.project), validation)]
48
49 def tearDown(self):
50 for db in self.dbs:
51 db.write()
52 testutils.remove_project(self.project)
53 super(ObjectDBTest, self).tearDown()
54
55 @_do_for_all_dbs
56 def test_simple_per_name(self, db):
57 db.add_pername('file', 'key', 'name', 1)
58 self.assertEqual(1, db.get_pername('file', 'key', 'name'))
59
60 @_do_for_all_dbs
61 def test_simple_per_name_does_not_exist(self, db):
62 self.assertEquals(None, db.get_pername('file', 'key', 'name'))
63
64 @_do_for_all_dbs
65 def test_simple_per_name_after_syncing(self, db):
66 db.add_pername('file', 'key', 'name', 1)
67 db.write()
68 self.assertEquals(1, db.get_pername('file', 'key', 'name'))
69
70 @_do_for_all_dbs
71 def test_getting_returned(self, db):
72 db.add_callinfo('file', 'key', (1, 2), 3)
73 self.assertEquals(3, db.get_returned('file', 'key', (1, 2)))
74
75 @_do_for_all_dbs
76 def test_getting_returned_when_does_not_match(self, db):
77 db.add_callinfo('file', 'key', (1, 2), 3)
78 self.assertEquals(None, db.get_returned('file', 'key', (1, 1)))
79
80 @_do_for_all_dbs
81 def test_getting_call_info(self, db):
82 db.add_callinfo('file', 'key', (1, 2), 3)
83
84 call_infos = list(db.get_callinfos('file', 'key'))
85 self.assertEquals(1, len(call_infos))
86 self.assertEquals((1, 2), call_infos[0].get_parameters())
87 self.assertEquals(3, call_infos[0].get_returned())
88
89 @_do_for_all_dbs
90 def test_invalid_per_name(self, db):
91 db.add_pername('file', 'key', 'name', -1)
92 self.assertEquals(None, db.get_pername('file', 'key', 'name'))
93
94 @_do_for_all_dbs
95 def test_overwriting_per_name(self, db):
96 db.add_pername('file', 'key', 'name', 1)
97 db.add_pername('file', 'key', 'name', 2)
98 self.assertEquals(2, db.get_pername('file', 'key', 'name'))
99
100 @_do_for_all_dbs
101 def test_not_overwriting_with_invalid_per_name(self, db):
102 db.add_pername('file', 'key', 'name', 1)
103 db.add_pername('file', 'key', 'name', -1)
104 self.assertEquals(1, db.get_pername('file', 'key', 'name'))
105
106 @_do_for_all_dbs
107 def test_getting_invalid_returned(self, db):
108 db.add_callinfo('file', 'key', (1, 2), -1)
109 self.assertEquals(None, db.get_returned('file', 'key', (1, 2)))
110
111 @_do_for_all_dbs
112 def test_not_overwriting_with_invalid_returned(self, db):
113 db.add_callinfo('file', 'key', (1, 2), 3)
114 db.add_callinfo('file', 'key', (1, 2), -1)
115 self.assertEquals(3, db.get_returned('file', 'key', (1, 2)))
116
117 @_do_for_all_dbs
118 def test_get_files(self, db):
119 db.add_callinfo('file1', 'key', (1, 2), 3)
120 db.add_callinfo('file2', 'key', (1, 2), 3)
121 self.assertEquals(set(['file1', 'file2']), set(db.get_files()))
122
123 @_do_for_all_dbs
124 def test_validating_files(self, db):
125 db.add_callinfo('invalid', 'key', (1, 2), 3)
126 db.validate_files()
127 self.assertEquals(0, len(db.get_files()))
128
129 @_do_for_all_dbs
130 def test_validating_file_for_scopes(self, db):
131 db.add_callinfo('file', 'invalid', (1, 2), 3)
132 db.validate_file('file')
133 self.assertEquals(1, len(db.get_files()))
134 self.assertEquals(0, len(list(db.get_callinfos('file', 'invalid'))))
135
136 @_do_for_all_dbs
137 def test_validating_file_moved(self, db):
138 db.add_callinfo('file', 'key', (1, 2), 3)
139
140 db.file_moved('file', 'newfile')
141 self.assertEquals(1, len(db.get_files()))
142 self.assertEquals(1, len(list(db.get_callinfos('newfile', 'key'))))
143
144 @_do_for_all_dbs
145 def test_using_file_list_observer(self, db):
146 db.add_callinfo('invalid', 'key', (1, 2), 3)
147 observer = _MockFileListObserver()
148 db.add_file_list_observer(observer)
149 db.validate_files()
150 self.assertEquals('removed invalid ', observer.log)
151
152
153 def suite():
154 result = unittest.TestSuite()
155 result.addTests(unittest.makeSuite(ObjectDBTest))
156 return result
157
158
159 if __name__ == '__main__':
160 unittest.main()
OLDNEW
« no previous file with comments | « tools/telemetry/third_party/rope/ropetest/historytest.py ('k') | tools/telemetry/third_party/rope/ropetest/objectinfertest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698