(executor)
| 395 | |
| 396 | @dbtest |
| 397 | def test_watch_works(executor): |
| 398 | cli = PGCli(pgexecute=executor) |
| 399 | |
| 400 | def run_with_watch(query, target_call_count=1, expected_output="", expected_timing=None): |
| 401 | """ |
| 402 | :param query: Input to the CLI |
| 403 | :param target_call_count: Number of times the user lets the command run before Ctrl-C |
| 404 | :param expected_output: Substring expected to be found for each executed query |
| 405 | :param expected_timing: value `time.sleep` expected to be called with on every invocation |
| 406 | """ |
| 407 | with mock.patch.object(cli, "echo_via_pager") as mock_echo, mock.patch("pgcli.main.sleep") as mock_sleep: |
| 408 | mock_sleep.side_effect = [None] * (target_call_count - 1) + [KeyboardInterrupt] |
| 409 | cli.handle_watch_command(query) |
| 410 | # Validate that sleep was called with the right timing |
| 411 | for i in range(target_call_count - 1): |
| 412 | assert mock_sleep.call_args_list[i][0][0] == expected_timing |
| 413 | # Validate that the output of the query was expected |
| 414 | assert mock_echo.call_count == target_call_count |
| 415 | for i in range(target_call_count): |
| 416 | assert expected_output in mock_echo.call_args_list[i][0][0] |
| 417 | |
| 418 | # With no history, it errors. |
| 419 | with mock.patch("pgcli.main.click.secho") as mock_secho: |
| 420 | cli.handle_watch_command(r"\watch 2") |
| 421 | mock_secho.assert_called() |
| 422 | assert r"\watch cannot be used with an empty query" in mock_secho.call_args_list[0][0][0] |
| 423 | |
| 424 | # Usage 1: Run a query and then re-run it with \watch across two prompts. |
| 425 | run_with_watch("SELECT 111", expected_output="111") |
| 426 | run_with_watch("\\watch 10", target_call_count=2, expected_output="111", expected_timing=10) |
| 427 | |
| 428 | # Usage 2: Run a query and \watch via the same prompt. |
| 429 | run_with_watch( |
| 430 | "SELECT 222; \\watch 4", |
| 431 | target_call_count=3, |
| 432 | expected_output="222", |
| 433 | expected_timing=4, |
| 434 | ) |
| 435 | |
| 436 | # Usage 3: Re-run the last watched command with a new timing |
| 437 | run_with_watch("\\watch 5", target_call_count=4, expected_output="222", expected_timing=5) |
| 438 | |
| 439 | |
| 440 | def test_missing_rc_dir(tmpdir): |
nothing calls this directly
no test coverage detected