|
1 | 1 | import os
|
| 2 | +import sys |
2 | 3 | import subprocess
|
3 | 4 | import tempfile
|
4 | 5 | from pathlib import Path
|
5 | 6 | import venv
|
6 | 7 |
|
| 8 | + |
7 | 9 | def test_package_installation_and_dependencies():
|
8 | 10 | """
|
9 | 11 | Test that the package can be installed in a fresh virtual environment
|
@@ -53,3 +55,170 @@ def test_package_installation_and_dependencies():
|
53 | 55 | # We consider the test passed if it can be installed and run
|
54 | 56 | # Even if it returns an error code for an empty directory
|
55 | 57 | assert True
|
| 58 | + |
| 59 | +def test_reference_url_integration(): |
| 60 | + """ |
| 61 | + Test that the EPPO_REFERENCE_URL environment variable is correctly added to the payload. |
| 62 | + """ |
| 63 | + # Create a temporary directory for test files |
| 64 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 65 | + # Create a simple valid YAML file |
| 66 | + test_dir = os.path.join(temp_dir, "reference_url_test") |
| 67 | + os.makedirs(test_dir) |
| 68 | + |
| 69 | + test_yaml = os.path.join(test_dir, "simple_metric.yaml") |
| 70 | + with open(test_yaml, "w") as f: |
| 71 | + f.write("""fact_sources: |
| 72 | +- name: Test Source |
| 73 | + sql: | |
| 74 | + SELECT |
| 75 | + ts as TS, |
| 76 | + user_id, |
| 77 | + value |
| 78 | + FROM test_table |
| 79 | + timestamp_column: TS |
| 80 | + entities: |
| 81 | + - entity_name: User |
| 82 | + column: user_id |
| 83 | + facts: |
| 84 | + - name: Test Fact |
| 85 | + column: value |
| 86 | +metrics: |
| 87 | +- name: Test Metric |
| 88 | + entity: User |
| 89 | + type: simple |
| 90 | + numerator: |
| 91 | + fact_name: Test Fact |
| 92 | + operation: sum |
| 93 | +""") |
| 94 | + |
| 95 | + # Also create a YAML file with its own reference_url |
| 96 | + yaml_with_ref = os.path.join(test_dir, "yaml_with_ref.yaml") |
| 97 | + yaml_ref_url = "https://yaml-ref-url.example.com" |
| 98 | + with open(yaml_with_ref, "w") as f: |
| 99 | + f.write(f"""reference_url: {yaml_ref_url} |
| 100 | +fact_sources: |
| 101 | +- name: Test Source With Ref |
| 102 | + sql: | |
| 103 | + SELECT |
| 104 | + ts as TS, |
| 105 | + user_id, |
| 106 | + value |
| 107 | + FROM test_table |
| 108 | + timestamp_column: TS |
| 109 | + entities: |
| 110 | + - entity_name: User |
| 111 | + column: user_id |
| 112 | + facts: |
| 113 | + - name: Test Fact With Ref |
| 114 | + column: value |
| 115 | +metrics: |
| 116 | +- name: Test Metric With Ref |
| 117 | + entity: User |
| 118 | + type: simple |
| 119 | + numerator: |
| 120 | + fact_name: Test Fact With Ref |
| 121 | + operation: sum |
| 122 | +""") |
| 123 | + |
| 124 | + # Set up test environment |
| 125 | + test_reference_url = "https://test-reference-url.example.com" |
| 126 | + |
| 127 | + # Save original environment |
| 128 | + original_env = os.environ.copy() |
| 129 | + |
| 130 | + try: |
| 131 | + |
| 132 | + # Install the package first in the current environment |
| 133 | + subprocess.run( |
| 134 | + [sys.executable, '-m', 'pip', 'install', '-e', str(Path(__file__).parent.parent.absolute())], |
| 135 | + check=True |
| 136 | + ) |
| 137 | + |
| 138 | + # Now import after installation |
| 139 | + from eppo_metrics_sync.helper import load_yaml |
| 140 | + from eppo_metrics_sync.eppo_metrics_sync import EppoMetricsSync |
| 141 | + |
| 142 | + # Set environment variables for testing |
| 143 | + os.environ["EPPO_API_KEY"] = "test_api_key" |
| 144 | + os.environ["EPPO_REFERENCE_URL"] = test_reference_url |
| 145 | + os.environ["EPPO_SYNC_TAG"] = "test_tag" |
| 146 | + |
| 147 | + # Test case 1: Directory with no reference_url - should use env var |
| 148 | + eppo_sync = EppoMetricsSync(directory=test_dir) |
| 149 | + eppo_sync.read_yaml_files() |
| 150 | + eppo_sync.validate() |
| 151 | + |
| 152 | + # Create the payload as it's done in the sync method |
| 153 | + payload = { |
| 154 | + "sync_tag": "test_tag", |
| 155 | + "fact_sources": eppo_sync.fact_sources, |
| 156 | + "metrics": eppo_sync.metrics |
| 157 | + } |
| 158 | + # Attach reference URL |
| 159 | + payload = eppo_sync._attach_reference_url(payload) |
| 160 | + |
| 161 | + # Verify the reference URL is in the payload |
| 162 | + assert "reference_url" in payload, "reference_url not found in payload" |
| 163 | + assert payload["reference_url"] == test_reference_url, \ |
| 164 | + f"Expected reference_url to be {test_reference_url}, got {payload.get('reference_url')}" |
| 165 | + |
| 166 | + # Also check that the reference URL is not in the individual metrics/fact_sources |
| 167 | + for fact_source in payload.get("fact_sources", []): |
| 168 | + assert "reference_url" not in fact_source, "reference_url incorrectly set in fact_source" |
| 169 | + |
| 170 | + for metric in payload.get("metrics", []): |
| 171 | + assert "reference_url" not in metric, "reference_url incorrectly set in metric" |
| 172 | + |
| 173 | + # Test case 2: Test without environment variable |
| 174 | + os.environ.pop("EPPO_REFERENCE_URL", None) |
| 175 | + |
| 176 | + eppo_sync_no_ref = EppoMetricsSync(directory=test_dir) |
| 177 | + eppo_sync_no_ref.read_yaml_files() |
| 178 | + eppo_sync_no_ref.validate() |
| 179 | + |
| 180 | + # Create payload without reference URL |
| 181 | + payload_no_ref = { |
| 182 | + "sync_tag": "test_tag", |
| 183 | + "fact_sources": eppo_sync_no_ref.fact_sources, |
| 184 | + "metrics": eppo_sync_no_ref.metrics |
| 185 | + } |
| 186 | + payload_no_ref = eppo_sync_no_ref._attach_reference_url(payload_no_ref) |
| 187 | + |
| 188 | + # Verify the reference URL is not in the payload when not provided |
| 189 | + assert "reference_url" not in payload_no_ref, "reference_url found in payload when not provided" |
| 190 | + |
| 191 | + # Test case 3: Test file with its own reference_url |
| 192 | + # Since EppoMetricsSync doesn't store the reference_url from the YAML, |
| 193 | + # we need to read it directly from the file |
| 194 | + yaml_data = load_yaml(yaml_with_ref) |
| 195 | + yaml_reference_url = yaml_data.get("reference_url") |
| 196 | + |
| 197 | + assert yaml_reference_url == yaml_ref_url, \ |
| 198 | + f"Expected reference_url in YAML to be {yaml_ref_url}, got {yaml_reference_url}" |
| 199 | + |
| 200 | + # Test case 4: Test env variable overriding YAML reference_url |
| 201 | + os.environ["EPPO_REFERENCE_URL"] = test_reference_url |
| 202 | + |
| 203 | + eppo_sync_override = EppoMetricsSync(directory=None) |
| 204 | + eppo_sync_override.load_eppo_yaml(yaml_with_ref) |
| 205 | + eppo_sync_override.validate() |
| 206 | + |
| 207 | + # Create payload with env var |
| 208 | + payload_override = { |
| 209 | + "sync_tag": "test_tag", |
| 210 | + "fact_sources": eppo_sync_override.fact_sources, |
| 211 | + "metrics": eppo_sync_override.metrics |
| 212 | + } |
| 213 | + payload_override = eppo_sync_override._attach_reference_url(payload_override) |
| 214 | + |
| 215 | + # The env variable should override the YAML file's reference_url |
| 216 | + assert "reference_url" in payload_override, "reference_url not found in payload with env override" |
| 217 | + assert payload_override["reference_url"] == test_reference_url, \ |
| 218 | + f"Expected reference_url to be {test_reference_url} (from env), got {payload_override.get('reference_url')}" |
| 219 | + |
| 220 | + finally: |
| 221 | + # Clean up |
| 222 | + # Restore original environment |
| 223 | + os.environ.clear() |
| 224 | + os.environ.update(original_env) |
0 commit comments