|
| 1 | +""" |
| 2 | +Steps for testing -f/--file option behavioral tests. |
| 3 | +""" |
| 4 | + |
| 5 | +import subprocess |
| 6 | +import tempfile |
| 7 | +import os |
| 8 | +from behave import when, then |
| 9 | + |
| 10 | + |
| 11 | +@when('we create a file with "{content}"') |
| 12 | +def step_create_file_with_content(context, content): |
| 13 | + """Create a temporary file with the given content.""" |
| 14 | + # Create a temporary file that will be cleaned up automatically |
| 15 | + temp_file = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.sql') |
| 16 | + temp_file.write(content) |
| 17 | + temp_file.close() |
| 18 | + context.temp_file_path = temp_file.name |
| 19 | + |
| 20 | + |
| 21 | +@when('we run pgcli with -f and the file') |
| 22 | +def step_run_pgcli_with_f(context): |
| 23 | + """Run pgcli with -f flag and the temporary file.""" |
| 24 | + cmd = [ |
| 25 | + "pgcli", |
| 26 | + "-h", |
| 27 | + context.conf["host"], |
| 28 | + "-p", |
| 29 | + str(context.conf["port"]), |
| 30 | + "-U", |
| 31 | + context.conf["user"], |
| 32 | + "-d", |
| 33 | + context.conf["dbname"], |
| 34 | + "-f", |
| 35 | + context.temp_file_path, |
| 36 | + ] |
| 37 | + try: |
| 38 | + context.cmd_output = subprocess.check_output(cmd, cwd=context.package_root, stderr=subprocess.STDOUT, timeout=5) |
| 39 | + context.exit_code = 0 |
| 40 | + except subprocess.CalledProcessError as e: |
| 41 | + context.cmd_output = e.output |
| 42 | + context.exit_code = e.returncode |
| 43 | + except subprocess.TimeoutExpired: |
| 44 | + context.cmd_output = b"Command timed out" |
| 45 | + context.exit_code = -1 |
| 46 | + finally: |
| 47 | + # Clean up the temporary file |
| 48 | + if hasattr(context, 'temp_file_path') and os.path.exists(context.temp_file_path): |
| 49 | + os.unlink(context.temp_file_path) |
| 50 | + |
| 51 | + |
| 52 | +@when('we run pgcli with --file and the file') |
| 53 | +def step_run_pgcli_with_file(context): |
| 54 | + """Run pgcli with --file flag and the temporary file.""" |
| 55 | + cmd = [ |
| 56 | + "pgcli", |
| 57 | + "-h", |
| 58 | + context.conf["host"], |
| 59 | + "-p", |
| 60 | + str(context.conf["port"]), |
| 61 | + "-U", |
| 62 | + context.conf["user"], |
| 63 | + "-d", |
| 64 | + context.conf["dbname"], |
| 65 | + "--file", |
| 66 | + context.temp_file_path, |
| 67 | + ] |
| 68 | + try: |
| 69 | + context.cmd_output = subprocess.check_output(cmd, cwd=context.package_root, stderr=subprocess.STDOUT, timeout=5) |
| 70 | + context.exit_code = 0 |
| 71 | + except subprocess.CalledProcessError as e: |
| 72 | + context.cmd_output = e.output |
| 73 | + context.exit_code = e.returncode |
| 74 | + except subprocess.TimeoutExpired: |
| 75 | + context.cmd_output = b"Command timed out" |
| 76 | + context.exit_code = -1 |
| 77 | + finally: |
| 78 | + # Clean up the temporary file |
| 79 | + if hasattr(context, 'temp_file_path') and os.path.exists(context.temp_file_path): |
| 80 | + os.unlink(context.temp_file_path) |
| 81 | + |
| 82 | + |
| 83 | +@then("we see the query result") |
| 84 | +def step_see_query_result(context): |
| 85 | + """Verify that the query result is in the output.""" |
| 86 | + output = context.cmd_output.decode('utf-8') |
| 87 | + # Check for common query result indicators |
| 88 | + assert any([ |
| 89 | + "SELECT" in output, |
| 90 | + "test_diego_column" in output, |
| 91 | + "greeting" in output, |
| 92 | + "hello" in output, |
| 93 | + "+-" in output, # table border |
| 94 | + "|" in output, # table column separator |
| 95 | + ]), f"Expected query result in output, but got: {output}" |
| 96 | + |
| 97 | + |
| 98 | +@then("we see both query results") |
| 99 | +def step_see_both_query_results(context): |
| 100 | + """Verify that both query results are in the output.""" |
| 101 | + output = context.cmd_output.decode('utf-8') |
| 102 | + # Should contain output from both SELECT statements |
| 103 | + assert "SELECT" in output, f"Expected SELECT in output, but got: {output}" |
| 104 | + # The output should have multiple result sets |
| 105 | + assert output.count("SELECT") >= 2, f"Expected at least 2 SELECT results, but got: {output}" |
| 106 | + |
| 107 | + |
| 108 | +@then("we see the command output") |
| 109 | +def step_see_command_output(context): |
| 110 | + """Verify that the special command output is present.""" |
| 111 | + output = context.cmd_output.decode('utf-8') |
| 112 | + # `\dt` renders its column headers whether or not any tables exist, so the |
| 113 | + # headers are what tells us the special command actually ran and produced |
| 114 | + # its listing (rather than erroring out). |
| 115 | + for header in ("Schema", "Name", "Type", "Owner"): |
| 116 | + assert header in output, f"Expected {header!r} in \\dt output, but got: {output}" |
| 117 | + assert context.exit_code == 0, f"Expected exit code 0, but got: {context.exit_code}" |
| 118 | + |
| 119 | + |
| 120 | +@then("we see an error message") |
| 121 | +def step_see_error_message(context): |
| 122 | + """Verify that an error message is in the output.""" |
| 123 | + output = context.cmd_output.decode('utf-8') |
| 124 | + assert any([ |
| 125 | + "does not exist" in output, |
| 126 | + "error" in output.lower(), |
| 127 | + "ERROR" in output, |
| 128 | + ]), f"Expected error message in output, but got: {output}" |
| 129 | + |
| 130 | + |
| 131 | +@then("pgcli exits successfully") |
| 132 | +def step_pgcli_exits_successfully(context): |
| 133 | + """Verify that pgcli exited with code 0.""" |
| 134 | + assert context.exit_code == 0, f"Expected exit code 0, but got: {context.exit_code}" |
| 135 | + # Clean up |
| 136 | + context.cmd_output = None |
| 137 | + context.exit_code = None |
0 commit comments