Verify the manager writes results to the DB via on_result callback from run().
| 101 | |
| 102 | |
| 103 | class TestBatchPerBatchDbWrites(unittest.TestCase): |
| 104 | """Verify the manager writes results to the DB via on_result callback |
| 105 | from run().""" |
| 106 | |
| 107 | @patch("explainshell.extraction.common.gz_sha256", side_effect=lambda p: p) |
| 108 | @patch("explainshell.manager.run") |
| 109 | @patch("explainshell.manager.make_extractor") |
| 110 | @patch("explainshell.util.collect_gz_files") |
| 111 | @patch("explainshell.manager.config.source_from_path") |
| 112 | def test_db_writes_after_each_batch( |
| 113 | self, |
| 114 | mock_source, |
| 115 | mock_collect, |
| 116 | mock_make_ext, |
| 117 | mock_run, |
| 118 | _mock_sha, |
| 119 | ): |
| 120 | """Verify on_result callback writes to DB for each successful file.""" |
| 121 | with _temp_db() as db_path: |
| 122 | gz_files = [ |
| 123 | "/fake/distro/release/1/alpha.1.gz", |
| 124 | "/fake/distro/release/1/bravo.1.gz", |
| 125 | "/fake/distro/release/1/charlie.1.gz", |
| 126 | "/fake/distro/release/1/delta.1.gz", |
| 127 | ] |
| 128 | mock_collect.return_value = gz_files |
| 129 | mock_source.side_effect = lambda p: "/".join(p.split("/")[-4:]) |
| 130 | |
| 131 | mock_make_ext.return_value = MagicMock() |
| 132 | |
| 133 | # When run() is called, simulate per-file callbacks |
| 134 | writes_at_callback: list[int] = [] |
| 135 | |
| 136 | def _fake_run( |
| 137 | ext, |
| 138 | files, |
| 139 | batch_size=None, |
| 140 | jobs=1, |
| 141 | on_start=None, |
| 142 | on_result=None, |
| 143 | manifest=None, |
| 144 | ): |
| 145 | batch = BatchResult() |
| 146 | for gz_path in files: |
| 147 | if on_start: |
| 148 | on_start(gz_path) |
| 149 | source = mock_source(gz_path) |
| 150 | mp = _make_manpage_from_source(source) |
| 151 | raw = _make_raw(sha256=gz_path) |
| 152 | entry = ExtractionResult( |
| 153 | gz_path=gz_path, |
| 154 | outcome=ExtractionOutcome.SUCCESS, |
| 155 | mp=mp, |
| 156 | raw=raw, |
| 157 | stats=ExtractionStats(), |
| 158 | ) |
| 159 | batch.n_succeeded += 1 |
| 160 | if on_result: |
nothing calls this directly
no outgoing calls
no test coverage detected