Skip to content

Commit

Permalink
Fix #3 (#5)
Browse files Browse the repository at this point in the history
Better tests structure
  • Loading branch information
ngr committed Nov 10, 2018
1 parent ee50d58 commit b6a167b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 20 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@


setup(name='sosww',
version='0.1.9',
version='0.3.0',
description='SOSW Worker',
url='http://github.com/bimpression/sosww',
author='Nikolay Grishchenko',
author_email='nikolay@bimpression.com',
license='MIT',
classifiers=[
'Development Status :: 1 - Planning',
'Development Status :: 3 - Alpha',
'Operating System :: Other OS',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.6',
Expand Down
10 changes: 5 additions & 5 deletions sosww/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from importlib import import_module
from collections import defaultdict

from .components.helpers import *
from .components.ssm import get_config
from .components.tasks_api_client_for_workers import close_task
from sosww.components.helpers import *
from sosww.components.ssm import get_config
from sosww.components.tasks_api_client_for_workers import close_task


__author__ = "Nikolay Grishchenko"
__email__ = "dev@bimpression.com"
__version__ = "1.01"
__version__ = "0.3.0"
__license__ = "MIT"
__status__ = "Production"

Expand Down Expand Up @@ -43,7 +43,7 @@ def __init__(self, custom_config=None, **kwargs):
raise RuntimeError("You must specify a custom config from your testcase to run processor in test mode.")

self.config = self.DEFAULT_CONFIG.copy()
self.config.update(get_config(f"{__name__}_config"))
self.config.update(get_config(f"{os.environ.get('AWS_LAMBDA_FUNCTION_NAME')}_config"))
self.config.update(custom_config or {})
logger.info(f"Final processor config: {self.config}")

Expand Down
2 changes: 1 addition & 1 deletion sosww/components/sns.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, **kwargs):

self.test = kwargs.get('test')
if self.test:
self.recipient = 'arn:aws:sns:us-west-2:737060422660:autotest_topic'
self.recipient = 'arn:aws:sns:us-west-2:000000000000:autotest_topic'


def __del__(self):
Expand Down
2 changes: 1 addition & 1 deletion sosww/components/test/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ..helpers import *


class helpers_TestCase(unittest.TestCase):
class helpers_UnitTestCase(unittest.TestCase):

def setUp(self):
pass
Expand Down
36 changes: 34 additions & 2 deletions sosww/components/test/test_ssm.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import boto3
import datetime
import logging
import os
import time
import unittest

from random import randint
from ..ssm import *
from unittest import mock
from unittest.mock import Mock, MagicMock
from sosww.components.ssm import *


logging.getLogger('botocore').setLevel(logging.WARNING)

os.environ["STAGE"] = "test"


class ssm_TestCase(unittest.TestCase):
class ssm_IntegrationTestCase(unittest.TestCase):
name = None


Expand Down Expand Up @@ -57,5 +60,34 @@ def test_get_credentials_by_prefix(self):
self.assertEqual(len(get_credentials_by_prefix('bad_test')), 0)


class ssm_UnitTestCase(unittest.TestCase):


@mock.patch("boto3.client")
def test_get_config(self, mock_boto_client):

client = MagicMock()
client.get_parameters = Mock(return_value={'Parameters': [{'Name': 'autotest_other',
'Type': 'String',
'Value': '{\r\n "cars": 2,\r\n "cats": 3\r\n}',
'Version': 37,
'LastModifiedDate': datetime.datetime(2018, 1, 1, 1, 1, 1),
'ARN': 'arn:aws:ssm:us-west-2:737060422660:parameter/autotest_other'}],
'InvalidParameters': [],
'ResponseMetadata': {'RequestId': 'ab9ac1ee-67db-4f40-9f7d-b90fe794f23f',
'HTTPStatusCode': 200,
'HTTPHeaders': {'x-amzn-requestid': 'ab9ac1ee-67db-4f40-9f7d-b90fe794f23f',
'content-type': 'application/x-amz-json-1.1',
'content-length': '250',
'date': 'Fri, 09 Nov 2018 07:27:05 GMT'},
'RetryAttempts': 0}})

mock_boto_client.return_value = client

self.assertEqual(get_config('test')['cars'], 2)
self.assertEqual(get_config('test')['cats'], 3)
self.assertIsNone(get_config('test').get('not_existing'))


if __name__ == '__main__':
unittest.main()
11 changes: 7 additions & 4 deletions sosww/test/suite_3_6_unit.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import unittest

from .test_app import app_TestCase
from ..components.test.test_helpers import helpers_TestCase
from .test_app import app_UnitTestCase
from ..components.test.test_helpers import helpers_UnitTestCase
from ..components.test.test_siblings import siblings_TestCase
from ..components.test.test_ssm import ssm_UnitTestCase


def suite():
test_suite = unittest.TestSuite()
test_suite.addTest(unittest.makeSuite(app_TestCase))
test_suite.addTest(unittest.makeSuite(helpers_TestCase))
test_suite.addTest(unittest.makeSuite(app_UnitTestCase))
test_suite.addTest(unittest.makeSuite(helpers_UnitTestCase))
test_suite.addTest(unittest.makeSuite(siblings_TestCase))
test_suite.addTest(unittest.makeSuite(ssm_UnitTestCase))

return test_suite


Expand Down
23 changes: 18 additions & 5 deletions sosww/test/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
from unittest import mock
from unittest.mock import MagicMock

from ..app import Processor
from ..components.sns import SnsManager
from ..components.siblings import SiblingsManager
from sosww.app import Processor
from sosww.components.sns import SnsManager
from sosww.components.siblings import SiblingsManager


os.environ["STAGE"] = "test"
os.environ["autotest"] = "True"


class app_TestCase(unittest.TestCase):
class app_UnitTestCase(unittest.TestCase):
TEST_CONFIG = {'test': True}


Expand All @@ -23,7 +23,10 @@ def setUp(self):


def tearDown(self):
pass
try:
del (os.environ['AWS_LAMBDA_FUNCTION_NAME'])
except:
pass


@mock.patch("boto3.client")
Expand Down Expand Up @@ -70,3 +73,13 @@ def test_app_init__with_some_invalid_client(self, mock_boto_client):
'init_clients': ['NotExists', 'Sns']
}
self.assertRaises(RuntimeError, Processor, custom_config=custom_config)


@mock.patch("sosww.app.get_config")
def test_app_calls_get_config(self, mock_ssm):

mock_ssm.return_value = {'mock': 'called'}
os.environ['AWS_LAMBDA_FUNCTION_NAME'] = 'test_func'

Processor(custom_config=self.TEST_CONFIG)
mock_ssm.assert_called_once_with('test_func_config')

0 comments on commit b6a167b

Please sign in to comment.