Skip to content

Commit d39f0cc

Browse files
aidaphalvarolopez
authored andcommitted
test: replace testtools for unittest to make tests work
1 parent 115ffa6 commit d39f0cc

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

caso/tests/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
from oslo_config import cfg
2525
from oslo_config import fixture as config_fixture
2626
import six
27-
import testtools
27+
import unittest
2828

2929
CONF = cfg.CONF
3030

3131
_TRUE_VALUES = ("True", "true", "1", "yes")
3232

3333

34-
class TestCase(testtools.TestCase):
34+
class TestCase(unittest.TestCase):
3535
"""Test case base class for all unit tests."""
3636

3737
REQUIRES_LOCKING = False

caso/tests/extract/test_manager.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919

2020
import dateutil.parser
2121
from dateutil import tz
22-
import mock
22+
from oslo_config import cfg
2323
import six
24+
import unittest
25+
from unittest import mock
2426

2527
from caso.extract import manager
2628
from caso.tests import base
2729

30+
CONF = cfg.CONF
2831

29-
class TestCasoManager(base.TestCase):
32+
class TestCasoManager(unittest.TestCase):
3033
"""Test case for the cASO extractor manager."""
3134

3235
def setUp(self):
@@ -63,6 +66,7 @@ def test_extract_empty_projects(self):
6366
self.assertFalse(self.m_extractor.extract.called)
6467
self.assertEqual(ret, [])
6568

69+
6670
def test_extract(self):
6771
"""Test that we extract records for a given project."""
6872
self.flags(dry_run=True)
@@ -75,7 +79,7 @@ def test_extract(self):
7579
ret = self.manager.get_records()
7680
self.m_extractor.assert_called_once_with(
7781
"bazonk",
78-
mock.ANY,
82+
unittest.mock.ANY,
7983
)
8084
self.m_extractor.return_value.extract.assert_called_once_with(
8185
dateutil.parser.parse(extract_from).replace(tzinfo=tz.tzutc()),
@@ -102,15 +106,15 @@ def test_get_records_with_lastrun(self):
102106
extract_to = "2015-12-19"
103107
self.flags(extract_to=extract_to)
104108

105-
with mock.patch.object(self.manager, "get_lastrun") as m:
109+
with unittest.mock.patch.object(self.manager, "get_lastrun") as m:
106110
m.return_value = lastrun
107111

108112
ret = self.manager.get_records()
109113

110114
m.assert_called_once_with("bazonk")
111115
self.m_extractor.assert_called_once_with(
112116
"bazonk",
113-
mock.ANY,
117+
unittest.mock.ANY,
114118
)
115119
self.m_extractor.return_value.extract.assert_called_once_with(
116120
dateutil.parser.parse(lastrun).replace(tzinfo=tz.tzutc()),
@@ -126,9 +130,9 @@ def test_lastrun_exists(self):
126130
else:
127131
builtins_open = "__builtin__.open"
128132

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):
132136
path.return_value = True
133137
self.assertEqual(expected, self.manager.get_lastrun("foo"))
134138

@@ -138,9 +142,9 @@ def test_lastrun_is_invalid(self):
138142
builtins_open = "builtins.open"
139143
else:
140144
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):
144148
path.return_value = True
145149
self.assertRaises(ValueError, self.manager.get_lastrun, "foo")
146150

@@ -149,7 +153,7 @@ def test_write_lastrun_dry_run(self):
149153
self.flags(dry_run=True)
150154
self.flags(projects=["bazonk"])
151155

152-
with mock.patch.object(self.manager, "write_lastrun") as m:
156+
with unittest.mock.patch.object(self.manager, "write_lastrun") as m:
153157
self.manager.get_records()
154158
m.assert_called_once_with("bazonk")
155159

@@ -161,6 +165,16 @@ def test_write_lastrun(self):
161165
else:
162166
builtins_open = "__builtin__.open"
163167

164-
with mock.patch(builtins_open, mock.mock_open()) as m:
168+
with unittest.mock.patch(builtins_open, unittest.mock.mock_open()) as m:
165169
self.manager.get_records()
166170
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()

caso/tests/extract/test_nova.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
"""Tests for the OpenStack nova extractor."""
1616

1717
from caso.extract.openstack import nova
18+
from caso.extract import manager
1819
from caso.tests import base
1920

21+
import unittest
2022

21-
class TestCasoManager(base.TestCase):
23+
class TestCasoManager(unittest.TestCase):
2224
"""Test case for Nova extractor."""
2325

2426
def setUp(self):

caso/tests/test_manager.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414

1515
"""Tests for `caso.manager` module."""
1616

17-
import mock
17+
from unittest.mock import patch
1818
from oslo_concurrency.fixture import lockutils as lock_fixture
1919
import six
20+
import unittest
21+
from unittest import mock
2022

2123
from caso import manager
2224
from caso.tests import base
2325

2426

25-
class TestCasoManager(base.TestCase):
27+
class TestCasoManager(unittest.TestCase):
2628
"""Test case for the cASO Manager."""
2729

28-
REQUIRES_LOCKING = True
29-
3030
def setUp(self):
3131
"""Set global test fixtures and mocks."""
3232
self.useFixture(lock_fixture.ExternalLockFixture())
@@ -48,3 +48,4 @@ def tearDown(self):
4848
p.stop()
4949

5050
super(TestCasoManager, self).tearDown()
51+

0 commit comments

Comments
 (0)