File size: 5,055 Bytes
ea8c728 | 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 | import asyncio
import tempfile
from pathlib import Path
from shinka.database import DatabaseConfig, Program, ProgramDatabase
from shinka.database.async_dbase import AsyncProgramDatabase
def _program(program_id: str) -> Program:
return Program(
id=program_id,
code="def f():\n return 1\n",
correct=True,
combined_score=1.0,
generation=0,
island_idx=0,
)
def test_program_database_init_without_openai_key(monkeypatch):
"""DB construction should not require API credentials."""
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
with tempfile.TemporaryDirectory() as tmpdir:
db_path = Path(tmpdir) / "no_key_init.db"
db = ProgramDatabase(config=DatabaseConfig(db_path=str(db_path), num_islands=1))
try:
db.add(_program("p0"))
assert db.get("p0") is not None
finally:
db.close()
def test_async_db_add_without_openai_key_when_embeddings_disabled(monkeypatch):
"""Async wrapper should preserve disabled embedding mode in worker DBs."""
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
async def _run():
with tempfile.TemporaryDirectory() as tmpdir:
db_path = Path(tmpdir) / "no_key_async.db"
sync_db = ProgramDatabase(
config=DatabaseConfig(db_path=str(db_path), num_islands=1),
embedding_model="",
)
async_db = AsyncProgramDatabase(sync_db=sync_db)
try:
await async_db.add_program_async(_program("async-p0"))
assert sync_db.get("async-p0") is not None
finally:
await async_db.close_async()
sync_db.close()
asyncio.run(_run())
def test_async_db_add_skips_duplicate_source_job_id(monkeypatch):
"""Async DB writes should be idempotent for the same completed scheduler job."""
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
async def _run():
with tempfile.TemporaryDirectory() as tmpdir:
db_path = Path(tmpdir) / "duplicate_source_job.db"
sync_db = ProgramDatabase(
config=DatabaseConfig(db_path=str(db_path), num_islands=1),
embedding_model="",
)
async_db = AsyncProgramDatabase(sync_db=sync_db)
try:
first = _program("async-p0")
first.metadata = {"source_job_id": "job-123"}
second = _program("async-p1")
second.metadata = {"source_job_id": "job-123"}
await async_db.add_program_async(first)
await async_db.add_program_async(second)
assert sync_db.get("async-p0") is not None
assert sync_db.get("async-p1") is None
assert sync_db._count_programs_in_db() == 1
assert sync_db.has_program_with_source_job_id("job-123") is True
finally:
await async_db.close_async()
sync_db.close()
asyncio.run(_run())
def test_async_db_source_job_id_check_treats_inflight_insert_as_existing(monkeypatch):
"""Retries should see an in-flight source_job_id before commit finishes."""
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
async def _run():
with tempfile.TemporaryDirectory() as tmpdir:
db_path = Path(tmpdir) / "inflight_source_job.db"
sync_db = ProgramDatabase(
config=DatabaseConfig(db_path=str(db_path), num_islands=1),
embedding_model="",
)
async_db = AsyncProgramDatabase(sync_db=sync_db)
try:
async_db._in_flight_source_job_ids.add("job-123")
assert await async_db.has_program_with_source_job_id_async("job-123")
finally:
await async_db.close_async()
sync_db.close()
asyncio.run(_run())
def test_async_db_add_skips_source_job_id_while_another_insert_is_inflight(monkeypatch):
"""Do not insert a duplicate row while the same source job is still in flight."""
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
async def _run():
with tempfile.TemporaryDirectory() as tmpdir:
db_path = Path(tmpdir) / "inflight_duplicate_source_job.db"
sync_db = ProgramDatabase(
config=DatabaseConfig(db_path=str(db_path), num_islands=1),
embedding_model="",
)
async_db = AsyncProgramDatabase(sync_db=sync_db)
try:
async_db._in_flight_source_job_ids.add("job-123")
duplicate = _program("async-p1")
duplicate.metadata = {"source_job_id": "job-123"}
await async_db.add_program_async(duplicate)
assert sync_db.get("async-p1") is None
assert sync_db._count_programs_in_db() == 0
finally:
await async_db.close_async()
sync_db.close()
asyncio.run(_run())
|