Skip to content

Commit

Permalink
Merge pull request #7 from solidarium/insurance
Browse files Browse the repository at this point in the history
Insurance
  • Loading branch information
allisson committed Jul 30, 2016
2 parents 621b510 + 4479b7b commit e646cdd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions correios/models/posting.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
MIN_CYLINDER_LENGTH, MAX_CYLINDER_LENGTH = 18, 105 # cm
MIN_SIZE, MAX_SIZE = 29, 200 # cm
MAX_CYLINDER_SIZE = 28
INSURANCE_VALUE_THRESHOLD = 50 # R$


class EventStatus:
Expand Down Expand Up @@ -204,6 +205,15 @@ def calculate_posting_weight(cls, weight, volumetric_weight):
return weight
return math.ceil(max(volumetric_weight, weight))


@classmethod
def calculate_insurance(cls, per_unit_value, quantity=1):
value = 0
if per_unit_value > INSURANCE_VALUE_THRESHOLD:
value = float(per_unit_value - INSURANCE_VALUE_THRESHOLD) * 0.007

return Decimal(value * quantity).quantize(Decimal('0.00'))

@classmethod
def validate(cls, package_type: int, width: int = 0, height: int = 0, length: int = 0, diameter: int = 0,
service: Optional[Service] = None, weight: int = 0):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_posting_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.


from decimal import Decimal
import os

import pytest
Expand Down Expand Up @@ -251,3 +252,22 @@ def test_fail_add_same_shipping_label_twice_in_posting_list(shipping_label):

with pytest.raises(PostingListError):
posting_list.add_shipping_label(shipping_label)


def test_calculate_insurance_when_not_applicable():
value = Package.calculate_insurance(per_unit_value=50, quantity=2)
assert value == Decimal(0)

value = Package.calculate_insurance(per_unit_value=Decimal(10))
assert value == Decimal(0)


def test_calculate_insurance():
value = Package.calculate_insurance(per_unit_value=193)
assert value == Decimal(1)

value = Package.calculate_insurance(per_unit_value=Decimal(193), quantity=2)
assert value == Decimal(2)

value = Package.calculate_insurance(per_unit_value=Decimal(500), quantity=2)
assert value == Decimal('6.30')

0 comments on commit e646cdd

Please sign in to comment.