Commit 0866b279 authored by Jean-samuel TETTEKPOE's avatar Jean-samuel TETTEKPOE
Browse files

Fix "reload()" module type error

parent 6ca57d56
Pipeline #9078 failed with stage
in 53 seconds
image: python:2.7
image: python:3.9
build:
script:
- pip install coveralls
......
......@@ -4,11 +4,20 @@ from bars_core.models.bar import Bar, BarSerializer, BarSettingsSerializer
from bars_core.models.user import User, UserSerializer
from bars_core.models.role import Role
from bars_core.models.account import Account, AccountSerializer
import importlib
from types import ModuleType
import sys
from imp import reload
def reload(obj):
return obj.__class__.objects.get(pk=obj.pk)
_reload_builtin = reload
def reload(thing):
if isinstance(thing, ModuleType):
_reload_builtin(thing)
elif hasattr(thing, '__module__') and thing.__module__:
module = sys.modules[thing.__module__]
_reload_builtin(module)
else:
raise TypeError("reload() argument must be a module or have an __module__")
class BackendTests(APITestCase):
......@@ -59,7 +68,7 @@ class BarTests(APITestCase):
root_bar = get_root_bar()
self.manager, _ = User.objects.get_or_create(username="manager")
Role.objects.get_or_create(bar=root_bar, user=self.manager, name="admin")
self.manager = importlib.reload(self.manager) # prevent role caching
self.manager = reload(self.manager) # prevent role caching
self.bar, _ = Bar.objects.get_or_create(id="barrouje")
......@@ -98,7 +107,7 @@ class BarTests(APITestCase):
response = self.client.put(self.bar_url, self.data)
self.assertEqual(response.status_code, 200)
self.assertEqual(importlib.reload(self.bar).name, self.data['name'])
self.assertEqual(reload(self.bar).name, self.data['name'])
class BarSettingsTests(APITestCase):
......@@ -109,7 +118,7 @@ class BarSettingsTests(APITestCase):
self.manager, _ = User.objects.get_or_create(username="manager")
self.manager.role_set.all().delete()
Role.objects.get_or_create(bar=self.bar, user=self.manager, name="staff")
self.manager = importlib.reload(self.manager) # prevent role caching
self.manager = reload(self.manager) # prevent role caching
self.barsettings = self.bar.settings
......@@ -134,7 +143,7 @@ class BarSettingsTests(APITestCase):
self.client.force_authenticate(user=User())
response = self.client.get(self.barsettings_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(importlib.reload(self.barsettings).agios_enabled, response.data['agios_enabled'])
self.assertEqual(reload(self.barsettings).agios_enabled, response.data['agios_enabled'])
def test_change_no_perms(self):
......@@ -149,7 +158,7 @@ class BarSettingsTests(APITestCase):
response = self.client.put(self.barsettings_url, self.data)
self.assertEqual(response.status_code, 200)
self.assertEqual(importlib.reload(self.barsettings).agios_enabled, self.data['agios_enabled'])
self.assertEqual(reload(self.barsettings).agios_enabled, self.data['agios_enabled'])
class UserTests(APITestCase):
......@@ -160,7 +169,7 @@ class UserTests(APITestCase):
root_bar = get_root_bar()
self.manager, _ = User.objects.get_or_create(username="manager")
Role.objects.get_or_create(bar=root_bar, user=self.manager, name="admin")
self.manager = importlib.reload(self.manager) # prevent role caching
self.manager = reload(self.manager) # prevent role caching
self.user, _ = User.objects.get_or_create(username="bob")
self.user.set_password("password")
......@@ -220,7 +229,7 @@ class UserTests(APITestCase):
response = self.client.put(self.user_url, self.data)
self.assertEqual(response.status_code, 200)
self.assertEqual(importlib.reload(self.user).username, self.data['username'])
self.assertEqual(reload(self.user).username, self.data['username'])
def test_change_user_self(self):
# Authenticated as self
......@@ -229,7 +238,7 @@ class UserTests(APITestCase):
response = self.client.put(self.user_url, self.data)
self.assertEqual(response.status_code, 200)
self.assertEqual(importlib.reload(self.user).username, self.data['username'])
self.assertEqual(reload(self.user).username, self.data['username'])
def test_change_password(self):
......@@ -260,7 +269,7 @@ class AccountTests(APITestCase):
self.user, _ = User.objects.get_or_create(username='nadrieril')
self.user2, _ = User.objects.get_or_create(username='ntag')
Role.objects.get_or_create(name='admin', bar=self.bar, user=self.user2)
self.user2 = importlib.reload(self.user2)
self.user2 = reload(self.user2)
self.create_data = {'owner': self.user2.id}
self.account, _ = Account.objects.get_or_create(owner=self.user, bar=self.bar)
......@@ -306,28 +315,28 @@ class AccountTests(APITestCase):
# Unauthenticated
response = self.client.put('/account/%d/?bar=natationjone' % self.account.id, self.update_data)
self.assertEqual(response.status_code, 401)
self.assertEqual(importlib.reload(self.account).deleted, self.account.deleted)
self.assertEqual(reload(self.account).deleted, self.account.deleted)
def test_change_account2(self):
# Wrong permissions
self.client.force_authenticate(user=self.user)
response = self.client.put('/account/%d/?bar=natationjone' % self.account.id, self.update_data)
self.assertEqual(response.status_code, 403)
self.assertEqual(importlib.reload(self.account).deleted, self.account.deleted)
self.assertEqual(reload(self.account).deleted, self.account.deleted)
def test_change_account4(self):
# Correct permissions
self.client.force_authenticate(user=self.user2)
response = self.client.put('/account/%d/?bar=natationjone' % self.account.id, self.update_data)
self.assertEqual(response.status_code, 200)
self.assertEqual(importlib.reload(self.account).deleted, self.update_data['deleted'])
self.assertEqual(reload(self.account).deleted, self.update_data['deleted'])
def test_change_account5(self):
# Wrong bar
self.client.force_authenticate(user=self.user2)
response = self.client.put('/account/%d/?bar=avironjone' % self.account.id, self.update_data)
self.assertEqual(response.status_code, 403)
self.assertEqual(importlib.reload(self.account).deleted, self.account.deleted)
self.assertEqual(reload(self.account).deleted, self.account.deleted)
......@@ -345,9 +354,9 @@ class RoleTests(APITestCase):
self.root, _ = User.objects.get_or_create(username='root')
Role.objects.get_or_create(name='admin', bar=self.bar, user=self.user2)
self.user2 = importlib.reload(self.user2)
self.user2 = reload(self.user2)
Role.objects.get_or_create(name='admin', bar=get_root_bar(), user=self.root)
self.root = importlib.reload(self.root)
self.root = reload(self.root)
self.create_data = {'user': self.user.id, 'name': 'customer'}
self.create_data_root = {'user': self.user.id, 'name': 'usermanager'}
......
......@@ -40,6 +40,7 @@ MIDDLEWARE = [
# 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'bars_django.utils.BarMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
]
......
......@@ -13,11 +13,20 @@ from bars_items.models.itemdetails import ItemDetails, ItemDetailsSerializer
from bars_items.models.sellitem import SellItem, SellItemSerializer
from bars_items.models.stockitem import StockItem, StockItemSerializer
from bars_items.models.suggesteditem import SuggestedItem, SuggestedItemSerializer
import importlib
from types import ModuleType
import sys
from imp import reload
def reload(obj):
return obj.__class__.objects.get(pk=obj.pk)
_reload_builtin = reload
def reload(thing):
if isinstance(thing, ModuleType):
_reload_builtin(thing)
elif hasattr(thing, '__module__') and thing.__module__:
module = sys.modules[thing.__module__]
_reload_builtin(module)
else:
raise TypeError("reload() argument must be a module or have an __module__")
class AutoTestGetMixin():
......@@ -137,7 +146,7 @@ class ItemTests(APITestCase):
self.staff_account, _ = Account.objects.get_or_create(bar=self.bar, owner=self.staff_user)
Role.objects.get_or_create(name='staff', bar=self.bar, user=self.staff_user)
Role.objects.get_or_create(name='admin', bar=get_root_bar(), user=self.staff_user)
self.staff_user = importlib.reload(self.staff_user)
self.staff_user = reload(self.staff_user)
self.sellitem, _ = SellItem.objects.get_or_create(bar=self.bar, name="Chocolat", tax=0.2)
self.itemdetails, _ = ItemDetails.objects.get_or_create(name="Chocolat")
......@@ -191,9 +200,9 @@ class SellItemTests(ItemTests, AutoTestBarMixin):
self.client.force_authenticate(user=self.staff_user)
response = self.client.put('/sellitem/set_global_tax/?bar=barjone', {'tax': 0.15});
self.assertEqual(response.status_code, 204)
self.assertEqual(importlib.reload(self.sellitem).tax, 0.15)
self.assertEqual(importlib.reload(self.sellitem2).tax, 0.15)
self.assertEqual(importlib.reload(self.sellitem3).tax, 0.1)
self.assertEqual(reload(self.sellitem).tax, 0.15)
self.assertEqual(reload(self.sellitem2).tax, 0.15)
self.assertEqual(reload(self.sellitem3).tax, 0.1)
def test_update_1(self):
self.client.force_authenticate(user=self.staff_user)
......@@ -202,7 +211,7 @@ class SellItemTests(ItemTests, AutoTestBarMixin):
old_sell_to_buy = self.stockitem.sell_to_buy
response = self.client.put("/sellitem/%d/?bar=%s" % (self.sellitem.id, self.bar), update_data)
self.assertEqual(response.status_code, 200)
self.assertAlmostEqual(importlib.reload(self.stockitem).sell_to_buy, old_sell_to_buy / update_data['unit_factor'])
self.assertAlmostEqual(reload(self.stockitem).sell_to_buy, old_sell_to_buy / update_data['unit_factor'])
class ItemDetailsTests(ItemTests, AutoTestMixin):
......
......@@ -2,6 +2,7 @@
import math
from django.core.mail import send_mail
from django.db.models import fields
from django.utils import timezone
from django.http import Http404
......@@ -25,6 +26,7 @@ ERROR_MESSAGES = {
class BaseTransactionSerializer(serializers.ModelSerializer):
class Meta:
model = Transaction
fields = '__all__'
read_only_fields = ('bar', 'author', 'timestamp', 'last_modified', 'moneyflow', )
def to_representation(self, transaction):
......
......@@ -17,11 +17,20 @@ from bars_items.models.stockitem import StockItem
from ..serializers import (BaseTransactionSerializer, BuyTransactionSerializer, GiveTransactionSerializer,
ThrowTransactionSerializer, DepositTransactionSerializer, PunishTransactionSerializer,
MealTransactionSerializer, ApproTransactionSerializer, InventoryTransactionSerializer,)
import importlib
from types import ModuleType
import sys
from imp import reload
def reload(obj):
return obj.__class__.objects.get(pk=obj.pk)
_reload_builtin = reload
def reload(thing):
if isinstance(thing, ModuleType):
_reload_builtin(thing)
elif hasattr(thing, '__module__') and thing.__module__:
module = sys.modules[thing.__module__]
_reload_builtin(module)
else:
raise TypeError("reload() argument must be a module or have an __module__")
class BaseSerializerTests(APITestCase):
......@@ -98,14 +107,14 @@ class SerializerTests(APITestCase):
self.stockitem2.qty = 10
self.stockitem2.save()
self.user = importlib.reload(self.user) # Avoid role caching
self.user = reload(self.user) # Avoid role caching
self.context = {'request': Mock(user=self.user, bar=self.bar)}
class BuySerializerTests(SerializerTests):
def setUp(self):
self.stockitem = importlib.reload(self.stockitem)
self.stockitem = reload(self.stockitem)
def tearDown(self):
self.stockitem.deleted = False
......@@ -118,8 +127,8 @@ class BuySerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, self.stockitem.sell_price * data['qty'])
self.assertAlmostEqual(importlib.reload(self.stockitem).sell_qty, self.stockitem.sell_qty - data['qty'])
self.assertAlmostEqual(importlib.reload(self.account).money, self.account.money - data['qty'] * self.stockitem.sell_price)
self.assertAlmostEqual(reload(self.stockitem).sell_qty, self.stockitem.sell_qty - data['qty'])
self.assertAlmostEqual(reload(self.account).money, self.account.money - data['qty'] * self.stockitem.sell_price)
def test_buy_sellitem(self):
data = {'type':'buy', 'sellitem':self.sellitem.id, 'qty':23}
......@@ -134,9 +143,9 @@ class BuySerializerTests(SerializerTests):
self.assertAlmostEqual(tct.moneyflow, self.sellitem.calc_price() * data['qty'])
total_qty = self.stockitem.sell_qty + stockitem3.sell_qty
self.assertAlmostEqual(importlib.reload(self.stockitem).sell_qty, self.stockitem.sell_qty * (1 - data['qty'] / total_qty))
self.assertAlmostEqual(importlib.reload(stockitem3).sell_qty, stockitem3.sell_qty * (1 - data['qty'] / total_qty))
self.assertAlmostEqual(importlib.reload(self.account).money, self.account.money - data['qty'] * self.sellitem.calc_price())
self.assertAlmostEqual(reload(self.stockitem).sell_qty, self.stockitem.sell_qty * (1 - data['qty'] / total_qty))
self.assertAlmostEqual(reload(stockitem3).sell_qty, stockitem3.sell_qty * (1 - data['qty'] / total_qty))
self.assertAlmostEqual(reload(self.account).money, self.account.money - data['qty'] * self.sellitem.calc_price())
def test_buy_itemdeleted(self):
self.stockitem.deleted = True
......@@ -181,8 +190,8 @@ class GiveSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, data['amount'])
self.assertAlmostEqual(importlib.reload(self.account).money, self.account.money - data['amount'])
self.assertAlmostEqual(importlib.reload(self.account2).money, self.account2.money + data['amount'])
self.assertAlmostEqual(reload(self.account).money, self.account.money - data['amount'])
self.assertAlmostEqual(reload(self.account2).money, self.account2.money + data['amount'])
def test_give_other_bar(self):
data = {'type':'give', 'account':self.wrong_account.id, 'amount':10}
......@@ -211,7 +220,7 @@ class ThrowSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, - self.stockitem.sell_price * data['qty'])
self.assertAlmostEqual(importlib.reload(self.stockitem).sell_qty, self.stockitem.sell_qty - data['qty'])
self.assertAlmostEqual(reload(self.stockitem).sell_qty, self.stockitem.sell_qty - data['qty'])
def test_throw_other_bar(self):
context = {'request': Mock(user=self.user, bar=self.wrong_bar)}
......@@ -249,8 +258,8 @@ class DepositSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, data['amount'])
self.assertAlmostEqual(importlib.reload(self.account).money, self.account.money + data['amount'])
self.assertAlmostEqual(importlib.reload(self.bar_account).money, self.bar_account.money + data['amount'])
self.assertAlmostEqual(reload(self.account).money, self.account.money + data['amount'])
self.assertAlmostEqual(reload(self.bar_account).money, self.bar_account.money + data['amount'])
def test_deposit_no_staff(self):
data = {'type':'deposit', 'account':self.account.id, 'amount':40}
......@@ -260,8 +269,8 @@ class DepositSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
s.save()
self.assertAlmostEqual(importlib.reload(self.account).money, self.account.money)
self.assertAlmostEqual(importlib.reload(self.bar_account).money, self.bar_account.money)
self.assertAlmostEqual(reload(self.account).money, self.account.money)
self.assertAlmostEqual(reload(self.bar_account).money, self.bar_account.money)
def test_deposit_other_bar(self):
data = {'type':'deposit', 'account':self.wrong_account.id, 'amount':30}
......@@ -272,8 +281,8 @@ class DepositSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
s.save()
self.assertAlmostEqual(importlib.reload(self.wrong_account).money, self.wrong_account.money)
self.assertAlmostEqual(importlib.reload(self.wrong_bar_account).money, self.wrong_bar_account.money)
self.assertAlmostEqual(reload(self.wrong_account).money, self.wrong_account.money)
self.assertAlmostEqual(reload(self.wrong_bar_account).money, self.wrong_bar_account.money)
def test_deposit_negative(self):
self.context = {'request': Mock(user=self.staff_user, bar=self.bar)}
......@@ -293,7 +302,7 @@ class PunishSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, -data['amount'])
self.assertAlmostEqual(importlib.reload(self.account).money, self.account.money - data['amount'])
self.assertAlmostEqual(reload(self.account).money, self.account.money - data['amount'])
def test_punish_no_staff(self):
data = {'type':'punish', 'account':self.account.id, 'amount':50, 'motive':'vaisselle'}
......@@ -303,7 +312,7 @@ class PunishSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
s.save()
self.assertAlmostEqual(importlib.reload(self.account).money, self.account.money)
self.assertAlmostEqual(reload(self.account).money, self.account.money)
def test_punish_negative(self):
data = {'type':'punish', 'account':self.account.id, 'amount':-50, 'motive':'cadeau'}
......@@ -344,9 +353,9 @@ class MealSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, self.stockitem.sell_price * data['items'][0]['qty'])
self.assertAlmostEqual(importlib.reload(self.stockitem).sell_qty, self.stockitem.sell_qty - data['items'][0]['qty'])
self.assertAlmostEqual(reload(self.stockitem).sell_qty, self.stockitem.sell_qty - data['items'][0]['qty'])
end_money = self.account.money - data['items'][0]['qty'] * self.stockitem.sell_price
self.assertAlmostEqual(importlib.reload(self.account).money, end_money)
self.assertAlmostEqual(reload(self.account).money, end_money)
def test_meal_no_item(self):
data = {
......@@ -377,8 +386,8 @@ class MealSerializerTests(SerializerTests):
self.assertTrue(s.is_valid())
tct = s.save()
self.assertAlmostEqual(importlib.reload(self.stockitem).sell_qty, self.stockitem.sell_qty - data['items'][0]['qty'])
self.assertAlmostEqual(importlib.reload(self.stockitem2).sell_qty, self.stockitem2.sell_qty - data['items'][1]['qty'])
self.assertAlmostEqual(reload(self.stockitem).sell_qty, self.stockitem.sell_qty - data['items'][0]['qty'])
self.assertAlmostEqual(reload(self.stockitem2).sell_qty, self.stockitem2.sell_qty - data['items'][1]['qty'])
total_money = data['items'][0]['qty'] * self.stockitem.sell_price
total_money += data['items'][1]['qty'] * self.stockitem2.sell_price
......@@ -388,8 +397,8 @@ class MealSerializerTests(SerializerTests):
end_money = self.account.money - total_money * data['accounts'][0]['ratio'] / total_ratio
end_money2 = self.account2.money - total_money * data['accounts'][1]['ratio'] / total_ratio
self.assertAlmostEqual(importlib.reload(self.account).money, end_money)
self.assertAlmostEqual(importlib.reload(self.account2).money, end_money2)
self.assertAlmostEqual(reload(self.account).money, end_money)
self.assertAlmostEqual(reload(self.account2).money, end_money2)
self.assertAlmostEqual(tct.moneyflow, total_money)
......@@ -416,20 +425,20 @@ class ApproSerializerTests(SerializerTests):
self.assertAlmostEqual(tct.moneyflow, data['items'][0]['price'] + self.buyitemprice2.price * data['items'][1]['qty'])
# Test quantities
self.assertAlmostEqual(importlib.reload(self.stockitem).sell_qty, self.stockitem.sell_qty + data['items'][0]['qty'] * self.buyitem.itemqty / self.stockitem.sell_to_buy)
self.assertAlmostEqual(importlib.reload(self.stockitem2).sell_qty, self.stockitem2.sell_qty + data['items'][1]['qty'] * self.buyitem2.itemqty / self.stockitem2.sell_to_buy)
self.assertAlmostEqual(reload(self.stockitem).sell_qty, self.stockitem.sell_qty + data['items'][0]['qty'] * self.buyitem.itemqty / self.stockitem.sell_to_buy)
self.assertAlmostEqual(reload(self.stockitem2).sell_qty, self.stockitem2.sell_qty + data['items'][1]['qty'] * self.buyitem2.itemqty / self.stockitem2.sell_to_buy)
# Test prices
stockitem_newprice = (self.stockitem.qty * self.stockitem.price + data['items'][0]['price']) / (self.stockitem.qty + data['items'][0]['qty'] * self.buyitem.itemqty)
self.assertAlmostEqual(importlib.reload(self.stockitem).price, stockitem_newprice)
self.assertAlmostEqual(importlib.reload(self.stockitem2).price, self.stockitem2.price)
self.assertAlmostEqual(importlib.reload(self.buyitemprice).price, 25)
self.assertAlmostEqual(importlib.reload(self.buyitemprice2).price, self.buyitemprice2.price)
self.assertAlmostEqual(reload(self.stockitem).price, stockitem_newprice)
self.assertAlmostEqual(reload(self.stockitem2).price, self.stockitem2.price)
self.assertAlmostEqual(reload(self.buyitemprice).price, 25)
self.assertAlmostEqual(reload(self.buyitemprice2).price, self.buyitemprice2.price)
end_money = self.bar_account.money
end_money -= data['items'][0]['price']
end_money -= data['items'][1]['qty'] * self.buyitemprice2.price
self.assertAlmostEqual(importlib.reload(self.bar_account).money, end_money)
self.assertAlmostEqual(reload(self.bar_account).money, end_money)
def test_appro_no_staff(self):
data = {'type':'appro',
......@@ -444,8 +453,8 @@ class ApproSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
tct = s.save()
self.assertAlmostEqual(importlib.reload(self.stockitem).qty, self.stockitem.qty)
self.assertAlmostEqual(importlib.reload(self.bar_account).money, self.bar_account.money)
self.assertAlmostEqual(reload(self.stockitem).qty, self.stockitem.qty)
self.assertAlmostEqual(reload(self.bar_account).money, self.bar_account.money)
class InventorySerializerTests(SerializerTests):
......@@ -468,8 +477,8 @@ class InventorySerializerTests(SerializerTests):
moneyflow += self.stockitem2.get_price('sell') * (data['items'][1]['qty'] - s2_sell_qty)
self.assertAlmostEqual(tct.moneyflow, moneyflow)
self.assertAlmostEqual(importlib.reload(self.stockitem).sell_qty, data['items'][0]['qty'])
self.assertAlmostEqual(importlib.reload(self.stockitem2).sell_qty, data['items'][1]['qty'])
self.assertAlmostEqual(reload(self.stockitem).sell_qty, data['items'][0]['qty'])
self.assertAlmostEqual(reload(self.stockitem2).sell_qty, data['items'][1]['qty'])
def test_inventory_no_staff(self):
self.context = {'request': Mock(user=self.user, bar=self.bar)}
......@@ -486,5 +495,5 @@ class InventorySerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
s.save()
self.assertAlmostEqual(importlib.reload(self.stockitem).sell_qty, self.stockitem.sell_qty)
self.assertAlmostEqual(importlib.reload(self.stockitem2).sell_qty, self.stockitem2.sell_qty)
self.assertAlmostEqual(reload(self.stockitem).sell_qty, self.stockitem.sell_qty)
self.assertAlmostEqual(reload(self.stockitem2).sell_qty, self.stockitem2.sell_qty)
......@@ -13,11 +13,20 @@ from bars_items.models.sellitem import SellItem
from bars_items.models.stockitem import StockItem
from bars_transactions.models import Transaction
import importlib
from types import ModuleType
import sys
from imp import reload
def reload(obj):
return obj.__class__.objects.get(pk=obj.pk)
_reload_builtin = reload
def reload(thing):
if isinstance(thing, ModuleType):
_reload_builtin(thing)
elif hasattr(thing, '__module__') and thing.__module__:
module = sys.modules[thing.__module__]
_reload_builtin(module)
else:
raise TypeError("reload() argument must be a module or have an __module__")
class TransactionTests(APITestCase):
@classmethod
......@@ -30,7 +39,7 @@ class TransactionTests(APITestCase):
self.user, _ = User.objects.get_or_create(username='user')
self.user.role_set.all().delete()
self.user = importlib.reload(self.user)
self.user = reload(self.user)
self.account, _ = Account.objects.get_or_create(bar=self.bar, owner=self.user)
self.account.money = 100
self.account.save()
......@@ -59,14 +68,14 @@ class TransactionTests(APITestCase):
self.transaction.canceled = False
self.transaction.save()
Role.objects.get_or_create(user=self.user, bar=self.bar, name='customer')
self.user = importlib.reload(self.user)
self.staff_user = importlib.reload(self.staff_user)
self.user = reload(self.user)
self.staff_user = reload(self.staff_user)
def test_cancel_transaction(self):
self.client.force_authenticate(user=self.user)
response = self.client.put(self.transaction_url + 'cancel/', {})
self.assertEqual(response.status_code, 200)
self.assertTrue(importlib.reload(self.transaction).canceled)
self.assertTrue(reload(self.transaction).canceled)
def test_restore_transaction(self):
self.transaction.canceled = True
......@@ -75,21 +84,21 @@ class TransactionTests(APITestCase):
response = self.client.put(self.transaction_url + 'restore/', {})
self.assertEqual(response.status_code, 200)
self.assertFalse(importlib.reload(self.transaction).canceled)
self.assertFalse(reload(self.transaction).canceled)
def test_cancel_transaction_wrong_user(self):
self.client.force_authenticate(user=self.wrong_user)
response = self.client.put(self.transaction_url + 'cancel/', {})
self.assertEqual(response.status_code, 403)
self.assertFalse(importlib.reload(self.transaction).canceled)
self.assertFalse(reload(self.transaction).canceled)
def test_cancel_transaction_staff(self):
self.client.force_authenticate(user=self.staff_user)
response = self.client.put(self.transaction_url + 'cancel/', {})
self.assertEqual(response.status_code, 200)
self.assertTrue(importlib.reload(self.transaction).canceled)
self.assertTrue(reload(self.transaction).canceled)
def test_cancel_transaction_staff_wrong_bar(self):
self.client.force_authenticate(user=self.staff_user)
......@@ -97,4 +106,4 @@ class TransactionTests(APITestCase):
response = self.client.put('/transaction/%d/cancel/' % transaction.id, {})
self.assertEqual(response.status_code, 403)
self.assertFalse(importlib.reload(transaction).canceled)
self.assertFalse(reload(transaction).canceled)
......@@ -13,11 +13,20 @@ from bars_items.models.sellitem import SellItem
from bars_items.models.stockitem import StockItem
from bars_transactions.models import Transaction
import importlib
from types import ModuleType
import sys
from imp import reload
def reload(obj):
return obj.__class__.objects.get(pk=obj.pk)
_reload_builtin = reload
def reload(thing):
if isinstance(thing, ModuleType):
_reload_builtin(thing)
elif hasattr(thing, '__module__') and thing.__module__:
module = sys.modules[thing.__module__]
_reload_builtin(module)
else:
raise TypeError("reload() argument must be a module or have an __module__")
class TransactionOperationTests(APITestCase):
@classmethod
......@@ -30,7 +39,7 @@ class TransactionOperationTests(APITestCase):
self.user, _ = User.objects.get_or_create(username='user')
self.user.role_set.all().delete()
self.user = importlib.reload(self.user)
self.user = reload(self.user)
self.account, _ = Account.objects.get_or_create(bar=self.bar, owner=self.user)
self.account.money = 100
self.account.save()
......@@ -66,54 +75,54 @@ class TransactionOperationTests(APITestCase):
self.transaction.canceled = False
self.transaction.save()
Role.objects.get_or_create(user=self.user, bar=self.bar, name='customer')
self.user = importlib.reload(self.user)
self.staff_user = importlib.reload(self.staff_user)
<