File size: 7,593 Bytes
99f834c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | """Tests for the database ingestion layer."""
import os
import sqlite3
import tempfile
import pandas as pd
import pytest
from core.database import ConnectionConfig, SQLiteConnector, CSVConnector
from core.database.base import FieldMapping, SchemaMapper, SEQUENCE_FIELDS
# ββ SchemaMapper ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestSchemaMapper:
def test_from_dict(self):
mapper = SchemaMapper.from_dict({
"gene_name": "name",
"mrna_seq": "full_mrna",
})
assert len(mapper.mappings) == 2
def test_requires_name_mapping(self):
with pytest.raises(ValueError, match="name"):
SchemaMapper.from_dict({"mrna_seq": "full_mrna"})
def test_invalid_target_field(self):
with pytest.raises(ValueError):
FieldMapping("col", "not_a_real_field")
def test_map_row(self):
mapper = SchemaMapper.from_dict({
"gene": "name",
"sequence": "full_mrna",
"utr": "five_prime_utr",
}, db_source="test_db")
row = {"gene": "GFP", "sequence": "ATGCCC", "utr": "AAAA", "extra": "foo"}
seq = mapper.map_row(row)
assert seq.name == "GFP"
assert seq.full_mrna == "ATGCCC"
assert seq.five_prime_utr == "AAAA"
assert seq.source == "database"
assert seq.db_source == "test_db"
assert seq.raw_metadata["extra"] == "foo"
def test_map_dataframe(self):
mapper = SchemaMapper.from_dict({"name_col": "name", "cds_col": "cds"})
df = pd.DataFrame({
"name_col": ["seq1", "seq2"],
"cds_col": ["ATGCCC", "ATGTTT"],
})
seqs = mapper.map_dataframe(df)
assert len(seqs) == 2
assert seqs[0].name == "seq1"
assert seqs[1].cds == "ATGTTT"
def test_transform_applied(self):
mapper = SchemaMapper([
FieldMapping("gene", "name"),
FieldMapping("seq", "full_mrna", transform=str.upper),
])
row = {"gene": "test", "seq": "atgccc"}
seq = mapper.map_row(row)
assert seq.full_mrna == "ATGCCC"
# ββ SQLite Connector ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@pytest.fixture
def sqlite_db():
"""Create a temporary SQLite database with sample sequence data."""
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
db_path = f.name
conn = sqlite3.connect(db_path)
conn.execute("""
CREATE TABLE sequences (
id INTEGER PRIMARY KEY,
gene_name TEXT,
mrna_sequence TEXT,
gc_target REAL
)
""")
conn.execute("INSERT INTO sequences VALUES (1, 'GFP', 'ATGCCCATG', 0.55)")
conn.execute("INSERT INTO sequences VALUES (2, 'RFP', 'ATGTTTGGG', 0.45)")
conn.commit()
conn.close()
yield db_path
os.unlink(db_path)
class TestSQLiteConnector:
def test_connect(self, sqlite_db):
config = ConnectionConfig("sqlite", "test", {"path": sqlite_db})
connector = SQLiteConnector(config)
connector.connect()
assert connector.is_connected
connector.disconnect()
def test_list_tables(self, sqlite_db):
config = ConnectionConfig("sqlite", "test", {"path": sqlite_db})
connector = SQLiteConnector(config)
connector.connect()
tables = connector.list_tables()
assert "sequences" in tables
connector.disconnect()
def test_get_records(self, sqlite_db):
config = ConnectionConfig("sqlite", "test", {"path": sqlite_db})
connector = SQLiteConnector(config)
connector.connect()
df = connector.get_records("sequences")
assert len(df) == 2
assert "gene_name" in df.columns
connector.disconnect()
def test_get_records_with_limit(self, sqlite_db):
config = ConnectionConfig("sqlite", "test", {"path": sqlite_db})
connector = SQLiteConnector(config)
connector.connect()
df = connector.get_records("sequences", limit=1)
assert len(df) == 1
connector.disconnect()
def test_get_columns(self, sqlite_db):
config = ConnectionConfig("sqlite", "test", {"path": sqlite_db})
connector = SQLiteConnector(config)
connector.connect()
cols = connector.get_columns("sequences")
assert "gene_name" in cols
assert "mrna_sequence" in cols
connector.disconnect()
def test_not_connected_raises(self):
config = ConnectionConfig("sqlite", "test", {"path": "/nonexistent.db"})
connector = SQLiteConnector(config)
with pytest.raises(RuntimeError):
connector.list_tables()
def test_full_import_pipeline(self, sqlite_db):
"""Full end-to-end: connect β get records β map β mRNASequence list."""
config = ConnectionConfig("sqlite", "test_lims", {"path": sqlite_db})
connector = SQLiteConnector(config)
connector.connect()
df = connector.get_records("sequences")
mapper = SchemaMapper.from_dict({
"gene_name": "name",
"mrna_sequence": "full_mrna",
}, db_source="test_lims")
sequences = mapper.map_dataframe(df)
connector.disconnect()
assert len(sequences) == 2
assert sequences[0].name == "GFP"
assert sequences[0].full_mrna == "ATGCCCATG"
assert sequences[0].db_source == "test_lims"
# ββ CSV Connector βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@pytest.fixture
def csv_file():
with tempfile.NamedTemporaryFile(
mode="w", suffix=".csv", delete=False
) as f:
f.write("name,cds,utr5\n")
f.write("GFP,ATGCCCATG,AAAA\n")
f.write("RFP,ATGTTTGGG,TTTT\n")
path = f.name
yield path
os.unlink(path)
class TestCSVConnector:
def test_connect(self, csv_file):
config = ConnectionConfig("csv", "test_csv", {"path": csv_file})
connector = CSVConnector(config)
connector.connect()
assert connector.is_connected
connector.disconnect()
def test_list_tables(self, csv_file):
config = ConnectionConfig("csv", "test_csv", {"path": csv_file})
connector = CSVConnector(config)
connector.connect()
tables = connector.list_tables()
# Table name = filename stem
assert len(tables) == 1
connector.disconnect()
def test_get_records(self, csv_file):
config = ConnectionConfig("csv", "test_csv", {"path": csv_file})
connector = CSVConnector(config)
connector.connect()
table = connector.list_tables()[0]
df = connector.get_records(table)
assert len(df) == 2
assert "name" in df.columns
connector.disconnect()
def test_get_records_with_query(self, csv_file):
config = ConnectionConfig("csv", "test_csv", {"path": csv_file})
connector = CSVConnector(config)
connector.connect()
table = connector.list_tables()[0]
df = connector.get_records(table, query="name == 'GFP'")
assert len(df) == 1
assert df.iloc[0]["name"] == "GFP"
connector.disconnect()
|