示例#1
0
def random_geolocations():
    longitude_1 = random.uniform(-180.0, 180.0)
    latitude_1 = random.uniform(-90.0, 90.0)
    longitude_2 = random.uniform(-180.0, 180.0)
    latitude_2 = random.uniform(-90.0, 90.0)
    return GeoLocation(longitude_1,
                       latitude_1), GeoLocation(longitude_2, latitude_2)
示例#2
0
 def all() -> Generator:
     with open(DATA_STORE_PATH, 'r') as file:
         for line in file.readlines():
             json_data = json.loads(line)
             geo_location = GeoLocation(float(json_data['longitude']),
                                        float(json_data['latitude']))
             customer_obj = Customer(json_data['user_id'],
                                     json_data['name'], geo_location)
             yield customer_obj
示例#3
0
    def invite_customers(longitude: float, latitude: float,
                         max_customer_distance: float) -> dict:
        """
        Function to read from the file, and then calculate the the customer which are eligible

        :param longitude: Longitude of the office
        :param latitude: Latitude of the office
        :param max_customer_distance: Max distance under-which customers should be invited.
        :return: A dictionary with id as key - of customers who are eligible for invitation
        :rtype: dict
        """
        if longitude is None:
            raise ValueError('Longitude cannot be blank.')

        if latitude is None:
            raise ValueError('Latitude cannot be blank.')

        if not isinstance(longitude, float) and not isinstance(longitude, int):
            raise ValueError('Longitude must be a numerical value.')

        if not isinstance(latitude, float) and not isinstance(latitude, int):
            raise ValueError('Latitude must be a numerical value.')

        if not isinstance(max_customer_distance, float) and not isinstance(
                max_customer_distance, int):
            raise ValueError(
                'Max Customer Distance must be a numerical value.')

        if max_customer_distance < 0:
            raise ValueError('Max Customer Distance cannot be less than 0.')

        source = GeoLocation(longitude, latitude)
        # Take care of duplicate IDs
        result = defaultdict(list)
        for customer in CustomerDb.all():
            if is_under_distance(source, customer.geo_location,
                                 max_customer_distance):
                result[customer.user_id].append(customer)
        return result
示例#4
0
 def test_invalid_customer_name(self, customer_name):
     random_id = random.randint(500, 999)
     with pytest.raises(ValueError):
         Customer(random_id, customer_name,
                  GeoLocation(-9.742744, 51.999447))
示例#5
0
 def test_invalid_customer_id(self, customer_id, random_name):
     with pytest.raises(ValueError):
         Customer(customer_id, random_name,
                  GeoLocation(-9.742744, 51.999447))
