コード例 #1
0
    def test_unset_environment_variable(self):
        """Check negative situation when environment variable for database config is not set.

        1. Try to parse config without setting environment variable.
        2. Expect exception with particular message.
        """
        with pytest.raises(AssertionError) as exception_info:
            database_config.get_database_config()
        assert exception_info.value.args[0] == "Path to database config is not set", (
            "Wrong exception message"
            )
コード例 #2
0
    def test_non_existing_file(self, tmpdir, monkeypatch):
        """Check negative situation when environment vairable for database config
        refers to the file that does not exist.

        1. Set environment variable such that it refers to non-existsing file.
        2. Try to parse database config.
        3. Expect exception with particular message.
        """
        non_existsing_file = str(tmpdir.join('fake_config.ini'))
        monkeypatch.setenv(database_config.ENV_DB_CONFIG_PATH, non_existsing_file)
        with pytest.raises(AssertionError) as exception_info:
            database_config.get_database_config()

        assert exception_info.value.args[0] == "Failed to find path {}".format(
            non_existsing_file
            ), "Wrong exception message"
コード例 #3
0
    def test_directory_path(self, tmpdir, monkeypatch):
        """Check negative situation when environment variable for database config
        refers to the directory instead of the file.

        1. Set environment variable to the directory.
        2. Try to parse database config.
        3. Expect exception with particular message.
        """
        config_path = str(tmpdir)
        monkeypatch.setenv(database_config.ENV_DB_CONFIG_PATH, config_path)

        with pytest.raises(AssertionError) as exception_info:
            database_config.get_database_config()
        expected_message = "Specified path for database config ({}) is not a file".format(
            config_path
            )
        assert exception_info.value.args[0] == expected_message, "Wrong exception message"
コード例 #4
0
def get_autostorage_api():
    """Get autostorage api object.

    :returns: autostorage api object.
    """
    app = get_application()
    database_config = get_database_config(app.config["AUTOSTORAGE_DATABASE_CONFIG"])
    return API(database_config)
コード例 #5
0
def test_get_engine_explicit_config(config_with_in_memory_database):
    """Check that created engine use proper connection string from explicit config.

    1. Provide database config explicitly to the method to create engine.
    2. Check that engine use connection string from config.
    """
    database_config = get_database_config(config_with_in_memory_database)
    engine = database_common.get_engine(database_config)
    assert str(engine.url) == database_config.get_connection_string(), "Wrong engine url"
コード例 #6
0
ファイル: conftest.py プロジェクト: KillAChicken/AutoStorage
def fresh_database_config(config_with_sqlite_database):
    """Create new database structure using database config.

    :param config_with_sqlite_database: path to database config.
    :returns: instance of :class:`DatabaseConfig
        <autostorage.database.database_config.DatabaseConfig>`.
    """
    database_config = get_database_config(config_with_sqlite_database)
    create_all(database_config)
    return database_config
コード例 #7
0
def test_get_engine_implicit_config(config_with_in_memory_database, monkeypatch):
    """Check that created engine use proper connection string from implicit config.

    1. Provide database config implicitly to the method to create engine.
    2. Check that engine use connection string from config.
    """
    monkeypatch.setenv(ENV_DB_CONFIG_PATH, config_with_in_memory_database)
    engine = database_common.get_engine()
    database_config = get_database_config(config_with_in_memory_database)
    assert str(engine.url) == database_config.get_connection_string(), "Wrong engine url"
コード例 #8
0
    def test_parser_representation(self, tmpdir):
        """Check representation of the config object.

        1. Create config object from empty file.
        2. Check its representation string.
        """
        config_path = str(tmpdir)
        parsed_config = database_config.get_database_config(config_path)
        assert repr(parsed_config) == "DatabaseConfig({0})".format(config_path), (
            "Wrong representation string for database config"
            )
