Commit 4fde85b5 authored by Jean-samuel TETTEKPOE's avatar Jean-samuel TETTEKPOE
Browse files

Solve failing calculus assert

parent 3f6bab83
Pipeline #9114 failed with stage
in 40 seconds
......@@ -5,6 +5,8 @@ from bars_core.models.user import User
from bars_core.models.bar import Bar
from bars_bugtracker.models import BugReport
import json
class BugreportTests(APITestCase):
@classmethod
def setUpClass(self):
......@@ -30,7 +32,7 @@ class BugreportTests(APITestCase):
# Correct permissions
with patch.object(User, 'has_perm', return_value=True) as m:
self.client.force_authenticate(user=self.user)
response = self.client.post('/bugreport/?bar=natationjone', self.create_data, content_type="application/json")
response = self.client.post('/bugreport/?bar=natationjone', json.dumps(self.create_data), content_type="application/json")
self.assertEqual(response.status_code, 201)
self.assertEqual(m.call_args[0][0], 'bars_bugtracker.add_bugreport')
......
......@@ -5,6 +5,7 @@ from bars_core.models.user import User, UserSerializer
from bars_core.models.role import Role
from bars_core.models.account import Account, AccountSerializer
import json
class BackendTests(APITestCase):
@classmethod
......@@ -288,7 +289,7 @@ class AccountTests(APITestCase):
def test_create_account2(self):
# Correct permissions
self.client.force_authenticate(user=self.user2)
response = self.client.post('/account/?bar=natationjone', self.create_data, content_type="application/json")
response = self.client.post('/account/?bar=natationjone', json.dumps(self.create_data), content_type="application/json")
self.assertEqual(response.status_code, 201)
def test_create_account3(self):
......@@ -301,21 +302,23 @@ class AccountTests(APITestCase):
# Unauthenticated
response = self.client.put('/account/%d/?bar=natationjone' % self.account.id, self.update_data, content_type="application/json")
self.assertEqual(response.status_code, 401)
old_account_deleted = self.account.deleted
self.account.refresh_from_db()
self.assertEqual(self.account.deleted, self.account.deleted)
self.assertEqual(self.account.deleted, old_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, content_type="application/json")
self.assertEqual(response.status_code, 403)
old_account_deleted = self.account.deleted
self.account.refresh_from_db()
self.assertEqual(self.account.deleted, self.account.deleted)
self.assertEqual(self.account.deleted, old_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, content_type="application/json")
response = self.client.put('/account/%d/?bar=natationjone' % self.account.id, json.dumps(self.update_data), content_type="application/json")
self.assertEqual(response.status_code, 200)
self.account.refresh_from_db()
self.assertEqual(self.account.deleted, self.update_data['deleted'])
......@@ -325,8 +328,9 @@ class AccountTests(APITestCase):
self.client.force_authenticate(user=self.user2)
response = self.client.put('/account/%d/?bar=avironjone' % self.account.id, self.update_data, content_type="application/json")
self.assertEqual(response.status_code, 403)
old_account_deleted = self.account.deleted
self.account.refresh_from_db()
self.assertEqual(self.account.deleted, self.account.deleted)
self.assertEqual(self.account.deleted, old_account_deleted)
......@@ -371,7 +375,7 @@ class RoleTests(APITestCase):
def test_create_role2(self):
# Correct permissions
self.client.force_authenticate(user=self.user2)
response = self.client.post('/role/?bar=natationjone', self.create_data, content_type="application/json")
response = self.client.post('/role/?bar=natationjone', self.create_data)
self.assertEqual(response.status_code, 201)
def test_create_role3(self):
......@@ -387,5 +391,5 @@ class RoleTests(APITestCase):
def test_create_role_root2(self):
self.client.force_authenticate(user=self.root)
response = self.client.post('/role/?bar=root', self.create_data_root, content_type="application/json")
response = self.client.post('/role/?bar=root', self.create_data_root)
self.assertEqual(response.status_code, 201)
from mock import Mock
from rest_framework import exceptions, serializers
from rest_framework.test import APITestCase
from bars_django.utils import get_root_bar
......@@ -8,11 +6,11 @@ from bars_core.models.user import User
from bars_core.models.role import Role
from bars_core.models.account import Account
from bars_items.models.buyitem import BuyItem, BuyItemSerializer, BuyItemPrice, BuyItemPriceSerializer
from bars_items.models.buyitem import BuyItem, BuyItemSerializer, BuyItemPrice
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
from bars_items.models.suggesteditem import SuggestedItem
class AutoTestGetMixin():
......@@ -37,7 +35,7 @@ class AutoTestCreateBarMixin():
def test_create2(self):
# Correct permissions
self.client.force_authenticate(user=self.staff_user)
response = self.client.post(self.create_url % self.bar.id, self.create_data, content_type="application/json")
response = self.client.post(self.create_url % self.bar.id, self.create_data)
self.assertEqual(response.status_code, 201)
def test_create3(self):
......@@ -198,12 +196,11 @@ class SellItemTests(ItemTests, AutoTestBarMixin):
update_data = self.update_data.copy()
update_data['unit_factor'] = 20
old_sell_to_buy = self.stockitem.sell_to_buy
response = self.client.put("/sellitem/%d/?bar=%s" % (self.sellitem.id, self.bar), update_data)
response = self.client.put("/sellitem/%d/?bar=%s" % (self.sellitem.id, self.bar.id), update_data)
self.assertEqual(response.status_code, 200)
self.stockitem.refresh_from_db()
self.assertAlmostEqual(self.stockitem.sell_to_buy, old_sell_to_buy / update_data['unit_factor'])
class ItemDetailsTests(ItemTests, AutoTestMixin):
@classmethod
def setUpTestData(self):
......@@ -292,7 +289,7 @@ class SuggestedItemTests(APITestCase):
def test_create_suggesteditems2(self):
# Correct permissions
self.client.force_authenticate(user=self.user2)
response = self.client.post('/suggesteditem/?bar=natationjone', self.create_data, content_type="application/json")
response = self.client.post('/suggesteditem/?bar=natationjone', self.create_data)
self.assertEqual(response.status_code, 201)
def test_create_suggesteditems3(self):
......
......@@ -49,7 +49,7 @@ class MenuTests(APITestCase):
def test_create_menu_2(self):
# Correct permissions
self.client.force_authenticate(user=self.user1)
response = self.client.post('/menu/?bar=avironjone', self.create_data, format='json', content_type="application/json")
response = self.client.post('/menu/?bar=avironjone', self.create_data, format='json')
self.assertEqual(response.status_code, 201)
self.assertEqual(len(response.data['items']), 2)
......
......@@ -41,7 +41,7 @@ class NewsTests(APITestCase):
def test_create_news2(self):
# Correct permissions
self.client.force_authenticate(user=self.user2)
response = self.client.post('/news/?bar=natationjone', self.create_data, content_type="application/json")
response = self.client.post('/news/?bar=natationjone', self.create_data)
self.assertEqual(response.status_code, 201)
def test_create_news3(self):
......
......@@ -113,10 +113,12 @@ class BuySerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, self.stockitem.sell_price * data['qty'])
old_stockitem_sell_qty = self.stockitem.sell_qty
old_account_money = self.account.money
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)
self.assertAlmostEqual(self.stockitem.sell_qty, old_stockitem_sell_qty - data['qty'])
self.assertAlmostEqual(self.account.money, old_account_money - data['qty'] * self.stockitem.sell_price)
def test_buy_sellitem(self):
data = {'type':'buy', 'sellitem':self.sellitem.id, 'qty':23}
......@@ -131,12 +133,15 @@ class BuySerializerTests(SerializerTests):
self.assertAlmostEqual(tct.moneyflow, self.sellitem.calc_price() * data['qty'])
total_qty = self.stockitem.sell_qty + stockitem3.sell_qty
old_stockitem_sell_qty = self.stockitem.sell_qty
old_stockitem3_sell_qty = stockitem3.sell_qty
old_account_money = self.account.money
self.stockitem.refresh_from_db()
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())
self.assertAlmostEqual(self.stockitem.sell_qty, old_stockitem_sell_qty * (1 - data['qty'] / total_qty))
self.assertAlmostEqual(stockitem3.sell_qty, old_stockitem3_sell_qty * (1 - data['qty'] / total_qty))
self.assertAlmostEqual(self.account.money, old_account_money - data['qty'] * self.sellitem.calc_price())
def test_buy_itemdeleted(self):
self.stockitem.deleted = True
......@@ -181,10 +186,12 @@ class GiveSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, data['amount'])
old_account_money = self.account.money
old_account2_money = self.account2.money
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'])
self.assertAlmostEqual(self.account.money, old_account_money - data['amount'])
self.assertAlmostEqual(self.account2.money, old_account2_money + data['amount'])
def test_give_other_bar(self):
data = {'type':'give', 'account':self.wrong_account.id, 'amount':10}
......@@ -213,8 +220,9 @@ class ThrowSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, - self.stockitem.sell_price * data['qty'])
old_stockitem_sell_qty = self.stockitem.sell_qty
self.stockitem.refresh_from_db()
self.assertAlmostEqual(self.stockitem.sell_qty, self.stockitem.sell_qty - data['qty'])
self.assertAlmostEqual(self.stockitem.sell_qty, old_stockitem_sell_qty - data['qty'])
def test_throw_other_bar(self):
context = {'request': Mock(user=self.user, bar=self.wrong_bar)}
......@@ -252,10 +260,12 @@ class DepositSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, data['amount'])
old_account_money = self.account.money
old_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 + data['amount'])
self.assertAlmostEqual(self.bar_account.money, self.bar_account.money + data['amount'])
self.assertAlmostEqual(self.account.money, old_account_money + data['amount'])
self.assertAlmostEqual(self.bar_account.money, old_bar_account_money + data['amount'])
def test_deposit_no_staff(self):
data = {'type':'deposit', 'account':self.account.id, 'amount':40}
......@@ -265,10 +275,12 @@ class DepositSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
s.save()
old_account_money = self.account.money
old_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)
self.assertAlmostEqual(self.account.money, old_account_money)
self.assertAlmostEqual(self.bar_account.money, old_bar_account_money)
def test_deposit_other_bar(self):
data = {'type':'deposit', 'account':self.wrong_account.id, 'amount':30}
......@@ -279,10 +291,12 @@ class DepositSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
s.save()
old_wrong_account_money = self.wrong_account.money
old_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)
self.assertAlmostEqual(self.wrong_account.money, old_wrong_account_money)
self.assertAlmostEqual(self.wrong_bar_account.money, old_wrong_bar_account_money)
def test_deposit_negative(self):
self.context = {'request': Mock(user=self.staff_user, bar=self.bar)}
......@@ -302,8 +316,9 @@ class PunishSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, -data['amount'])
old_account_money = self.account.money
self.account.refresh_from_db()
self.assertAlmostEqual(self.account.money, self.account.money - data['amount'])
self.assertAlmostEqual(self.account.money, old_account_money - data['amount'])
def test_punish_no_staff(self):
data = {'type':'punish', 'account':self.account.id, 'amount':50, 'motive':'vaisselle'}
......@@ -313,8 +328,9 @@ class PunishSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
s.save()
old_account_money = self.account.money
self.account.refresh_from_db()
self.assertAlmostEqual(self.account.money, self.account.money)
self.assertAlmostEqual(self.account.money, old_account_money)
def test_punish_negative(self):
data = {'type':'punish', 'account':self.account.id, 'amount':-50, 'motive':'cadeau'}
......@@ -355,8 +371,9 @@ class MealSerializerTests(SerializerTests):
tct = s.save()
self.assertAlmostEqual(tct.moneyflow, self.stockitem.sell_price * data['items'][0]['qty'])
old_stockitem_sell_qty = self.stockitem.sell_qty
self.stockitem.refresh_from_db()
self.assertAlmostEqual(self.stockitem.sell_qty, self.stockitem.sell_qty - data['items'][0]['qty'])
self.assertAlmostEqual(self.stockitem.sell_qty, old_stockitem_sell_qty - data['items'][0]['qty'])
end_money = self.account.money - data['items'][0]['qty'] * self.stockitem.sell_price
self.account.refresh_from_db()
self.assertAlmostEqual(self.account.money, end_money)
......@@ -390,10 +407,12 @@ class MealSerializerTests(SerializerTests):
self.assertTrue(s.is_valid())
tct = s.save()
old_stockitem_sell_qty = self.stockitem.sell_qty
old_stockitem2_sell_qty = self.stockitem2.sell_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'])
self.stockitem2.refresh_from_db()
self.assertAlmostEqual(self.stockitem.sell_qty, old_stockitem_sell_qty - data['items'][0]['qty'])
self.assertAlmostEqual(self.stockitem2.sell_qty, old_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
......@@ -433,21 +452,26 @@ class ApproSerializerTests(SerializerTests):
self.assertAlmostEqual(tct.moneyflow, data['items'][0]['price'] + self.buyitemprice2.price * data['items'][1]['qty'])
# Test quantities
old_stockitem_sell_qty = self.stockitem.sell_qty
old_stockitem_qty = self.stockitem.qty
old_stockitem_price = self.stockitem.price
old_stockitem2_sell_qty = self.stockitem2.sell_qty
old_stockitem2_price = self.stockitem2.price
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)
self.assertAlmostEqual(self.stockitem.sell_qty, old_stockitem_sell_qty + data['items'][0]['qty'] * self.buyitem.itemqty / self.stockitem.sell_to_buy)
self.assertAlmostEqual(self.stockitem2.sell_qty, old_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)
stockitem_newprice = (old_stockitem_qty * old_stockitem_price + data['items'][0]['price']) / (old_stockitem_qty + data['items'][0]['qty'] * self.buyitem.itemqty)
self.stockitem.refresh_from_db()
self.stockitem2.refresh_from_db()
self.buyitemprice.refresh_from_db()
old_buyitemprice2_price = self.buyitemprice2.price
self.buyitemprice2.refresh_from_db()
self.assertAlmostEqual(self.stockitem.price, stockitem_newprice)
self.assertAlmostEqual(self.stockitem2.price, self.stockitem2.price)
self.assertAlmostEqual(self.stockitem2.price, old_stockitem2_price)
self.assertAlmostEqual(self.buyitemprice.price, 25)
self.assertAlmostEqual(self.buyitemprice2.price, self.buyitemprice2.price)
self.assertAlmostEqual(self.buyitemprice2.price, old_buyitemprice2_price)
end_money = self.bar_account.money
end_money -= data['items'][0]['price']
end_money -= data['items'][1]['qty'] * self.buyitemprice2.price
......@@ -468,10 +492,12 @@ class ApproSerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
tct = s.save()
old_stockitem_qty = self.stockitem.qty
old_bar_account_money = self.bar_account.money
self.stockitem.refresh_from_db()
self.bar_account.refresh_from_db()
self.assertAlmostEqual(self.stockitem.qty, self.stockitem.qty)
self.assertAlmostEqual(self.bar_account.money, self.bar_account.money)
self.assertAlmostEqual(self.stockitem.qty, old_stockitem_qty)
self.assertAlmostEqual(self.bar_account.money, old_bar_account_money)
class InventorySerializerTests(SerializerTests):
......@@ -514,7 +540,10 @@ class InventorySerializerTests(SerializerTests):
with self.assertRaises(exceptions.PermissionDenied):
s.save()
old_stockitem_sell_qty = self.stockitem.sell_qty
old_stockitem2_sell_qty = self.stockitem2.sell_qty
self.stockitem.refresh_from_db()
self.stockitem2.refresh_from_db()
self.assertAlmostEqual(self.stockitem.sell_qty, self.stockitem.sell_qty)
self.assertAlmostEqual(self.stockitem2.sell_qty, self.stockitem2.sell_qty)
self.assertAlmostEqual(self.stockitem.sell_qty, old_stockitem_sell_qty)
self.assertAlmostEqual(self.stockitem2.sell_qty, old_stockitem2_sell_qty)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment