Ejemplo n.º 1
0
    def test_flat_rate_cost(self):
        IL = self.IL
        t, d = self.t, self.d
        city_a = City.objects.get(name="תל אביב יפו")
        city_b = City.objects.get(name="אור יהודה")
        add_flat_rate_rule(country=IL, from_city=city_a, to_city=city_b, fixed_cost_tariff_1=66, fixed_cost_tariff_2=70)

        expected_type = CostType.FLAT
        logging.info("Testing flat rate prices, with cities: %s , %s" % (city_a.name, city_b.name))

        flat_rate_rules = IL.flat_rate_rules.filter(city1=city_a,city2=city_b)
        if not flat_rate_rules:
            flat_rate_rules = IL.flat_rate_rules.filter(city1=city_b,city2=city_a)

        self.assertTrue(flat_rate_rules, "No flat rate rule found between cities %s, %s" % (city_a.name,city_b.name))
        expected_cost_tariff1 = min([r.fixed_cost for r in flat_rate_rules]) + self.phone_order_price
        expected_cost_tariff2 = max([r.fixed_cost for r in flat_rate_rules]) + self.phone_order_price

        # tariff 1 test
        cost, type = estimate_cost(t, d, IL.code, day=1, time=datetime.time(12, 00), cities=[city_a.id, city_b.id])
        self.assertEqual((cost, type), (expected_cost_tariff1, expected_type), "flat rate tariff1 test failed: %d (expected %d)" % (cost, expected_cost_tariff1))

        cost, type = estimate_cost(t, d, IL.code, day=6, time=datetime.time(16, 59), cities=[city_a.id, city_b.id])
        self.assertEqual((cost, type), (expected_cost_tariff1, expected_type), "flat rate tariff1 test failed: %d (expected %d)" % (cost, expected_cost_tariff1))

        # tariff 2 test
        cost, type = estimate_cost(t, d, IL.code, day=1, time=datetime.time(21, 01), cities=[city_a.id, city_b.id])
        self.assertEqual((cost, type), (expected_cost_tariff2, expected_type), "flat rate tariff2 test failed: %d (expected %d)" % (cost, expected_cost_tariff2))

        cost, type = estimate_cost(t, d, IL.code, day=6, time=datetime.time(17, 01), cities=[city_a.id, city_b.id])
        self.assertEqual((cost, type), (expected_cost_tariff2, expected_type), "flat rate tariff2 test failed: %d (expected %d)" % (cost, expected_cost_tariff2))

        cost, type = estimate_cost(t, d, IL.code, day=7, time=datetime.time(12, 01), cities=[city_a.id, city_b.id])
        self.assertEqual((cost, type), (expected_cost_tariff2, expected_type), "flat rate tariff2 test failed: %d (expected %d)" % (cost, expected_cost_tariff2))
Ejemplo n.º 2
0
    def test_meter_cost(self):
        logging.info("Testing meter cost estimation, with estimated duration: %d & estimated distance: %d" % (self.t, self.d))
        IL = self.IL
        t, d = self.t, self.d
        
        expected_type = CostType.METER
        expected_cost_tariff1 = calculate_tariff(t, d, tariff1_dict) + self.phone_order_price
        expected_cost_tariff2 = calculate_tariff(t, d, tariff2_dict) + self.phone_order_price
        extras = [IsraelExtraCosts.NATBAG_AIRPORT, IsraelExtraCosts.KVISH_6]
        extras_cost = sum([IL.extra_charge_rules.get(rule_name=extra).cost for extra in extras])

        # tariff 1 test
        cost, type = estimate_cost(t, d, IL.code, day=1,time=datetime.time(05, 30, 00))
