Skip to content

Commit ab130b7

Browse files
committed
first tests
1 parent 06b0099 commit ab130b7

File tree

6 files changed

+103
-38
lines changed

6 files changed

+103
-38
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
21
# Additional requirements go here
2+
six==1.10.0

tests/services.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django_application_logic.core import validation_func, validator
2+
3+
4+
@validation_func
5+
def can_remove_user(by_user, user):
6+
return True
7+
8+
9+
@validator(can_remove_user)
10+
def remove_user(by_user, user):
11+
user.delete()

tests/settings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
}
1717
}
1818

19-
ROOT_URLCONF = "tests.urls"
20-
2119
INSTALLED_APPS = [
2220
"django.contrib.auth",
2321
"django.contrib.contenttypes",

tests/test_api.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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))

tests/test_models.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/urls.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)