コード例 #9
0
def test_method_creation_explicit(config_with_sqlite_database):
    """Check method to create database.

    1. Provide database config explicitly to the method to create database.
    2. Check that tables exist.
    3. Check that database is populated.
    """
    database_config = get_database_config(config_with_sqlite_database)
    create_all(database_config)
    engine = create_engine(database_config.get_connection_string())
    assert set(engine.table_names()) == _get_expected_tables_names(), "Wrong tables set"
    _check_initial_records(engine)
コード例 #10
0
    def test_non_existing_file(self, tmpdir):
        """Check that database config could be created with new file.

        1. Create config object with non-existing file.
        2. Force config to update the file.
        3. Check that empty file is created.
        """
        config_path = str(tmpdir.join('new_config.ini'))
        config = database_config.get_database_config(config_path)
        config._update_config()     # pylint: disable=protected-access
        assert os.path.exists(config_path), "Empty config has not been created."
        assert os.stat(config_path).st_size == 0, "Empty config does not have 0 size."
コード例 #11
0
def test_method_creation_implicit(config_with_sqlite_database, monkeypatch):
    """Check method to create database.

    1. Provide database config implicitly to the method to create database.
    2. Check that tables exist.
    3. Check that database is populated.
    """
    monkeypatch.setenv(ENV_DB_CONFIG_PATH, config_with_sqlite_database)
    create_all()
    database_config = get_database_config(config_with_sqlite_database)
    engine = create_engine(database_config.get_connection_string())
    assert set(engine.table_names()) == _get_expected_tables_names(), "Wrong tables set"
    _check_initial_records(engine)
コード例 #12
0
ファイル: conftest.py プロジェクト: KillAChicken/AutoStorage
def config_with_sqlite_database(sqlite_connection_string, tmpdir, request):  # pylint: disable=redefined-outer-name
    """Create database config with connection string to sqlite database in file.

    :param tmpdir: Pytest py.local object.
    :param monkeypatch: Pytest monkeypatch object.
    ;returns: Path to the database config.
    """
    config_path = str(tmpdir.join('sqlite_db.ini'))
    database_config = get_database_config(config_path)
    database_config.set_connection_string(sqlite_connection_string)

    request.addfinalizer(lambda: os.unlink(config_path))

    return config_path
コード例 #13
0
ファイル: conftest.py プロジェクト: KillAChicken/AutoStorage
def config_with_in_memory_database(tmpdir, request):
    """Create database config with connection string to in-memory database.

    :param tmpdir: Pytest py.local object.
    :param request: Pytest Request object.
    :returns: Path to the database config.
    """
    config_path = str(tmpdir.join('in_memory_db.ini'))
    database_config = get_database_config(config_path)
    database_config.set_connection_string('sqlite:///:memory:')

    request.addfinalizer(lambda: os.unlink(config_path))

    return config_path
コード例 #14
0
def test_script_creation_implicit(config_with_sqlite_database, monkeypatch):
    """Check commandline to create database.

    1. Do not provide database config to the script to create database.
    2. Check exit code of command line execution.
    3. Check that tables exist.
    4. Check that database is populated.
    """
    monkeypatch.setenv(ENV_DB_CONFIG_PATH, config_with_sqlite_database)
    subprocess.check_call([sys.executable, inspect.getabsfile(create_all)])

    database_config = get_database_config(config_with_sqlite_database)
    engine = create_engine(database_config.get_connection_string())
    assert set(engine.table_names()) == _get_expected_tables_names(), "Wrong tables set"
    _check_initial_records(engine)
コード例 #15
0
    def test_set_connection_string(self, tmpdir):
        """Test set-method for the connection string.

        1. Create new config.
        2. Set connection string.
        3. Get connection string from config.
        4. Check that connection string is set correctly.
        """
        config_path = str(tmpdir.join('new_config.ini'))
        config = database_config.get_database_config(config_path)
        expected_connection_string = 'test'
        config.set_connection_string(expected_connection_string)
        assert config.get_connection_string() == expected_connection_string, (
            "Wrong connection string has been set"
            )
