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

Side by Side Diff: infra/services/gnumbd/test/util_test.py

Issue 355153002: Refactor infra git libs and testing. (Closed) Base URL: https://chromium.googlesource.com/infra/infra@fake_testing_support
Patch Set: Change config ref to have a sandard naming scheme Created 6 years, 5 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 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import unittest
6 import textwrap
7 import collections
8
9 from infra.services.gnumbd.support import util
10
11
12 class TestCalledProcessError(unittest.TestCase):
13 def testBasic(self):
14 cpe = util.CalledProcessError(100, ('cat', 'dog'), None, None)
15 self.assertEqual(
16 str(cpe), "Command ('cat', 'dog') returned non-zero exit status 100.\n")
17
18 def testErr(self):
19 cpe = util.CalledProcessError(
20 100, ('cat', 'dog'), None,
21 'Cat says the dog smells funny.\nCat is not amused.')
22
23 self.assertEqual(str(cpe), textwrap.dedent('''\
24 Command ('cat', 'dog') returned non-zero exit status 100:
25 STDERR ========================================
26 Cat says the dog smells funny.
27 Cat is not amused.
28 '''))
29
30 def testOut(self):
31 cpe = util.CalledProcessError(
32 100, ('cat', 'dog'),
33 'This totally worked! Squirrels are awesome!',
34 '')
35
36 self.assertEqual(str(cpe), textwrap.dedent('''\
37 Command ('cat', 'dog') returned non-zero exit status 100:
38 STDOUT ========================================
39 This totally worked! Squirrels are awesome!
40 '''))
41
42 def testBoth(self):
43 cpe = util.CalledProcessError(
44 100, ('cat', 'dog'),
45 'This totally worked! Squirrels are awesome!',
46 'Cat says the dog smells funny.\nCat is not amused.')
47
48 self.assertEqual(str(cpe), textwrap.dedent('''\
49 Command ('cat', 'dog') returned non-zero exit status 100:
50 STDOUT ========================================
51 This totally worked! Squirrels are awesome!
52 STDERR ========================================
53 Cat says the dog smells funny.
54 Cat is not amused.
55 '''))
56
57
58 class TestCachedProperty(unittest.TestCase):
59 def setUp(self):
60 self.calls = calls = []
61 class Foo(object):
62 def __init__(self, success=True, override=None):
63 self.success = success
64 self.override = override
65
66 @util.cached_property
67 def happy(self):
68 calls.append(1)
69 if self.override is not None:
70 self._happy = self.override # pylint: disable=W0201
71 if not self.success:
72 raise Exception('nope')
73 return 'days'
74 self.Foo = Foo
75
76 def testBasic(self):
77 f = self.Foo()
78 self.assertEqual(f.happy, 'days')
79 self.assertEqual(f.happy, 'days')
80 self.assertEqual(sum(self.calls), 1)
81
82 def testBareReturnsSelf(self):
83 self.assertIsInstance(self.Foo.happy, util.cached_property)
84
85 def testOverride(self):
86 f = self.Foo(override='cowabunga!')
87 self.assertEqual(f.happy, 'cowabunga!')
88 self.assertEqual(f.happy, 'cowabunga!')
89 self.assertEqual(sum(self.calls), 1)
90
91 def testNoCache(self):
92 f = self.Foo(False)
93 with self.assertRaises(Exception):
94 f.happy # pylint: disable=W0104
95 f.success = True
96 self.assertEqual(f.happy, 'days')
97 self.assertEqual(sum(self.calls), 2)
98
99 def testDel(self):
100 f = self.Foo()
101 self.assertEqual(f.happy, 'days')
102 self.assertEqual(f.happy, 'days')
103 del f.happy
104 self.assertEqual(f.happy, 'days')
105 del f.happy
106 del f.happy
107 self.assertEqual(f.happy, 'days')
108 self.assertEqual(sum(self.calls), 3)
109
110
111 class TestFreeze(unittest.TestCase):
112 def testDict(self):
113 d = collections.OrderedDict()
114 d['cat'] = 100
115 d['dog'] = 0
116
117 f = util.freeze(d)
118 self.assertEqual(d, f)
119 self.assertIsInstance(f, util.FrozenDict)
120 self.assertEqual(
121 hash(f),
122 hash((0, ('cat', 100))) ^ hash((1, ('dog', 0)))
123 )
124 self.assertEqual(len(d), len(f))
125
126 # Cover equality
127 self.assertEqual(f, f)
128 self.assertNotEqual(f, 'dog')
129 self.assertNotEqual(f, {'bob': 'hat'})
130 self.assertNotEqual(f, {'cat': 20, 'dog': 10})
131
132 def testList(self):
133 l = [1, 2, {'bob': 100}]
134 f = util.freeze(l)
135 self.assertSequenceEqual(l, f)
136 self.assertIsInstance(f, tuple)
137
138 def testSet(self):
139 s = {1, 2, util.freeze({'bob': 100})}
140 f = util.freeze(s)
141 self.assertEqual(s, f)
142 self.assertIsInstance(f, frozenset)
143
144
145 class TestThaw(unittest.TestCase):
146 def testDict(self):
147 d = collections.OrderedDict()
148 d['cat'] = 100
149 d['dog'] = 0
150 f = util.freeze(d)
151 t = util.thaw(f)
152 self.assertEqual(d, f)
153 self.assertEqual(t, f)
154 self.assertEqual(d, t)
155 self.assertIsInstance(t, collections.OrderedDict)
156
157 def testList(self):
158 l = [1, 2, {'bob': 100}]
159 f = util.freeze(l)
160 t = util.thaw(f)
161 self.assertSequenceEqual(l, f)
162 self.assertSequenceEqual(f, t)
163 self.assertSequenceEqual(l, t)
164 self.assertIsInstance(t, list)
165
166 def testSet(self):
167 s = {1, 2, 'cat'}
168 f = util.freeze(s)
169 t = util.thaw(f)
170 self.assertEqual(s, f)
171 self.assertEqual(f, t)
172 self.assertEqual(t, s)
173 self.assertIsInstance(t, set)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698