示例#6
0
class TestCustomer:
    """
        Unit tests for the Customer model.
    """
    @pytest.mark.parametrize(
        'user_id, name, geo_location',
        [(901, 'Ronaldo', GeoLocation(-9.742744, 51.999447)),
         (178, 'Roger Waters', GeoLocation(-7.11167, 53.74452))])
    def test_valid_customer(self, user_id, name, geo_location):
        Customer(user_id, name, geo_location)

    @pytest.mark.parametrize('customer_obj, user_id', [
        (Customer(901, 'Ronaldo', GeoLocation(-9.742744, 51.999447)), 901),
        (Customer(178, 'Roger Waters', GeoLocation(-7.11167, 53.74452)), 178)
    ])
    def test_customer_user_id(self, customer_obj, user_id):
        assert customer_obj.user_id == user_id

    @pytest.mark.parametrize(
        'customer_obj, name',
        [(Customer(901, 'Ronaldo', GeoLocation(-9.742744,
                                               51.999447)), 'Ronaldo'),
         (Customer(178, 'Roger Waters', GeoLocation(
             -7.11167, 53.74452)), 'Roger Waters')])
    def test_customer_name(self, customer_obj, name):
        assert customer_obj.name == name

    @pytest.mark.parametrize(
        'customer_obj, geolocation',
        [(Customer(901, 'Ronaldo', GeoLocation(
            -9.742744, 51.999447)), GeoLocation(-9.742744, 51.999447)),
         (Customer(178, 'Roger Waters', GeoLocation(
             -7.11167, 53.74452)), GeoLocation(-7.11167, 53.74452)),
         (Customer(903, 'Jim Waters', GeoLocation(
             -72.11167, 44.74452)), GeoLocation(-72.11167, 44.74452))])
    def test_customer_geo_location(self, customer_obj, geolocation):
        assert isinstance(customer_obj.geo_location, GeoLocation)
        assert customer_obj.geo_location == geolocation

    @pytest.mark.parametrize('customer_id', [
        None, 'abc', 'random-string', '', pytest, range, (), (5, 9, 10), {},
        {90, 43, 901}, dict, pytest, [56, 901], [{
            'abc': 1,
            'efg': 2,
            'lmo': 3
        }], {
            '1': 903,
            '2': ['xyz']
        }
    ])
    def test_invalid_customer_id(self, customer_id, random_name):
        with pytest.raises(ValueError):
            Customer(customer_id, random_name,
                     GeoLocation(-9.742744, 51.999447))

    @pytest.mark.parametrize('customer_name', [
        None, 123, 8.001, '', (),
        (5, 9, 10), {}, {90, 43, 901}, range, dict, float, pytest, [56, 901],
        [{
            'abc': 1,
            'efg': 2,
            'lmo': 3
        }], {
            '1': 903,
            '2': ['xyz']
        }
    ])
    def test_invalid_customer_name(self, customer_name):
        random_id = random.randint(500, 999)
        with pytest.raises(ValueError):
            Customer(random_id, customer_name,
                     GeoLocation(-9.742744, 51.999447))

    @pytest.mark.parametrize('geolocation', [
        None, 123, 8.001, '', (),
        (5, 9, 10), {}, {90, 43, 901}, range, dict, float, pytest,
        [{
            'abc': 1,
            'efg': 2,
            'lmo': 3
        }], [56, 901], {
            '1': 903,
            '2': ['xyz']
        }
    ])
    def test_invalid_customer_geolocation(self, geolocation, random_name):
        random_id = random.randint(500, 999)
        with pytest.raises(ValueError):
            Customer(random_id, random_name, geolocation)
示例#7
0
 def test_invalid_latitude(self, latitude, random_longitude):
     with pytest.raises(ValueError):
         GeoLocation(random_longitude, latitude)
示例#8
0
 def test_none_langitude_latitude(self):
     with pytest.raises(ValueError):
         GeoLocation(None, None)
示例#9
0
 def test_none_latitude(self, random_longitude):
     with pytest.raises(ValueError):
         GeoLocation(random_longitude, None)
示例#10
0
 def test_valid_geolocation(self, longitude, latitude):
     assert isinstance(GeoLocation(longitude, latitude), GeoLocation)
示例#11
0
 def test_invalid_location2_calculate_geo_distance(self, location2,
                                                   random_longitude,
                                                   random_latitude):
     with pytest.raises(ValueError):
         location1 = GeoLocation(random_longitude, random_latitude)
         calculate_geo_distance(location1, location2)
