#!/usr/bin/env python3 """Validate Manual Invoice configuration and BigQuery access without reading rows.""" from __future__ import annotations import argparse import json import os import sys from runtime_config import load_runtime_config def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Test Manual Invoice BigQuery access.") parser.add_argument("--dataset", help="Optional project.dataset to inspect.") return parser.parse_args() def main() -> int: args = parse_args() config_path = load_runtime_config() try: from google.cloud import bigquery except ImportError: print("Run bootstrap.py before testing BigQuery.", file=sys.stderr) return 2 requested_project = os.environ.get("BQ_JOB_PROJECT") or None requested_location = os.environ.get("BQ_LOCATION") or None if requested_location and requested_location.lower() == "auto": requested_location = None try: client = bigquery.Client(project=requested_project) job = client.query( "SELECT 1 AS connection_ok", job_config=bigquery.QueryJobConfig(dry_run=True, use_query_cache=False), location=requested_location, ) result = { "status": "ok", "config_path": str(config_path) if config_path else None, "requested_job_project": requested_project, "job_project": client.project, "requested_location": requested_location, "location": job.location or requested_location, "bytes_processed": job.total_bytes_processed, } if args.dataset: dataset = client.get_dataset(args.dataset) result["dataset"] = f"{dataset.project}.{dataset.dataset_id}" result["dataset_location"] = dataset.location print(json.dumps(result, ensure_ascii=False)) return 0 except Exception as exc: print( json.dumps( { "status": "error", "config_path": str(config_path) if config_path else None, "error_type": type(exc).__name__, "error": str(exc), }, ensure_ascii=False, ) ) return 1 if __name__ == "__main__": raise SystemExit(main())