|
| 1 | +""" |
| 2 | +Tests for the JsonQueue class. |
| 3 | +
|
| 4 | +This module contains tests for the JsonQueue class, which implements |
| 5 | +a queue for JSON array data returned by the SEA backend. |
| 6 | +""" |
| 7 | + |
| 8 | +import pytest |
| 9 | +from databricks.sql.utils import JsonQueue |
| 10 | + |
| 11 | + |
| 12 | +class TestJsonQueue: |
| 13 | + """Test suite for the JsonQueue class.""" |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def sample_data_array(self): |
| 17 | + """Create a sample data array for testing.""" |
| 18 | + return [ |
| 19 | + [1, "value1"], |
| 20 | + [2, "value2"], |
| 21 | + [3, "value3"], |
| 22 | + [4, "value4"], |
| 23 | + [5, "value5"], |
| 24 | + ] |
| 25 | + |
| 26 | + def test_init(self, sample_data_array): |
| 27 | + """Test initializing JsonQueue with a data array.""" |
| 28 | + queue = JsonQueue(sample_data_array) |
| 29 | + assert queue.data_array == sample_data_array |
| 30 | + assert queue.cur_row_index == 0 |
| 31 | + assert queue.n_valid_rows == 5 |
| 32 | + |
| 33 | + def test_next_n_rows_partial(self, sample_data_array): |
| 34 | + """Test getting a subset of rows.""" |
| 35 | + queue = JsonQueue(sample_data_array) |
| 36 | + rows = queue.next_n_rows(3) |
| 37 | + |
| 38 | + # Check that we got the first 3 rows |
| 39 | + assert rows == sample_data_array[:3] |
| 40 | + |
| 41 | + # Check that the current row index was updated |
| 42 | + assert queue.cur_row_index == 3 |
| 43 | + |
| 44 | + def test_next_n_rows_all(self, sample_data_array): |
| 45 | + """Test getting all rows at once.""" |
| 46 | + queue = JsonQueue(sample_data_array) |
| 47 | + rows = queue.next_n_rows(10) # More than available |
| 48 | + |
| 49 | + # Check that we got all rows |
| 50 | + assert rows == sample_data_array |
| 51 | + |
| 52 | + # Check that the current row index was updated |
| 53 | + assert queue.cur_row_index == 5 |
| 54 | + |
| 55 | + def test_next_n_rows_empty(self): |
| 56 | + """Test getting rows from an empty queue.""" |
| 57 | + queue = JsonQueue([]) |
| 58 | + rows = queue.next_n_rows(5) |
| 59 | + |
| 60 | + # Check that we got an empty list |
| 61 | + assert rows == [] |
| 62 | + |
| 63 | + # Check that the current row index was not updated |
| 64 | + assert queue.cur_row_index == 0 |
| 65 | + |
| 66 | + def test_next_n_rows_zero(self, sample_data_array): |
| 67 | + """Test getting zero rows.""" |
| 68 | + queue = JsonQueue(sample_data_array) |
| 69 | + rows = queue.next_n_rows(0) |
| 70 | + |
| 71 | + # Check that we got an empty list |
| 72 | + assert rows == [] |
| 73 | + |
| 74 | + # Check that the current row index was not updated |
| 75 | + assert queue.cur_row_index == 0 |
| 76 | + |
| 77 | + def test_next_n_rows_sequential(self, sample_data_array): |
| 78 | + """Test getting rows in multiple sequential calls.""" |
| 79 | + queue = JsonQueue(sample_data_array) |
| 80 | + |
| 81 | + # Get first 2 rows |
| 82 | + rows1 = queue.next_n_rows(2) |
| 83 | + assert rows1 == sample_data_array[:2] |
| 84 | + assert queue.cur_row_index == 2 |
| 85 | + |
| 86 | + # Get next 2 rows |
| 87 | + rows2 = queue.next_n_rows(2) |
| 88 | + assert rows2 == sample_data_array[2:4] |
| 89 | + assert queue.cur_row_index == 4 |
| 90 | + |
| 91 | + # Get remaining rows |
| 92 | + rows3 = queue.next_n_rows(2) |
| 93 | + assert rows3 == sample_data_array[4:] |
| 94 | + assert queue.cur_row_index == 5 |
| 95 | + |
| 96 | + def test_remaining_rows(self, sample_data_array): |
| 97 | + """Test getting all remaining rows.""" |
| 98 | + queue = JsonQueue(sample_data_array) |
| 99 | + |
| 100 | + # Get first 2 rows |
| 101 | + queue.next_n_rows(2) |
| 102 | + |
| 103 | + # Get remaining rows |
| 104 | + rows = queue.remaining_rows() |
| 105 | + |
| 106 | + # Check that we got the remaining rows |
| 107 | + assert rows == sample_data_array[2:] |
| 108 | + |
| 109 | + # Check that the current row index was updated to the end |
| 110 | + assert queue.cur_row_index == 5 |
| 111 | + |
| 112 | + def test_remaining_rows_empty(self): |
| 113 | + """Test getting remaining rows from an empty queue.""" |
| 114 | + queue = JsonQueue([]) |
| 115 | + rows = queue.remaining_rows() |
| 116 | + |
| 117 | + # Check that we got an empty list |
| 118 | + assert rows == [] |
| 119 | + |
| 120 | + # Check that the current row index was not updated |
| 121 | + assert queue.cur_row_index == 0 |
| 122 | + |
| 123 | + def test_remaining_rows_after_all_consumed(self, sample_data_array): |
| 124 | + """Test getting remaining rows after all rows have been consumed.""" |
| 125 | + queue = JsonQueue(sample_data_array) |
| 126 | + |
| 127 | + # Consume all rows |
| 128 | + queue.next_n_rows(10) |
| 129 | + |
| 130 | + # Try to get remaining rows |
| 131 | + rows = queue.remaining_rows() |
| 132 | + |
| 133 | + # Check that we got an empty list |
| 134 | + assert rows == [] |
| 135 | + |
| 136 | + # Check that the current row index was not updated |
| 137 | + assert queue.cur_row_index == 5 |
0 commit comments