2021-02-18 06:04:30 +01:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
|
2021-05-01 15:20:14 +02:00
|
|
|
# external imports
|
2021-02-18 06:04:30 +01:00
|
|
|
import pytest
|
|
|
|
import sqlparse
|
2021-05-01 15:20:14 +02:00
|
|
|
import alembic
|
|
|
|
from alembic.config import Config as AlembicConfig
|
2021-02-18 06:04:30 +01:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from cic_cache.db.models.base import SessionBase
|
|
|
|
from cic_cache.db import dsn_from_config
|
2021-05-01 15:20:14 +02:00
|
|
|
from cic_cache.db import add_tag
|
2021-02-18 06:04:30 +01:00
|
|
|
|
|
|
|
logg = logging.getLogger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def database_engine(
|
|
|
|
load_config,
|
|
|
|
):
|
|
|
|
if load_config.get('DATABASE_ENGINE') == 'sqlite':
|
|
|
|
SessionBase.transactional = False
|
|
|
|
SessionBase.poolable = False
|
2021-12-22 20:12:08 +01:00
|
|
|
name = 'cic_cache'
|
|
|
|
database_name = name
|
|
|
|
if load_config.get('DATABASE_PREFIX'):
|
|
|
|
database_name = '{}_{}'.format(load_config.get('DATABASE_PREFIX'), database_name)
|
2021-02-18 06:04:30 +01:00
|
|
|
try:
|
2021-12-22 20:12:08 +01:00
|
|
|
os.unlink(database_name)
|
2021-02-18 06:04:30 +01:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2021-12-22 20:12:08 +01:00
|
|
|
dsn = dsn_from_config(load_config, name)
|
2021-05-01 15:20:14 +02:00
|
|
|
SessionBase.connect(dsn, debug=load_config.true('DATABASE_DEBUG'))
|
2021-02-18 06:04:30 +01:00
|
|
|
return dsn
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def init_database(
|
|
|
|
load_config,
|
|
|
|
database_engine,
|
|
|
|
):
|
|
|
|
|
|
|
|
rootdir = os.path.dirname(os.path.dirname(__file__))
|
2021-05-01 15:20:14 +02:00
|
|
|
dbdir = os.path.join(rootdir, 'cic_cache', 'db')
|
|
|
|
migrationsdir = os.path.join(dbdir, 'migrations', load_config.get('DATABASE_ENGINE'))
|
|
|
|
if not os.path.isdir(migrationsdir):
|
|
|
|
migrationsdir = os.path.join(dbdir, 'migrations', 'default')
|
|
|
|
logg.info('using migrations directory {}'.format(migrationsdir))
|
2021-02-18 06:04:30 +01:00
|
|
|
|
|
|
|
session = SessionBase.create_session()
|
2021-05-01 15:20:14 +02:00
|
|
|
|
|
|
|
ac = AlembicConfig(os.path.join(migrationsdir, 'alembic.ini'))
|
|
|
|
ac.set_main_option('sqlalchemy.url', database_engine)
|
|
|
|
ac.set_main_option('script_location', migrationsdir)
|
|
|
|
|
|
|
|
alembic.command.downgrade(ac, 'base')
|
|
|
|
alembic.command.upgrade(ac, 'head')
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
|
2021-02-18 06:04:30 +01:00
|
|
|
yield session
|
|
|
|
session.commit()
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def list_tokens(
|
|
|
|
):
|
|
|
|
return {
|
|
|
|
'foo': '0x' + os.urandom(20).hex(),
|
|
|
|
'bar': '0x' + os.urandom(20).hex(),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def list_actors(
|
|
|
|
):
|
|
|
|
return {
|
|
|
|
'alice': '0x' + os.urandom(20).hex(),
|
|
|
|
'bob': '0x' + os.urandom(20).hex(),
|
|
|
|
'charlie': '0x' + os.urandom(20).hex(),
|
|
|
|
'diane': '0x' + os.urandom(20).hex(),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def list_defaults(
|
|
|
|
):
|
|
|
|
|
|
|
|
return {
|
|
|
|
'block': 420000,
|
|
|
|
}
|
2021-05-01 15:20:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def tags(
|
|
|
|
init_database,
|
|
|
|
):
|
|
|
|
|
|
|
|
add_tag(init_database, 'foo')
|
|
|
|
add_tag(init_database, 'baz', domain='bar')
|
|
|
|
add_tag(init_database, 'xyzzy', domain='bar')
|
|
|
|
init_database.commit()
|