"""Inventory includes refusals and both question streams, and rejects stale totals.""" import csv import json import sqlite3 import pytest from np.bench_db import build, readonly, summary def test_bench_db(tmp_path): def put(path, value): path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(value)) def tsv(name, rows): with (tmp_path / name).open('w', newline='') as f: w = csv.DictWriter(f, fieldnames=rows[0], delimiter='\t') w.writeheader() w.writerows(rows) version = 'distance.value.at_time/single_ego' tsv('per-batch.tsv', [dict(batch='b', code='abc', denominator=2, rows=2, complete=1, questions=2)]) segs = [dict(id=s, batch='b', house='H1', humans_requested=2, humans_actual=2, complete=c, questions=n, rc=rc, refusal=reason) for s, c, n, rc, reason in [('a', 'True', 2, 0, ''), ('b', 'False', 0, 2, 'no speed')]] tsv('per-seg.tsv', segs) base = tmp_path / 'batches/b' put(base / 'batch.json', dict(selected_configs=[dict(id=s, duration_s=50, scene='scene', seed=17, observers=[{}, {}]) for s in ('a', 'b')])) for filename in ('groups.jsonl', 'questions.jsonl'): put(base / 'vout/a' / filename, dict(public=dict(version_id=version), candidate_id=filename, scope=dict(group=dict(unit='1.1', id='g1', form='1.1', set=0, kind=1, anchor=filename)))) put(base / 'vout/b/summary.json', dict(versions={'speed.value.between_events/single_ego': dict(reject_reasons={'missing': 3})})) out = tmp_path.parent / (tmp_path.name + '-stats') with pytest.raises(ValueError, match='outside'): build(tmp_path, tmp_path / 'stats') for _ in range(2): with readonly(build(tmp_path, out)) as db: result = summary(db) assert (result['houses'], result['segments'], result['complete'], result['questions']) == (1, 2, 1, 2) assert result['observers'][0]['observers'] == 2 assert db.execute('SELECT refusal FROM segments WHERE id="b"').fetchone() == ('no speed',) assert db.execute('SELECT unit,wp,side,answer_type FROM questions LIMIT 1').fetchone() == ('1.1', 'S1', 'single', 'scalar') assert any(any('missing' in r['reject_reasons'] for r in v['reject_reasons']) for v in result['zero_versions']) with pytest.raises(sqlite3.OperationalError): db.execute('DELETE FROM questions') with readonly(out / 'bench.sqlite') as db: assert db.execute('SELECT count(*) FROM groups').fetchone()[0] == 1 assert db.execute('SELECT count(*) FROM group_members').fetchone()[0] == 2 assert db.execute('SELECT count(*) FROM question_dimensions WHERE house="H1"').fetchone()[0] == 2 assert db.execute('SELECT sum(questions) FROM version_status').fetchone()[0] == 2 for table, count in [('batches', 1), ('segments', 2), ('questions', 2)]: with (out / f'{table}.csv').open() as f: assert len(list(csv.DictReader(f))) == count tsv('per-seg.tsv', segs[:1]) with pytest.raises(ValueError, match='segments/complete/questions'): build(tmp_path, out) put(base / 'report/audit-abc.json', dict(code='abc', records=[segs[1]])) with readonly(build(tmp_path, out)) as db: assert summary(db)['segments'] == 2 assert db.execute('SELECT manifest_source FROM segments WHERE id="b"').fetchone()[0] == 'audit.records' segs[0]['questions'] = 3 tsv('per-seg.tsv', segs) with pytest.raises(ValueError, match='JSONL questions=2'): build(tmp_path, out) with readonly(out / 'bench.sqlite') as db: assert summary(db)['questions'] == 2 def test_bodies_categories(): from np.bench_db import bodies, summary_of cfg = dict(observers=[dict(id='A', kind='virtual_bound'), dict(id='B', kind='embodied'), dict(id='C', kind='embodied', body='humanoid_cam')], cast=[dict(id='h0', kind='humanoid'), dict(id='q0', kind='quadruped')]) who = bodies(cfg) assert [(r[3], r[4]) for r in who if r[0] == 'observer'] == [('camera', 'virtual'), ('quadruped_cam', 'quadruped'), ('humanoid_cam', 'humanoid')] assert summary_of(who) == [2, 1, 1, 2, 'virtual,quadruped,humanoid']