From 9867b260e1b4efac96eaae76337f9da61f4a8b9c Mon Sep 17 00:00:00 2001 From: Guy Taylor Date: Tue, 16 Oct 2018 12:41:20 +0100 Subject: [PATCH] Improve Python testing Introduce pytest, wire it into Make and convert previous tests. Note this is not wired into Travis --- Makefile | 8 ++++- requirements.txt | 1 + src/mako/lib/util.py | 61 +---------------------------------- src/mako/lib/util_test.py | 67 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 61 deletions(-) create mode 100644 src/mako/lib/util_test.py diff --git a/Makefile b/Makefile index 589901e044..a246f4805b 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/requirements.txt b/requirements.txt index 580618b8ba..4a03020845 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ mako pyyaml mkdocs==0.16.3 +pytest diff --git a/src/mako/lib/util.py b/src/mako/lib/util.py index 8a0cef1070..f0bf78a5ff 100644 --- a/src/mako/lib/util.py +++ b/src/mako/lib/util.py @@ -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') diff --git a/src/mako/lib/util_test.py b/src/mako/lib/util_test.py new file mode 100644 index 0000000000..7d0a723e2c --- /dev/null +++ b/src/mako/lib/util_test.py @@ -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()