Improve Python testing

Introduce pytest, wire it into Make and convert previous tests.

Note this is not wired into Travis
This commit is contained in:
Guy Taylor
2018-10-16 12:41:20 +01:00
parent 19e9943279
commit 9867b260e1
4 changed files with 76 additions and 61 deletions

View File

@@ -1,4 +1,4 @@
.PHONY: help deps regen-apis license clean
.PHONY: help deps regen-apis license test-gen test clean
.SUFFIXES:
VIRTUALENV_VERSION = 16.0.0
@@ -7,6 +7,7 @@ VENV_DIR := .pyenv-$(shell uname)
PYTHON := $(VENV_DIR)/bin/python
PIP := $(VENV_DIR)/bin/pip
MAKO_RENDER := etc/bin/mako-render
PYTEST := $(PYTHON) -m pytest
API_VERSION_GEN := etc/bin/api_version_to_yaml.py
TPL := $(PYTHON) $(MAKO_RENDER)
MKDOCS := $(shell pwd)/$(VENV_DIR)/bin/mkdocs
@@ -81,6 +82,11 @@ license: LICENSE.md
regen-apis: | clean-all-api clean-all-cli gen-all-api gen-all-cli license
test-gen: $(PYTHON)
$(PYTEST) src
test: test-gen
clean: clean-all-api clean-all-cli docs-all-clean
-rm -Rf $(VENV_DIR)
-rm $(API_DEPS) $(CLI_DEPS)

View File

@@ -1,3 +1,4 @@
mako
pyyaml
mkdocs==0.16.3
pytest

View File

@@ -1037,63 +1037,4 @@ def size_to_bytes(size):
if __name__ == '__main__':
def test_to_version():
for v, want in (('v1.3', '1d3'),
('v1', '1'),
('directory_v1', '1_directory'),
('directory_v1.3', '1d3_directory'),
('v1beta2', '1_beta2'),
('v1sandbox', '1_sandbox'),
('v2.0', '2'),
('v2.0.1', '2d0d1'),
('v0.0', '0'),
('v0.1.0', '0d1'),
('v2.0beta3', '2_beta3'),
('alpha', 'alpha'),
('beta', 'beta'),
('vm_beta', 'vm_beta')):
res = to_api_version(v)
assert res == want, "%s == %s" % (res, want)
# end for each pair
for iv in ('some_branch_name', '1.3'):
try:
to_api_version(iv)
except AssertionError:
pass
else:
AssertionError("Call should have failed")
# end for each invalid version
# suite
def test_library_name():
for v, want in (('v1', 'oauth2_v1'),
('v1.4', 'oauth2_v1d4'),
('alpha', 'oauth2_alpha'),
('beta', 'oauth2_beta'),
('vm_beta', 'oauth2_vm_beta')):
res = library_name('oauth2', v)
assert res == want, "%s ~== %s" % (res, want)
def test_url_substitution():
url = "https://www.googleapis.com/resumable/upload/groups/v1/groups/{groupId}/{foo}/archive"
ms = list(re_find_replacements.finditer(url))
assert len(ms) == 2
assert ms[0].group(0) == '{groupId}'
assert ms[1].group(0) == '{foo}'
url = "customer/{customerId}/orgunits{/orgUnitPath*}"
ms = list(re_find_replacements.findall(url))
assert len(ms) == 2
assert ms[0] == '{customerId}'
assert ms[1] == '{/orgUnitPath*}'
url = "{+project}/subscriptions"
ms = list(re_find_replacements.findall(url))
assert len(ms) == 1
assert ms[0] == '{+project}'
test_to_version()
test_library_name()
test_url_substitution()
raise AssertionError('For import only')

67
src/mako/lib/util_test.py Normal file
View File

@@ -0,0 +1,67 @@
#!/usr/bin/env python
import unittest
from util import to_api_version, library_name, re_find_replacements
class UtilsTest(unittest.TestCase):
def test_to_version_ok(self):
for v, want in (('v1.3', '1d3'),
('v1', '1'),
('directory_v1', '1_directory'),
('directory_v1.3', '1d3_directory'),
('v1beta2', '1_beta2'),
('v1sandbox', '1_sandbox'),
('v2.0', '2'),
('v2.0.1', '2d0d1'),
('v0.0', '0'),
('v0.1.0', '0d1'),
('v2.0beta3', '2_beta3'),
('alpha', 'alpha'),
('beta', 'beta'),
('vm_beta', 'vm_beta')):
res = to_api_version(v)
self.assertEqual(res, want)
def test_to_version_fail(self):
for iv in ('some_branch_name', '1.3'):
with self.assertRaises(AssertionError):
to_api_version(iv)
def test_library_name(self):
for v, want in (('v1', 'oauth2_v1'),
('v1.4', 'oauth2_v1d4'),
('alpha', 'oauth2_alpha'),
('beta', 'oauth2_beta'),
('vm_beta', 'oauth2_vm_beta')):
res = library_name('oauth2', v)
self.assertEqual(res, want)
def test_url_substitution(self):
url = "https://www.googleapis.com/resumable/upload/groups/v1/groups/{groupId}/{foo}/archive"
ms = list(re_find_replacements.finditer(url))
self.assertEqual(len(ms), 2)
self.assertEqual(ms[0].group(0), '{groupId}')
self.assertEqual(ms[1].group(0), '{foo}')
url = "customer/{customerId}/orgunits{/orgUnitPath*}"
ms = list(re_find_replacements.findall(url))
self.assertEqual(len(ms), 2)
self.assertEqual(ms[0], '{customerId}')
self.assertEqual(ms[1], '{/orgUnitPath*}')
url = "{+project}/subscriptions"
ms = list(re_find_replacements.findall(url))
self.assertEqual(len(ms), 1)
self.assertEqual(ms[0], '{+project}')
def main():
unittest.main()
if __name__ == "__main__":
main()