|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from verdin.api import ApiError |
| 6 | +from verdin.api.datasources import DataSourceNotFoundError |
| 7 | +from tests.utils import retry |
| 8 | + |
| 9 | + |
| 10 | +class TestDataSourcesApi: |
| 11 | + def test_list(self, client): |
| 12 | + response = client.api.datasources.list() |
| 13 | + |
| 14 | + assert len(response.datasources) >= 1 |
| 15 | + |
| 16 | + # find "simple" datasource in the list of data sources |
| 17 | + ds = None |
| 18 | + for datasource in response.datasources: |
| 19 | + if datasource["name"] == "simple": |
| 20 | + ds = datasource |
| 21 | + break |
| 22 | + |
| 23 | + assert ds |
| 24 | + |
| 25 | + # smoke tests some attributes |
| 26 | + assert ds["engine"]["engine"] == "MergeTree" |
| 27 | + assert "simple_kv" in [x["name"] for x in ds["used_by"]] |
| 28 | + |
| 29 | + def test_get_information(self, client): |
| 30 | + response = client.api.datasources.get_information("simple") |
| 31 | + |
| 32 | + # smoke tests some attributes |
| 33 | + assert response.info["name"] == "simple" |
| 34 | + assert response.info["engine"]["engine"] == "MergeTree" |
| 35 | + assert "simple_kv" in [x["name"] for x in response.info["used_by"]] |
| 36 | + |
| 37 | + def test_get_information_on_non_existing_datasource(self, client): |
| 38 | + with pytest.raises(DataSourceNotFoundError) as e: |
| 39 | + client.api.datasources.get_information("non_existing_datasource") |
| 40 | + |
| 41 | + e.match('Data Source "non_existing_datasource" does not exist') |
| 42 | + assert e.value.status_code == 404 |
| 43 | + |
| 44 | + def test_truncate(self, client): |
| 45 | + ds = client.datasource("simple") |
| 46 | + ds.append_ndjson( |
| 47 | + [ |
| 48 | + { |
| 49 | + "Id": "e7f2af3e-99d1-4d4f-8a8c-d6aee4ab89b0", |
| 50 | + "Timestamp": "2024-01-23T10:30:00.123456", |
| 51 | + "Key": "foo", |
| 52 | + "Value": "bar", |
| 53 | + }, |
| 54 | + { |
| 55 | + "Id": "d7792957-21d8-46e6-a4e0-188eb36e2758", |
| 56 | + "Timestamp": "2024-02-23T11:45:00.234567", |
| 57 | + "Key": "baz", |
| 58 | + "Value": "ed", |
| 59 | + }, |
| 60 | + ] |
| 61 | + ) |
| 62 | + |
| 63 | + def _wait_for_count(cnt: int): |
| 64 | + query = client.sql("SELECT count(*) as cnt FROM simple") |
| 65 | + assert query.json().data == [{"cnt": cnt}] |
| 66 | + |
| 67 | + retry(_wait_for_count, args=(2,)) |
| 68 | + |
| 69 | + client.api.datasources.truncate("simple") |
| 70 | + |
| 71 | + retry(_wait_for_count, args=(0,)) |
| 72 | + |
| 73 | + def test_append_to_non_existing_data_source(self, client): |
| 74 | + with pytest.raises(ApiError) as e: |
| 75 | + client.api.datasources.append("non_existing_datasource", "foo,bar\n") |
| 76 | + |
| 77 | + # this is odd behavior, but currently, this raises a 403, with the error |
| 78 | + # "Adding or modifying data sources to this workspace can only be done via deployments" |
| 79 | + # due to the way tinybird behaves (apparently it doesn't check mode=append) |
| 80 | + |
| 81 | + assert e.value.status_code == 403 |
| 82 | + |
| 83 | + def test_append_csv(self, client): |
| 84 | + ds = client.api.datasources |
| 85 | + |
| 86 | + data = "5b6859d2-e060-40a4-949a-7e7fab8e3207,2024-01-23T10:30:00.123456,foo,bar\n" |
| 87 | + data += "af49ffce-559c-426e-9787-ddb08628b547,2024-02-23T11:45:00.234567,baz,ed" |
| 88 | + |
| 89 | + response = ds.append("simple", data) |
| 90 | + assert not response.error |
| 91 | + assert response.quarantine_rows == 0 |
| 92 | + assert response.invalid_lines == 0 |
| 93 | + assert response.datasource["name"] == "simple" |
| 94 | + |
| 95 | + assert client.sql("SELECT * FROM simple").json().data == [ |
| 96 | + { |
| 97 | + "id": "5b6859d2-e060-40a4-949a-7e7fab8e3207", |
| 98 | + "timestamp": "2024-01-23 10:30:00.123456", |
| 99 | + "key": "foo", |
| 100 | + "value": "bar", |
| 101 | + }, |
| 102 | + { |
| 103 | + "id": "af49ffce-559c-426e-9787-ddb08628b547", |
| 104 | + "timestamp": "2024-02-23 11:45:00.234567", |
| 105 | + "key": "baz", |
| 106 | + "value": "ed", |
| 107 | + }, |
| 108 | + ] |
| 109 | + |
| 110 | + def test_append_csv_with_invalid_data(self, client): |
| 111 | + ds = client.api.datasources |
| 112 | + |
| 113 | + data = "5b6859d2-e060-40a4-949a-7e7fab8e3207,2024-01-23T10:30:00.123456,foo,bar\n" |
| 114 | + data += "af49ffce-559c-426e-9787-ddb08628b5472024-02-23T11:45:00.234567,baz,ed" # error in this line |
| 115 | + |
| 116 | + response = ds.append("simple", data) |
| 117 | + assert ( |
| 118 | + response.error |
| 119 | + == "There was an error with file contents: 1 row in quarantine and 1 invalid line." |
| 120 | + ) |
| 121 | + assert response.invalid_lines == 1 |
| 122 | + assert response.quarantine_rows == 1 |
| 123 | + |
| 124 | + def test_append_ndjson(self, client): |
| 125 | + ds = client.api.datasources |
| 126 | + ds.truncate("simple") |
| 127 | + |
| 128 | + records = [ |
| 129 | + { |
| 130 | + "Id": "e7f2af3e-99d1-4d4f-8a8c-d6aee4ab89b0", |
| 131 | + "Timestamp": "2024-01-23T10:30:00.123456", |
| 132 | + "Key": "foo", |
| 133 | + "Value": "bar", |
| 134 | + }, |
| 135 | + { |
| 136 | + "Id": "d7792957-21d8-46e6-a4e0-188eb36e2758", |
| 137 | + "Timestamp": "2024-02-23T11:45:00.234567", |
| 138 | + "Key": "baz", |
| 139 | + "Value": "ed", |
| 140 | + }, |
| 141 | + ] |
| 142 | + |
| 143 | + def _data(): |
| 144 | + for r in records: |
| 145 | + yield json.dumps(r) + "\n" |
| 146 | + |
| 147 | + response = ds.append("simple", _data(), format="ndjson") |
| 148 | + assert not response.error |
| 149 | + assert response.quarantine_rows == 0 |
| 150 | + assert response.invalid_lines == 0 |
| 151 | + assert response.datasource["name"] == "simple" |
| 152 | + |
| 153 | + assert client.sql("SELECT * FROM simple").json().data == [ |
| 154 | + { |
| 155 | + "id": "e7f2af3e-99d1-4d4f-8a8c-d6aee4ab89b0", |
| 156 | + "timestamp": "2024-01-23 10:30:00.123456", |
| 157 | + "key": "foo", |
| 158 | + "value": "bar", |
| 159 | + }, |
| 160 | + { |
| 161 | + "id": "d7792957-21d8-46e6-a4e0-188eb36e2758", |
| 162 | + "timestamp": "2024-02-23 11:45:00.234567", |
| 163 | + "key": "baz", |
| 164 | + "value": "ed", |
| 165 | + }, |
| 166 | + ] |
0 commit comments