Esempio n. 1
0
    def test_get_google_logger_name_method(self):
        AppLogger.config_dict = None
        self.assertEqual(None, AppLogger.get_google_logger_name())

        AppLogger.config_dict = {
            'handlers': {
                'google_cloud': {
                    'log_name': 'test'
                }
            }
        }
        self.assertEqual('test', AppLogger.get_google_logger_name())

        AppLogger.config_dict = {'handlers': ''}
        self.assertEqual(None, AppLogger.get_google_logger_name())

        AppLogger.config_dict = {'handlers': {'google_cloud': ''}}
        self.assertEqual(None, AppLogger.get_google_logger_name())

        AppLogger.config_dict = {
            'handlers': {
                'google_cloud': {
                    'log_name': 'test2'
                }
            }
        }
        self.assertNotEqual('test', AppLogger.get_google_logger_name())
Esempio n. 2
0
    def test__google_logger_name_exists_method(self):
        AppLogger.config_dict = None
        self.assertEqual(False, AppLogger._google_logger_name_exists())

        AppLogger.config_dict = {
            'handlers': {
                'google_cloud': {
                    'log_name': 'test'
                }
            }
        }
        self.assertEqual(True, AppLogger._google_logger_name_exists())

        AppLogger.config_dict = {'handlers': ''}
        self.assertEqual(False, AppLogger._google_logger_name_exists())

        AppLogger.config_dict = {'handlers': {'google_cloud': ''}}
        self.assertEqual(False, AppLogger._google_logger_name_exists())

        AppLogger.config_dict = {
            'handlers': {
                'google_cloud': {
                    'log_name': 'test2'
                }
            }
        }
        self.assertNotEqual(False, AppLogger._google_logger_name_exists())
Esempio n. 3
0
    def test__stream_name_exists_method(self):
        AppLogger.config_dict = None
        self.assertEqual(False, AppLogger._stream_name_exists())

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'stream_name': 'test'
                }
            }
        }
        self.assertEqual(True, AppLogger._stream_name_exists())

        AppLogger.config_dict = {'handlers': ''}
        self.assertEqual(False, AppLogger._stream_name_exists())

        AppLogger.config_dict = {'handlers': {'watchtower': ''}}
        self.assertEqual(False, AppLogger._stream_name_exists())

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'stream_name': 'test2'
                }
            }
        }
        self.assertNotEqual(False, AppLogger._stream_name_exists())
Esempio n. 4
0
    def test_get_stream_name_method(self):
        AppLogger.config_dict = None
        self.assertEqual(None, AppLogger.get_stream_name())

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'stream_name': 'test'
                }
            }
        }
        self.assertEqual('test', AppLogger.get_stream_name())

        AppLogger.config_dict = {'handlers': ''}
        self.assertEqual(None, AppLogger.get_stream_name())

        AppLogger.config_dict = {'handlers': {'watchtower': ''}}
        self.assertEqual(None, AppLogger.get_stream_name())

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'stream_name': 'test2'
                }
            }
        }
        self.assertNotEqual('test', AppLogger.get_stream_name())
Esempio n. 5
0
    def test_get_log_group_method(self):
        AppLogger.config_dict = None
        self.assertEqual(None, AppLogger.get_log_group())

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'log_group': 'test'
                }
            }
        }
        self.assertEqual('test', AppLogger.get_log_group())

        AppLogger.config_dict = {'handlers': ''}
        self.assertEqual(None, AppLogger.get_log_group())

        AppLogger.config_dict = {'handlers': {'watchtower': ''}}
        self.assertEqual(None, AppLogger.get_log_group())

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'log_group': 'test2'
                }
            }
        }
        self.assertNotEqual('test', AppLogger.get_log_group())
Esempio n. 6
0
    def test__log_group_exists_method(self):
        AppLogger.config_dict = None
        self.assertEqual(False, AppLogger._log_group_exists())

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'log_group': 'test'
                }
            }
        }
        self.assertEqual(True, AppLogger._log_group_exists())

        AppLogger.config_dict = {'handlers': ''}
        self.assertEqual(False, AppLogger._log_group_exists())

        AppLogger.config_dict = {'handlers': {'watchtower': ''}}
        self.assertEqual(False, AppLogger._log_group_exists())

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'log_group': 'test2'
                }
            }
        }
        self.assertNotEqual(False, AppLogger._log_group_exists())
