|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +test_django-application-logic |
| 6 | +------------ |
| 7 | +
|
| 8 | +Tests for `django-application-logic` external API. |
| 9 | +""" |
| 10 | + |
| 11 | +from django.test import TestCase |
| 12 | + |
| 13 | +from django_application_logic import core, exceptions |
| 14 | + |
| 15 | + |
| 16 | +def create_validation_func(should_raise=False): |
| 17 | + @core.validation_func |
| 18 | + def func(*args, **kwargs): |
| 19 | + if should_raise: |
| 20 | + raise exceptions.ServiceException("Test exception") |
| 21 | + return func |
| 22 | + |
| 23 | + |
| 24 | +def create_validator_func(validation_func_raise=False, validator_raise=False): |
| 25 | + @core.validator(create_validation_func(validation_func_raise)) |
| 26 | + def wrapper(*args, **kwargs): |
| 27 | + if validator_raise: |
| 28 | + raise exceptions.ServiceException |
| 29 | + return wrapper |
| 30 | + |
| 31 | + |
| 32 | +class TestPermissionResult(TestCase): |
| 33 | + |
| 34 | + def test_permission_result_coercible_to_bool(self): |
| 35 | + result_success = core.PermissionResult(success=True) |
| 36 | + result_failure = core.PermissionResult(success=False) |
| 37 | + self.assertTrue(result_success) |
| 38 | + self.assertFalse(result_failure) |
| 39 | + |
| 40 | + def test_permission_result_delegates_errors_and_error_code(self): |
| 41 | + exception = exceptions.ServiceException("test", error_code="CODE", errors=object()) |
| 42 | + result = core.PermissionResult(success=False, error=exception) |
| 43 | + self.assertEqual(result.error_code, exception.error_code) |
| 44 | + self.assertEqual(result.errors, exception.errors) |
| 45 | + |
| 46 | + def test_permission_result_repr(self): |
| 47 | + result_success = core.PermissionResult(success=True) |
| 48 | + self.assertEqual( |
| 49 | + repr(result_success), |
| 50 | + u'<PermissionResult success=True error=None>') |
| 51 | + |
| 52 | + |
| 53 | +class TestDjangoApplicationValidation(TestCase): |
| 54 | + |
| 55 | + def test_validation_func_raises_exception(self): |
| 56 | + func = create_validation_func(should_raise=True) |
| 57 | + self.assertRaises(exceptions.ServiceException, lambda: func()) |
| 58 | + self.assertRaises(exceptions.ServiceException, lambda: func(raise_exception=True)) |
| 59 | + |
| 60 | + def test_validation_func_returns_permission_result_if_not_raising(self): |
| 61 | + success_func = create_validation_func(should_raise=False) |
| 62 | + failure_func = create_validation_func(should_raise=True) |
| 63 | + success1 = success_func(raise_exception=False) |
| 64 | + success2 = success_func(raise_exception=True) |
| 65 | + failure1 = failure_func(raise_exception=False) |
| 66 | + self.assertIsInstance(success1, core.PermissionResult) |
| 67 | + self.assertIsInstance(success2, core.PermissionResult) |
| 68 | + self.assertIsInstance(failure1, core.PermissionResult) |
| 69 | + self.assertTrue(success1) |
| 70 | + self.assertTrue(success2) |
| 71 | + self.assertEqual(success1, success2) |
| 72 | + self.assertFalse(failure1) |
| 73 | + |
| 74 | + def test_returned_failure_contains_exception(self): |
| 75 | + func = create_validation_func(should_raise=True) |
| 76 | + result = func(raise_exception=False) |
| 77 | + self.assertFalse(result) |
| 78 | + self.assertFalse(result.success) |
| 79 | + self.assertIsInstance(result.error, exceptions.ServiceException) |
| 80 | + # it should be none because exception is not created using ServiceErrors |
| 81 | + self.assertIsNone(result.errors) |
| 82 | + self.assertIsNone(result.error_code) |
| 83 | + |
| 84 | + def test_raise_exception_argument_is_swallowed(self): |
| 85 | + @core.validation_func |
| 86 | + def func_without_args(): |
| 87 | + return True |
| 88 | + |
| 89 | + self.assertTrue(func_without_args()) |
| 90 | + self.assertTrue(func_without_args(raise_exception=False)) |
| 91 | + self.assertTrue(func_without_args(raise_exception=True)) |
0 commit comments