Exemplo n.º 1
0
class TestMain(unittest.TestCase):
    def set_default_values(self):
        self.testMain = Main()
        self.setTestControlDefaults()
        self.testMain.setDistanceControlCustomer()
        self.testMain.raw_data = (
            '{"latitude": "39.7645187", "user_id": 12, "name": "Denver", "longitude": "-104.9951948"}\n'
            +
            '{"latitude": "25.7825453", "user_id": 1, "name": "Miami", "longitude": " -80.2994985"}\n'
            +
            '{"latitude": "41.8339037", "user_id": 2, "name": "Chicago", "longitude": "-87.8720471"}\n'
        )
        self.testMain.DISTANCE_CHECK = 2000

    def setTestControlDefaults(self):
        """
            The test control customer was defined as the test head office since all other varying customers (loaded customers) will be compared to it 
            Loaded with New York's coordinates
         """
        self.controlCustomer = Customer()
        self.controlCustomer.latitude = 40.6976637
        self.controlCustomer.longitude = -74.1197643
        self.controlCustomer.name = "Test Head Office"
        self.controlCustomer.user_id = -1
        self.controlCustomer.calculate()
        self.testMain.controlCustomer = self.controlCustomer

    def test_control_customer_details(self):
        """
        Test to validate Control Customer's details were correctly loaded
        """
        self.set_default_values()
        self.assertEqual(self.testMain.controlCustomer.latitude, 40.6976637)

    def test_control_customer_details_distance(self):
        """
        Test to validate Control Customer's details were correctly loaded into the distance object
        """
        self.set_default_values()
        self.assertEqual(self.testMain.distance.control_customer.latitude,
                         40.6976637)

    def test_customer_data(self):
        """
        Test to validate all raw_data were properly parsed and saved
        """
        self.set_default_values()
        self.testMain.load_and_set_all_sorted_customer_data()
        self.assertEqual(self.testMain.load_data.countCustomerData(), 3)

    def test_customer_data_sorted(self):
        """
        Test to validate all raw_data were properly sorted
        """
        self.set_default_values()
        self.testMain.load_and_set_all_sorted_customer_data()
        self.assertEqual(self.testMain.customer_data[0].user_id, 1)

    def test_DISTANCE_CHECK(self):
        """
        Test to validate all raw_data were properly sorted
        """
        self.set_default_values()
        self.assertEqual(self.testMain.DISTANCE_CHECK, 2000)

    def test_DINNER_LIST(self):
        """
        Test to validate only those customers within 2000Km were invited (Miami and Chicago)
        """
        self.set_default_values()
        self.testMain.load_and_set_all_sorted_customer_data()
        self.testMain.findDinnerList()
        self.assertEqual(len(self.testMain.dinner_list), 2)

    def test_program_output(self):
        """
        Test to validate the program output
        """
        self.set_default_values()
        self.testMain.load_and_set_all_sorted_customer_data()
        self.testMain.findDinnerList()
        self.assertEqual(
            self.testMain.getOutput(),
            "---------LIST OF CUSTOMERS INVITED FOR DINNER--------\n-----Start Customer #1 -------\n User Id: 1\n User Name: Miami\n Latitude: 25.7825453\n Longitude: -80.2994985\n Distance to Head Office: 1754.0Km\n-----End Customer #1-------\n-----Start Customer #2 -------\n User Id: 2\n User Name: Chicago\n Latitude: 41.8339037\n Longitude: -87.8720471\n Distance to Head Office: 1155.09Km\n-----End Customer #2-------\n"
        )
