Skip to content

Commit

Permalink
Merge pull request #4 from Superpedestrian/feature/tests
Browse files Browse the repository at this point in the history
Added test coverage for lambada module
  • Loading branch information
carsongee committed Sep 30, 2016
2 parents 593d12f + 0a5f6ca commit 9db5da0
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 24 deletions.
6 changes: 3 additions & 3 deletions lambada/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __call__(self, *args, **kwargs):
"""
Calls the function.
"""
self.function(*args, **kwargs)
return self.function(*args, **kwargs)


class Lambada(object):
Expand Down Expand Up @@ -104,10 +104,10 @@ def __call__(self, event, context):
"""
dancer = context.function_name
try:
self.dancers[dancer](event, context)
return self.dancers[dancer](event, context)
except KeyError:
raise Exception(
'No Matching Dancer for the tune: {}'.format(
'No matching dancer for the Lambda function: {}'.format(
dancer
)
)
Expand Down
21 changes: 21 additions & 0 deletions lambada/tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Common test functions used across test files.
"""
import os


def make_fixture_path(folder, filename='lambda.py'):
"""
Make path to fixture using given args.
folder (str): Folder name that contains fixture(s)
filename (str): Filename to pass, if ``None`` will return folder
"""
path = os.path.abspath(
os.path.join(
os.path.dirname(__file__), 'fixtures', folder
)
)
if filename:
path = os.path.join(path, filename)
return path
21 changes: 19 additions & 2 deletions lambada/tests/fixtures/basic/lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""
Basic sample lambada module
"""
from __future__ import print_function
from lambada import Lambada

tune = Lambada(role='arn:aws:iam:xxxxxxx:role/lambda')
Expand All @@ -12,4 +11,22 @@
@tune.dancer
def test_lambada(event, _):
"""Print the event we created"""
print('Event: {}'.format(event))
return 'Event: {}'.format(event)


@tune.dancer(name='hi')
def test_named(event, _):
"""Print the event we created"""
return 'Event: {}'.format(event)


@tune.dancer()
def test_argless(event, _):
"""Print the event we created"""
return 'Event: {}'.format(event)


@tune.dancer(memory=4242, region='us-west-2')
def test_multiarg(event, _):
"""Print the event we created"""
return 'Event: {}'.format(event)
2 changes: 1 addition & 1 deletion lambada/tests/fixtures/two/lambda0.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
@tune.dancer
def lambda0(event, _):
"""Print the event we created"""
print('Event: {}'.format(event))
print('Event: {}'.format(event)) # pragma: no cover
2 changes: 1 addition & 1 deletion lambada/tests/fixtures/two/lambda1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
@tune.dancer
def lambda1(event, _):
"""Print the event we created"""
print('Event: {}'.format(event))
print('Event: {}'.format(event)) # pragma: no cover
18 changes: 1 addition & 17 deletions lambada/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,7 @@
from six import iteritems, assertRaisesRegex

from lambada import common


def make_fixture_path(folder, filename='lambda.py'):
"""
Make path to fixture using given args.
folder (str): Folder name that contains fixture(s)
filename (str): Filename to pass, if ``None`` will return folder
"""
path = os.path.abspath(
os.path.join(
os.path.dirname(__file__), 'fixtures', folder
)
)
if filename:
path = os.path.join(path, filename)
return path
from lambada.tests.common import make_fixture_path


class TestCommon(TestCase):
Expand Down
79 changes: 79 additions & 0 deletions lambada/tests/test_lambada.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""
Tests for the :mod::`lambada` module.
"""
from unittest import TestCase

from mock import MagicMock
from six import assertRaisesRegex

import lambada
from lambada.common import LambdaContext, get_lambada_class
from lambada.tests.common import make_fixture_path


class TestLambada(TestCase):
"""
Test class for :mod::`lambada` module.
"""
@staticmethod
def sample_dancer(event, context):
"""Function to use as wrapper."""
return event, context

def test_dancer_class(self):
"""
Verify the dancer class constructor, props, and call.
"""

dancer = lambada.Dancer(self.sample_dancer, 'foo', 'bar', memory=128)
# Confirm __init__
self.assertEqual(dancer.name, 'foo')
self.assertEqual(dancer.description, 'bar')
self.assertDictEqual(dancer.override_config, dict(memory=128))
# Validate config merge property
self.assertDictEqual(
dancer.config,
dict(name='foo', description='bar', memory=128)
)
self.assertEqual((1, 2), dancer(1, 2))

def test_lambada_class(self):
"""Validate the base lambada class."""
tune = lambada.Lambada()
# Make sure we make the attributes we need
self.assertIsNotNone(getattr(tune, 'dancers'))
self.assertIsNotNone(getattr(tune, 'config'))

# Create a dancer and call it
tune.dancers['test'] = MagicMock()
context = LambdaContext('test')
tune('hi', context)
tune.dancers['test'].assert_called()
tune.dancers['test'].assert_called_with('hi', context)

# Try a dancer that doesn't exist
context = LambdaContext('nope')
with assertRaisesRegex(self, Exception, 'No matching dancer'):
tune('bye', context)

def test_decorator(self):
"""
Test out decorators using a test fixture.
"""
path = make_fixture_path('basic')
tune = get_lambada_class(path)
self.assertEqual(
set(('test_lambada', 'hi', 'test_argless', 'test_multiarg')),
set(tune.dancers.keys())
)
# verify that test_multiarg is properly getting it's config
self.assertEqual(4242, tune.dancers['test_multiarg'].config['memory'])
self.assertEqual(
'us-west-2',
tune.dancers['test_multiarg'].config['region']
)

# Call all of them to verify everything is working as expected.
for dancer in tune.dancers.keys():
context = LambdaContext(dancer)
self.assertEqual('Event: fhqwhgads', tune('fhqwhgads', context))
1 change: 1 addition & 0 deletions lambada/tests/test_package.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
Tests related to package/API and versioning.
"""
Expand Down

0 comments on commit 9db5da0

Please sign in to comment.