|
1 | 1 | """ |
2 | 2 | Tests for list-based iteration in insert_all and upsert_all |
3 | 3 | """ |
| 4 | + |
4 | 5 | import pytest |
5 | 6 | from sqlite_utils import Database |
6 | 7 |
|
@@ -173,3 +174,92 @@ def test_backwards_compatibility_dict_mode(): |
173 | 174 | rows = list(db["people"].rows) |
174 | 175 | assert len(rows) == 2 |
175 | 176 | assert rows[0] == {"id": 1, "name": "Alice", "age": 30} |
| 177 | + |
| 178 | + |
| 179 | +def test_insert_all_tuple_mode_basic(): |
| 180 | + """Test basic insert_all with tuple-based iteration""" |
| 181 | + db = Database(memory=True) |
| 182 | + |
| 183 | + def data_generator(): |
| 184 | + # First yield column names as tuple |
| 185 | + yield ("id", "name", "age") |
| 186 | + # Then yield data rows as tuples |
| 187 | + yield (1, "Alice", 30) |
| 188 | + yield (2, "Bob", 25) |
| 189 | + yield (3, "Charlie", 35) |
| 190 | + |
| 191 | + db["people"].insert_all(data_generator()) |
| 192 | + |
| 193 | + rows = list(db["people"].rows) |
| 194 | + assert len(rows) == 3 |
| 195 | + assert rows[0] == {"id": 1, "name": "Alice", "age": 30} |
| 196 | + assert rows[1] == {"id": 2, "name": "Bob", "age": 25} |
| 197 | + assert rows[2] == {"id": 3, "name": "Charlie", "age": 35} |
| 198 | + |
| 199 | + |
| 200 | +def test_insert_all_mixed_list_tuple(): |
| 201 | + """Test insert_all with mixed lists and tuples for data rows""" |
| 202 | + db = Database(memory=True) |
| 203 | + |
| 204 | + def data_generator(): |
| 205 | + # Column names as list |
| 206 | + yield ["id", "name", "age"] |
| 207 | + # Mix of list and tuple data rows |
| 208 | + yield [1, "Alice", 30] |
| 209 | + yield (2, "Bob", 25) |
| 210 | + yield [3, "Charlie", 35] |
| 211 | + yield (4, "Diana", 40) |
| 212 | + |
| 213 | + db["people"].insert_all(data_generator()) |
| 214 | + |
| 215 | + rows = list(db["people"].rows) |
| 216 | + assert len(rows) == 4 |
| 217 | + assert rows[0] == {"id": 1, "name": "Alice", "age": 30} |
| 218 | + assert rows[1] == {"id": 2, "name": "Bob", "age": 25} |
| 219 | + assert rows[2] == {"id": 3, "name": "Charlie", "age": 35} |
| 220 | + assert rows[3] == {"id": 4, "name": "Diana", "age": 40} |
| 221 | + |
| 222 | + |
| 223 | +def test_upsert_all_tuple_mode(): |
| 224 | + """Test upsert_all with tuple-based iteration""" |
| 225 | + db = Database(memory=True) |
| 226 | + |
| 227 | + # Initial insert with tuples |
| 228 | + def initial_data(): |
| 229 | + yield ("id", "name", "value") |
| 230 | + yield (1, "Alice", 100) |
| 231 | + yield (2, "Bob", 200) |
| 232 | + |
| 233 | + db["data"].insert_all(initial_data(), pk="id") |
| 234 | + |
| 235 | + # Upsert with tuples |
| 236 | + def upsert_data(): |
| 237 | + yield ("id", "name", "value") |
| 238 | + yield (1, "Alice", 150) # Update existing |
| 239 | + yield (3, "Charlie", 300) # Insert new |
| 240 | + |
| 241 | + db["data"].upsert_all(upsert_data(), pk="id") |
| 242 | + |
| 243 | + rows = list(db["data"].rows_where(order_by="id")) |
| 244 | + assert len(rows) == 3 |
| 245 | + assert rows[0] == {"id": 1, "name": "Alice", "value": 150} |
| 246 | + assert rows[1] == {"id": 2, "name": "Bob", "value": 200} |
| 247 | + assert rows[2] == {"id": 3, "name": "Charlie", "value": 300} |
| 248 | + |
| 249 | + |
| 250 | +def test_tuple_mode_shorter_rows(): |
| 251 | + """Test that tuple rows shorter than column list get NULL values""" |
| 252 | + db = Database(memory=True) |
| 253 | + |
| 254 | + def data_generator(): |
| 255 | + yield "id", "name", "age", "city" |
| 256 | + yield 1, "Alice", 30, "NYC" |
| 257 | + yield 2, "Bob" # Missing age and city |
| 258 | + yield 3, "Charlie", 35 # Missing city |
| 259 | + |
| 260 | + db["people"].insert_all(data_generator()) |
| 261 | + |
| 262 | + rows = list(db["people"].rows_where(order_by="id")) |
| 263 | + assert rows[0] == {"id": 1, "name": "Alice", "age": 30, "city": "NYC"} |
| 264 | + assert rows[1] == {"id": 2, "name": "Bob", "age": None, "city": None} |
| 265 | + assert rows[2] == {"id": 3, "name": "Charlie", "age": 35, "city": None} |
0 commit comments