diff --git a/kfet/tests/test_views.py b/kfet/tests/test_views.py index 1cd83ffd..ff9803c9 100644 --- a/kfet/tests/test_views.py +++ b/kfet/tests/test_views.py @@ -93,8 +93,7 @@ class AccountCreateViewTests(ViewTestCaseMixin, TestCase): 'email': 'email@domain.net', } - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=['kfet.add_account']), } @@ -212,8 +211,7 @@ class AccountReadViewTests(ViewTestCaseMixin, TestCase): auth_user = 'team' auth_forbidden = [None, 'user'] - @property - def users_extra(self): + def get_users_extra(self): return { 'user1': create_user('user1', '001'), } @@ -292,8 +290,7 @@ class AccountUpdateViewTests(ViewTestCaseMixin, TestCase): 'pwd2': '', } - @property - def users_extra(self): + def get_users_extra(self): return { 'user1': create_user('user1', '001'), 'team1': create_team('team1', '101', perms=[ @@ -359,8 +356,7 @@ class AccountGroupListViewTests(ViewTestCaseMixin, TestCase): auth_user = 'team1' auth_forbidden = [None, 'user', 'team'] - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=['kfet.manage_perms']), } @@ -390,8 +386,7 @@ class AccountGroupCreateViewTests(ViewTestCaseMixin, TestCase): auth_user = 'team1' auth_forbidden = [None, 'user', 'team'] - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=['kfet.manage_perms']), } @@ -449,8 +444,7 @@ class AccountGroupUpdateViewTests(ViewTestCaseMixin, TestCase): def url_expected(self): return '/k-fet/accounts/groups/{}/edit'.format(self.group.pk) - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=['kfet.manage_perms']), } @@ -502,8 +496,7 @@ class AccountNegativeListViewTests(ViewTestCaseMixin, TestCase): auth_user = 'team1' auth_forbidden = [None, 'user', 'team'] - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=['kfet.view_negs']), } @@ -533,8 +526,7 @@ class AccountStatOperationListViewTests(ViewTestCaseMixin, TestCase): auth_user = 'user1' auth_forbidden = [None, 'user', 'team'] - @property - def users_extra(self): + def get_users_extra(self): return {'user1': create_user('user1', '001')} def test_ok(self): @@ -594,8 +586,7 @@ class AccountStatOperationViewTests(ViewTestCaseMixin, TestCase): auth_user = 'user1' auth_forbidden = [None, 'user', 'team'] - @property - def users_extra(self): + def get_users_extra(self): return {'user1': create_user('user1', '001')} def test_ok(self): @@ -611,8 +602,7 @@ class AccountStatBalanceListViewTests(ViewTestCaseMixin, TestCase): auth_user = 'user1' auth_forbidden = [None, 'user', 'team'] - @property - def users_extra(self): + def get_users_extra(self): return {'user1': create_user('user1', '001')} def test_ok(self): @@ -666,8 +656,7 @@ class AccountStatBalanceViewTests(ViewTestCaseMixin, TestCase): auth_user = 'user1' auth_forbidden = [None, 'user', 'team'] - @property - def users_extra(self): + def get_users_extra(self): return {'user1': create_user('user1', '001')} def test_ok(self): @@ -724,8 +713,7 @@ class CheckoutCreateViewTests(ViewTestCaseMixin, TestCase): # 'is_protected': not checked } - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=['kfet.add_checkout']), } @@ -807,8 +795,7 @@ class CheckoutUpdateViewTests(ViewTestCaseMixin, TestCase): def url_expected(self): return '/k-fet/checkouts/{}/edit'.format(self.checkout.pk) - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ 'kfet.change_checkout', @@ -927,8 +914,7 @@ class CheckoutStatementCreateViewTests(ViewTestCaseMixin, TestCase): def url_expected(self): return '/k-fet/checkouts/{}/statements/add'.format(self.checkout.pk) - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '001', perms=[ 'kfet.add_checkoutstatement', @@ -1014,8 +1000,7 @@ class CheckoutStatementUpdateViewTests(ViewTestCaseMixin, TestCase): pk=self.statement.pk, ) - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ 'kfet.change_checkoutstatement', @@ -1109,8 +1094,7 @@ class ArticleCategoryUpdateViewTests(ViewTestCaseMixin, TestCase): def url_expected(self): return '/k-fet/categories/{}/edit'.format(self.category.pk) - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ 'kfet.change_articlecategory', @@ -1188,8 +1172,7 @@ class ArticleCreateViewTests(ViewTestCaseMixin, TestCase): auth_user = 'team' auth_forbidden = [None, 'user'] - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=['kfet.add_article']), } @@ -1276,8 +1259,7 @@ class ArticleUpdateViewTests(ViewTestCaseMixin, TestCase): def url_expected(self): return '/k-fet/articles/{}/edit'.format(self.article.pk) - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ 'kfet.change_article', @@ -1564,8 +1546,7 @@ class KPsulUpdateAddcost(ViewTestCaseMixin, TestCase): 'amount': '0.5', } - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ 'kfet.manage_addcosts', @@ -1649,8 +1630,7 @@ class SettingsListViewTests(ViewTestCaseMixin, TestCase): auth_user = 'team1' auth_forbidden = [None, 'user', 'team'] - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ 'kfet.see_config', @@ -1682,8 +1662,7 @@ class SettingsUpdateViewTests(ViewTestCaseMixin, TestCase): 'kfet_cancel_duration': '00:20:00', } - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ 'kfet.change_config', @@ -1749,8 +1728,7 @@ class TransferPerformViewTests(ViewTestCaseMixin, TestCase): auth_user = 'team1' auth_forbidden = [None, 'user', 'team'] - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ # Required @@ -1806,8 +1784,7 @@ class TransferCancelViewTests(ViewTestCaseMixin, TestCase): auth_user = 'team1' auth_forbidden = [None, 'user', 'team'] - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ # Convenience @@ -1898,8 +1875,7 @@ class InventoryCreateViewTests(ViewTestCaseMixin, TestCase): auth_user = 'team' auth_forbidden = [None, 'user'] - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ 'kfet.add_inventory', @@ -2069,8 +2045,7 @@ class SupplierUpdateViewTests(ViewTestCaseMixin, TestCase): def url_expected(self): return '/k-fet/orders/suppliers/{}/edit'.format(self.supplier.pk) - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ 'kfet.change_supplier', @@ -2124,8 +2099,7 @@ class OrderCreateViewTests(ViewTestCaseMixin, TestCase): def url_expected(self): return '/k-fet/orders/suppliers/{}/new-order'.format(self.supplier.pk) - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=['kfet.add_order']), } @@ -2195,8 +2169,7 @@ class OrderToInventoryViewTests(ViewTestCaseMixin, TestCase): def url_expected(self): return '/k-fet/orders/{}/to_inventory'.format(self.order.pk) - @property - def users_extra(self): + def get_users_extra(self): return { 'team1': create_team('team1', '101', perms=[ 'kfet.order_to_inventory', diff --git a/kfet/tests/testcases.py b/kfet/tests/testcases.py index e2fc09ff..d7d7eac5 100644 --- a/kfet/tests/testcases.py +++ b/kfet/tests/testcases.py @@ -5,6 +5,7 @@ from django.core.urlresolvers import reverse from django.http import QueryDict from django.test import Client from django.utils import timezone +from django.utils.functional import cached_property from .utils import create_root, create_team, create_user @@ -180,9 +181,9 @@ class ViewTestCaseMixin(TestCaseMixin): - 'root': a superuser, account trigramme: 200. Their password is their username. - One can create additionnal users with 'users_extra' attribute, or prevent - these 3 users to be created with 'users_base' attribute. See these two - properties for further informations. + One can create additionnal users with 'get_users_extra' method, or prevent + these 3 users to be created with 'get_users_base' method. See these two + methods for further informations. By using 'register_user' method, these users can then be accessed at 'users' attribute by their label. Similarly, their kfet account is @@ -240,7 +241,7 @@ class ViewTestCaseMixin(TestCaseMixin): self.users = {} self.accounts = {} - for label, user in {**self.users_base, **self.users_extra}.items(): + for label, user in dict(self.users_base, **self.users_extra).items(): self.register_user(label, user) if self.auth_user: @@ -252,8 +253,20 @@ class ViewTestCaseMixin(TestCaseMixin): ) ) - @property - def users_base(self): + def tearDown(self): + del self.users_base + del self.users_extra + + def get_users_base(self): + """ + Dict of . + + Note: Don't access yourself this property. Use 'users_base' attribute + which cache the returned value from here. + It allows to give functions calls, which creates users instances, as + values here. + + """ # Format desc: username, password, trigramme return { # user, user, 000 @@ -264,10 +277,26 @@ class ViewTestCaseMixin(TestCaseMixin): 'root': create_root(), } - @property - def users_extra(self): + @cached_property + def users_base(self): + return self.get_users_base() + + def get_users_extra(self): + """ + Dict of . + + Note: Don't access yourself this property. Use 'users_base' attribute + which cache the returned value from here. + It allows to give functions calls, which create users instances, as + values here. + + """ return {} + @cached_property + def users_extra(self): + return self.get_users_extra() + def register_user(self, label, user): self.users[label] = user if hasattr(user.profile, 'account_kfet'):