"""Test suite for lesson 12."""importiofromcontextlibimportredirect_stdoutimportunittestfromunittest.mockimportpatchfromlessons.lesson_12.mainimport(colors,pins,pwms,calc_pwm,pwms_off,initial_setup,led_on,get_color,)
[docs]classLesson12TestCase(unittest.TestCase):""" Test case for lesson 12. """
[docs]deftest_colors_data_type(self):""" colors data type should be dictionary. :return: """self.assertIsInstance(colors,dict)
[docs]deftest_pins_data_type(self):""" pins data type should be dictionary. :return: """self.assertIsInstance(pins,dict)
[docs]deftest_pwms_data_type(self):""" pwms data type should be tuple. :return: """self.assertIsInstance(pwms,tuple)
[docs]deftest_calc_pwm_0(self):""" Pass 0 as color_value. Result should be 0. :return: """color_value=0self.assertEqual(calc_pwm(color_value),0)
[docs]deftest_calc_pwm_255(self):""" Pass 255 as color_value. Result should be 65550. :return: """color_value=255self.assertEqual(calc_pwm(color_value),65550)
[docs]deftest_calc_pwm_165(self):""" Pass 165 as color_value. Result should be 42414. :return: """color_value=165self.assertEqual(calc_pwm(color_value),42414)
[docs]deftest_pwms_off_returns_none(self):""" pwms_off should return none. :return: """self.assertIsNone(pwms_off())
[docs]deftest_initial_setup_returns_none(self):""" initial_setup should return none. :return: """self.assertIsNone(initial_setup())
[docs]deftest_led_on_print_debug_red(self):""" Test that led_on prints proper debug line for red color. Source: https://stackoverflow.com/questions/69036159/how-to-write-python-unit-test-for-the-print-statement :return: """color:str='red'# color RGB values 255, 0, 0txt:str=f"\nDEBUG -> R: {65550}, G: {0}, B: {0}\n"f=io.StringIO()withredirect_stdout(f):led_on(color)self.assertTrue(txtinf.getvalue())
[docs]deftest_led_on_print_debug_white(self):""" Test that led_on prints proper debug line for white color. Source: https://stackoverflow.com/questions/69036159/how-to-write-python-unit-test-for-the-print-statement :return: """color:str='white'# color RGB values 255, 255, 255txt:str=f"\nDEBUG -> R: {65550}, G: {65550}, B: {65550}\n"f=io.StringIO()withredirect_stdout(f):led_on(color)self.assertTrue(txtinf.getvalue())
[docs]@patch('builtins.input',return_value="eXiT")deftest_get_color_exit_mixed(self,mock_input):# pylint: disable=W0613""" Verify that get_color returns exit on eXiT as user input :return: """result=get_color()self.assertTrue(result,'exit')
[docs]@patch('builtins.input',return_value="rEd")deftest_get_color_red_mixed(self,mock_input):# pylint: disable=W0613""" Verify that get_color returns red on rEd as user input :return: """result=get_color()self.assertTrue(result,'red')
[docs]@patch('builtins.input',side_effect=["sergtry",'wHitE'])deftest_get_color_invalid_input(self,mock_input):# pylint: disable=W0613""" Verify that get_color returns error message on invalid input and white on wHitE as user input. Source: https://andressa.dev/2019-07-20-using-pach-to-test-inputs/ :return: """txt:str=("\nPlease choose your color only from the listed options ""or type 'exit' to stop the execution.")f=io.StringIO()withredirect_stdout(f):result=get_color()self.assertTrue(txtinf.getvalue())self.assertTrue(result,'white')