def test_save_png_base64_to_file(self): with Log_To_String(logger_png) as log_to_string: assert save_png_base64_to_file(png_data={}) is None assert log_to_string.contents( ) == 'Png data was not a string: {}\n' with Log_To_String(logger_png) as log_to_string: assert save_png_base64_to_file(png_data="aaaaa_bbbbbb") is None print() print(log_to_string.contents()) assert log_to_string.contents( ) == 'png save error: Incorrect padding\naaaaa_bbbbbb\n'
def test_save_png_bytes_to_file(self): with Log_To_String(logger_png) as log_to_string: assert file_not_exists(self.path_temp_png) png_file = save_png_bytes_to_file(bytes=TEST_PNG_BYTES, png_file=self.path_temp_png) assert png_file == self.path_temp_png assert file_exists(self.path_temp_png) assert log_to_string.contents( ) == f'Png data with size 148 saved to {png_file}\n' with Log_To_String(logger_png) as log_to_string: png_file = save_png_bytes_to_file(bytes=TEST_PNG_BYTES) assert file_exists(png_file) assert log_to_string.contents( ) == f'Png data with size 148 saved to {png_file}\n'
def test_dumps__bad_object(self): bad_obj = {"date": datetime.now()} expected_message = "TypeError: Object of type datetime is not JSON serializable" with Log_To_String(logger_json) as log_to_string: assert json_dumps(bad_obj) == None assert expected_message in log_to_string.contents()
def test__logging_an_exception(self): with Log_To_String(self.logger) as log_to_string: try: print(10 / 0) except: self.logger.exception('this should not work') assert 'this should not work' in log_to_string.contents() assert 'Traceback (most recent call last):' in log_to_string.contents( ) assert 'ZeroDivisionError: division by zero' in log_to_string.contents( )
def test_json_loads__bad_json(self): bad_json = "{ bad : json }" expected_message = 'json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)\n' with Log_To_String(logger_json) as log_to_string: assert json_loads(bad_json) == {} assert expected_message in log_to_string.contents()
def test_set_level_warning(self): with Log_To_String(self.logger) as log_to_string: log_to_string.set_level_warning() self.add_test_log_entires() assert log_to_string.contents( ) == "critical message\nerror message\nwarning message\n"
def test__enter____leave__(self): with Log_To_String(self.logger) as log_to_string: self.add_test_log_entires() assert log_to_string.contents( ) == "critical message\nerror message\nwarning message\n" self.logger.exception("an exception")