def test_post_empty(self): self.assertEqual(BankBalance.get_price(1), 40000) self.assertEqual(BankBalance.get_price(2), 20000) self.assertEqual(CreditCheckedDate.get_price(1), 30000) self.assertEqual(CreditCheckedDate.get_price(2), 2000) self.client.force_login(User.objects.create_user(self.username)) response = self.client.post(reverse('moneybook:now_bank')) self.assertEqual(response.status_code, 200) response_json = json.loads(response.content.decode()) self.assertEqual(response_json['balance'], 54054 - (40000 + 20000 - 30000 - 2000)) self.assertEqual(BankBalance.get_price(1), 40000) self.assertEqual(BankBalance.get_price(2), 20000) self.assertEqual(CreditCheckedDate.get_price(1), 30000) self.assertEqual(CreditCheckedDate.get_price(2), 2000)
def test_post_guest(self): self.assertEqual(BankBalance.get_price(1), 40000) self.assertEqual(BankBalance.get_price(2), 20000) self.assertEqual(CreditCheckedDate.get_price(1), 30000) self.assertEqual(CreditCheckedDate.get_price(2), 2000) response = self.client.post(reverse('moneybook:now_bank'), { 'bank-1': 50000, 'bank-2': 10000, 'credit-1': 20000, 'credit-2': 3000 }) self.assertEqual(response.status_code, 403) self.assertEqual(BankBalance.get_price(1), 40000) self.assertEqual(BankBalance.get_price(2), 20000) self.assertEqual(CreditCheckedDate.get_price(1), 30000) self.assertEqual(CreditCheckedDate.get_price(2), 2000)
def test_post_str_credit(self): self.assertEqual(BankBalance.get_price(1), 40000) self.assertEqual(BankBalance.get_price(2), 20000) self.assertEqual(CreditCheckedDate.get_price(1), 30000) self.assertEqual(CreditCheckedDate.get_price(2), 2000) self.client.force_login(User.objects.create_user(self.username)) response = self.client.post(reverse('moneybook:now_bank'), { 'bank-1': 50000, 'bank-2': 10000, 'credit-1': 'a', 'credit-2': 3000 }) self.assertEqual(response.status_code, 400) self.assertEqual(BankBalance.get_price(1), 40000) self.assertEqual(BankBalance.get_price(2), 20000) self.assertEqual(CreditCheckedDate.get_price(1), 30000) self.assertEqual(CreditCheckedDate.get_price(2), 2000)
def post(self, request, *args, **kwargs): written_bank_data = Data.get_checked_data( Data.get_bank_data(Data.get_all_data())) bank_sum = 0 bb = BankBalance.get_all() cc = CreditCheckedDate.get_all() # フォーマットチェック try: for b in bb: key = "bank-" + str(b.pk) if key in request.POST: int(request.POST.get(key)) for c in cc: key = "credit-" + str(c.pk) if key in request.POST: int(request.POST.get(key)) except ValueError: return HttpResponseBadRequest( json.dumps({"message": "invalid parameter"})) # 更新と計算 for b in bb: key = "bank-" + str(b.pk) if key in request.POST: value = int(request.POST.get(key)) BankBalance.set(b.pk, value) bank_sum += BankBalance.get_price(b.pk) for c in cc: key = "credit-" + str(c.pk) if key in request.POST: value = int(request.POST.get(key)) CreditCheckedDate.set_price(c.pk, value) bank_sum -= CreditCheckedDate.get_price(c.pk) return HttpResponse( json.dumps({ "balance": Data.get_income_sum(written_bank_data) - Data.get_outgo_sum(written_bank_data) - bank_sum }))
def test_get_price_invalid_pk(self): self.assertEqual(BankBalance.get_price(10000), 0)
def test_get_price(self): self.assertEqual(BankBalance.get_price(1), 40000) self.assertEqual(BankBalance.get_price(2), 20000)