示例#12
0
class TestDistanceCalculations:
    """
        Unit tests to test the distance calculation module.
    """
    @pytest.mark.parametrize('input_degrees, output_radians',
                             [(90, 1.570796), (60, 1.04719755), (35, 0.610865),
                              (176.90, 3.087487)])
    def test_degree_to_radians(self, input_degrees, output_radians):
        assert degree_to_radians(input_degrees) == pytest.approx(
            output_radians)

    @pytest.mark.parametrize('geo_distance_1, geo_distance_2, actual_distance',
                             [(GeoLocation(-6.043701, 52.986375),
                               GeoLocation(-10.27699, 51.92893), 310.277),
                              (GeoLocation(35.00875, 61.09623),
                               GeoLocation(-35.97992, -52.33815), 14097.450),
                              (GeoLocation(56.49281, 16.81308),
                               GeoLocation(59.97301, 43.04321), 2938.425)])
    def test_calculate_geo_distance(self, geo_distance_1, geo_distance_2,
                                    actual_distance):
        assert calculate_geo_distance(
            geo_distance_1, geo_distance_2) == pytest.approx(actual_distance)

    @pytest.mark.parametrize(
        'source_geo_location, final_geo_location, limit, is_under_limit_or_not',
        [(GeoLocation(-6.043701, 52.986375), GeoLocation(
            -10.27699, 51.92893), 300, False),
         (GeoLocation(-6.043701, 52.986375), GeoLocation(
             -10.27699, 51.92893), 311.98, True),
         (GeoLocation(-30.34382, -25.56970), GeoLocation(
             -108.48439, 58.83732), 3000, False),
         (GeoLocation(-30.34382, -25.56970), GeoLocation(
             -108.48439, 58.83732), 11784, False),
         (GeoLocation(-30.34382, -25.56970), GeoLocation(
             -108.48439, 58.83732), 11784.759, True)])
    def test_is_under_distance(self, source_geo_location, final_geo_location,
                               limit, is_under_limit_or_not):
        assert is_under_distance(source_geo_location, final_geo_location,
                                 limit) == is_under_limit_or_not

    @pytest.mark.parametrize('invalid_degrees', [
        None, '', {}, [8922, 00], range, {23, 9, 901, 31.3}, 'abcdef', range,
        pytest, (), (90, 9.01), [{
            1: 45,
            90: ['pqr']
        }]
    ])
    def test_invalid_degrees(self, invalid_degrees):
        with pytest.raises(ValueError):
            degree_to_radians(invalid_degrees)

    @pytest.mark.parametrize('invalid_degrees', [
        None, '', {}, [8922, 00], range, GeoLocation, {23, 9, 901, 31.3},
        'abcdef', range, pytest, (), (90, 9.01), [{
            1: 45,
            90: ['pqr']
        }]
    ])
    def test_invalid_degrees_to_radians(self, invalid_degrees):
        with pytest.raises(ValueError):
            degree_to_radians(invalid_degrees)

    @pytest.mark.parametrize('location1', [
        None, '', {}, [8922, 00], range, -123, -190.00, -180.0001, -180.001,
        GeoLocation, {23, 9, 901, 31.3}, 'abcdef', range, pytest, (),
        ('abc', '190', 90.00, -1), (90, 9.01), [{
            1: 45,
            90: ['pqr']
        }]
    ])
    def test_invalid_location1_calculate_geo_distance(self, location1,
                                                      random_longitude,
                                                      random_latitude):
        with pytest.raises(ValueError):
            location2 = GeoLocation(random_longitude, random_latitude)
            calculate_geo_distance(location1, location2)

    @pytest.mark.parametrize('location2', [
        None, '', {}, [8922, 00], range, -123, -190.00, -180.0001, -180.001,
        GeoLocation, {23, 9, 901, 31.3}, 'abcdef', range, pytest, (),
        ('abc', '190', 90.00, -1), (90, 9.01), [{
            1: 45,
            90: ['pqr']
        }]
    ])
    def test_invalid_location2_calculate_geo_distance(self, location2,
                                                      random_longitude,
                                                      random_latitude):
        with pytest.raises(ValueError):
            location1 = GeoLocation(random_longitude, random_latitude)
            calculate_geo_distance(location1, location2)

    @pytest.mark.parametrize('source', [
        None, '', {}, [8922, 00], range, -123, -190.00, -180.0001, -180.001,
        GeoLocation, {23, 9, 901, 31.3}, 'abcdef', range, pytest, (),
        ('abc', '190', 90.00, -1), (90, 9.01), [{
            1: 45,
            90: ['pqr']
        }]
    ])
    def test_invalid_source_is_under_distance(self, source, random_longitude,
                                              random_latitude,
                                              random_distance):
        with pytest.raises(ValueError):
            target = GeoLocation(random_longitude, random_latitude)
            is_under_distance(source, target, random_distance)

    @pytest.mark.parametrize('random_distance', [
        None, '', {}, [8922, 00], range, -0.00000001, -123, -190.00, -180.0001,
        -180.001, GeoLocation, {23, 9, 901, 31.3}, 'abcdef', range, pytest, (),
        ('abc', '190', 90.00, -1), (90, 9.01), [{
            1: 45,
            90: ['pqr']
        }]
    ])
    def test_invalid_limit_is_under_distance(self, random_geolocations,
                                             random_distance):
        with pytest.raises(ValueError):
            source, target = random_geolocations
            is_under_distance(source, target, random_distance)
示例#13
0
 def test_invalid_source_is_under_distance(self, source, random_longitude,
                                           random_latitude,
                                           random_distance):
     with pytest.raises(ValueError):
         target = GeoLocation(random_longitude, random_latitude)
         is_under_distance(source, target, random_distance)