1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
test_shoogle
----------------------------------
Tests for `shoogle` module.
"""
import collections
from contextlib import contextmanager
import json
from io import StringIO
import logging
import re
import sys
import tempfile
import unittest
import shoogle
from shoogle import lib
from shoogle import config
import jsmin
def load_json(string):
return json.loads(jsmin.jsmin(string))
@contextmanager
def temporal_file(contents):
with tempfile.NamedTemporaryFile() as fd:
fd.write(bytes(contents, "utf8"))
fd.flush()
yield fd.name
Execution = collections.namedtuple("Execution", ["status", "out", "err"])
def main(*args, **kwargs):
old_stdout, old_stderr = sys.stdout, sys.stderr
new_stdout, new_stderr = StringIO(), StringIO()
sys.stdout, sys.stderr = new_stdout, new_stderr
config.logger = lib.get_logger("shoogle-test", level=logging.ERROR, channel=new_stderr)
try:
status = shoogle.main(*args, **kwargs)
finally:
sys.stdout, sys.stderr = old_stdout, old_stderr
return Execution(status=status, out=new_stdout.getvalue(), err=new_stderr.getvalue())
class TestShoogle(unittest.TestCase):
def test_main_without_arguments_shows_usage_and_help_messages(self):
e = main([])
self.assertEqual(2, e.status)
self.assertIn("usage: ", e.err)
self.assertIn("positional arguments:", e.err)
self.assertIn("{show,execute}", e.err)
self.assertIn("optional arguments:", e.err)
def test_main_with_option_shows_usage_and_help_messages(self):
e = main(["-h"])
self.assertEqual(2, e.status)
self.assertIn("usage: ", e.out)
self.assertIn("positional arguments:", e.out)
self.assertIn("{show,execute}", e.out)
self.assertIn("optional arguments:", e.out)
def test_main_with_option_shows_version(self):
e = main(["-v"])
self.assertEqual(0, e.status)
self.assertEqual(shoogle.__version__, e.out.strip())
def test_main_with_empty_show_command_shows_all_services_sorted(self):
e = main(["show"])
self.assertEqual(0, e.status)
lines = e.out.splitlines()
services = [line.split()[0] for line in lines]
self.assertIn("webmasters:v3", services)
self.assertIn("youtube:v3", services)
self.assertEqual(list(sorted(services)), services)
def test_main_with_string_show_command_shows_services_with_that_string(self):
e = main(["show", "youtu"])
self.assertEqual(0, e.status)
lines = e.out.splitlines()
services = [line.split()[0] for line in lines]
self.assertEqual(list(sorted(services)), services)
for service in services:
self.assertIn("youtu", service)
def test_main_with_exact_service_string_shows_all_resources_sorted(self):
e = main(["show", "youtube:v3"])
self.assertEqual(0, e.status)
lines = e.out.splitlines()
services = [line.split()[0] for line in lines]
self.assertIn("youtube:v3.activities", services)
self.assertIn("youtube:v3.videos", services)
resources = [service.split(".")[-1] for service in services]
self.assertEqual(list(sorted(resources)), resources)
def test_main_with_partial_resource_string_shows_resources_sorted(self):
e = main(["show", "youtube:v3.vid"])
lines = e.out.splitlines()
resources = [line.split()[0] for line in lines]
self.assertEqual(0, e.status)
self.assertNotIn("youtube:v3.activities", resources)
self.assertIn("youtube:v3.videos", resources)
self.assertEqual(list(sorted(resources)), resources)
def test_main_with_exact_resource_string_shows_all_methods_sorted(self):
e = main(["show", "youtube:v3.videos"])
lines = e.out.splitlines()
methods = [line.split()[0] for line in lines if " - " in line]
self.assertEqual(0, e.status)
self.assertIn("youtube:v3.videos.list", methods)
self.assertIn("youtube:v3.videos.insert", methods)
self.assertEqual(list(sorted(methods)), methods)
def test_main_with_partial_method_string_shows_matching_methods_sorted(self):
e = main(["show", "youtube:v3.videos.li"])
lines = e.out.splitlines()
methods = [line.split()[0] for line in lines]
self.assertEqual(0, e.status)
self.assertIn("youtube:v3.videos.list", methods)
self.assertNotIn("youtube:v3.videos.insert", methods)
self.assertEqual(list(sorted(methods)), methods)
def test_main_with_exact_method_shows_request_and_response_details(self):
e = main(["show", "youtube:v3.videos.list"])
lines = e.out.splitlines()
self.assertEqual(0, e.status)
self.assertTrue("Request" in line for line in lines)
self.assertTrue("Response" in line for line in lines)
def test_main_with_exact_method_shows_request_with_minimal_params(self):
e = main(["show", "tasks:v1.tasks.get"])
jsons = re.findall(r"^\{$.*?^\}$", e.out, re.MULTILINE | re.DOTALL)
self.assertEqual(0, e.status)
self.assertEqual(2, len(jsons))
request_json = load_json(jsons[0])
response_json = load_json(jsons[1])
self.assertEqual(["task", "tasklist"], list(request_json.keys()))
self.assertEqual({"$ref": 'Task'}, response_json)
def test_main_execute_with_missing_parameter(self):
with temporal_file("{}") as request_file:
e = main(["execute", "tasks:v1.tasks.get", request_file])
self.assertEqual(0, e.status)
self.assertIn('Missing required parameter', e.err)
if __name__ == '__main__':
sys.exit(unittest.main())
|