Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ cover-package=stackinabox
cover-erase=1
cover-inclusive=true
cover-branches=true
cover-min-percentage=91
cover-min-percentage=92
92 changes: 92 additions & 0 deletions stackinabox/tests/test_httpretty_decorator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Stack-In-A-Box: Basic Test
"""
import collections
import types
import unittest

import requests
Expand Down Expand Up @@ -72,3 +74,93 @@ def test_basic(self):

res = requests.put('http://localhost/advanced2/i')
self.assertEqual(res.status_code, 597)


def httpretty_generator():
yield HelloService()


class TestHttprettyBasicWithDecoratorAndGenerator(unittest.TestCase):

def test_verify_generator(self):
self.assertIsInstance(httpretty_generator(), types.GeneratorType)

@stack_decorator.stack_activate(
'localhost',
httpretty_generator()
)
def test_basic(self):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, 200)
self.assertEqual(res.text, 'Hello')

@stack_decorator.stack_activate(
'localhost',
httpretty_generator(),
200, value='Hello'
)
def test_basic_with_parameters(self, response_code, value='alpha'):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, response_code)
self.assertEqual(res.text, value)

@stack_decorator.stack_activate(
'localhost',
httpretty_generator(),
200, value='Hello',
access_services="stack"
)
def test_basic_with_stack_acccess(self, response_code, value='alpha',
stack=None):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, response_code)
self.assertEqual(res.text, value)
self.assertEqual(len(stack), 1)
self.assertTrue(HelloService().name in stack)
self.assertIsInstance(stack[list(stack.keys())[0]], HelloService)


def httpretty_list():
return [
HelloService()
]


class TestHttprettyBasicWithDecoratorAndList(unittest.TestCase):

def test_verify_list(self):
self.assertIsInstance(httpretty_list(), collections.Iterable)

@stack_decorator.stack_activate(
'localhost',
httpretty_list()
)
def test_basic(self):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, 200)
self.assertEqual(res.text, 'Hello')

@stack_decorator.stack_activate(
'localhost',
httpretty_list(),
200, value='Hello'
)
def test_basic_with_parameters(self, response_code, value='alpha'):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, response_code)
self.assertEqual(res.text, value)

@stack_decorator.stack_activate(
'localhost',
httpretty_list(),
200, value='Hello',
access_services="stack"
)
def test_basic_with_stack_acccess(self, response_code, value='alpha',
stack=None):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, response_code)
self.assertEqual(res.text, value)
self.assertEqual(len(stack), 1)
self.assertTrue(HelloService().name in stack)
self.assertIsInstance(stack[list(stack.keys())[0]], HelloService)
74 changes: 74 additions & 0 deletions stackinabox/tests/test_requests_mock_decorator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""
Stack-In-A-Box: Basic Test
"""
import collections
import json
import logging
import types
import unittest

import requests
Expand Down Expand Up @@ -92,3 +94,75 @@ def test_basic(self, session):
self.assertEqual(res.status_code, 597)

session.close()


def requests_mock_generator():
yield HelloService()


class TestRequestsMockBasicWithDecoratorAndGenerator(unittest.TestCase):

def test_verify_generator(self):
self.assertIsInstance(requests_mock_generator(), types.GeneratorType)

@stack_decorator.stack_activate('localhost', requests_mock_generator())
def test_basic_requests_mock(self):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, 200)
self.assertEqual(res.text, 'Hello')

@stack_decorator.stack_activate('localhost', requests_mock_generator(),
200, value='Hello')
def test_basic_with_parameters(self, response_code, value='alpha'):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, response_code)
self.assertEqual(res.text, value)

@stack_decorator.stack_activate('localhost', requests_mock_generator(),
200, value='Hello',
access_services="stack")
def test_basic_with_stack_acccess(self, response_code, value='alpha',
stack=None):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, response_code)
self.assertEqual(res.text, value)
self.assertEqual(len(stack), 1)
self.assertTrue(HelloService().name in stack)
self.assertIsInstance(stack[list(stack.keys())[0]], HelloService)


def requests_mock_list():
return [
HelloService()
]


class TestRequestsMockBasicWithDecoratorAndGenerator(unittest.TestCase):

def test_verify_list(self):
self.assertIsInstance(requests_mock_list(), collections.Iterable)

@stack_decorator.stack_activate('localhost', requests_mock_list())
def test_basic_requests_mock(self):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, 200)
self.assertEqual(res.text, 'Hello')

@stack_decorator.stack_activate('localhost', requests_mock_list(),
200, value='Hello')
def test_basic_with_parameters(self, response_code, value='alpha'):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, response_code)
self.assertEqual(res.text, value)

