Commit 04242a20 authored by Jean-samuel TETTEKPOE's avatar Jean-samuel TETTEKPOE
Browse files

Reload properly

parent 0866b279
Pipeline #9079 failed with stage
in 37 seconds
......@@ -30,6 +30,7 @@ class BugReport(models.Model):
class BugReportSerializer(serializers.ModelSerializer):
class Meta:
model = BugReport
fields = '__all__'
_type = VirtualField("BugReport")
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentBarCreateOnlyDefault())
......
......@@ -5,10 +5,6 @@ from bars_core.models.user import User
from bars_core.models.bar import Bar
from bars_bugtracker.models import BugReport
def reload(obj):
return obj.__class__.objects.get(pk=obj.pk)
class BugreportTests(APITestCase):
@classmethod
def setUpClass(self):
......
......@@ -33,12 +33,13 @@ class Account(models.Model):
def save(self, *args, **kwargs):
if not self.pk:
Role.objects.get_or_create(name='customer', bar=self.bar, user=self.owner)
super(Account, self).save(*args, **kwargs)
super().save(*args, **kwargs)
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = '__all__'
read_only_fields = ('bar', 'money', 'overdrawn_since', 'last_modified')
_type = VirtualField("Account")
......
......@@ -21,7 +21,7 @@ class Bar(models.Model):
return self.id
def save(self, *args, **kwargs):
super(Bar, self).save(*args, **kwargs)
super().save(*args, **kwargs)
from bars_core.models.bar import BarSettings
BarSettings.objects.get_or_create(bar=self)
......@@ -204,6 +204,7 @@ class BarSettings(models.Model):
class BarSettingsSerializer(serializers.ModelSerializer):
class Meta:
model = BarSettings
fields = '__all__'
_type = VirtualField("BarSettings")
id = serializers.PrimaryKeyRelatedField(read_only=True, source='bar') # To help the client
bar = serializers.PrimaryKeyRelatedField(read_only=True)
......
......@@ -19,7 +19,7 @@ from bars_core.models.loginattempt import LoginAttempt
class UserManager(BaseUserManager):
def get_queryset(self):
return super(UserManager, self).get_queryset().prefetch_related(
return super().get_queryset().prefetch_related(
'role_set',
)
......
......@@ -116,7 +116,7 @@ class BarRolePermissionLogic(PermissionLogic):
@debug_perm("Logic")
def has_perm(self, user, perm, obj=None):
if not user.is_authenticated() or not user.is_active:
if not user.is_authenticated or not user.is_active:
return False
if obj is None:
......@@ -134,7 +134,7 @@ class RootBarRolePermissionLogic(PermissionLogic):
@debug_perm("Logic (root)")
def has_perm(self, user, perm, obj=None):
if not user.is_authenticated() or not user.is_active:
if not user.is_authenticated or not user.is_active:
return False
bar = get_root_bar()
......@@ -158,7 +158,7 @@ class PermissionBackend(PermissionBackend_):
@debug_perm("Backend")
def has_perm(self, user, perm, obj=None):
if not user.is_authenticated() or not user.is_active:
if not user.is_authenticated or not user.is_active:
return False
bar_perm = "bar" == perm.split(".")[1].split("_")[1]
......
......@@ -5,20 +5,6 @@ from bars_core.models.user import User, UserSerializer
from bars_core.models.role import Role
from bars_core.models.account import Account, AccountSerializer
from types import ModuleType
import sys
from imp import reload
_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):
@classmethod
......@@ -68,7 +54,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 = reload(self.manager) # prevent role caching
self.manager.refresh_from_db() # prevent role caching
self.bar, _ = Bar.objects.get_or_create(id="barrouje")
......@@ -106,8 +92,8 @@ class BarTests(APITestCase):
self.client.force_authenticate(user=self.manager)
response = self.client.put(self.bar_url, self.data)
self.assertEqual(response.status_code, 200)
self.assertEqual(reload(self.bar).name, self.data['name'])
self.bar.refresh_from_db()
self.assertEqual(self.bar.name, self.data['name'])
class BarSettingsTests(APITestCase):
......@@ -118,7 +104,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 = reload(self.manager) # prevent role caching
self.manager.refresh_from_db() # prevent role caching
self.barsettings = self.bar.settings
......@@ -143,7 +129,8 @@ class BarSettingsTests(APITestCase):
self.client.force_authenticate(user=User())
response = self.client.get(self.barsettings_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(reload(self.barsettings).agios_enabled, response.data['agios_enabled'])
self.barsettings.refresh_from_db()
self.assertEqual(self.barsettings.agios_enabled, response.data['agios_enabled'])
def test_change_no_perms(self):
......@@ -157,8 +144,8 @@ class BarSettingsTests(APITestCase):
self.client.force_authenticate(user=self.manager)
response = self.client.put(self.barsettings_url, self.data)
self.assertEqual(response.status_code, 200)
self.assertEqual(reload(self.barsettings).agios_enabled, self.data['agios_enabled'])
self.barsettings.refresh_from_db()
self.assertEqual(self.barsettings.agios_enabled, self.data['agios_enabled'])
class UserTests(APITestCase):
......@@ -169,7 +156,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 = reload(self.manager) # prevent role caching
self.manager.refresh_from_db() # prevent role caching
self.user, _ = User.objects.get_or_create(username="bob")
self.user.set_password("password")
......@@ -228,8 +215,8 @@ class UserTests(APITestCase):
self.client.force_authenticate(user=self.manager)
response = self.client.put(self.user_url, self.data)
self.assertEqual(response.status_code, 200)
self.assertEqual(reload(self.user).username, self.data['username'])
self.user.refresh_from_db()
self.assertEqual(self.user.username, self.data['username'])
def test_change_user_self(self):
# Authenticated as self
......@@ -237,8 +224,8 @@ class UserTests(APITestCase):
self.client.force_authenticate(user=self.user)
response = self.client.put(self.user_url, self.data)
self.assertEqual(response.status_code, 200)
self.assertEqual(reload(self.user).username, self.data['username'])
self.user.refresh_from_db()
self.assertEqual(self.user.username, self.data['username'])
def test_change_password(self):
......@@ -269,7 +256,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 = reload(self.user2)
self.user2.refresh_from_db()
self.create_data = {'owner': self.user2.id}
self.account, _ = Account.objects.get_or_create(owner=self.user, bar=self.bar)
......@@ -315,28 +302,32 @@ 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(reload(self.account).deleted, self.account.deleted)
self.account.refresh_from_db()
self.assertEqual(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(reload(self.account).deleted, self.account.deleted)
self.account.refresh_from_db()
self.assertEqual(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(reload(self.account).deleted, self.update_data['deleted'])
self.account.refresh_from_db()
self.assertEqual(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(reload(self.account).deleted, self.account.deleted)
self.account.refresh_from_db()
self.assertEqual(self.account.deleted, self.account.deleted)
......@@ -354,9 +345,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 = reload(self.user2)
self.user2.refresh_from_db()
Role.objects.get_or_create(name='admin', bar=get_root_bar(), user=self.root)
self.root = reload(self.root)
self.root.refresh_from_db()
self.create_data = {'user': self.user.id, 'name': 'customer'}
self.create_data_root = {'user': self.user.id, 'name': 'usermanager'}
......
from rest_framework import fields
from django.utils.deprecation import MiddlewareMixin
class VirtualField(fields.ReadOnlyField):
"""
Identify the type of resource that is returned by the API. Needed by client's API Model.
......@@ -45,7 +47,7 @@ get_root_bar._cache = None
from django.http import Http404
class BarMiddleware(object):
class BarMiddleware(MiddlewareMixin):
"""
Define a Django middleware to attach a Bar instance to any request with a GET parameter called "bar".
"""
......
......@@ -14,20 +14,6 @@ from bars_items.models.sellitem import SellItem, SellItemSerializer
from bars_items.models.stockitem import StockItem, StockItemSerializer
from bars_items.models.suggesteditem import SuggestedItem, SuggestedItemSerializer
from types import ModuleType
import sys
from imp import reload
_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():
def test_get(self):
......@@ -146,7 +132,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 = reload(self.staff_user)
self.staff_user.refresh_from_db()
self.sellitem, _ = SellItem.objects.get_or_create(bar=self.bar, name="Chocolat", tax=0.2)
self.itemdetails, _ = ItemDetails.objects.get_or_create(name="Chocolat")
......@@ -200,9 +186,12 @@ 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(reload(self.sellitem).tax, 0.15)
self.assertEqual(reload(self.sellitem2).tax, 0.15)
self.assertEqual(reload(self.sellitem3).tax, 0.1)
self.sellitem.refresh_from_db()
self.sellitem2.refresh_from_db()
self.sellitem3.refresh_from_db()
self.assertEqual(self.sellitem.tax, 0.15)
self.assertEqual(self.sellitem2.tax, 0.15)
self.assertEqual(self.sellitem3.tax, 0.1)
def test_update_1(self):
self.client.force_authenticate(user=self.staff_user)
......@@ -211,7 +200,8 @@ 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(reload(self.stockitem).sell_to_buy, old_sell_to_buy / update_data['unit_factor'])
self.stockitem.refresh_from_db()
self.assertAlmostEqual(self.stockitem.sell_to_buy, old_sell_to_buy / update_data['unit_factor'])
class ItemDetailsTests(ItemTests, AutoTestMixin):
......
......@@ -4,7 +4,7 @@ from bars_core.perms import debug_perm
class MenuOwnerPermissionLogic(AuthorPermissionLogic):
@debug_perm("Logic (menu)")
def has_perm(self, user, perm, obj=None):
if not user.is_authenticated() or not user.is_active:
if not user.is_authenticated or not user.is_active:
return False
return super(MenuOwnerPermissionLogic, self).has_perm(user, perm, obj)
......@@ -127,7 +127,7 @@ class BaseOperation(models.Model):
if not self.pk:
self.op_model.objects.filter(pk=self.target.id).update(**{self.op_model_field: self.next_value})
super(BaseOperation, self).save(*args, **kwargs)
super().save(*args, **kwargs)
def propagate(self):
olders_or_self = (self.__class__.objects.select_related()
......@@ -192,7 +192,7 @@ class AccountOperation(BaseOperation):
bar = self.target.bar.name
)
send_mail(**message)
super(AccountOperation, self).save(*args, **kwargs)
super().save(*args, **kwargs)
op_model = Account
op_model_field = 'money'
......@@ -6,7 +6,7 @@ from bars_core.perms import debug_perm, BarRolePermissionLogic
class TransactionAuthorPermissionLogic(AuthorPermissionLogic):
@debug_perm("Logic (transaction)")
def has_perm(self, user, perm, obj=None):
if not user.is_authenticated() or not user.is_active:
if not user.is_authenticated or not user.is_active:
return False
bar_role_perm = BarRolePermissionLogic().has_perm(user, perm, obj)
......
......@@ -18,20 +18,6 @@ from ..serializers import (BaseTransactionSerializer, BuyTransactionSerializer,
ThrowTransactionSerializer, DepositTransactionSerializer, PunishTransactionSerializer,
MealTransactionSerializer, ApproTransactionSerializer, InventoryTransactionSerializer,)
from types import ModuleType
import sys
from imp import reload
_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):
@classmethod
......@@ -107,14 +93,14 @@ class SerializerTests(APITestCase):
self.stockitem2.qty = 10
self.stockitem2.save()
self.user = reload(self.user) # Avoid role caching
self.user.refresh_from_db() # Avoid role caching
self.context = {'request': Mock(user=self.user, bar=self.bar)}
class BuySerializerTests(SerializerTests):
def setUp(self):
self.stockitem = reload(self.stockitem)
self.stockitem.refresh_from_db()
def tearDown(self):
self.stockitem.deleted = False
......@@ -127,8 +113,10 @@ class BuySerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, self.stockitem.sell_price * data['qty'])
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)
self.stockitem.refresh_from_db()
self.account.refresh_from_db()
self.assertAlmostEqual(self.stockitem.sell_qty, self.stockitem.sell_qty - data['qty'])
self.assertAlmostEqual(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}
......@@ -143,9 +131,12 @@ class BuySerializerTests(SerializerTests):
self.assertAlmostEqual(tct.moneyflow, self.sellitem.calc_price() * data['qty'])
total_qty = self.stockitem.sell_qty + stockitem3.sell_qty
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())
self.stockitem.refresh_from_db()
self.stockitem3.refresh_from_db()
self.account.refresh_from_db()
self.assertAlmostEqual(self.stockitem.sell_qty, self.stockitem.sell_qty * (1 - data['qty'] / total_qty))
self.assertAlmostEqual(stockitem3.sell_qty, stockitem3.sell_qty * (1 - data['qty'] / total_qty))
self.assertAlmostEqual(self.account.money, self.account.money - data['qty'] * self.sellitem.calc_price())
def test_buy_itemdeleted(self):
self.stockitem.deleted = True
......@@ -190,8 +181,10 @@ class GiveSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, data['amount'])
self.assertAlmostEqual(reload(self.account).money, self.account.money - data['amount'])
self.assertAlmostEqual(reload(self.account2).money, self.account2.money + data['amount'])
self.account.refresh_from_db()
self.account2.refresh_from_db()
self.assertAlmostEqual(self.account.money, self.account.money - data['amount'])
self.assertAlmostEqual(self.account2.money, self.account2.money + data['amount'])
def test_give_other_bar(self):
data = {'type':'give', 'account':self.wrong_account.id, 'amount':10}
......@@ -220,7 +213,8 @@ class ThrowSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, - self.stockitem.sell_price * data['qty'])
self.assertAlmostEqual(reload(self.stockitem).sell_qty, self.stockitem.sell_qty - data['qty'])
self.stockitem.refresh_from_db()
self.assertAlmostEqual(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)}
......@@ -258,8 +252,10 @@ class DepositSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, 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'])
self.account.refresh_from_db()
self.bar_account.refresh_from_db()
self.assertAlmostEqual(self.account.money, self.account.money + data['amount'])
self.assertAlmostEqual(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}
......@@ -269,8 +265,10 @@ class DepositSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
s.save()
self.assertAlmostEqual(reload(self.account).money, self.account.money)
self.assertAlmostEqual(reload(self.bar_account).money, self.bar_account.money)
self.account.refresh_from_db()
self.bar_account.refresh_from_db()
self.assertAlmostEqual(self.account.money, self.account.money)
self.assertAlmostEqual(self.bar_account.money, self.bar_account.money)
def test_deposit_other_bar(self):
data = {'type':'deposit', 'account':self.wrong_account.id, 'amount':30}
......@@ -281,8 +279,10 @@ class DepositSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
s.save()
self.assertAlmostEqual(reload(self.wrong_account).money, self.wrong_account.money)
self.assertAlmostEqual(reload(self.wrong_bar_account).money, self.wrong_bar_account.money)
self.wrong_account.refresh_from_db()
self.wrong_bar_account.refresh_from_db()
self.assertAlmostEqual(self.wrong_account.money, self.wrong_account.money)
self.assertAlmostEqual(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)}
......@@ -302,7 +302,8 @@ class PunishSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, -data['amount'])
self.assertAlmostEqual(reload(self.account).money, self.account.money - data['amount'])
self.account.refresh_from_db()
self.assertAlmostEqual(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'}
......@@ -312,7 +313,8 @@ class PunishSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
s.save()
self.assertAlmostEqual(reload(self.account).money, self.account.money)
self.account.refresh_from_db()
self.assertAlmostEqual(self.account.money, self.account.money)
def test_punish_negative(self):
data = {'type':'punish', 'account':self.account.id, 'amount':-50, 'motive':'cadeau'}
......@@ -353,9 +355,11 @@ class MealSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, self.stockitem.sell_price * data['items'][0]['qty'])
self.assertAlmostEqual(reload(self.stockitem).sell_qty, self.stockitem.sell_qty - data['items'][0]['qty'])
self.stockitem.refresh_from_db()
self.assertAlmostEqual(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(reload(self.account).money, end_money)
self.account.refresh_from_db()
self.assertAlmostEqual(self.account.money, end_money)
def test_meal_no_item(self):
data = {
......@@ -386,8 +390,10 @@ class MealSerializerTests(SerializerTests):
self.assertTrue(s.is_valid())
tct = s.save()
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'])
self.stockitem.refresh_from_db()
self.account2.refresh_from_db()
self.assertAlmostEqual(self.stockitem.sell_qty, self.stockitem.sell_qty - data['items'][0]['qty'])
self.assertAlmostEqual(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
......@@ -397,8 +403,10 @@ 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(reload(self.account).money, end_money)
self.assertAlmostEqual(reload(self.account2).money, end_money2)
self.account.refresh_from_db()
self.account2.refresh_from_db()
self.assertAlmostEqual(self.account.money, end_money)
self.assertAlmostEqual(self.account2.money, end_money2)
self.assertAlmostEqual(tct.moneyflow, total_money)
......@@ -425,20 +433,27 @@ class ApproSerializerTests(SerializerTests):
self.assertAlmostEqual(tct.moneyflow, data['items'][0]['price'] + self.buyitemprice2.price * data['items'][1]['qty'])
# Test quantities
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)
self.stockitem.refresh_from_db()
self.stockitem2.refresh_from_db()
self.assertAlmostEqual(self.stockitem.sell_qty, self.stockitem.sell_qty + data['items'][0]['qty'] * self.buyitem.itemqty / self.stockitem.sell_to_buy)
self.assertAlmostEqual(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(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)
self.stockitem.refresh_from_db()
self.stockitem2.refresh_from_db()
self.buyitemprice.refresh_from_db()
self.buyitemprice2.refresh_from_db()
self.assertAlmostEqual(self.stockitem.price, stockitem_newprice)
self.assertAlmostEqual(self.stockitem2.price, self.stockitem2.price)
self.assertAlmostEqual(self.buyitemprice.price, 25)
self.assertAlmostEqual(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(reload(self.bar_account).money, end_money)
self.bar_account.refresh_from_db()
self.assertAlmostEqual(self.bar_account.money, end_money)
def test_appro_no_staff(self):
data = {'type':'appro',
......@@ -453,8 +468,10 @@ class ApproSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
tct = s.save()
self.assertAlmostEqual(reload(self.stockitem).qty, self.stockitem.qty)
self.assertAlmostEqual(reload(self.bar_account).money, self.bar_account.money)
self.stockitem.refresh_from_db()