|
9 | 9 |
|
10 | 10 | import pyarrow as pa |
11 | 11 |
|
12 | | -from vgi._test_fixtures.copy_to import ExampleLinesCopyToArgs, ExampleLinesCopyToFunction |
| 12 | +from vgi._test_fixtures.copy_to import ( |
| 13 | + ExampleLinesCopyToArgs, |
| 14 | + ExampleLinesCopyToFunction, |
| 15 | + SecretLinesCopyToArgs, |
| 16 | + SecretLinesCopyToFunction, |
| 17 | +) |
13 | 18 | from vgi._test_fixtures.worker import ExampleCatalog |
| 19 | +from vgi.table_function import ResolvedSecrets, SecretsAccessor |
14 | 20 |
|
15 | 21 | SCHEMA = pa.schema([("a", pa.int64()), ("b", pa.string())]) |
16 | 22 |
|
@@ -100,6 +106,81 @@ def test_close_empty_input_with_header_writes_header_only() -> None: |
100 | 106 | assert _read(out_name) == "a,b\n" |
101 | 107 |
|
102 | 108 |
|
| 109 | +# --------------------------------------------------------------------------- |
| 110 | +# Secret forwarding (CopyToFunction.on_secrets hook) |
| 111 | +# --------------------------------------------------------------------------- |
| 112 | + |
| 113 | + |
| 114 | +def _secret_params(store: _Store, secrets: ResolvedSecrets) -> types.SimpleNamespace: |
| 115 | + """A TableBufferingParams-shaped stub carrying resolved secrets for write/close.""" |
| 116 | + bind_call = types.SimpleNamespace(input_schema=SCHEMA) |
| 117 | + init_call = types.SimpleNamespace(bind_call=bind_call) |
| 118 | + return types.SimpleNamespace(storage=store, init_call=init_call, execution_id=b"x", secrets=secrets) |
| 119 | + |
| 120 | + |
| 121 | +def test_on_secrets_requests_destination_scoped_secret() -> None: |
| 122 | + """on_secrets() registers a pending lookup scoped to the COPY destination path.""" |
| 123 | + accessor = SecretsAccessor(None) # nothing resolved yet → first-call behavior |
| 124 | + params = types.SimpleNamespace( |
| 125 | + args=SecretLinesCopyToArgs(), # default secret_type='vgi_example' |
| 126 | + bind_call=types.SimpleNamespace(copy_to=types.SimpleNamespace(file_path="s3://bucket/out.bin")), |
| 127 | + secrets=accessor, |
| 128 | + ) |
| 129 | + SecretLinesCopyToFunction.on_secrets(params) |
| 130 | + # The hook must have asked the framework for a scoped two-phase resolution. |
| 131 | + assert accessor.needs_resolution |
| 132 | + pending = accessor.pending_lookups |
| 133 | + assert len(pending) == 1 |
| 134 | + assert pending[0].secret_type == "vgi_example" |
| 135 | + assert pending[0].scope == "s3://bucket/out.bin" |
| 136 | + |
| 137 | + |
| 138 | +def test_close_forwards_resolved_secret_api_key() -> None: |
| 139 | + """close() writes the resolved (destination-scoped) secret's api_key + row count.""" |
| 140 | + store = _Store() |
| 141 | + out_name = _tmp_path() |
| 142 | + secrets = ResolvedSecrets( |
| 143 | + { |
| 144 | + "writer_creds": { |
| 145 | + "type": pa.scalar("vgi_example"), |
| 146 | + "scope": pa.scalar(out_name), |
| 147 | + "api_key": pa.scalar("WRITER_KEY"), |
| 148 | + } |
| 149 | + } |
| 150 | + ) |
| 151 | + params = _secret_params(store, secrets) |
| 152 | + opts = SecretLinesCopyToArgs() |
| 153 | + |
| 154 | + SecretLinesCopyToFunction.write( |
| 155 | + batch=pa.record_batch({"a": [1, 2], "b": ["x", "y"]}, schema=SCHEMA), |
| 156 | + options=opts, |
| 157 | + file_path=out_name, |
| 158 | + params=params, |
| 159 | + ) |
| 160 | + n = SecretLinesCopyToFunction.close(options=opts, file_path=out_name, params=params) |
| 161 | + assert n == 2 |
| 162 | + assert _read(out_name) == "api_key=WRITER_KEY\nrows=2\n" |
| 163 | + |
| 164 | + |
| 165 | +def test_close_writes_none_when_secret_absent() -> None: |
| 166 | + """A genuinely missing secret resolves to 'NONE' (silent miss, not an error).""" |
| 167 | + store = _Store() |
| 168 | + out_name = _tmp_path() |
| 169 | + params = _secret_params(store, ResolvedSecrets()) |
| 170 | + opts = SecretLinesCopyToArgs() |
| 171 | + n = SecretLinesCopyToFunction.close(options=opts, file_path=out_name, params=params) |
| 172 | + assert n == 0 |
| 173 | + assert _read(out_name) == "api_key=NONE\nrows=0\n" |
| 174 | + |
| 175 | + |
| 176 | +def test_catalog_advertises_secret_lines_out_format() -> None: |
| 177 | + """The secret-forwarding writer is advertised like any other COPY TO format.""" |
| 178 | + formats = ExampleCatalog().copy_from_formats(attach_opaque_data=b"", transaction_opaque_data=None) |
| 179 | + by = {(f.direction, f.format_name): f for f in formats} |
| 180 | + assert ("to", "secret_lines_out") in by |
| 181 | + assert by[("to", "secret_lines_out")].handler == "secret_lines_writer" |
| 182 | + |
| 183 | + |
103 | 184 | def test_catalog_advertises_copy_to_format() -> None: |
104 | 185 | """The example catalog advertises example_lines_out with direction='to'.""" |
105 | 186 | formats = ExampleCatalog().copy_from_formats(attach_opaque_data=b"", transaction_opaque_data=None) |
|
0 commit comments