Esempio n. 7
0
    def test_get_methods(self):
        AppLogger.configure_and_get_logger("test_logger")
        AppLogger.set_debug_level()

        temp_dir, file_dir, full_file_name_list = UtilForTesting.file_setup(
            'test_df_3',
            extension="",
            content=
            "col1|col2\ntest1|test2\ntest1|test2\ntest1|test2\ntest1|test2\ntest1|test2",
            count=2)

        response = ApiReponse(
            self.app.post(
                '/load_from_csv/',
                # headers={
                #     'Authorization': 'Bearer ' + JWT.create_access_token(user_name="123", password="******")},
                data={
                    "server_name": "localhost\sqlexpress",
                    "database_name": "master",
                    "schema_name": "dbo",
                    "table_name": "ms_sql_server_proxy_load_from_csv_1",
                    "csv_root_directory": file_dir
                },
                follow_redirects=True))
        self.assertEqual(201, response.status_code)
        for file in full_file_name_list:
            self.assertEqual(True, os.path.exists(f"{file}.done"))

        ms_sql = MsSqlServer(
            "DRIVER={ODBC Driver 13 for SQL Server}; SERVER=localhost\sqlexpress; DATABASE=master; Trusted_Connection=yes;"
        )
        df = ms_sql.read_data_into_dataframe(
            "select count(1) as record_count from [dbo].[ms_sql_server_proxy_load_from_csv_1]"
        )
        self.assertEqual(10, df.iloc[0]['record_count'])
 def main(logger_name):
     config_dict = RunAppBase.get_config_dict()
     AppLogger.configure_and_get_logger(
         logger_name,
         AppLoggerJsonConfigName.default_with_watchtower_format_1(),
         watchtower_log_group=config_dict["APPLICATION_NAME"],
         watchtower_stream_name=config_dict["ENVIRONMENT"])
     if config_dict["DEBUG_MODE"].upper() == "TRUE":
         AppLogger.set_debug_level()
Esempio n. 9
0
    def test_4__configure_logger_method(self):
        AppLogger.logger = logging.getLogger('test_configure_3')
        AppLogger.logger.level = logging.DEBUG
        AppLogger.config_dict = None

        AppLogger._configure_logger(
            AppLoggerJsonConfigName.default_with_watchtower_format_1(),
            'test_log_group', 'test_log_stream')
        AppLogger.set_debug_level()
        AppLogger.logger.debug('This is a test')
Esempio n. 10
0
 def test_cloud_watch_logging_method(self):
     AppLogger.configure_and_get_logger(
         logger_name='test_cloud_watch',
         config_section_name=AppLoggerJsonConfigName.
         default_with_watchtower_format_1(),
         watchtower_log_group='test_log_group',
         watchtower_stream_name='local')
     with LogCapture() as lc:
         AppLogger.logger.info("test_me_aws")
         lc.check(('test_cloud_watch', 'INFO', 'test_me_aws'), )
    def test_2_get_json_from_file_method(self):
        AppLogger.configure_and_get_logger('2_get_json_from_file')
        with LogCapture() as lc:
            temp_dir, file_dir, full_file_name_list = UtilForTesting.file_setup(sub_directory="file_action",
                                                                                content='{"test1":"test2"')
            self.assertEqual(None, FileAction.get_json_from_file(full_file_name_list[0]))
            UtilForTesting.file_teardown(temp_dir)

            UtilForTesting.check_log_capture(
                self, lc, [('2_get_json_from_file', 'ERROR', "Error get_json_from_file: Expecting ',' delimiter")],
                ("exact", "exact", "startswith")
            )