Ejemplo n.º 3
0
def estimate_ride_cost(request):
    from_lon = request.GET["from_lon"]
    from_lat = request.GET["from_lat"]
    to_lon = request.GET["to_lon"]
    to_lat = request.GET["to_lat"]
    from_city = int(request.GET["from_city"])
    to_city = int(request.GET["to_city"])
    result = calculate_time_and_distance(from_lon, from_lat, to_lon, to_lat)
    estimated_duration, estimated_distance = result[
        "estimated_duration"], result["estimated_distance"]
    cities = [from_city, to_city]
    if not (estimated_distance and estimated_duration):
        return JSONResponse({
            "estimated_cost": "",
            "estimated_duration": "",
            "currency": "",
            "label": _("Ride estimation not available")
        })
    ride_cost, ride_type = estimate_cost(estimated_duration,
                                         estimated_distance,
                                         cities=cities)

    if ride_type == CostType.METER:
        label = _('Estimated ride cost')
        cost = "%d-%d" % (ride_cost, ride_cost *
                          (1 + ESTIMATION_FUZZINESS_FACTOR))
    elif ride_type == CostType.FLAT:
        label = _('Flat rate')
        cost = "%d" % ride_cost

    result = {
        "estimated_cost":
        cost,
        "estimated_duration":
        "%d-%d %s" % (estimated_duration / 60, estimated_duration *
                      (1 + ESTIMATION_FUZZINESS_FACTOR) / 60, _('min')),
        "currency":
        u'₪',  # TODO_WB: change to non hardcoded version
        "label":
        label
    }
    return JSONResponse(result)
Ejemplo n.º 4
0
def estimate_ride_cost(request):
    from_lon = request.GET["from_lon"]
    from_lat = request.GET["from_lat"]
    to_lon = request.GET["to_lon"]
    to_lat = request.GET["to_lat"]
    from_city = int(request.GET["from_city"])
    to_city = int(request.GET["to_city"])
    result = calculate_time_and_distance(from_lon, from_lat, to_lon, to_lat)
    estimated_duration, estimated_distance = result["estimated_duration"], result["estimated_distance"]
    cities = [from_city, to_city]
    if not (estimated_distance and estimated_duration):
        return JSONResponse(
            {
                "estimated_cost": "",
                "estimated_duration": "",
                "currency": "",
                "label": _("Ride estimation not available"),
            }
        )
    ride_cost, ride_type = estimate_cost(estimated_duration, estimated_distance, cities=cities)

    if ride_type == CostType.METER:
        label = _("Estimated ride cost")
        cost = "%d-%d" % (ride_cost, ride_cost * (1 + ESTIMATION_FUZZINESS_FACTOR))
    elif ride_type == CostType.FLAT:
        label = _("Flat rate")
        cost = "%d" % ride_cost

    result = {
        "estimated_cost": cost,
        "estimated_duration": "%d-%d %s"
        % (estimated_duration / 60, estimated_duration * (1 + ESTIMATION_FUZZINESS_FACTOR) / 60, _("min")),
        "currency": u"₪",  # TODO_WB: change to non hardcoded version
        "label": label,
    }
    return JSONResponse(result)
