More unit tests

This gives us one positive test for every stanza

Change-Id: I09bd2bf0f2756be4d232be402190dee8cea68ae2
This commit is contained in:
Sean Dague 2016-05-26 06:22:29 -04:00
parent 8fab9214df
commit fccfbb9baf
4 changed files with 147 additions and 5 deletions

View File

@ -16,6 +16,8 @@
# under the License.
import os
import fixtures
import testtools
@ -23,6 +25,73 @@ def example_dir(name=""):
return os.path.join(os.path.dirname(__file__), 'examples', name)
_TRUE_VALUES = ('True', 'true', '1', 'yes')
class OutputStreamCapture(fixtures.Fixture):
"""Capture output streams during tests.
This fixture captures errant printing to stderr / stdout during
the tests and lets us see those streams at the end of the test
runs instead. Useful to see what was happening during failed
tests.
"""
def setUp(self):
super(OutputStreamCapture, self).setUp()
if os.environ.get('OS_STDOUT_CAPTURE') in _TRUE_VALUES:
self.out = self.useFixture(fixtures.StringStream('stdout'))
self.useFixture(
fixtures.MonkeyPatch('sys.stdout', self.out.stream))
if os.environ.get('OS_STDERR_CAPTURE') in _TRUE_VALUES:
self.err = self.useFixture(fixtures.StringStream('stderr'))
self.useFixture(
fixtures.MonkeyPatch('sys.stderr', self.err.stream))
@property
def stderr(self):
return self.err._details["stderr"].as_text()
@property
def stdout(self):
return self.out._details["stdout"].as_text()
class Timeout(fixtures.Fixture):
"""Setup per test timeouts.
In order to avoid test deadlocks we support setting up a test
timeout parameter read from the environment. In almost all
cases where the timeout is reached this means a deadlock.
A class level TIMEOUT_SCALING_FACTOR also exists, which allows
extremely long tests to specify they need more time.
"""
def __init__(self, timeout, scaling=1):
super(Timeout, self).__init__()
try:
self.test_timeout = int(timeout)
except ValueError:
# If timeout value is invalid do not set a timeout.
self.test_timeout = 0
if scaling >= 1:
self.test_timeout *= scaling
else:
raise ValueError('scaling value must be >= 1')
def setUp(self):
super(Timeout, self).setUp()
if self.test_timeout > 0:
self.useFixture(fixtures.Timeout(self.test_timeout, gentle=True))
class TestCase(testtools.TestCase):
"""Test case base class for all unit tests."""
def setUp(self):
"""Run before each test method to initialize test environment."""
super(TestCase, self).setUp()
self.useFixture(Timeout(
os.environ.get('OS_TEST_TIMEOUT', 0)))
self.useFixture(OutputStreamCapture())

View File

@ -1,3 +1,13 @@
.. rest_expand_all::
I am text, hear me roar!
==============
List Servers
==============
.. rest_method:: GET /servers
.. rest_parameters:: parameters.yaml
- name: name

View File

@ -0,0 +1,6 @@
name:
in: body
required: true
type: string
description: |
The name of things

View File

@ -34,14 +34,71 @@ class TestBasicExample(base.TestCase):
@with_app(buildername='html', srcdir=base.example_dir('basic'),
copy_srcdir_to_tmpdir=True)
def test_sphinx_build(self, app, status, warning):
app.build()
html = (app.outdir / 'index.html').read_text()
soup = BeautifulSoup(html, 'html.parser')
content = str(soup.find(id='expand-all'))
def setUp(self, app, status, warning):
super(TestBasicExample, self).setUp()
self.app = app
self.status = status
self.warning = warning
self.app.build()
self.html = (app.outdir / 'index.html').read_text()
self.soup = BeautifulSoup(self.html, 'html.parser')
self.content = str(self.soup)
def test_expand_all(self):
"""Do we get an expand all button like we expect."""
content = str(self.soup.find(id='expand-all'))
example_button = ('<button class="btn btn-info btn-sm btn-expand-all" '
'data-toggle="collapse" id="expand-all">'
'Show All</button>')
self.assertEqual(
example_button,
content)
def test_rest_method(self):
"""Do we get a REST method call block"""
# TODO(sdague): it probably would make sense to do this as a
# whole template instead of parts.
content = str(self.soup.find_all(class_='operation-grp'))
self.assertIn(
'<span class="glyphicon glyphicon-link"></span>',
str(content))
self.assertIn(
'<span class="label label-GET">GET</span>',
str(content))
self.assertIn(
'<div class="col-md-5">/servers</div>',
str(content))
self.assertIn(
('<button class="btn btn-info btn-sm btn-detail" '
'data-target="#list-servers-detail" data-toggle="collapse" '
'id="list-servers-detail-btn">detail</button>'),
str(content))
def test_parameters(self):
"""Do we get some parameters table"""
table = """<table border="1" class="docutils">
<colgroup>
<col width="20%"></col>
<col width="10%"></col>
<col width="10%"></col>
<col width="60%"></col>
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Name</th>
<th class="head">In</th>
<th class="head">Type</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>name</td>
<td>body</td>
<td>string</td>
<td>The name of things</td>
</tr>
</tbody>
</table>"""
self.assertIn(table, self.content)