Repository navigation
Expand file tree
/
Copy pathtest_experiment_database.py
More file actions
56 lines (40 loc) · 1.65 KB
/
Copy pathtest_experiment_database.py
File metadata and controls
56 lines (40 loc) · 1.65 KB
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
"""Unit tests for the reproducible Experiment Database and SQLite export."""
from __future__ import annotations
import sqlite3
from pathlib import Path
import numpy as np
import pytest
from lifeforge.experiments.database import ExperimentDatabase, ExperimentRecord
from lifeforge.substrates.ca import OuterTotalisticCA
def test_database_log_and_query(tmp_path: Path):
db_file = tmp_path / "test_experiments.jsonl"
db = ExperimentDatabase(db_file)
conway = OuterTotalisticCA.conway()
init_state = OuterTotalisticCA.random_state(20, 20, density=0.25, seed=42)
rec = db.log_simulation(conway, init_state, steps=30, seed=42)
assert db.count() == 1
assert rec.rule_notation == "B3/S23"
assert rec.substrate == "outer_totalistic_ca"
assert "modes" in rec.to_dict()
# Query tests
results = db.query(substrate="outer_totalistic_ca")
assert len(results) == 1
empty_results = db.query(substrate="non_existent")
assert len(empty_results) == 0
def test_sqlite_export(tmp_path: Path):
db_file = tmp_path / "test_experiments.jsonl"
sqlite_file = tmp_path / "experiments.db"
db = ExperimentDatabase(db_file)
ca = OuterTotalisticCA.seeds()
init_state = OuterTotalisticCA.random_state(20, 20, density=0.25, seed=1)
db.log_simulation(ca, init_state, steps=20, seed=1)
db.export_sqlite(sqlite_file)
assert sqlite_file.exists()
conn = sqlite3.connect(sqlite_file)
cur = conn.cursor()
cur.execute("SELECT rule_notation, substrate FROM experiments")
rows = cur.fetchall()
conn.close()
assert len(rows) == 1
assert rows[0][0] == "B2/S"
assert rows[0][1] == "outer_totalistic_ca"