def sample(): """Set up a set of samples for testing using the factory boy factories. The sample factory cascades to all related tables. """ # create_all is already in test/database__init__.py. # I don't know why I have to call it here again, but I do. Base.metadata.create_all(bind=dal.engine) # Add some data here print("Hey, here come some samples...") session = dal.Session() samples = SampleFactory.create_batch(5) for s in samples: fids = [f.id for f in s.sem_files] s.primary_sem_file_id = fids[random.randint(0, len(fids) - 1)] for f in s.sem_files: ids = [a.id for a in f.analyses] lids = len(ids) f.default_analysis_id = ids[random.randint(0, lids - 1)] rs = RamanSet(sample_id=s.id, experiment_date=s.experiment_date) session.add(rs) session.flush() for rf in s.raman_files: rf.raman_spectrum.set_id = rs.id session.commit() SoftwareFactory.create_batch(5) yield samples # Drop and ditch print("Tearing down test samples and DB") with dal.session_scope(autocommit=True) as sess: for s in sess.query(Sample).all(): sess.delete(s) Base.metadata.drop_all(bind=dal.engine)
import os from gresq.config import Config from gresq.database import dal, Base from gresq.database.models import Sample import logging logging.basicConfig(level=logging.DEBUG) config_prefix = "DEV_DATABASE" config_suffix = "_ADMIN" conf = Config(prefix=config_prefix, suffix=config_suffix, debug=True, try_secrets=False) dal.init_db(conf, privileges={"read": True, "write": True, "validate": True}) with dal.session_scope(autocommit=True) as sess: for s in sess.query(Sample).all(): sess.delete(s) dal.Session().close() #print(Base.metadata.tables) ret = Base.metadata.drop_all(bind=dal.engine) print(f"ret: {ret}")
def test_delete__1stsample_cascade(self, sample): with dal.session_scope(autocommit=True) as sess: smpl = sess.query(Sample).filter(Sample.id == 1).one() assert smpl.id == 1 sess.delete(smpl) print(sess.query(Sample.id).all()[0][0])
import os from gresq.config import Config from gresq.database import dal from gresq.util.csv2db3 import build_db import logging logging.basicConfig(level=logging.DEBUG) config_prefix = "DEV_DATABASE" config_suffix = "_ADMIN" conf = Config(prefix=config_prefix, suffix=config_suffix, debug=True, try_secrets=False) dal.init_db(conf, privileges={"read": True, "write": True, "validate": True}) BOX_CONFIG_PATH = os.environ["BOX_CONFIG_PATH"] csv2db_file = os.path.join(os.getcwd(), "data", "SEM_Raman_Test") with dal.session_scope() as session: build_db( session, os.path.join(os.getcwd(), "data"), csv2db_file, box_config_path=BOX_CONFIG_PATH, )