Example #1
0
 def test_connect_testdatabase(self, config_test_database, sqlite_database):
     """
     GIVEN an instantiated SQLDataLoader and an existing test sqlite database.
     WHEN the method 'connect_to_server' is evoked
     THEN the connection can be established.
     """
     config_test_database["host"] = str(sqlite_database)
     SQLDataLoader.from_config(config_test_database)._connect_to_server()
Example #2
0
 def test_nonexistent_query_path(self, sqldataloader_config):
     """
     GIVEN an instantiated SQLDataLoader with the wrong path to the sql file.
     WHEN the method 'read_query' is called
     THEN a NameError is raised.
     """
     sqldataloader_config["path_sql_query"] = "wrong_test_path"
     with pytest.raises(NameError):
         SQLDataLoader.from_config(sqldataloader_config)._read_query()
Example #3
0
 def test_config_output_equal_input(self, sqldataloader_config):
     """
     GIVEN a correct config file.
     WHEN the config file is recreated by the method 'to_dict'
     THEN the two instantiated samplers are equal.
     """
     loader = SQLDataLoader(**sqldataloader_config)
     config_dict = loader.to_dict()
     assert loader == SQLDataLoader.from_config(config_dict)
Example #4
0
 def test_no_environment_connection(self, sqldataloader_config,
                                    mock_env_missing):
     """
     GIVEN that the environment variable defining the connection to the
         sql database is missing and an instantiated SQLDataLoader
     WHEN the method 'connect_to_server' is called
     THEN an EnvironmentError is raised."""
     sqldataloader_config["user"] = True
     with pytest.raises(EnvironmentError):
         SQLDataLoader.from_config(
             sqldataloader_config)._connect_to_server()
Example #5
0
 def test_load_testquery(self, sqldataloader_config):
     """
     GIVEN a sql test file and a correct path.
     WHEN the SQLDataLoader is instantiated with that path and the method
         'read_query' is called
     THEN the query is read in correctly.
     """
     sqldataloader_config["path_sql_query"] = (settings.TEST_PATH / "data" /
                                               "test_sql_query.sql")
     loader = SQLDataLoader(**sqldataloader_config)
     query = loader._read_query()
     assert query == "SELECT Id, name, salary, active FROM emp"
Example #6
0
    def test_no_existence_sqlite(self, config_test_database):
        """
        GIVEN a dictionary configuring the SQLDataLoader with a sqlite
            dialect and an non existent host file.
        WHEN a SQLDataLoader is instantiated from that dict and tries to load the data
        THEN a ValueError is raised.
        """
        config_test_database["dialect"] = "sqlite"
        config_test_database["host"] = "test_host"

        with pytest.raises(ValueError):
            SQLDataLoader.from_config(
                config_test_database)._connect_to_server()
Example #7
0
 def test_connect_set_environment_but_user_false(self, config_test_database,
                                                 mock_env_user,
                                                 sqlite_database):
     """
     GIVEN set environment variables for the user and the password to
         connect to the sql database.
     WHEN a SQLDataLoader is instantiated with the user set to False
     THEN the connection to the sql database is established by
         ignoring the environment variables.
     """
     config_test_database["host"] = str(sqlite_database)
     config_test_database["user"] = False
     SQLDataLoader.from_config(config_test_database)._connect_to_server()
Example #8
0
 def test_loader_instantiation(self, sqldataloader_config):
     """
     GIVEN a correct config file.
     WHEN the SQLDataLoader is instantiated with that config file
     THEN the SQLDataLoader is instantiated.
     """
     SQLDataLoader(**sqldataloader_config)
Example #9
0
 def test_wrong_init_type(self, sqldataloader_config, init_param,
                          new_value):
     """
     GIVEN wrong input types of the dictionary params for the SQLDataLoader.
     WHEN the SQLDataLoader is instantiated
     THEN a TypeError is raised.
     """
     sqldataloader_config[init_param] = new_value
     with pytest.raises(TypeError):
         SQLDataLoader(**sqldataloader_config)
Example #10
0
 def test_pathlib_path_to_yaml(self, sqldataloader_config):
     """
     GIVEN a configuration dictonary.
     WHEN the path to the sql query is set to a pathlib path and a SQLDataLoader is
          instantiated with that configuration
          THEN it can be saves as a yaml file.
     """
     sqldataloader_config["path_sql_query"] = (settings.TEST_PATH / "data" /
                                               "test_sql_query.sql")
     loader = SQLDataLoader.from_config(sqldataloader_config)
     yaml.dump(loader.to_dict())
Example #11
0
    def test_load_data(self, config_test_database, sqlite_database,
                       dummy_dataframe):
        """
        GIVEN a dictionary defining the connection to the test sql database.
        WHEN a SQLDataLoader is instantiated and the method 'load_data' is used
        THEN it returns a dataframe equal to the dataframe of the sql test database.
        """
        config_test_database["host"] = str(sqlite_database)
        loader = SQLDataLoader.from_config(config_test_database)
        loaded_dataframe = loader.load_data()

        pd.testing.assert_frame_equal(loaded_dataframe, dummy_dataframe)
Example #12
0
    def test_write_equals_load_data(self, config_test_database,
                                    sqlite_database, dummy_dataframe):
        """
        GIVEN a dictionary defining the connection to the test sql database.
        WHEN a SQLDataLoader is instantiated and the method 'write_query_to_hdf' is
            used
        THEN it writes a dataframe to the test data folder and the resulting dataframe
            is the same then the read in dataframe.
        """
        test_data_path = settings.TEST_PATH / "data" / "test_raw_data.hdf"
        config_test_database["host"] = str(sqlite_database)
        loader = SQLDataLoader.from_config(config_test_database)
        loader.write_query_to_hdf(output_name=test_data_path)

        reloaded_data = pd.read_hdf(test_data_path, key="raw_data", mode="r+")
        os.remove(test_data_path)

        pd.testing.assert_frame_equal(reloaded_data, dummy_dataframe)
Example #13
0
 def sqldataloader_init(self, sqldataloader_config):
     """Instantiate the SQLDataLoader."""
     return SQLDataLoader(**sqldataloader_config)