Esempio n. 12
0
    def test_google_cloud_logging_method(self):
        AppLogger.configure_and_get_logger(
            logger_name='test_google',
            config_section_name=AppLoggerJsonConfigName.
            default_with_google_cloud_format_1(),
            google_project_name='sandbox')
        self.assertEqual('sandbox', AppLogger.get_google_project_name())

        with LogCapture() as lc:
            data = {"data": "{'field1': 'test1', 'field2': 'test2'}"}
            AppLogger.logger.info(f"test_me_gcp", extra=data)
            lc.check(('test_google', 'INFO', 'test_me_gcp'), )
Esempio n. 13
0
    def test_1__configure_logger_method(self):
        AppLogger.logger = logging.getLogger('test_configure_1')
        AppLogger.logger.level = logging.DEBUG
        AppLogger.config_dict = None

        with LogCapture() as lc:
            AppLogger._configure_logger()
            lc.check(
                ('test_configure_1', 'DEBUG',
                 'Getting Config Section for [default_format_1]'),
                ('test_configure_1', 'DEBUG',
                 "Set logging configuration to: {'version': 1, 'disable_existing_loggers': 'false', 'formatters': {'default-single-line': {'style': '{', 'datefmt': '%Y-%m-%dT%H:%M:%S', 'format': '{name:<10s} | {levelname:8s} | {message:s}'}}, 'handlers': {'console': {'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'default-single-line', 'stream': 'ext://sys.stdout'}}, 'loggers': {}, 'root': {'handlers': ['console']}}"
                 ),
            )
Esempio n. 14
0
    def test_start_stop_test_methods(self):
        AppLogger.configure_and_get_logger('start_stop_test')
        AppLogger.set_debug_level()

        with LogCapture() as lc:
            timer = Timer()
            timer.start_timer()
            timer.pause(time_in_seconds=2)
            timer.stop_timer()

            self.assertEqual(timer.time_elapsed_in_seconds, 2)
            lc.check_present(
                ('start_stop_test', 'DEBUG', 'Sleep for [2] seconds'),
            )
Esempio n. 15
0
    def test_set_logging_level_with_aws_logging_method(self):
        AppLogger.configure_and_get_logger(
            logger_name='test_logging_level',
            config_section_name=AppLoggerJsonConfigName.
            default_with_watchtower_format_1(),
            watchtower_log_group="test_log_group",
            watchtower_stream_name="test_log_stream")

        AppLogger.logger.debug("debug test1")
        AppLogger.logger.info("info test1")
        AppLogger.logger.error("error test1")
        AppLogger.set_level(logging.DEBUG)
        AppLogger.logger.debug("debug test2")
        AppLogger.logger.info("info test2")
        AppLogger.logger.error("error test2")
Esempio n. 16
0
    def test_set_logging_level_method(self):
        AppLogger.configure_and_get_logger(logger_name='test_logging_level',
                                           logging_level=logging.INFO)

        with LogCapture() as lc:
            AppLogger.logger.debug("debug test1")
            AppLogger.logger.info("info test1")
            AppLogger.logger.error("error test1")
            AppLogger.set_level(logging.DEBUG)
            AppLogger.logger.debug("debug test2")
            AppLogger.logger.info("info test2")
            AppLogger.logger.error("error test2")
            lc.check(
                ('test_logging_level', 'INFO', 'info test1'),
                ('test_logging_level', 'ERROR', 'error test1'),
                ('test_logging_level', 'DEBUG', 'debug test2'),
                ('test_logging_level', 'INFO', 'info test2'),
                ('test_logging_level', 'ERROR', 'error test2'),
            )
