Created
February 24, 2014 18:19
-
-
Save Ogreman/9193793 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # test_my_module.py | |
| import unittest | |
| class MyClassTest(unittest.TestCase): | |
| def setUp(self): | |
| self.user = SomeUserClass() | |
| self.obj = MyClass(user=user) | |
| def test_valid_input(self): | |
| for i in xrange(18, 100): | |
| self.assertEqual( | |
| self.obj.handle_input(i), | |
| MyClass.STATUSES.OK | |
| ) | |
| def test_invalid_input(self): | |
| for i in xrange(0, 17): | |
| self.assertEqual( | |
| self.obj.handle_input(i), | |
| MyClass.STATUSES.FAILED | |
| ) | |
| def test_exception(self): | |
| with self.assertRaises(MyClass.InputException): | |
| self.obj.handle_input('xyz') | |
| def test_none_input(self): | |
| with self.assertRaises(MyClass.InputException): | |
| self.obj.handle_input(None) | |
| # my_module.py | |
| from myhelpermodule import Choices | |
| class MyClass(object): | |
| class InputException(Exception): | |
| pass | |
| STATUSES = Choices( | |
| 'OK', | |
| 'FAILED', | |
| 'NA', | |
| ) | |
| def __init__(self, user): | |
| self.user = user | |
| self.status = MyClass.STATUSES.NA | |
| def handle_input(self, n_in=None): | |
| if isinstance(n_in, int): | |
| if n_in >= 18 < 100: | |
| return MyClass.STATUSES.OK | |
| else: | |
| return MyClass.STATUSES.FAILED | |
| raise MyClass.InputException | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment