|
7 | 7 | import pytest
|
8 | 8 | from unittest.mock import Mock, MagicMock, patch
|
9 | 9 |
|
| 10 | +from databricks.sql.backend.sea.queue import JsonQueue, SeaResultSetQueueFactory |
| 11 | +from databricks.sql.backend.sea.models.base import ResultData, ResultManifest |
| 12 | +from databricks.sql.backend.sea.utils.constants import ResultFormat |
| 13 | + |
| 14 | + |
| 15 | +class TestJsonQueue: |
| 16 | + """Test suite for the JsonQueue class.""" |
| 17 | + |
| 18 | + @pytest.fixture |
| 19 | + def sample_data(self): |
| 20 | + """Create sample data for testing.""" |
| 21 | + return [ |
| 22 | + ["value1", 1, True], |
| 23 | + ["value2", 2, False], |
| 24 | + ["value3", 3, True], |
| 25 | + ["value4", 4, False], |
| 26 | + ["value5", 5, True], |
| 27 | + ] |
| 28 | + |
| 29 | + def test_init(self, sample_data): |
| 30 | + """Test initialization of JsonQueue.""" |
| 31 | + queue = JsonQueue(sample_data) |
| 32 | + assert queue.data_array == sample_data |
| 33 | + assert queue.cur_row_index == 0 |
| 34 | + assert queue.num_rows == len(sample_data) |
| 35 | + |
| 36 | + def test_next_n_rows_partial(self, sample_data): |
| 37 | + """Test fetching a subset of rows.""" |
| 38 | + queue = JsonQueue(sample_data) |
| 39 | + result = queue.next_n_rows(2) |
| 40 | + assert result == sample_data[:2] |
| 41 | + assert queue.cur_row_index == 2 |
| 42 | + |
| 43 | + def test_next_n_rows_all(self, sample_data): |
| 44 | + """Test fetching all rows.""" |
| 45 | + queue = JsonQueue(sample_data) |
| 46 | + result = queue.next_n_rows(len(sample_data)) |
| 47 | + assert result == sample_data |
| 48 | + assert queue.cur_row_index == len(sample_data) |
| 49 | + |
| 50 | + def test_next_n_rows_more_than_available(self, sample_data): |
| 51 | + """Test fetching more rows than available.""" |
| 52 | + queue = JsonQueue(sample_data) |
| 53 | + result = queue.next_n_rows(len(sample_data) + 10) |
| 54 | + assert result == sample_data |
| 55 | + assert queue.cur_row_index == len(sample_data) |
| 56 | + |
| 57 | + def test_next_n_rows_after_partial(self, sample_data): |
| 58 | + """Test fetching rows after a partial fetch.""" |
| 59 | + queue = JsonQueue(sample_data) |
| 60 | + queue.next_n_rows(2) # Fetch first 2 rows |
| 61 | + result = queue.next_n_rows(2) # Fetch next 2 rows |
| 62 | + assert result == sample_data[2:4] |
| 63 | + assert queue.cur_row_index == 4 |
| 64 | + |
| 65 | + def test_remaining_rows_all(self, sample_data): |
| 66 | + """Test fetching all remaining rows at once.""" |
| 67 | + queue = JsonQueue(sample_data) |
| 68 | + result = queue.remaining_rows() |
| 69 | + assert result == sample_data |
| 70 | + assert queue.cur_row_index == len(sample_data) |
| 71 | + |
| 72 | + def test_remaining_rows_after_partial(self, sample_data): |
| 73 | + """Test fetching remaining rows after a partial fetch.""" |
| 74 | + queue = JsonQueue(sample_data) |
| 75 | + queue.next_n_rows(2) # Fetch first 2 rows |
| 76 | + result = queue.remaining_rows() # Fetch remaining rows |
| 77 | + assert result == sample_data[2:] |
| 78 | + assert queue.cur_row_index == len(sample_data) |
| 79 | + |
| 80 | + def test_empty_data(self): |
| 81 | + """Test with empty data array.""" |
| 82 | + queue = JsonQueue([]) |
| 83 | + assert queue.next_n_rows(10) == [] |
| 84 | + assert queue.remaining_rows() == [] |
| 85 | + assert queue.cur_row_index == 0 |
| 86 | + assert queue.num_rows == 0 |
| 87 | + |
| 88 | + |
| 89 | +class TestSeaResultSetQueueFactory: |
| 90 | + """Test suite for the SeaResultSetQueueFactory class.""" |
| 91 | + |
| 92 | + @pytest.fixture |
| 93 | + def mock_sea_client(self): |
| 94 | + """Create a mock SEA client.""" |
| 95 | + client = Mock() |
| 96 | + client.max_download_threads = 10 |
| 97 | + return client |
| 98 | + |
| 99 | + @pytest.fixture |
| 100 | + def mock_description(self): |
| 101 | + """Create a mock column description.""" |
| 102 | + return [ |
| 103 | + ("col1", "string", None, None, None, None, None), |
| 104 | + ("col2", "int", None, None, None, None, None), |
| 105 | + ("col3", "boolean", None, None, None, None, None), |
| 106 | + ] |
| 107 | + |
| 108 | + def _create_empty_manifest(self, format: ResultFormat): |
| 109 | + return ResultManifest( |
| 110 | + format=format.value, |
| 111 | + schema={}, |
| 112 | + total_row_count=-1, |
| 113 | + total_byte_count=-1, |
| 114 | + total_chunk_count=-1, |
| 115 | + ) |
| 116 | + |
| 117 | + def test_build_queue_with_inline_data(self, mock_sea_client, mock_description): |
| 118 | + """Test building a queue with inline JSON data.""" |
| 119 | + # Create sample data for inline JSON result |
| 120 | + data = [ |
| 121 | + ["value1", "1", "true"], |
| 122 | + ["value2", "2", "false"], |
| 123 | + ] |
| 124 | + |
| 125 | + # Create a ResultData object with inline data |
| 126 | + result_data = ResultData(data=data, external_links=None, row_count=len(data)) |
| 127 | + |
| 128 | + # Create a manifest (not used for inline data) |
| 129 | + manifest = self._create_empty_manifest(ResultFormat.JSON_ARRAY) |
| 130 | + |
| 131 | + # Build the queue |
| 132 | + queue = SeaResultSetQueueFactory.build_queue( |
| 133 | + result_data, |
| 134 | + manifest, |
| 135 | + "test-statement-123", |
| 136 | + description=mock_description, |
| 137 | + sea_client=mock_sea_client, |
| 138 | + ) |
| 139 | + |
| 140 | + # Verify the queue is a JsonQueue with the correct data |
| 141 | + assert isinstance(queue, JsonQueue) |
| 142 | + assert queue.data_array == data |
| 143 | + assert queue.num_rows == len(data) |
| 144 | + |
| 145 | + def test_build_queue_with_empty_data(self, mock_sea_client, mock_description): |
| 146 | + """Test building a queue with empty data.""" |
| 147 | + # Create a ResultData object with no data |
| 148 | + result_data = ResultData(data=[], external_links=None, row_count=0) |
| 149 | + |
| 150 | + # Build the queue |
| 151 | + queue = SeaResultSetQueueFactory.build_queue( |
| 152 | + result_data, |
| 153 | + self._create_empty_manifest(ResultFormat.JSON_ARRAY), |
| 154 | + "test-statement-123", |
| 155 | + description=mock_description, |
| 156 | + sea_client=mock_sea_client, |
| 157 | + ) |
| 158 | + |
| 159 | + # Verify the queue is a JsonQueue with empty data |
| 160 | + assert isinstance(queue, JsonQueue) |
| 161 | + assert queue.data_array == [] |
| 162 | + assert queue.num_rows == 0 |
| 163 | + |
| 164 | + def test_build_queue_with_external_links(self, mock_sea_client, mock_description): |
| 165 | + """Test building a queue with external links raises NotImplementedError.""" |
| 166 | + # Create a ResultData object with external links |
| 167 | + result_data = ResultData( |
| 168 | + data=None, external_links=["link1", "link2"], row_count=10 |
| 169 | + ) |
| 170 | + |
| 171 | + # Verify that NotImplementedError is raised |
| 172 | + with pytest.raises( |
| 173 | + NotImplementedError, |
| 174 | + match="EXTERNAL_LINKS disposition is not implemented for SEA backend", |
| 175 | + ): |
| 176 | + SeaResultSetQueueFactory.build_queue( |
| 177 | + result_data, |
| 178 | + self._create_empty_manifest(ResultFormat.ARROW_STREAM), |
10 | 179 | "test-statement-123",
|
11 | 180 | description=mock_description,
|
12 | 181 | sea_client=mock_sea_client,
|
|
0 commit comments