@stack_decorator.stack_activate('localhost', requests_mock_list(),
200, value='Hello',
access_services="stack")
def test_basic_with_stack_acccess(self, response_code, value='alpha',
stack=None):
res = requests.get('http://localhost/hello/')
self.assertEqual(res.status_code, response_code)
self.assertEqual(res.text, value)
self.assertEqual(len(stack), 1)
self.assertTrue(HelloService().name in stack)
self.assertIsInstance(stack[list(stack.keys())[0]], HelloService)
76 changes: 76 additions & 0 deletions stackinabox/tests/test_responses_decorator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""
Stack-In-A-Box: Basic Test
"""
import collections
import json
import logging
import types
import unittest

import requests
Expand All @@ -15,13 +17,45 @@
logger = logging.getLogger(__name__)


def responses_generator():
yield HelloService()


def responses_list():
return [
HelloService()
]


def test_verify_generator():
assert isinstance(responses_generator(), types.GeneratorType)


def test_verify_list():
assert isinstance(responses_list(), collections.Iterable)


@stack_decorator.stack_activate('localhost', HelloService())
def test_basic_responses():
res = requests.get('http://localhost/hello/')
assert res.status_code == 200
assert res.text == 'Hello'


@stack_decorator.stack_activate('localhost', responses_generator())
def test_basic_responses_and_generator():
res = requests.get('http://localhost/hello/')
assert res.status_code == 200
assert res.text == 'Hello'


@stack_decorator.stack_activate('localhost', responses_list())
def test_basic_responses_and_list():
res = requests.get('http://localhost/hello/')
assert res.status_code == 200
assert res.text == 'Hello'


@stack_decorator.stack_activate('localhost', HelloService(),
200, value='Hello')
def test_basic_with_parameters(response_code, value='alpha'):
Expand All @@ -30,6 +64,22 @@ def test_basic_with_parameters(response_code, value='alpha'):
assert res.text == value


@stack_decorator.stack_activate('localhost', responses_generator(),
200, value='Hello')
def test_basic_with_parameters_and_generator(response_code, value='alpha'):
res = requests.get('http://localhost/hello/')
assert res.status_code == response_code
assert res.text == value


@stack_decorator.stack_activate('localhost', responses_list(),
200, value='Hello')
def test_basic_with_parameters_and_list(response_code, value='alpha'):
res = requests.get('http://localhost/hello/')
assert res.status_code == response_code
assert res.text == value


@stack_decorator.stack_activate('localhost', HelloService(),
200, value='Hello',
access_services="stack")
Expand All @@ -43,6 +93,32 @@ def test_basic_with_stack_acccess(response_code, value='alpha',
assert isinstance(stack[list(stack.keys())[0]], HelloService)


@stack_decorator.stack_activate('localhost', responses_generator(),
200, value='Hello',
access_services="stack")
def test_basic_with_stack_acccess_and_generator(response_code, value='alpha',
stack=None):
res = requests.get('http://localhost/hello/')
assert res.status_code == response_code
assert res.text == value
assert len(stack) == 1
assert HelloService().name in stack
assert isinstance(stack[list(stack.keys())[0]], HelloService)


@stack_decorator.stack_activate('localhost', responses_list(),
200, value='Hello',
access_services="stack")
def test_basic_with_stack_acccess_and_list(response_code, value='alpha',
stack=None):
res = requests.get('http://localhost/hello/')
assert res.status_code == response_code
assert res.text == value
assert len(stack) == 1
assert HelloService().name in stack
assert isinstance(stack[list(stack.keys())[0]], HelloService)


@stack_decorator.stack_activate('localhost', AdvancedService())
def test_advanced_responses():
res = requests.get('http://localhost/advanced/')
Expand Down
24 changes: 22 additions & 2 deletions stackinabox/util/httpretty/decorator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""
Stack-In-A-Box: HTTPretty Support via decorator
"""
import collections
import functools
import logging
import re
import types

import httpretty
import six
Expand Down Expand Up @@ -51,9 +53,27 @@ def __init__(self, uri, *args, **kwargs):
else:
self.enable_service_access = None

def process_service(arg_based_service, raise_on_type=True):
if isinstance(arg_based_service, StackInABoxService):
logger.debug("Registering {0}".format(arg_based_service.name))
self.services[arg_based_service.name] = arg_based_service
return True
elif raise_on_type:
raise TypeError(
"Generator or Iterable must provide a "
"StackInABoxService in all of its results."
)
return False

for arg in args:
if isinstance(arg, StackInABoxService):
self.services[arg.name] = arg
if process_service(arg, raise_on_type=False):
pass
elif (
isinstance(arg, types.GeneratorType) or
isinstance(arg, collections.Iterable)
):
for sub_arg in arg:
process_service(sub_arg, raise_on_type=True)
else:
self.args.append(arg)

Expand Down
24 changes: 22 additions & 2 deletions stackinabox/util/requests_mock/decorator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""
Stack-In-A-Box: Requests-Mock Support via Decorator
"""
import collections
import functools
import logging
import re
import types

import requests

Expand Down Expand Up @@ -62,9 +64,27 @@ def __init__(self, uri, *args, **kwargs):
else:
self.session = None

def process_service(arg_based_service, raise_on_type=True):
if isinstance(arg_based_service, StackInABoxService):
logger.debug("Registering {0}".format(arg_based_service.name))
self.services[arg_based_service.name] = arg_based_service
return True
elif raise_on_type:
raise TypeError(
"Generator or Iterable must provide a "
"StackInABoxService in all of its results."
)
return False

for arg in args:
if isinstance(arg, StackInABoxService):
self.services[arg.name] = arg
if process_service(arg, raise_on_type=False):
pass
elif (
isinstance(arg, types.GeneratorType) or
isinstance(arg, collections.Iterable)
):
for sub_arg in arg:
process_service(sub_arg, raise_on_type=True)
else:
self.args.append(arg)

Expand Down
Loading