def test_bmi_bar_chart_with_correct_data_matches_reference_chart(self):
        # Arrange
        file_name = 'testdata\\test_output.txt'
        chart_type = "bmi"
        expected_image_file = 'reference_charts\\bar_chart_bmi.png'
        actual_image_file = 'created_charts\\bar_chart.png'

        file_contents = []
        with open(file_name, "r") as file:
            for line in file:
                a_line = line.rstrip()
                file_contents.append(a_line)
        file.close()

        calc_data = CalcData()
        calc_data.calculate(file_contents, chart_type)

        # Act
        calc_data.bar_chart(chart_type)

        # Assert
        if filecmp.cmp(expected_image_file, actual_image_file, shallow=False):
            chart_images_match = True
        else:
            chart_images_match = False

        self.assertTrue(chart_images_match)
    def test_gender_pie_chart_with_bad_data_does_not_match_reference_chart(
            self):
        # Arrange
        file_name = 'testdata\\test_output_all_male_all_500_sales.txt'
        chart_type = "sales"
        expected_image_file = 'reference_charts\\pie_chart_sales.png'
        actual_image_file = 'created_charts\\pie_chart.png'

        file_contents = []
        with open(file_name, "r") as file:
            for line in file:
                a_line = line.rstrip()
                file_contents.append(a_line)
        file.close()

        calc_data = CalcData()
        calc_data.calculate(file_contents, chart_type)

        # Act
        calc_data.pie_chart(chart_type)

        # Assert
        if filecmp.cmp(expected_image_file, actual_image_file, shallow=False):
            chart_images_match = True
        else:
            chart_images_match = False

        self.assertFalse(chart_images_match)
    def test_line_chart_age_and_salary_gets_correct_data(self):
        # Arrange
        result = False
        file_name = 'testdata\\test_output.txt'
        chart_type = "line"
        expected_age_list = [
            '21', '45', '21', '21', '45', '45', '21', '21', '21', '45', '46',
            '46', '46', '20', '20'
        ]
        expected_salary_list = [
            '12', '725', '12', '12', '725', '725', '12', '12', '12', '75',
            '725', '725', '725', '12', '12'
        ]
        expected_title = 'Salary Vs Age'
        expected_y_label = "Salary"
        expected_x_label = "Age of Staff"
        expected_grid = True

        file_contents = []
        with open(file_name, "r") as file:
            for line in file:
                a_line = line.rstrip()
                file_contents.append(a_line)
        file.close()

        calc_data = CalcData()
        calc_data.calculate(file_contents, chart_type)

        # Act
        calc_data.line_chart()

        # Assert
        actual_age_list = ChartLine.get_data(ChartLine)[0]
        actual_salary_list = ChartLine.get_data(ChartLine)[1]
        actual_title = ChartLine.get_title(ChartLine)
        actual_y_label = ChartLine.get_y_label(ChartLine)
        actual_x_label = ChartLine.get_x_label(ChartLine)
        actual_grid = ChartLine.get_grid(ChartLine)

        if actual_age_list == expected_age_list:
            if actual_salary_list == expected_salary_list:
                if actual_title == expected_title:
                    if actual_x_label == expected_x_label:
                        if actual_y_label == expected_y_label:
                            if actual_grid == expected_grid:
                                result = True

        self.assertTrue(result)
Example #4
0
    def get_data(self, choice):
        # choose file to get data from
        file_name = input("Please enter the filename to read data from >>> ")
        # check file exists
        try:
            file_contents = self._load_file_data(file_name)
            # create instance of CalcData to calc data for particular chart
            i = CalcData()

            if file_contents:  # if contents isn't empty
                if i.is_valid(file_contents):  # as long as contents are valid
                    i.calculate(file_contents,
                                choice)  # calculate the chart data
                    return i  # return i so chart can be called
                else:
                    print(Err.get_error_message(210))
        except FileNotFoundError:
            print(Err.ErrorHandler.get_error_message(201))
Example #5
0
 def test_calculate_false(self):
     test_name = "Calc Chart Data Test #34"
     data_to_test = "test"
     class_to_test = CalcData()
     expected_result = None
     # Action
     result = class_to_test.calculate(data_to_test, "line")
     # Assert
     try:
         self.assertTrue(result == expected_result)
     except AssertionError:
         print("{} Failed - Should be {}, but was {}.".format(
             test_name, expected_result, result))
     else:
         print("{} Passed".format(test_name))
Example #6
0
 def test_calculate_bmi(self):
     test_name = "Calc Chart Data Test #39"
     data_to_test = ['', 'A001,gender F,age 21,sales 001,bmi Normal,salary 12,birthday 01/01/1996,valid 1,',\
     'Q001,gender M,age 45,sales 999,bmi Underweight,salary 725,birthday 31/12/1971,valid 1,',\
      'A002,gender F,age 21,sales 001,bmi Normal,salary 12,birthday 01/01/1996,valid 1,']
     class_to_test = CalcData()
     expected_result = None
     # Action
     result = class_to_test.calculate(data_to_test, 'bmi')
     # Assert
     try:
         self.assertTrue(result == expected_result)
     except AssertionError:
         print("{} Failed - Should be {}, but was {}.".format(
             test_name, expected_result, result))
     else:
         print("{} Passed".format(test_name))