Exemplo n.º 2
0
class TestDistance(unittest.TestCase):
    """
        Test will focus on only attributes consumed by the Distance class
    """
    def set_default_values(self):
        self.controlData = Customer()
        self.controlData.latitude = 28.426846
        self.controlData.user_id = 1000
        self.controlData.name = "Control Customer's Location"
        self.controlData.longitude = 77.088834
        self.controlData.calculate()

        self.varyingData = Customer()
        self.varyingData.latitude = 28.394231
        self.varyingData.user_id = 1001
        self.varyingData.name = "Varying Customer's Location"
        self.varyingData.longitude = 77.050308
        self.varyingData.calculate()

        self.distance = Distance()
        self.distance.control_customer = self.controlData
        self.distance.varying_customer = self.varyingData
        self.distance.calculate_longitude_abs_diff()
        self.distance.calculate_central_angle()
        self.distance.calculate_central_angle_degrees()
        self.distance.calculate_distance_km()
        self.distance.calculate_distance_miles()

    def test_DEGREES_IN_RADIAN(self):
        """
        Test to DEGREES_IN_RADIAN value  
        """
        self.set_default_values()
        self.assertEqual(self.distance.get_DEGREES_IN_RADIAN(), 57.29577951)

    def test_MEAN_EARTH_RADIUS_KM(self):
        """
        Test to MEAN_EARTH_RADIUS value  
        """
        self.set_default_values()
        self.assertEqual(self.distance.get_MEAN_EARTH_RADIUS_KM(), 6371)

    def test_KILOMETRES_IN_MILE(self):
        """
        Test to KILOMETRES_IN_MILE value  
        """
        self.set_default_values()
        self.assertEqual(self.distance.get_KILOMETRES_IN_MILE(), 1.60934)

    def test_varying_customer_lattitude_longitude_validity(self):
        """
        Test to confirm if varying customer_lattitude_longitude_ is valid
        """
        self.set_default_values()
        self.assertEqual(self.distance.varying_customer.valid, True)

    def test_control_customer_latitude_longitude_validity(self):
        """
        Test to confirm if control customer_lattitude_longitude_ is valid
        """
        self.set_default_values()
        self.assertEqual(self.distance.control_customer.valid, True)

    def test_control_customer_latitude_radians(self):
        """
        Test to confirm if control_customer_latitude_radians is 0.496142
        """
        self.set_default_values()
        self.assertEqual(
            float("{:.6f}".format(
                self.distance.control_customer.latitude_radians)), 0.496142)

    def test_control_customer_longitude_radians(self):
        """
        Test to confirm if control_customer_longitude_radians 1.345454
        """
        self.set_default_values()
        self.assertEqual(
            float("{:.6f}".format(
                self.distance.control_customer.longitude_radians)), 1.345454)

    def test_varying_customer_latitude_radians(self):
        """
        Test to will  
        """
        self.set_default_values()
        self.assertEqual(
            float("{:.6f}".format(
                self.distance.varying_customer.latitude_radians)), 0.495573)

    def test_varying_customer_longitude_radians(self):
        """
        Test to will  
        """
        self.set_default_values()
        self.assertEqual(
            float("{:.6f}".format(
                self.distance.varying_customer.longitude_radians)), 1.344782)

    def test_longitude_absolute_difference(self):
        """
        Test to longitudes_abs_diff attribute  
        """
        self.set_default_values()
        self.assertEqual(
            float("{:.6f}".format(self.distance.longitudes_abs_diff)),
            0.000672)

    def test_central_angle_radians(self):
        """
        Test to longitudes_abs_diff attribute  
        """
        self.set_default_values()
        self.assertEqual(
            float("{:.6f}".format(self.distance.central_angle_radians)),
            0.000821)

    def test_central_angle_degrees(self):
        """
        Test to longitudes_abs_diff attribute  
        """
        self.set_default_values()
        self.assertEqual(
            float("{:.6f}".format(self.distance.central_angle_degrees)),
            0.047032)

    def test_distance_in_km(self):
        """
        Test to validate the right distance was gotten 
        """
        self.set_default_values()
        self.assertEqual(
            float("{:.6f}".format(self.distance.distance_kilometres)),
            5.229706)

    def test_distance_in_miles(self):
        """
        Test to validate the right distance was gotten 
        """
        self.set_default_values()
        self.assertEqual(float("{:.2f}".format(self.distance.distance_miles)),
                         3.25)