|
| 1 | +"""Integration tests for DatasetsClient with HTTP mocking.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import respx |
| 5 | +from httpx import Response |
| 6 | + |
| 7 | +from amp.admin import AdminClient |
| 8 | +from amp.admin.errors import DatasetNotFoundError, InvalidManifestError |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.integration |
| 12 | +class TestDatasetsClient: |
| 13 | + """Test DatasetsClient operations with mocked HTTP responses.""" |
| 14 | + |
| 15 | + @respx.mock |
| 16 | + def test_register_dataset(self): |
| 17 | + """Test dataset registration.""" |
| 18 | + manifest = { |
| 19 | + 'kind': 'manifest', |
| 20 | + 'dependencies': { 'eth': '_/[email protected]'}, |
| 21 | + 'tables': { |
| 22 | + 'blocks': { |
| 23 | + 'input': {'sql': 'SELECT * FROM eth.blocks'}, |
| 24 | + 'schema': {'arrow': {'fields': []}}, |
| 25 | + 'network': 'mainnet', |
| 26 | + } |
| 27 | + }, |
| 28 | + 'functions': {}, |
| 29 | + } |
| 30 | + |
| 31 | + respx.post('http://localhost:8080/datasets').mock(return_value=Response(201)) |
| 32 | + |
| 33 | + client = AdminClient('http://localhost:8080') |
| 34 | + client.datasets.register('_', 'test_dataset', '1.0.0', manifest) |
| 35 | + |
| 36 | + # Should complete without error |
| 37 | + |
| 38 | + @respx.mock |
| 39 | + def test_register_dataset_invalid_manifest(self): |
| 40 | + """Test dataset registration with invalid manifest.""" |
| 41 | + error_response = {'error_code': 'INVALID_MANIFEST', 'error_message': 'Manifest validation failed'} |
| 42 | + respx.post('http://localhost:8080/datasets').mock(return_value=Response(400, json=error_response)) |
| 43 | + |
| 44 | + client = AdminClient('http://localhost:8080') |
| 45 | + |
| 46 | + with pytest.raises(InvalidManifestError): |
| 47 | + client.datasets.register('_', 'test_dataset', '1.0.0', {}) |
| 48 | + |
| 49 | + @respx.mock |
| 50 | + def test_deploy_dataset(self): |
| 51 | + """Test dataset deployment.""" |
| 52 | + deploy_response = {'job_id': 123} |
| 53 | + respx.post('http://localhost:8080/datasets/_/test_dataset/versions/1.0.0/deploy').mock( |
| 54 | + return_value=Response(200, json=deploy_response) |
| 55 | + ) |
| 56 | + |
| 57 | + client = AdminClient('http://localhost:8080') |
| 58 | + response = client.datasets.deploy('_', 'test_dataset', '1.0.0', parallelism=4) |
| 59 | + |
| 60 | + assert response.job_id == 123 |
| 61 | + |
| 62 | + @respx.mock |
| 63 | + def test_deploy_dataset_with_end_block(self): |
| 64 | + """Test dataset deployment with end_block parameter.""" |
| 65 | + deploy_response = {'job_id': 456} |
| 66 | + respx.post('http://localhost:8080/datasets/_/test_dataset/versions/1.0.0/deploy').mock( |
| 67 | + return_value=Response(200, json=deploy_response) |
| 68 | + ) |
| 69 | + |
| 70 | + client = AdminClient('http://localhost:8080') |
| 71 | + response = client.datasets.deploy('_', 'test_dataset', '1.0.0', end_block='latest', parallelism=2) |
| 72 | + |
| 73 | + assert response.job_id == 456 |
| 74 | + |
| 75 | + @respx.mock |
| 76 | + def test_list_all_datasets(self): |
| 77 | + """Test listing all datasets.""" |
| 78 | + datasets_response = { |
| 79 | + 'datasets': [ |
| 80 | + {'namespace': '_', 'name': 'eth_firehose', 'latest_version': '1.0.0', 'versions': ['1.0.0']}, |
| 81 | + {'namespace': '_', 'name': 'base_firehose', 'latest_version': '0.1.0', 'versions': ['0.1.0']}, |
| 82 | + ] |
| 83 | + } |
| 84 | + respx.get('http://localhost:8080/datasets').mock(return_value=Response(200, json=datasets_response)) |
| 85 | + |
| 86 | + client = AdminClient('http://localhost:8080') |
| 87 | + response = client.datasets.list_all() |
| 88 | + |
| 89 | + assert len(response.datasets) == 2 |
| 90 | + assert response.datasets[0].name == 'eth_firehose' |
| 91 | + assert response.datasets[1].name == 'base_firehose' |
| 92 | + |
| 93 | + @respx.mock |
| 94 | + def test_get_versions(self): |
| 95 | + """Test getting dataset versions.""" |
| 96 | + versions_response = { |
| 97 | + 'namespace': '_', |
| 98 | + 'name': 'eth_firehose', |
| 99 | + 'versions': [ |
| 100 | + { |
| 101 | + 'version': '1.0.0', |
| 102 | + 'manifest_hash': 'hash1', |
| 103 | + 'created_at': '2024-01-01T00:00:00Z', |
| 104 | + 'updated_at': '2024-01-01T00:00:00Z', |
| 105 | + }, |
| 106 | + { |
| 107 | + 'version': '0.9.0', |
| 108 | + 'manifest_hash': 'hash2', |
| 109 | + 'created_at': '2024-01-01T00:00:00Z', |
| 110 | + 'updated_at': '2024-01-01T00:00:00Z', |
| 111 | + }, |
| 112 | + ], |
| 113 | + 'special_tags': {'latest': '1.0.0', 'dev': '1.0.0'}, |
| 114 | + } |
| 115 | + respx.get('http://localhost:8080/datasets/_/eth_firehose/versions').mock( |
| 116 | + return_value=Response(200, json=versions_response) |
| 117 | + ) |
| 118 | + |
| 119 | + client = AdminClient('http://localhost:8080') |
| 120 | + response = client.datasets.get_versions('_', 'eth_firehose') |
| 121 | + |
| 122 | + assert len(response.versions) == 2 |
| 123 | + assert response.special_tags.latest == '1.0.0' |
| 124 | + |
| 125 | + @respx.mock |
| 126 | + def test_get_version_info(self): |
| 127 | + """Test getting specific version info.""" |
| 128 | + version_info = { |
| 129 | + 'version': '1.0.0', |
| 130 | + 'created_at': '2024-01-01T00:00:00Z', |
| 131 | + 'updated_at': '2024-01-01T00:00:00Z', |
| 132 | + 'manifest_hash': 'abc123', |
| 133 | + } |
| 134 | + respx.get('http://localhost:8080/datasets/_/eth_firehose/versions/1.0.0').mock( |
| 135 | + return_value=Response(200, json=version_info) |
| 136 | + ) |
| 137 | + |
| 138 | + client = AdminClient('http://localhost:8080') |
| 139 | + response = client.datasets.get_version('_', 'eth_firehose', '1.0.0') |
| 140 | + |
| 141 | + assert response.manifest_hash == 'abc123' |
| 142 | + assert response.version == '1.0.0' |
| 143 | + |
| 144 | + @respx.mock |
| 145 | + def test_get_manifest(self): |
| 146 | + """Test getting dataset manifest.""" |
| 147 | + manifest = { |
| 148 | + 'kind': 'manifest', |
| 149 | + 'dependencies': {}, |
| 150 | + 'tables': {'blocks': {'input': {'sql': 'SELECT * FROM blocks'}, 'network': 'mainnet'}}, |
| 151 | + 'functions': {}, |
| 152 | + } |
| 153 | + respx.get('http://localhost:8080/datasets/_/eth_firehose/versions/1.0.0/manifest').mock( |
| 154 | + return_value=Response(200, json=manifest) |
| 155 | + ) |
| 156 | + |
| 157 | + client = AdminClient('http://localhost:8080') |
| 158 | + response = client.datasets.get_manifest('_', 'eth_firehose', '1.0.0') |
| 159 | + |
| 160 | + assert response['kind'] == 'manifest' |
| 161 | + assert 'blocks' in response['tables'] |
| 162 | + |
| 163 | + @respx.mock |
| 164 | + def test_delete_dataset(self): |
| 165 | + """Test deleting a dataset.""" |
| 166 | + respx.delete('http://localhost:8080/datasets/_/old_dataset').mock(return_value=Response(204)) |
| 167 | + |
| 168 | + client = AdminClient('http://localhost:8080') |
| 169 | + client.datasets.delete('_', 'old_dataset') |
| 170 | + |
| 171 | + # Should complete without error |
| 172 | + |
| 173 | + @respx.mock |
| 174 | + def test_dataset_not_found(self): |
| 175 | + """Test handling dataset not found error.""" |
| 176 | + error_response = {'error_code': 'DATASET_NOT_FOUND', 'error_message': 'Dataset not found'} |
| 177 | + respx.get('http://localhost:8080/datasets/_/missing/versions/1.0.0').mock( |
| 178 | + return_value=Response(404, json=error_response) |
| 179 | + ) |
| 180 | + |
| 181 | + client = AdminClient('http://localhost:8080') |
| 182 | + |
| 183 | + with pytest.raises(DatasetNotFoundError): |
| 184 | + client.datasets.get_version('_', 'missing', '1.0.0') |
0 commit comments