|
| 1 | +"""Tests for Store options support.""" |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +from wherobots.db.models import Store |
| 6 | +from wherobots.db.types import StorageFormat |
| 7 | + |
| 8 | + |
| 9 | +class TestStoreOptions: |
| 10 | + """Tests for the options field on Store.""" |
| 11 | + |
| 12 | + def test_default_options_is_none(self): |
| 13 | + store = Store(format=StorageFormat.PARQUET) |
| 14 | + assert store.options is None |
| 15 | + |
| 16 | + def test_options_set(self): |
| 17 | + store = Store( |
| 18 | + format=StorageFormat.CSV, |
| 19 | + options={"header": "false", "delimiter": "|"}, |
| 20 | + ) |
| 21 | + assert store.options == {"header": "false", "delimiter": "|"} |
| 22 | + |
| 23 | + def test_empty_options_normalized_to_none(self): |
| 24 | + store = Store(format=StorageFormat.PARQUET, options={}) |
| 25 | + assert store.options is None |
| 26 | + |
| 27 | + def test_none_options_stays_none(self): |
| 28 | + store = Store(format=StorageFormat.PARQUET, options=None) |
| 29 | + assert store.options is None |
| 30 | + |
| 31 | + def test_options_defensively_copied(self): |
| 32 | + original = {"header": "false"} |
| 33 | + store = Store(format=StorageFormat.CSV, options=original) |
| 34 | + # Mutating the original should not affect the store |
| 35 | + original["delimiter"] = "|" |
| 36 | + assert "delimiter" not in store.options |
| 37 | + |
| 38 | + |
| 39 | +class TestStoreForDownloadWithOptions: |
| 40 | + """Tests for Store.for_download() with options parameter.""" |
| 41 | + |
| 42 | + def test_for_download_default_no_options(self): |
| 43 | + store = Store.for_download() |
| 44 | + assert store.options is None |
| 45 | + |
| 46 | + def test_for_download_with_options(self): |
| 47 | + store = Store.for_download(options={"header": "false"}) |
| 48 | + assert store.options == {"header": "false"} |
| 49 | + assert store.single is True |
| 50 | + assert store.generate_presigned_url is True |
| 51 | + |
| 52 | + def test_for_download_with_format_and_options(self): |
| 53 | + store = Store.for_download( |
| 54 | + format=StorageFormat.CSV, |
| 55 | + options={"header": "false", "delimiter": "|"}, |
| 56 | + ) |
| 57 | + assert store.format == StorageFormat.CSV |
| 58 | + assert store.options == {"header": "false", "delimiter": "|"} |
| 59 | + |
| 60 | + def test_for_download_empty_options_normalized(self): |
| 61 | + store = Store.for_download(options={}) |
| 62 | + assert store.options is None |
| 63 | + |
| 64 | + |
| 65 | +class TestStoreSerializationWithOptions: |
| 66 | + """Tests for Store.to_dict() serialization.""" |
| 67 | + |
| 68 | + def test_serialize_without_options(self): |
| 69 | + store = Store.for_download(format=StorageFormat.GEOJSON) |
| 70 | + d = store.to_dict() |
| 71 | + assert d == { |
| 72 | + "format": "geojson", |
| 73 | + "single": "true", |
| 74 | + "generate_presigned_url": "true", |
| 75 | + } |
| 76 | + assert "options" not in d |
| 77 | + |
| 78 | + def test_serialize_with_options(self): |
| 79 | + store = Store.for_download( |
| 80 | + format=StorageFormat.CSV, |
| 81 | + options={"header": "false", "delimiter": "|"}, |
| 82 | + ) |
| 83 | + d = store.to_dict() |
| 84 | + assert d == { |
| 85 | + "format": "csv", |
| 86 | + "single": "true", |
| 87 | + "generate_presigned_url": "true", |
| 88 | + "options": {"header": "false", "delimiter": "|"}, |
| 89 | + } |
| 90 | + |
| 91 | + def test_serialize_empty_options_omitted(self): |
| 92 | + store = Store(format=StorageFormat.PARQUET, options={}) |
| 93 | + d = store.to_dict() |
| 94 | + assert "options" not in d |
| 95 | + |
| 96 | + def test_json_roundtrip_with_options(self): |
| 97 | + store = Store.for_download( |
| 98 | + format=StorageFormat.GEOJSON, |
| 99 | + options={"ignoreNullFields": "false"}, |
| 100 | + ) |
| 101 | + d = store.to_dict() |
| 102 | + payload = json.dumps(d) |
| 103 | + parsed = json.loads(payload) |
| 104 | + assert parsed["options"] == {"ignoreNullFields": "false"} |
| 105 | + |
| 106 | + def test_full_request_shape(self): |
| 107 | + """Verify the full execute_sql request dict shape with store options.""" |
| 108 | + store = Store.for_download( |
| 109 | + format=StorageFormat.CSV, |
| 110 | + options={"header": "false"}, |
| 111 | + ) |
| 112 | + request = { |
| 113 | + "kind": "execute_sql", |
| 114 | + "execution_id": "test-id", |
| 115 | + "statement": "SELECT 1", |
| 116 | + } |
| 117 | + store_dict = store.to_dict() |
| 118 | + request["store"] = store_dict |
| 119 | + |
| 120 | + assert request == { |
| 121 | + "kind": "execute_sql", |
| 122 | + "execution_id": "test-id", |
| 123 | + "statement": "SELECT 1", |
| 124 | + "store": { |
| 125 | + "format": "csv", |
| 126 | + "single": "true", |
| 127 | + "generate_presigned_url": "true", |
| 128 | + "options": {"header": "false"}, |
| 129 | + }, |
| 130 | + } |
0 commit comments