예제 #1
0
    def test_get_weight_of_box_package(self):
        weight0 = 0.5
        weight1 = 1.5

        package = BoxPackage()
        package.add_item(1.0, 2.0, 3.0, weight0)
        package.add_item(2.0, 3.0, 5.0, weight1)

        self.assertEqual(weight0 + weight1, package.get_weight())
예제 #2
0
    def test_get_dimensions_for_single_item_box_package_with_dimensions_over_the_minimum(
            self):
        height = 15.0
        width = 20.0
        depth = 25.0

        package = BoxPackage()
        package.add_item(height, width, depth, 0.3)

        dimensions = package.get_dimensions()
        self.assertTupleEqual(dimensions, (height, width, depth))
예제 #3
0
    def test_get_dimensions_for_single_item_box_package_with_minimum_dimensions(
            self):
        height = 1.0
        width = 2.0
        depth = 3.0

        package = BoxPackage()
        package.add_item(height, width, depth, 0.3)

        dimensions = package.get_dimensions()
        self.assertTupleEqual(dimensions,
                              (BoxPackage.MIN_HEIGHT, BoxPackage.MIN_WIDTH,
                               BoxPackage.MIN_DEPTH))
예제 #4
0
    def test_api_format_for_valid_box_package(self):
        height = 15.0
        width = 20.0
        depth = 35.0
        weight = 1.2

        package = BoxPackage()
        package.add_item(height, width, depth, weight)
        expected = {
            'nCdFormato': Package.FORMAT_BOX,
            'nVlAltura': height,
            'nVlLargura': width,
            'nVlComprimento': depth,
            'nVlPeso': weight
        }
        self.assertDictEqual(package.api_format(), expected)
예제 #5
0
    def test_get_dimensions_for_multiple_items_box_package_with_dimensions_over_the_minimum(
            self):
        height0 = 10
        width0 = 12
        depth0 = 25
        weight0 = 0.3

        height1 = 6
        width1 = 14
        depth1 = 20
        weight1 = 1.0

        package = BoxPackage()
        package.add_item(height0, width0, depth0, weight0)
        package.add_item(height1, width1, depth1, weight1)

        dimensions = package.get_dimensions()
        self.assertTupleEqual(dimensions, (height0 + height1, width1, depth0))
예제 #6
0
    def test_get_dimensions_for_multiple_items_box_package_with_minimum_dimensions(
            self):
        height0 = 0.5
        width0 = 0.5
        depth0 = 0.5
        weight0 = 0.3

        height1 = 0.5
        width1 = 0.8
        depth1 = 0.6
        weight1 = 1.0

        package = BoxPackage()
        package.add_item(height0, width0, depth0, weight0)
        package.add_item(height1, width1, depth1, weight1)

        dimensions = package.get_dimensions()
        self.assertTupleEqual(dimensions,
                              (BoxPackage.MIN_HEIGHT, BoxPackage.MIN_WIDTH,
                               BoxPackage.MIN_DEPTH))
예제 #7
0
    def test_add_single_item_to_box_package(self):
        height = 1.0
        width = 2.0
        depth = 3.0
        weight = 0.3

        package = BoxPackage()
        package.add_item(height, width, depth, weight)

        self.assertTrue(package.has_items())
        self.assertEqual(height, package.get_items()[0].height)
        self.assertEqual(width, package.get_items()[0].width)
        self.assertEqual(depth, package.get_items()[0].depth)
        self.assertEqual(weight, package.get_items()[0].weight)
예제 #8
0
    def test_add_multiple_items_to_box_package(self):
        height0 = 1.0
        width0 = 2.0
        depth0 = 3.0
        weight0 = 0.3

        height1 = 2.0
        width1 = 3.0
        depth1 = 4.0
        weight1 = 1.0

        package = BoxPackage()
        package.add_item(height0, width0, depth0, weight0)
        package.add_item(height1, width1, depth1, weight1)

        self.assertTrue(package.has_items())

        self.assertEqual(height0, package.get_items()[0].height)
        self.assertEqual(width0, package.get_items()[0].width)
        self.assertEqual(depth0, package.get_items()[0].depth)
        self.assertEqual(weight0, package.get_items()[0].weight)

        self.assertEqual(height1, package.get_items()[1].height)
        self.assertEqual(width1, package.get_items()[1].width)
        self.assertEqual(depth1, package.get_items()[1].depth)
        self.assertEqual(weight1, package.get_items()[1].weight)