Esempio n. 17
0
    def test_3__configure_logger_method(self):
        AppLogger.logger = logging.getLogger('test_configure_3')
        AppLogger.logger.level = logging.DEBUG
        AppLogger.config_dict = None

        with LogCapture() as lc:
            AppLogger._configure_logger(
                AppLoggerJsonConfigName.default_with_watchtower_format_1(),
                'test_log_group', 'test_log_stream')
            lc.check_present(
                ('test_configure_3', 'DEBUG',
                 'Getting Config Section for [default_with_watchtower_format_1]'
                 ),
                ('test_configure_3', 'DEBUG',
                 "Set logging configuration to: {'version': 1, 'disable_existing_loggers': 'false', 'formatters': {'default-single-line': {'style': '{', 'datefmt': '%Y-%m-%dT%H:%M:%S', 'format': '[{asctime:s}.{msecs:3.0f}] | {client_ip} | {host_name} | {levelname:8s} | {name:<10s} | {funcName:<10s} | {lineno:4d} | {message:s}'}}, 'handlers': {'console': {'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'default-single-line', 'stream': 'ext://sys.stdout'}, 'watchtower': {'level': 'DEBUG', 'class': 'watchtower.CloudWatchLogHandler', 'formatter': 'default-single-line', 'log_group': 'default_with_watchtower_format_1', 'stream_name': 'default', 'send_interval': 1, 'create_log_group': 'True'}}, 'loggers': {}, 'root': {'handlers': ['console', 'watchtower']}}"
                 ),
            )
            self.assertEqual('test_log_group', AppLogger.get_log_group())
            self.assertEqual('test_log_stream', AppLogger.get_stream_name())
Esempio n. 18
0
    def test__overwrite_logging_config_section_values_method(self):
        AppLogger.logger = logging.getLogger('test_overwrite')
        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'log_group': 'test1',
                    'stream_name': 'test2'
                }
            }
        }
        AppLogger._overwrite_logging_config_section_values(
            'log_group_test1', 'stream_name_test1')
        self.assertEqual(
            {
                'handlers': {
                    'watchtower': {
                        'log_group': 'log_group_test1',
                        'stream_name': 'stream_name_test1'
                    }
                }
            }, AppLogger.config_dict)

        AppLogger.logger = logging.getLogger('test_overwrite')
        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'log_group': 'test1',
                    'stream_name': 'test2'
                }
            }
        }
        AppLogger._overwrite_logging_config_section_values(None, None)
        self.assertEqual(
            {
                'handlers': {
                    'watchtower': {
                        'log_group': 'test1',
                        'stream_name': 'test2'
                    }
                }
            }, AppLogger.config_dict)
Esempio n. 19
0
    def test__set_stream_name_method(self):
        AppLogger.config_dict = None
        AppLogger._set_stream_name('test')
        self.assertEqual(None, AppLogger.config_dict)

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'stream_name': 'test1'
                }
            }
        }
        AppLogger._set_stream_name('test2')
        self.assertEqual(
            {'handlers': {
                'watchtower': {
                    'stream_name': 'test2'
                }
            }}, AppLogger.config_dict)

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'stream_name': 'test1'
                }
            }
        }
        AppLogger._set_stream_name(None)
        self.assertEqual(
            {'handlers': {
                'watchtower': {
                    'stream_name': 'test1'
                }
            }}, AppLogger.config_dict)
Esempio n. 20
0
    def test__set_google_logger_name(self):
        AppLogger.config_dict = None
        AppLogger._set_google_logger_name('test')
        self.assertEqual(None, AppLogger.config_dict)

        AppLogger.config_dict = {
            'handlers': {
                'google_cloud': {
                    'log_name': 'test1'
                }
            }
        }
        AppLogger._set_google_logger_name('test2')
        self.assertEqual({'handlers': {
            'google_cloud': {
                'log_name': 'test2'
            }
        }}, AppLogger.config_dict)

        AppLogger.config_dict = {
            'handlers': {
                'google_cloud': {
                    'log_name': 'test1'
                }
            }
        }
        AppLogger._set_google_logger_name(None)
        self.assertEqual({'handlers': {
            'google_cloud': {
                'log_name': 'test1'
            }
        }}, AppLogger.config_dict)
