19
19
20
20
import dateutil .parser
21
21
from dateutil import tz
22
- import mock
22
+ from oslo_config import cfg
23
23
import six
24
+ import unittest
25
+ from unittest import mock
24
26
25
27
from caso .extract import manager
26
28
from caso .tests import base
27
29
30
+ CONF = cfg .CONF
28
31
29
- class TestCasoManager (base .TestCase ):
32
+ class TestCasoManager (unittest .TestCase ):
30
33
"""Test case for the cASO extractor manager."""
31
34
32
35
def setUp (self ):
@@ -63,6 +66,7 @@ def test_extract_empty_projects(self):
63
66
self .assertFalse (self .m_extractor .extract .called )
64
67
self .assertEqual (ret , [])
65
68
69
+
66
70
def test_extract (self ):
67
71
"""Test that we extract records for a given project."""
68
72
self .flags (dry_run = True )
@@ -75,7 +79,7 @@ def test_extract(self):
75
79
ret = self .manager .get_records ()
76
80
self .m_extractor .assert_called_once_with (
77
81
"bazonk" ,
78
- mock .ANY ,
82
+ unittest . mock .ANY ,
79
83
)
80
84
self .m_extractor .return_value .extract .assert_called_once_with (
81
85
dateutil .parser .parse (extract_from ).replace (tzinfo = tz .tzutc ()),
@@ -102,15 +106,15 @@ def test_get_records_with_lastrun(self):
102
106
extract_to = "2015-12-19"
103
107
self .flags (extract_to = extract_to )
104
108
105
- with mock .patch .object (self .manager , "get_lastrun" ) as m :
109
+ with unittest . mock .patch .object (self .manager , "get_lastrun" ) as m :
106
110
m .return_value = lastrun
107
111
108
112
ret = self .manager .get_records ()
109
113
110
114
m .assert_called_once_with ("bazonk" )
111
115
self .m_extractor .assert_called_once_with (
112
116
"bazonk" ,
113
- mock .ANY ,
117
+ unittest . mock .ANY ,
114
118
)
115
119
self .m_extractor .return_value .extract .assert_called_once_with (
116
120
dateutil .parser .parse (lastrun ).replace (tzinfo = tz .tzutc ()),
@@ -126,9 +130,9 @@ def test_lastrun_exists(self):
126
130
else :
127
131
builtins_open = "__builtin__.open"
128
132
129
- fopen = mock .mock_open (read_data = str (expected ))
130
- with mock .patch ("os.path.exists" ) as path :
131
- with mock .patch (builtins_open , fopen ):
133
+ fopen = unittest . mock .mock_open (read_data = str (expected ))
134
+ with unittest . mock .patch ("os.path.exists" ) as path :
135
+ with unittest . mock .patch (builtins_open , fopen ):
132
136
path .return_value = True
133
137
self .assertEqual (expected , self .manager .get_lastrun ("foo" ))
134
138
@@ -138,9 +142,9 @@ def test_lastrun_is_invalid(self):
138
142
builtins_open = "builtins.open"
139
143
else :
140
144
builtins_open = "__builtin__.open"
141
- fopen = mock .mock_open (read_data = "foo" )
142
- with mock .patch ("os.path.exists" ) as path :
143
- with mock .patch (builtins_open , fopen ):
145
+ fopen = unittest . mock .mock_open (read_data = "foo" )
146
+ with unittest . mock .patch ("os.path.exists" ) as path :
147
+ with unittest . mock .patch (builtins_open , fopen ):
144
148
path .return_value = True
145
149
self .assertRaises (ValueError , self .manager .get_lastrun , "foo" )
146
150
@@ -149,7 +153,7 @@ def test_write_lastrun_dry_run(self):
149
153
self .flags (dry_run = True )
150
154
self .flags (projects = ["bazonk" ])
151
155
152
- with mock .patch .object (self .manager , "write_lastrun" ) as m :
156
+ with unittest . mock .patch .object (self .manager , "write_lastrun" ) as m :
153
157
self .manager .get_records ()
154
158
m .assert_called_once_with ("bazonk" )
155
159
@@ -161,6 +165,16 @@ def test_write_lastrun(self):
161
165
else :
162
166
builtins_open = "__builtin__.open"
163
167
164
- with mock .patch (builtins_open , mock .mock_open ()) as m :
168
+ with unittest . mock .patch (builtins_open , unittest . mock .mock_open ()) as m :
165
169
self .manager .get_records ()
166
170
m .assert_called_once_with ("/var/spool/caso/lastrun.bazonk" , "w" )
171
+
172
+ def flags (self , ** kw ):
173
+ """Override flag variables for a test."""
174
+ group = kw .pop ("group" , None )
175
+ for k , v in six .iteritems (kw ):
176
+ CONF .set_override (k , v , group )
177
+
178
+ def reset_flags (self ):
179
+ """Reset flags."""
180
+ CONF .reset ()
0 commit comments