1313
1414from khisto .core import compute_histograms
1515from khisto .core .backend import (
16- _parse_file_type ,
17- _process_histogram_files ,
16+ _process_histogram_file ,
1817 HistogramResult ,
1918)
2019
@@ -40,29 +39,6 @@ def _histogram_payload(
4039 }
4140
4241
43- class TestParseFileType :
44- """Tests for _parse_file_type function."""
45-
46- def test_series_json_file (self ):
47- """Test validation of histogram.series.json file."""
48- _parse_file_type (pathlib .Path ("histogram.series.json" ))
49-
50- def test_binary_series_json_file (self ):
51- """Test validation of histogram.series.bin.json file."""
52- _parse_file_type (pathlib .Path ("gaussian_histogram.series.bin.json" ))
53-
54- def test_invalid_file_name (self ):
55- """Test that invalid file names raise ValueError."""
56- with pytest .raises (ValueError , match = "Unrecognized histogram file name" ):
57- _parse_file_type (pathlib .Path ("histogram.json" ))
58-
59- with pytest .raises (ValueError , match = "Unrecognized histogram file name" ):
60- _parse_file_type (pathlib .Path ("histogram.csv" ))
61-
62- with pytest .raises (ValueError , match = "Unrecognized histogram file name" ):
63- _parse_file_type (pathlib .Path ("histogram.series.csv" ))
64-
65-
6642class TestHistogramResult :
6743 """Tests for HistogramResult dataclass."""
6844
@@ -161,6 +137,37 @@ def fail_run(*args, **kwargs):
161137 assert "khisto stderr message" in message
162138 assert "-j" in message
163139
140+ def test_compute_histograms_reports_cli_launch_failure (self , monkeypatch ):
141+ """Test that khisto launch failures raise a descriptive runtime error."""
142+
143+ def fail_run (* args , ** kwargs ):
144+ raise FileNotFoundError ("[Errno 2] No such file or directory: 'khisto'" )
145+
146+ monkeypatch .setattr ("khisto.core.backend.subprocess.run" , fail_run )
147+
148+ with pytest .raises (RuntimeError , match = "could not be started" ) as excinfo :
149+ compute_histograms (np .array ([0.0 , 1.0 ]))
150+
151+ message = str (excinfo .value )
152+ assert "No such file or directory" in message
153+ assert "-j" in message
154+
155+ def test_compute_histograms_reports_invalid_json_output (self , monkeypatch ):
156+ """Test that malformed JSON output raises a descriptive runtime error."""
157+
158+ def fake_run (cmd , ** kwargs ):
159+ output_path = pathlib .Path (cmd [- 1 ])
160+ output_path .write_text ("{not json" , encoding = "utf-8" )
161+
162+ monkeypatch .setattr ("khisto.core.backend.subprocess.run" , fake_run )
163+
164+ with pytest .raises (RuntimeError , match = "invalid JSON output" ) as excinfo :
165+ compute_histograms (np .array ([0.0 , 1.0 ]))
166+
167+ message = str (excinfo .value )
168+ assert "Expecting property name enclosed in double quotes" in message
169+ assert "-j" in message
170+
164171 def test_compute_histograms_writes_binary_input (self , monkeypatch ):
165172 """Test that compute_histograms writes binary input and requests JSON output."""
166173
@@ -178,15 +185,15 @@ def test_compute_histograms_writes_binary_input(self, monkeypatch):
178185 captured_values = None
179186
180187 monkeypatch .setattr (
181- "khisto.core.backend._process_histogram_files " ,
182- lambda temp_dir , base_name : expected_result ,
188+ "khisto.core.backend._process_histogram_file " ,
189+ lambda temp_output_file : expected_result ,
183190 )
184191
185192 def fake_run (cmd , ** kwargs ):
186193 nonlocal captured_values
187194 assert "-b" in cmd
188195 assert "-j" in cmd
189- assert cmd [- 1 ].endswith ("histogram.series .json" )
196+ assert cmd [- 1 ].endswith (".json" )
190197 input_path = pathlib .Path (cmd [- 2 ])
191198 captured_values = np .fromfile (input_path , dtype = np .float64 )
192199
@@ -233,7 +240,8 @@ def test_series_selects_finest_interpretable_histogram(self, tmp_path):
233240 }
234241 self ._write_json (tmp_path / "histogram.series.json" , payload )
235242
236- results = _process_histogram_files (str (tmp_path ), "histogram.series" )
243+ with (tmp_path / "histogram.series.json" ).open ("r" , encoding = "utf-8" ) as stream :
244+ results = _process_histogram_file (stream )
237245
238246 assert len (results ) == 3
239247 assert [result .granularity for result in results ] == [0 , 2 , 3 ]
@@ -270,7 +278,8 @@ def test_series_keeps_finest_histogram_when_all_interpretable(self, tmp_path):
270278 }
271279 self ._write_json (tmp_path / "histogram.series.json" , payload )
272280
273- results = _process_histogram_files (str (tmp_path ), "histogram.series" )
281+ with (tmp_path / "histogram.series.json" ).open ("r" , encoding = "utf-8" ) as stream :
282+ results = _process_histogram_file (stream )
274283
275284 assert len (results ) == 2
276285 assert [result .is_best for result in results ] == [False , True ]
0 commit comments