Esempio n. 21
0
    def test_write_df_to_csv_creating_instance_method(self):
        AppLogger.configure_and_get_logger(logger_name='test_simple')
        AppLogger.set_debug_level()

        temp_filename = os.path.join(os.path.dirname(__file__), 'temp',
                                     'test_file1')
        os.makedirs(os.path.dirname(temp_filename), exist_ok=True)
        df = pd.DataFrame({
            'seconds': ["3600"],
            'minutes': ["10"]
        },
                          columns=['seconds', 'minutes'])
        bcp_ff = BcpFileFormat(df, temp_filename)
        bcp_temp = BcpTempCsvFile.write_df_to_csv_creating_instance(df, bcp_ff)

        assert_frame_equal(df, bcp_temp.pandas_dataframe)
        self.assertEqual(bcp_ff, bcp_temp.bcp_file_format)
        self.assertEqual(f"{temp_filename}.csv", bcp_temp.csv_full_file_name)
        self.assertEqual(False, bcp_temp.use_index)
        self.assertEqual(True, os.path.exists(f"{temp_filename}.csv"))

        bcp_temp.remove_file()
Esempio n. 22
0
    def test__set_log_group_method(self):
        AppLogger.logger = None
        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'log_group': 'test1'
                }
            }
        }
        AppLogger._set_log_group('test2')
        self.assertEqual({'handlers': {
            'watchtower': {
                'log_group': 'test1'
            }
        }}, AppLogger.config_dict)

        AppLogger.logger = logging.getLogger('test_set_logger')
        AppLogger.config_dict = None
        AppLogger._set_log_group('test')
        self.assertEqual(None, AppLogger.config_dict)

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'log_group': 'test1'
                }
            }
        }
        AppLogger._set_log_group('test2')
        self.assertEqual({'handlers': {
            'watchtower': {
                'log_group': 'test2'
            }
        }}, AppLogger.config_dict)

        AppLogger.config_dict = {
            'handlers': {
                'watchtower': {
                    'log_group': 'test1'
                }
            }
        }
        AppLogger._set_log_group(None)
        self.assertEqual({'handlers': {
            'watchtower': {
                'log_group': 'test1'
            }
        }}, AppLogger.config_dict)
Esempio n. 23
0
    def test__set_logger_method(self):
        AppLogger.logger = None
        AppLogger._set_logger('test_set_logger1')
        self.assertEqual('test_set_logger1', AppLogger.logger.name)

        AppLogger.logger = logging.getLogger('test_set_logger2')
        AppLogger._set_logger('test_set_logger2')
        self.assertEqual('test_set_logger2', AppLogger.logger.name)

        AppLogger.logger = logging.getLogger('test_set_logger2')
        AppLogger._set_logger('test_set_logger3')
        self.assertEqual('test_set_logger3', AppLogger.logger.name)
Esempio n. 24
0
            # , Read, Upsert, Execute


class App:
    def create_app(self, class_object_list):
        AppLogger.logger.info("creating app")
        flask_app = Flask(__name__)
        # swagger = Swagger(flask_app)
        api = Api(flask_app)
        for class_object in class_object_list:
            class_object.add_routes(api)

        return flask_app

    @staticmethod
    def create_app_instance(class_object_list):
        AppLogger.logger.info("Create App Instance")
        return App().create_app(class_object_list)


if __name__ == '__main__':
    AppLogger.configure_and_get_logger(
        'ms_sql_server_proxy_monitor',
        AppLoggerJsonConfigName.default_with_watchtower_format_1(),
        watchtower_log_group="ms_sql_server_proxy",
        watchtower_stream_name="app"
    )
    AppLogger.set_debug_level()
    RunServer().run_server()

Esempio n. 25
0
    def test_simple_logging_method(self):
        AppLogger.configure_and_get_logger(logger_name='test_simple')

        with LogCapture() as lc:
            AppLogger.logger.info("test_me_simple")
            lc.check(('test_simple', 'INFO', 'test_me_simple'), )
Esempio n. 26
0
 def test__set_config_dict_method(self):
     AppLogger._set_config_dict('123')
     self.assertEqual('123', AppLogger.config_dict)
Esempio n. 27
0
 def test_find_root_of_project(self):
     AppLogger.configure_and_get_logger("test")
     AppLogger.set_debug_level()
     self.assertTrue(
         Root.find_root_of_project(__file__).endswith("zeppos_root"))