Ejemplo n.º 5
0
    def test_flat_rate_cost(self):
        IL = self.IL
        t, d = self.t, self.d
        city_a = City.objects.get(name="תל אביב יפו")
        city_b = City.objects.get(name="אור יהודה")
        add_flat_rate_rule(country=IL,
                           from_city=city_a,
                           to_city=city_b,
                           fixed_cost_tariff_1=66,
                           fixed_cost_tariff_2=70)

        expected_type = CostType.FLAT
        logging.info("Testing flat rate prices, with cities: %s , %s" %
                     (city_a.name, city_b.name))

        flat_rate_rules = IL.flat_rate_rules.filter(city1=city_a, city2=city_b)
        if not flat_rate_rules:
            flat_rate_rules = IL.flat_rate_rules.filter(city1=city_b,
                                                        city2=city_a)

        self.assertTrue(
            flat_rate_rules, "No flat rate rule found between cities %s, %s" %
            (city_a.name, city_b.name))
        expected_cost_tariff1 = min([r.fixed_cost for r in flat_rate_rules
                                     ]) + self.phone_order_price
        expected_cost_tariff2 = max([r.fixed_cost for r in flat_rate_rules
                                     ]) + self.phone_order_price

        # tariff 1 test
        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=1,
                                   time=datetime.time(12, 00),
                                   cities=[city_a.id, city_b.id])
        self.assertEqual((cost, type), (expected_cost_tariff1, expected_type),
                         "flat rate tariff1 test failed: %d (expected %d)" %
                         (cost, expected_cost_tariff1))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=6,
                                   time=datetime.time(16, 59),
                                   cities=[city_a.id, city_b.id])
        self.assertEqual((cost, type), (expected_cost_tariff1, expected_type),
                         "flat rate tariff1 test failed: %d (expected %d)" %
                         (cost, expected_cost_tariff1))

        # tariff 2 test
        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=1,
                                   time=datetime.time(21, 01),
                                   cities=[city_a.id, city_b.id])
        self.assertEqual((cost, type), (expected_cost_tariff2, expected_type),
                         "flat rate tariff2 test failed: %d (expected %d)" %
                         (cost, expected_cost_tariff2))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=6,
                                   time=datetime.time(17, 01),
                                   cities=[city_a.id, city_b.id])
        self.assertEqual((cost, type), (expected_cost_tariff2, expected_type),
                         "flat rate tariff2 test failed: %d (expected %d)" %
                         (cost, expected_cost_tariff2))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=7,
                                   time=datetime.time(12, 01),
                                   cities=[city_a.id, city_b.id])
        self.assertEqual((cost, type), (expected_cost_tariff2, expected_type),
                         "flat rate tariff2 test failed: %d (expected %d)" %
                         (cost, expected_cost_tariff2))
Ejemplo n.º 6
0
    def test_meter_cost(self):
        logging.info(
            "Testing meter cost estimation, with estimated duration: %d & estimated distance: %d"
            % (self.t, self.d))
        IL = self.IL
        t, d = self.t, self.d

        expected_type = CostType.METER
        expected_cost_tariff1 = calculate_tariff(
            t, d, tariff1_dict) + self.phone_order_price
        expected_cost_tariff2 = calculate_tariff(
            t, d, tariff2_dict) + self.phone_order_price
        extras = [IsraelExtraCosts.NATBAG_AIRPORT, IsraelExtraCosts.KVISH_6]
        extras_cost = sum([
            IL.extra_charge_rules.get(rule_name=extra).cost for extra in extras
        ])

        # tariff 1 test
        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=1,
                                   time=datetime.time(05, 30, 00))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(
            cost,
            expected_cost_tariff1,
            places=2,
            msg="tariff 1 estimation failed: %d (expected %d)" %
            (cost, expected_cost_tariff1))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=1,
                                   time=datetime.time(20, 59, 59))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(
            cost,
            expected_cost_tariff1,
            places=2,
            msg="tariff 1 estimation failed: %d (expected %d)" %
            (cost, expected_cost_tariff1))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=6,
                                   time=datetime.time(16, 59, 59))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(
            cost,
            expected_cost_tariff1,
            places=2,
            msg="Friday estimation failed: %d (expected %d)" %
            (cost, expected_cost_tariff1))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   time=datetime.time(12, 00),
                                   extras=extras)
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(
            cost,
            expected_cost_tariff1 + extras_cost,
            places=2,
            msg="tariff 1 + extras' estimation failed: %d (expected %d)" %
            (cost, expected_cost_tariff1 + extras_cost))

        # tariff2 test
        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=1,
                                   time=datetime.time(21, 00, 00))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(
            cost,
            expected_cost_tariff2,
            places=2,
            msg="tariff 2 estimation failed: %d (expected %d)" %
            (cost, expected_cost_tariff2))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=1,
                                   time=datetime.time(05, 29, 59))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(
            cost,
            expected_cost_tariff2,
            places=2,
            msg="tariff 2 estimation failed: %d (expected %d)" %
            (cost, expected_cost_tariff2))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=6,
                                   time=datetime.time(17, 00, 00))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(
            cost,
            expected_cost_tariff2,
            places=2,
            msg="Friday estimation failed: %d (expected %d)" %
            (cost, expected_cost_tariff2))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=7,
                                   time=datetime.time(23, 59, 59))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(
            cost,
            expected_cost_tariff2,
            places=2,
            msg="Saturday estimation failed: %d (expected %d)" %
            (cost, expected_cost_tariff2))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   time=datetime.time(22, 00),
                                   extras=extras)
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(
            cost,
            expected_cost_tariff2 + extras_cost,
            places=2,
            msg="tariff 2 + extras' estimation failed: %d (expected %d)" %
            (cost, expected_cost_tariff2 + extras_cost))

        # friday
        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=6,
                                   time=datetime.time(01, 00, 00))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(cost,
                               expected_cost_tariff2,
                               places=2,
                               msg="Friday tariff2 failed: %d (expected %d)" %
                               (cost, expected_cost_tariff2))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=6,
                                   time=datetime.time(06, 00, 00))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(cost,
                               expected_cost_tariff1,
                               places=2,
                               msg="Friday tariff1 failed: %d (expected %d)" %
                               (cost, expected_cost_tariff1))

        cost, type = estimate_cost(t,
                                   d,
                                   IL.code,
                                   day=6,
                                   time=datetime.time(17, 01, 00))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(cost,
                               expected_cost_tariff2,
                               places=2,
                               msg="Friday sabbath failed: %d (expected %d)" %
                               (cost, expected_cost_tariff2))
