Example #1
0
def pg_client_and_schema():
    """
    Creates a temporary schema for the testing session, drops everything
    at the end
    """
    db = _load_db_credentials()

    # set a new schema for this session, otherwise if two test sessions
    # are run at the same time, tests might conflict with each other
    # NOTE: avoid upper case characters, pandas.DataFrame.to_sql does not like
    # them
    schema = (''.join(random.choice(string.ascii_letters)
                      for i in range(12))).lower()

    # initialize client, set default schema
    # info: https://www.postgresonline.com/article_pfriendly/279.html
    client = SQLAlchemyClient(db['uri'],
                              create_engine_kwargs=dict(connect_args=dict(
                                  options=f'-c search_path={schema}')))

    # create schema
    client.execute('CREATE SCHEMA {};'.format(schema))

    df = pd.DataFrame({'x': range(10)})
    df.to_sql('data', client.engine)

    yield client, schema

    # clean up schema
    client.execute('DROP SCHEMA {} CASCADE;'.format(schema))
    client.close()
Example #2
0
def pg_client():
    db = _load_db_credentials()

    client = SQLAlchemyClient(db['uri'])

    # set a new schema for this session, otherwise if two test sessions
    # are run at the same time, tests might conflict with each other
    schema = (''.join(random.choice(string.ascii_letters) for i in range(8)))

    client.execute('CREATE SCHEMA {};'.format(schema))
    client.execute('SET search_path TO {};'.format(schema))

    yield client

    # clean up schema
    client.execute('drop schema {} cascade;'.format(schema))

    client.close()
Example #3
0
def pg_client_and_schema():
    db = _load_db_credentials()

    client = SQLAlchemyClient(db['uri'])

    # set a new schema for this session, otherwise if two test sessions
    # are run at the same time, tests might conflict with each other
    # NOTE: avoid upper case characters, pandas.DataFrame.to_sql does not like
    # them
    schema = (''.join(random.choice(string.ascii_letters)
                      for i in range(12))).lower()

    client.execute('CREATE SCHEMA {};'.format(schema))
    client.execute('SET search_path TO {};'.format(schema))

    yield client, schema

    # clean up schema
    client.execute('drop schema {} cascade;'.format(schema))

    client.close()
Example #4
0
def test_send_more_than_one_command_in_sqlite(code, split_source,
                                              tmp_directory):
    client = SQLAlchemyClient('sqlite:///my_db.db', split_source=split_source)
    client.execute(code)
Example #5
0
def test_pickle_sqlalchemyclient(tmp_directory):
    client = SQLAlchemyClient('sqlite:///my_db.db')
    client.execute('CREATE TABLE my_table (num INT)')
    assert pickle.dumps(client)
Example #6
0
def test_deepcopy_sqlalchemyclient(tmp_directory):
    client = SQLAlchemyClient('sqlite:///my_db.db')
    client.execute('CREATE TABLE my_table (num INT)')
    assert copy.deepcopy(client)
Example #7
0
def test_does_not_create_in_memory_sqlalchemyclient(tmp_directory):
    client = SQLAlchemyClient('sqlite://')
    client.execute('CREATE TABLE my_table (num INT)')
    # Assert no folder/file was created in the temporary folder:
    assert next(Path(tmp_directory).iterdir(), None) is None
Example #8
0
def test_creates_absolute_dir_sqlalchemyclient(tmp_directory):
    intermediate_path = Path(tmp_directory, "an/absolute/path")
    client = SQLAlchemyClient(f'sqlite:///{intermediate_path}/my_db.db')
    client.execute('CREATE TABLE my_table (num INT)')
    assert intermediate_path.exists()
Example #9
0
def test_creates_relative_dir_sqlalchemyclient(tmp_directory):
    intermediate_path = "a/relative/path"
    client = SQLAlchemyClient(f'sqlite:///{intermediate_path}/my_db.db')
    client.execute('CREATE TABLE my_table (num INT)')
    assert Path(tmp_directory + "/" + intermediate_path).exists()