예제 #9
0
 def test_create_box_package(self):
     package = BoxPackage()
     self.assertEqual(Package.FORMAT_BOX, package.get_format())
예제 #10
0
 def test_api_format_for_invalid_box_package(self):
     package = BoxPackage()
     package.add_item(100.0, 100.0, 100.0, 5.0)
     with self.assertRaises(Exception):
         package.api_format()
예제 #11
0
 def test_check_for_valid_box_package_with_exceeded_volume(self):
     package = BoxPackage()
     package.add_item(100.0, 100.0, 100.0, 5.0)
     self.assertFalse(package.is_valid())
예제 #12
0
 def test_check_for_valid_box_package_with_exceeded_dimensions(self):
     package = BoxPackage()
     package.add_item(BoxPackage.MAX_HEIGHT + 1.0,
                      BoxPackage.MAX_WIDTH + 1.0,
                      BoxPackage.MAX_DEPTH + 1.0, 2.0)
     self.assertFalse(package.is_valid())
예제 #13
0
 def test_check_for_valid_box_package_with_valid_dimensions(self):
     package = BoxPackage()
     package.add_item(15.0, 20.0, 35.0, 1.2)
     self.assertTrue(package.is_valid())
예제 #14
0
def options():
    # getting current request body
    logger.info("Options got requested with the following body")
    logger.info(request.get_data(as_text=True))
    req = request.get_json()

    # initializing correios client
    correios = Correios()
    available_services = [
        config.OPTION_PAC_SERVICE, config.OPTION_SEDEX_SERVICE
    ]

    # getting cart specs for shipping costs calculation
    origin = req.get('origin').get('postal_code')
    destination = req.get('destination').get('postal_code')
    items = req.get('items')

    # getting base shipping costs
    # creating a correios package
    package = reduce(item_to_package_item, items, BoxPackage())
    logger.info('CORREIOS PACKAGE')
    logger.info(package.api_format())

    # requesting rates from webservice
    rates = correios.get_shipping_rates(origin=origin,
                                        destination=destination,
                                        package=package,
                                        services=available_services)

    # checking for errors
    if rates.has_errors():
        # logging errors and returning result early
        for service in rates.services:
            code = service.code
            error = service.error_code
            message = service.error_message
            logger.warn(
                'The service {} returned the error {} with message: {}'.format(
                    code, error, message))
        return abort(400)

    # checking for free shipping items in cart
    free_shipping_items_qty = reduce(
        lambda c, i: c + 1 if i.get('free_shipping') else 0, items, 0)
    free_shipping_cart = free_shipping_items_qty == len(items)

    if free_shipping_items_qty > 0 and not free_shipping_cart:
        # performing another shipping costs calculation in order to get the consumer prices
        non_free_shipping_items = [
            item for item in items if item.get('free_shipping') is not True
        ]
        non_free_shipping_package = reduce(item_to_package_item,
                                           non_free_shipping_items,
                                           BoxPackage())

        # requesting rates from webservice
        non_free_shipping_rates = correios.get_shipping_rates(
            origin=origin,
            destination=destination,
            package=non_free_shipping_package,
            services=available_services)

        if non_free_shipping_rates.has_errors():
            for service in non_free_shipping_rates.services:
                code = service.code
                error = service.error_code
                message = service.error_message
                logger.warn(
                    'The service {} returned the error {} with message: {}'.
                    format(code, error, message))
            non_free_shipping_rates = rates
    else:
        non_free_shipping_rates = rates

    # parsing service rates to the shipping option format
    options = list(
        map(lambda s: rate_to_shipping_option(s, free_shipping_cart),
            zip(rates.services, non_free_shipping_rates.services)))

    return json.jsonify({'rates': options})