コード例 #16
0
def _main():
    """Parse command line and create database tables."""
    logger = logging.getLogger('autostorage')
    logger.setLevel(logging.INFO)
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.INFO)
    logger.addHandler(console_handler)

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-c', '--database-config', dest='config_path', default=None, type=str,
        help="Path to the database config."
        )
    args = parser.parse_args()
    database_config = get_database_config(args.config_path)
    create_all(database_config)
コード例 #17
0
    def test_get_connection_string(self, apply_config):
        """Test get-method for the connection string.

        1. Create config file with connection string.
        2. Parse database config.
        3. Check that connection string is parsed correctly.
        """
        config = ConfigParser()
        config.add_section(const.SECTION_SETTINGS)
        expected_connection_string = 'test'
        config.set(const.SECTION_SETTINGS, const.KEY_CONNECTION_STRING, expected_connection_string)
        apply_config(config)

        obtained_connection_string = database_config.get_database_config().get_connection_string()
        assert obtained_connection_string == expected_connection_string, (
            "Wrong connection string parsed."
            )
コード例 #18
0
    def test_overwrite_existing_file(self, tmpdir):
        """Check the writing of the config over the existing file.

        1. Create new config by setting connection string.
        2. Set another connection string.
        3. Get connection string from config.
        4. Check that connection string is set correctly.
        """
        config_path = str(tmpdir.join('new_config.ini'))
        config = database_config.get_database_config(config_path)
        config.set_connection_string('test_connection_string')
        initial_connection_string = 'test_connection_string'
        expected_connection_string = 'another_' + initial_connection_string
        config.set_connection_string(expected_connection_string)
        assert config.get_connection_string() == expected_connection_string, (
            "Wrong connection string has been set"
            )
コード例 #19
0
def test_script_creation_explicit(config_with_sqlite_database):
    """Check commandline to create database.

    1. Provide database config explicitly to the script to create database.
    2. Check exit code of command line execution.
    3. Check that tables exist.
    4. Check that database is populated.
    """
    subprocess.check_call([
        sys.executable,
        inspect.getabsfile(create_all),
        "-c", config_with_sqlite_database
        ])

    database_config = get_database_config(config_with_sqlite_database)
    engine = create_engine(database_config.get_connection_string())
    assert set(engine.table_names()) == _get_expected_tables_names(), "Wrong tables set"
    _check_initial_records(engine)
コード例 #20
0
    def test_context_manager(self, tmpdir):
        """Check the writing with context manager.

        1. Create config object with non-existing file.
        2. Change config inside with-statement.
        3. Check that file has not been created inside of with-statement.
        4. Check that file has been created outside of with-statement.
        """
        config_path = str(tmpdir.join('new_config.ini'))
        config = database_config.get_database_config(config_path)
        with config:
            assert not os.path.exists(config_path), (
                "Config has not been created inside of with statement."
                )
            config.set_connection_string('test')
            assert not os.path.exists(config_path), (
                "Config has not been created inside of with statement."
                )

        assert os.path.exists(config_path), "Config has not been created outside of with statement."
コード例 #21
0
    def test_simple_config_parsing(self, apply_config):
        """Check that simple database config is correctly parsed.

        1. Write ConfigParser with single section object to the temporary file.
        2. Parse database config.
        3. Check type of the parsed object.
        4. Check that parsed object is not empty.
        """
        config = ConfigParser()
        config.add_section(SECTION_SETTINGS)
        apply_config(config)

        parsed_config = database_config.get_database_config()
        assert isinstance(
            parsed_config,
            database_config.DatabaseConfig
            ), "Config is not of the type DatabaseConfig"
        raw_data = parsed_config._parsed_config     # pylint: disable=protected-access
        assert raw_data.has_section(
            SECTION_SETTINGS
            ), "Parsed config does not contain section for settings"