Ejemplo n.º 7
0
        logging.info("Testing meter cost estimation, with estimated duration: %d & estimated distance: %d" % (self.t, self.d))
        IL = self.IL
        t, d = self.t, self.d
        
        expected_type = CostType.METER
        expected_cost_tariff1 = calculate_tariff(t, d, tariff1_dict) + self.phone_order_price
        expected_cost_tariff2 = calculate_tariff(t, d, tariff2_dict) + self.phone_order_price
        extras = [IsraelExtraCosts.NATBAG_AIRPORT, IsraelExtraCosts.KVISH_6]
        extras_cost = sum([IL.extra_charge_rules.get(rule_name=extra).cost for extra in extras])

        # tariff 1 test
        cost, type = estimate_cost(t, d, IL.code, day=1,time=datetime.time(05, 30, 00))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(cost, expected_cost_tariff1, places=2, msg="tariff 1 estimation failed: %d (expected %d)" % (cost, expected_cost_tariff1))

        cost, type = estimate_cost(t, d, IL.code, day=1, time=datetime.time(20, 59, 59))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(cost, expected_cost_tariff1, places=2, msg="tariff 1 estimation failed: %d (expected %d)" % (cost, expected_cost_tariff1))

        cost, type = estimate_cost(t, d, IL.code, day=6, time=datetime.time(16, 59, 59))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(cost, expected_cost_tariff1, places=2, msg="Friday estimation failed: %d (expected %d)" % (cost, expected_cost_tariff1))

        cost, type = estimate_cost(t, d, IL.code, time=datetime.time(12, 00), extras=extras)
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(cost, expected_cost_tariff1 + extras_cost, places=2, msg="tariff 1 + extras' estimation failed: %d (expected %d)" % (cost, expected_cost_tariff1 + extras_cost))

        # tariff2 test
        cost, type = estimate_cost(t, d, IL.code, day=1, time=datetime.time(21, 00, 00))
        self.assertEqual(type, expected_type)
        self.assertAlmostEqual(cost, expected_cost_tariff2, places=2, msg="tariff 2 estimation failed: %d (expected %d)" % (cost, expected_cost_tariff2))