Skip to content

Commit

Permalink
Amount decimal field (#81)
Browse files Browse the repository at this point in the history
* Round to two decimal places

* Expand tests
  • Loading branch information
oleksiyVeretiuk authored and Andrew Leitsius committed Sep 10, 2018
1 parent 6afc722 commit 85c6d91
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
6 changes: 6 additions & 0 deletions openregistry/lots/loki/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import os
from copy import deepcopy
from decimal import Decimal, ROUND_HALF_UP
from uuid import uuid4

from openregistry.lots.loki.utils import get_now
Expand All @@ -27,6 +28,11 @@
test_loki_lot_data['sandboxParameters'] = 'quick, accelerator={}'.format(DEFAULT_ACCELERATION)


def round_to_two_decimal_places(value):
prec = Decimal('0.01')
return float(Decimal(str(value)).quantize(prec, ROUND_HALF_UP).normalize())


def add_decisions(self, lot):
asset_decision = {
'decisionDate': get_now().isoformat(),
Expand Down
40 changes: 31 additions & 9 deletions openregistry/lots/loki/tests/blanks/auction_blanks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
add_decisions,
add_auctions,
add_lot_decision,
round_to_two_decimal_places,
)


Expand Down Expand Up @@ -163,21 +164,33 @@ def patch_english_auction(self):

# Test second sellout.english(half values)
self.assertEqual(second_english['procurementMethodType'], 'sellout.english')
self.assertEqual(second_english['value']['amount'], round(english['value']['amount'] / 2, 2))
self.assertEqual(second_english['value']['amount'], round_to_two_decimal_places(english['value']['amount'] / 2))
self.assertEqual(second_english['registrationFee']['amount'], english['registrationFee']['amount'])
self.assertEqual(second_english['minimalStep']['amount'], english['minimalStep']['amount'] / 2)
self.assertEqual(second_english['guarantee']['amount'], round(english['guarantee']['amount'] / 2, 2))
self.assertEqual(
second_english['minimalStep']['amount'],
round_to_two_decimal_places(float(english['minimalStep']['amount']) / 2)
)
self.assertEqual(
second_english['guarantee']['amount'],
round_to_two_decimal_places(english['guarantee']['amount'] / 2)
)
self.assertEqual(second_english['bankAccount'], english['bankAccount'])
self.assertEqual(second_english['registrationFee'], english['registrationFee'])
self.assertEqual(second_english['auctionParameters']['type'], 'english')
self.assertNotIn('dutchSteps', second_english['auctionParameters'])

# Test second sellout.insider(half values)
self.assertEqual(insider['procurementMethodType'], 'sellout.insider')
self.assertEqual(insider['value']['amount'], round(english['value']['amount'] / 2, 2))
self.assertEqual(
insider['value']['amount'],
round_to_two_decimal_places(english['value']['amount'] / 2)
)
self.assertEqual(insider['registrationFee']['amount'], english['registrationFee']['amount'])
self.assertEqual(insider['minimalStep']['amount'], 0)
self.assertEqual(insider['guarantee']['amount'], round(english['guarantee']['amount'] / 2, 2))
self.assertEqual(
insider['guarantee']['amount'],
round_to_two_decimal_places(english['guarantee']['amount'] / 2)
)
self.assertEqual(insider['bankAccount'], english['bankAccount'])
self.assertEqual(insider['registrationFee'], english['registrationFee'])
self.assertEqual(insider['auctionParameters']['type'], 'insider')
Expand Down Expand Up @@ -213,19 +226,28 @@ def patch_english_auction(self):

# Test second sellout.english(half values)
self.assertEqual(second_english['procurementMethodType'], 'sellout.english')
self.assertEqual(second_english['value']['amount'], round(english['value']['amount'] / 2, 2))
self.assertEqual(
second_english['value']['amount'],
round_to_two_decimal_places(english['value']['amount'] / 2)
)
self.assertEqual(second_english['registrationFee']['amount'], english['registrationFee']['amount'])
self.assertEqual(second_english['minimalStep']['amount'], english['minimalStep']['amount'] / 2)
self.assertEqual(second_english['guarantee']['amount'], round(english['guarantee']['amount'] / 2, 2))
self.assertEqual(
second_english['guarantee']['amount'],
round_to_two_decimal_places(english['guarantee']['amount'] / 2)
)
self.assertEqual(second_english['auctionParameters']['type'], 'english')
self.assertNotIn('dutchSteps', second_english['auctionParameters'])

# Test second sellout.insider(half values)
self.assertEqual(insider['procurementMethodType'], 'sellout.insider')
self.assertEqual(insider['value']['amount'], round(english['value']['amount'] / 2, 2))
self.assertEqual(insider['value']['amount'], round_to_two_decimal_places(english['value']['amount'] / 2))
self.assertEqual(insider['registrationFee']['amount'], english['registrationFee']['amount'])
self.assertEqual(insider['minimalStep']['amount'], 0)
self.assertEqual(insider['guarantee']['amount'], round(english['guarantee']['amount'] / 2, 2))
self.assertEqual(
insider['guarantee']['amount'],
round_to_two_decimal_places(english['guarantee']['amount'] / 2)
)
self.assertEqual(insider['auctionParameters']['type'], 'insider')
self.assertEqual(insider['auctionParameters']['dutchSteps'], DEFAULT_DUTCH_STEPS)

Expand Down
4 changes: 3 additions & 1 deletion openregistry/lots/loki/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from openregistry.lots.core.utils import get_now, context_unpack, LOGGER
from decimal import Decimal, ROUND_HALF_UP


def check_status(request):
Expand Down Expand Up @@ -83,6 +84,7 @@ def process_caravan_contract_report_result(request):


def update_auctions(lot):
prec = Decimal('0.01')
auctions = sorted(lot.auctions, key=lambda a: a.tenderAttempts)
english = auctions[0]
second_english = auctions[1]
Expand All @@ -102,7 +104,7 @@ def update_auctions(lot):
else:
auction[key]['amount'] = (
0 if key == 'minimalStep' and auction.procurementMethodType == 'sellout.insider'
else round(english[key]['amount'] / 2, 2)
else (english[key]['amount'] / 2).quantize(prec, ROUND_HALF_UP)
)

insider.tenderingDuration = second_english.tenderingDuration

0 comments on commit 85c6d91

Please sign in to comment.