|
7 | 7 | encoder's bytes. |
8 | 8 | """ |
9 | 9 |
|
| 10 | +import contextlib |
| 11 | +import errno |
10 | 12 | import os |
11 | 13 | import pathlib |
12 | 14 | import shutil |
@@ -81,6 +83,22 @@ async def _arun(cmd) -> None: |
81 | 83 | await trio.run_process(cmd, capture_stdout=True, capture_stderr=True, check=True) |
82 | 84 |
|
83 | 85 |
|
| 86 | +@contextlib.asynccontextmanager |
| 87 | +async def _mount_bgen(tmp_path, vcz): |
| 88 | + """Mount ``vcz`` as a BGEN fileset; yield ``(mnt, basename)``.""" |
| 89 | + mnt = tmp_path / "mnt" |
| 90 | + mnt.mkdir() |
| 91 | + basename = vcz.path.stem |
| 92 | + sock_path = tmp_path / "bgen.sock" |
| 93 | + async with await encoder_client.EncoderClient.start( |
| 94 | + str(vcz.path), sock_path, formats.BGEN_SPEC |
| 95 | + ) as client: |
| 96 | + ops = encoder_ops.EncoderOps(client, basename, formats.BGEN_SPEC) |
| 97 | + async with fuse_adapter.mount(ops, str(mnt)): |
| 98 | + await _wait_for_mount(mnt) |
| 99 | + yield mnt, basename |
| 100 | + |
| 101 | + |
84 | 102 | class TestBgenBytes: |
85 | 103 | async def test_full_bgen_matches_encoder(self, fx_mounted_bgen): |
86 | 104 | mnt, basename, expected, _ = fx_mounted_bgen |
@@ -207,6 +225,56 @@ async def test_statfs_via_os_statvfs(self, fx_mounted_bgen): |
207 | 225 | assert info.f_namemax >= 255 |
208 | 226 |
|
209 | 227 |
|
| 228 | +class TestHaploidAndMixedPloidy: |
| 229 | + """Pins the mount-level behaviour for non-diploid VCZ inputs. |
| 230 | +
|
| 231 | + See ``tests/test_formats.py::TestBgenSpecHaploid`` and |
| 232 | + ``TestBgenSpecMixedPloidy`` for the encoder-layer contracts these |
| 233 | + end-to-end tests exercise. |
| 234 | + """ |
| 235 | + |
| 236 | + async def test_haploid_bgen_matches_encoder(self, tmp_path, fx_haploid_vcz): |
| 237 | + """Pure haploid is fully supported by BgenEncoder; the mounted |
| 238 | + ``.bgen`` bytes must match the in-process encoder's output.""" |
| 239 | + expected = _encoder_bytes(fx_haploid_vcz.path) |
| 240 | + async with _mount_bgen(tmp_path, fx_haploid_vcz) as (mnt, basename): |
| 241 | + data = await trio.to_thread.run_sync((mnt / f"{basename}.bgen").read_bytes) |
| 242 | + assert data == expected |
| 243 | + |
| 244 | + @needs_plink2 |
| 245 | + async def test_haploid_bgen_plink2_freq(self, tmp_path, fx_haploid_vcz): |
| 246 | + """``plink2 --bgen`` reads the mounted haploid view end-to-end.""" |
| 247 | + async with _mount_bgen(tmp_path, fx_haploid_vcz) as (mnt, basename): |
| 248 | + out = tmp_path / "p2_freq_hap" |
| 249 | + await _arun( |
| 250 | + [ |
| 251 | + PLINK2, |
| 252 | + "--bgen", |
| 253 | + str(mnt / f"{basename}.bgen"), |
| 254 | + "ref-first", |
| 255 | + "--sample", |
| 256 | + str(mnt / f"{basename}.sample"), |
| 257 | + "--freq", |
| 258 | + "--out", |
| 259 | + str(out), |
| 260 | + ] |
| 261 | + ) |
| 262 | + assert out.with_suffix(".afreq").exists() |
| 263 | + |
| 264 | + async def test_mixed_ploidy_bgen_read_raises_eio( |
| 265 | + self, tmp_path, fx_mixed_ploidy_vcz |
| 266 | + ): |
| 267 | + """The fixed-size BGEN encoder cannot represent mixed ploidy. |
| 268 | + The mount and the static sidecars (``.sample``, ``.bgen.bgi``) |
| 269 | + still serve, but the first ``.bgen`` read fails with EIO.""" |
| 270 | + async with _mount_bgen(tmp_path, fx_mixed_ploidy_vcz) as (mnt, basename): |
| 271 | + await trio.to_thread.run_sync((mnt / f"{basename}.sample").read_bytes) |
| 272 | + await trio.to_thread.run_sync((mnt / f"{basename}.bgen.bgi").read_bytes) |
| 273 | + with pytest.raises(OSError, match="Input/output error") as excinfo: |
| 274 | + await trio.to_thread.run_sync((mnt / f"{basename}.bgen").read_bytes) |
| 275 | + assert excinfo.value.errno == errno.EIO |
| 276 | + |
| 277 | + |
210 | 278 | class TestAccessTrace: |
211 | 279 | """Captures access patterns for inspection in phase-2 design.""" |
212 | 280 |
|
|
0 commit comments