| 1 |
"""Tests for the DBCore database abstraction.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import shutil |
| 5 |
import unittest |
| 6 |
from pathlib import Path |
| 7 |
from tempfile import mkstemp |
| 8 |
from typing import ClassVar |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from beets import dbcore |
| 13 |
from beets.dbcore import Index, query, sort, types |
| 14 |
from beets.dbcore.db import DBCustomFunctionError |
| 15 |
from beets.library import Album, Item |
| 16 |
from beets.test.fixtures import ModelFixture1 |
| 17 |
|
| 18 |
# Fixture: concrete database and model classes. For migration tests, we |
| 19 |
# have multiple models with different numbers of fields. |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture |
| 23 |
def db(model): |
| 24 |
db = model(":memory:") |
| 25 |
yield db |
| 26 |
db._connection().close() |
| 27 |
|
| 28 |
|
| 29 |
class DatabaseFixture1(dbcore.Database): |
| 30 |
_models = (ModelFixture1,) |
| 31 |
|
| 32 |
|
| 33 |
class ModelFixture2(ModelFixture1): |
| 34 |
_fields: ClassVar[dict[str, dbcore.types.Type]] = { |
| 35 |
"id": dbcore.types.PRIMARY_ID, |
| 36 |
"field_one": dbcore.types.INTEGER, |
| 37 |
"field_two": dbcore.types.INTEGER, |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
class DatabaseFixture2(dbcore.Database): |
| 42 |
_models = (ModelFixture2,) |
| 43 |
|
| 44 |
|
| 45 |
class ModelFixture3(ModelFixture1): |
| 46 |
_fields: ClassVar[dict[str, dbcore.types.Type]] = { |
| 47 |
"id": dbcore.types.PRIMARY_ID, |
| 48 |
"field_one": dbcore.types.INTEGER, |
| 49 |
"field_two": dbcore.types.INTEGER, |
| 50 |
"field_three": dbcore.types.INTEGER, |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
class DatabaseFixture3(dbcore.Database): |
| 55 |
_models = (ModelFixture3,) |
| 56 |
|
| 57 |
|
| 58 |
class ModelFixture4(ModelFixture1): |
| 59 |
_fields: ClassVar[dict[str, dbcore.types.Type]] = { |
| 60 |
"id": dbcore.types.PRIMARY_ID, |
| 61 |
"field_one": dbcore.types.INTEGER, |
| 62 |
"field_two": dbcore.types.INTEGER, |
| 63 |
"field_three": dbcore.types.INTEGER, |
| 64 |
"field_four": dbcore.types.INTEGER, |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
class DatabaseFixture4(dbcore.Database): |
| 69 |
_models = (ModelFixture4,) |
| 70 |
|
| 71 |
|
| 72 |
class AnotherModelFixture(ModelFixture1): |
| 73 |
_table = "another" |
| 74 |
_flex_table = "anotherflex" |
| 75 |
_fields: ClassVar[dict[str, dbcore.types.Type]] = { |
| 76 |
"id": dbcore.types.PRIMARY_ID, |
| 77 |
"foo": dbcore.types.INTEGER, |
| 78 |
} |
| 79 |
_indices = (Index("another_foo_index", ("foo",)),) |
| 80 |
|
| 81 |
|
| 82 |
class ModelFixture5(ModelFixture1): |
| 83 |
_fields: ClassVar[dict[str, dbcore.types.Type]] = { |
| 84 |
"some_string_field": dbcore.types.STRING, |
| 85 |
"some_float_field": dbcore.types.FLOAT, |
| 86 |
"some_boolean_field": dbcore.types.BOOLEAN, |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
class DatabaseFixtureTwoModels(dbcore.Database): |
| 91 |
_models = (ModelFixture2, AnotherModelFixture) |
| 92 |
|
| 93 |
|
| 94 |
class TestDatabasePath: |
| 95 |
@pytest.mark.parametrize( |
| 96 |
"path", [":memory:", b":memory:", Path(":memory:")] |
| 97 |
) |
| 98 |
def test_path_inputs_are_stored_as_path(self, path): |
| 99 |
db = DatabaseFixture1(path) |
| 100 |
try: |
| 101 |
assert db.path == Path(":memory:") |
| 102 |
assert isinstance(db.path, Path) |
| 103 |
finally: |
| 104 |
db._connection().close() |
| 105 |
|
| 106 |
def test_bytes_filesystem_path_opens_decoded_path(self, tmp_path): |
| 107 |
db_path = tmp_path / "library.db" |
| 108 |
db = DatabaseFixture1(db_path) |
| 109 |
try: |
| 110 |
assert db.path == db_path |
| 111 |
assert db_path.exists() |
| 112 |
finally: |
| 113 |
db._connection().close() |
| 114 |
|
| 115 |
|
| 116 |
class ModelFixtureWithGetters(dbcore.Model): |
| 117 |
@classmethod |
| 118 |
def _getters(cls): |
| 119 |
return {"aComputedField": (lambda s: "thing")} |
| 120 |
|
| 121 |
|
| 122 |
class MigrationTest(unittest.TestCase): |
| 123 |
"""Tests the ability to change the database schema between |
| 124 |
versions. |
| 125 |
""" |
| 126 |
|
| 127 |
@classmethod |
| 128 |
def setUpClass(cls): |
| 129 |
handle, orig_libfile = mkstemp("orig_db") |
| 130 |
cls.orig_libfile = Path(orig_libfile) |
| 131 |
os.close(handle) |
| 132 |
# Set up a database with the two-field schema. |
| 133 |
old_lib = DatabaseFixture2(cls.orig_libfile) |
| 134 |
|
| 135 |
# Add an item to the old library. |
| 136 |
old_lib._connection().execute( |
| 137 |
"insert into test (field_one, field_two) values (4, 2)" |
| 138 |
) |
| 139 |
old_lib._connection().commit() |
| 140 |
old_lib._connection().close() |
| 141 |
del old_lib |
| 142 |
|
| 143 |
@classmethod |
| 144 |
def tearDownClass(cls): |
| 145 |
cls.orig_libfile.unlink() |
| 146 |
|
| 147 |
def setUp(self): |
| 148 |
handle, libfile = mkstemp("db") |
| 149 |
self.libfile = Path(libfile) |
| 150 |
os.close(handle) |
| 151 |
shutil.copyfile(self.orig_libfile, self.libfile) |
| 152 |
|
| 153 |
def tearDown(self): |
| 154 |
self.libfile.unlink() |
| 155 |
|
| 156 |
def test_open_with_same_fields_leaves_untouched(self): |
| 157 |
new_lib = DatabaseFixture2(self.libfile) |
| 158 |
c = new_lib._connection().cursor() |
| 159 |
c.execute("select * from test") |
| 160 |
row = c.fetchone() |
| 161 |
c.connection.close() |
| 162 |
assert len(row.keys()) == len(ModelFixture2._fields) |
| 163 |
|
| 164 |
def test_open_with_new_field_adds_column(self): |
| 165 |
new_lib = DatabaseFixture3(self.libfile) |
| 166 |
c = new_lib._connection().cursor() |
| 167 |
c.execute("select * from test") |
| 168 |
row = c.fetchone() |
| 169 |
c.connection.close() |
| 170 |
assert len(row.keys()) == len(ModelFixture3._fields) |
| 171 |
|
| 172 |
def test_open_with_fewer_fields_leaves_untouched(self): |
| 173 |
new_lib = DatabaseFixture1(self.libfile) |
| 174 |
c = new_lib._connection().cursor() |
| 175 |
c.execute("select * from test") |
| 176 |
row = c.fetchone() |
| 177 |
c.connection.close() |
| 178 |
assert len(row.keys()) == len(ModelFixture2._fields) |
| 179 |
|
| 180 |
def test_open_with_multiple_new_fields(self): |
| 181 |
new_lib = DatabaseFixture4(self.libfile) |
| 182 |
c = new_lib._connection().cursor() |
| 183 |
c.execute("select * from test") |
| 184 |
row = c.fetchone() |
| 185 |
c.connection.close() |
| 186 |
assert len(row.keys()) == len(ModelFixture4._fields) |
| 187 |
|
| 188 |
def test_extra_model_adds_table(self): |
| 189 |
new_lib = DatabaseFixtureTwoModels(self.libfile) |
| 190 |
c = new_lib._connection() |
| 191 |
c.execute("select * from another") |
| 192 |
c.close() |
| 193 |
|
| 194 |
def test_index_creation(self): |
| 195 |
"""Test that declared indices are created on database initialization.""" |
| 196 |
db = DatabaseFixture1(":memory:") |
| 197 |
with db.transaction() as tx: |
| 198 |
rows = tx.query("PRAGMA index_info(field_one_index)") |
| 199 |
assert len(rows) > 0 # Index exists |
| 200 |
db._connection().close() |
| 201 |
|
| 202 |
|
| 203 |
class TransactionTest(unittest.TestCase): |
| 204 |
def setUp(self): |
| 205 |
self.db = DatabaseFixture1(":memory:") |
| 206 |
|
| 207 |
def tearDown(self): |
| 208 |
self.db._connection().close() |
| 209 |
|
| 210 |
def test_mutate_increase_revision(self): |
| 211 |
old_rev = self.db.revision |
| 212 |
with self.db.transaction() as tx: |
| 213 |
tx.mutate( |
| 214 |
f"INSERT INTO {ModelFixture1._table} (field_one) VALUES (?);", |
| 215 |
(111,), |
| 216 |
) |
| 217 |
assert self.db.revision > old_rev |
| 218 |
|
| 219 |
def test_query_no_increase_revision(self): |
| 220 |
old_rev = self.db.revision |
| 221 |
with self.db.transaction() as tx: |
| 222 |
tx.query(f"PRAGMA table_info({ModelFixture1._table})") |
| 223 |
assert self.db.revision == old_rev |
| 224 |
|
| 225 |
|
| 226 |
class ModelTest(unittest.TestCase): |
| 227 |
def setUp(self): |
| 228 |
self.db = DatabaseFixture1(":memory:") |
| 229 |
|
| 230 |
def tearDown(self): |
| 231 |
self.db._connection().close() |
| 232 |
|
| 233 |
def test_add_model(self): |
| 234 |
model = ModelFixture1() |
| 235 |
model.add(self.db) |
| 236 |
rows = self.db._connection().execute("select * from test").fetchall() |
| 237 |
assert len(rows) == 1 |
| 238 |
|
| 239 |
def test_store_fixed_field(self): |
| 240 |
model = ModelFixture1() |
| 241 |
model.add(self.db) |
| 242 |
model.field_one = 123 |
| 243 |
model.store() |
| 244 |
row = self.db._connection().execute("select * from test").fetchone() |
| 245 |
assert row["field_one"] == 123 |
| 246 |
|
| 247 |
def test_revision(self): |
| 248 |
old_rev = self.db.revision |
| 249 |
model = ModelFixture1() |
| 250 |
model.add(self.db) |
| 251 |
model.store() |
| 252 |
assert model._revision == self.db.revision |
| 253 |
assert self.db.revision > old_rev |
| 254 |
|
| 255 |
mid_rev = self.db.revision |
| 256 |
model2 = ModelFixture1() |
| 257 |
model2.add(self.db) |
| 258 |
model2.store() |
| 259 |
assert model2._revision > mid_rev |
| 260 |
assert self.db.revision > model._revision |
| 261 |
|
| 262 |
# revision changed, so the model should be re-loaded |
| 263 |
model.load() |
| 264 |
assert model._revision == self.db.revision |
| 265 |
|
| 266 |
# revision did not change, so no reload |
| 267 |
mod2_old_rev = model2._revision |
| 268 |
model2.load() |
| 269 |
assert model2._revision == mod2_old_rev |
| 270 |
|
| 271 |
def test_retrieve_by_id(self): |
| 272 |
model = ModelFixture1() |
| 273 |
model.add(self.db) |
| 274 |
other_model = self.db._get(ModelFixture1, model.id) |
| 275 |
assert model.id == other_model.id |
| 276 |
|
| 277 |
def test_store_and_retrieve_flexattr(self): |
| 278 |
model = ModelFixture1() |
| 279 |
model.add(self.db) |
| 280 |
model.foo = "bar" |
| 281 |
model.store() |
| 282 |
|
| 283 |
other_model = self.db._get(ModelFixture1, model.id) |
| 284 |
assert other_model.foo == "bar" |
| 285 |
|
| 286 |
def test_store_flexattr_in_fields(self): |
| 287 |
model = ModelFixture1() |
| 288 |
model.add(self.db) |
| 289 |
model.foo = "bar" |
| 290 |
model.store(fields=["foo"]) |
| 291 |
|
| 292 |
other_model = self.db._get(ModelFixture1, model.id) |
| 293 |
assert other_model.foo == "bar" |
| 294 |
|
| 295 |
def test_delete_flexattr(self): |
| 296 |
model = ModelFixture1() |
| 297 |
model["foo"] = "bar" |
| 298 |
assert "foo" in model |
| 299 |
del model["foo"] |
| 300 |
assert "foo" not in model |
| 301 |
|
| 302 |
def test_delete_flexattr_via_dot(self): |
| 303 |
model = ModelFixture1() |
| 304 |
model["foo"] = "bar" |
| 305 |
assert "foo" in model |
| 306 |
del model.foo |
| 307 |
assert "foo" not in model |
| 308 |
|
| 309 |
def test_delete_flexattr_persists(self): |
| 310 |
model = ModelFixture1() |
| 311 |
model.add(self.db) |
| 312 |
model.foo = "bar" |
| 313 |
model.store() |
| 314 |
|
| 315 |
model = self.db._get(ModelFixture1, model.id) |
| 316 |
del model["foo"] |
| 317 |
model.store() |
| 318 |
|
| 319 |
model = self.db._get(ModelFixture1, model.id) |
| 320 |
assert "foo" not in model |
| 321 |
|
| 322 |
def test_delete_non_existent_attribute(self): |
| 323 |
model = ModelFixture1() |
| 324 |
with pytest.raises(KeyError): |
| 325 |
del model["foo"] |
| 326 |
|
| 327 |
def test_delete_fixed_attribute(self): |
| 328 |
model = ModelFixture5() |
| 329 |
model.some_string_field = "foo" |
| 330 |
model.some_float_field = 1.23 |
| 331 |
model.some_boolean_field = True |
| 332 |
|
| 333 |
for field, type_ in model._fields.items(): |
| 334 |
assert model[field] != type_.null |
| 335 |
|
| 336 |
for field, type_ in model._fields.items(): |
| 337 |
del model[field] |
| 338 |
assert model[field] == type_.null |
| 339 |
|
| 340 |
def test_null_value_normalization_by_type(self): |
| 341 |
model = ModelFixture1() |
| 342 |
model.field_one = None |
| 343 |
assert model.field_one == 0 |
| 344 |
|
| 345 |
def test_null_value_stays_none_for_untyped_field(self): |
| 346 |
model = ModelFixture1() |
| 347 |
model.foo = None |
| 348 |
assert model.foo is None |
| 349 |
|
| 350 |
def test_normalization_for_typed_flex_fields(self): |
| 351 |
model = ModelFixture1() |
| 352 |
model.some_float_field = None |
| 353 |
assert model.some_float_field == 0.0 |
| 354 |
|
| 355 |
def test_load_deleted_flex_field(self): |
| 356 |
model1 = ModelFixture1() |
| 357 |
model1["flex_field"] = True |
| 358 |
model1.add(self.db) |
| 359 |
|
| 360 |
model2 = self.db._get(ModelFixture1, model1.id) |
| 361 |
assert "flex_field" in model2 |
| 362 |
|
| 363 |
del model1["flex_field"] |
| 364 |
model1.store() |
| 365 |
|
| 366 |
model2.load() |
| 367 |
assert "flex_field" not in model2 |
| 368 |
|
| 369 |
def test_check_db_fails(self): |
| 370 |
with pytest.raises(ValueError, match="no database"): |
| 371 |
dbcore.Model()._check_db() |
| 372 |
with pytest.raises(ValueError, match="no id"): |
| 373 |
ModelFixture1(self.db)._check_db() |
| 374 |
|
| 375 |
dbcore.Model(self.db)._check_db(need_id=False) |
| 376 |
|
| 377 |
def test_missing_field(self): |
| 378 |
with pytest.raises(AttributeError): |
| 379 |
ModelFixture1(self.db).nonExistingKey |
| 380 |
|
| 381 |
def test_computed_field(self): |
| 382 |
model = ModelFixtureWithGetters() |
| 383 |
assert model.aComputedField == "thing" |
| 384 |
with pytest.raises(KeyError, match=r"computed field .+ deleted"): |
| 385 |
del model.aComputedField |
| 386 |
|
| 387 |
def test_items(self): |
| 388 |
model = ModelFixture1(self.db) |
| 389 |
model.id = 5 |
| 390 |
assert {("id", 5), ("field_one", 0), ("field_two", "")} == set( |
| 391 |
model.items() |
| 392 |
) |
| 393 |
|
| 394 |
def test_delete_internal_field(self): |
| 395 |
model = dbcore.Model() |
| 396 |
del model._db |
| 397 |
with pytest.raises(AttributeError): |
| 398 |
model._db |
| 399 |
|
| 400 |
def test_parse_nonstring(self): |
| 401 |
with pytest.raises(TypeError, match="must be a string"): |
| 402 |
dbcore.Model._parse(None, 42) |
| 403 |
|
| 404 |
def test_pickle_dump(self): |
| 405 |
"""Tries to pickle an item. This tests the __getstate__ method |
| 406 |
of the Model ABC""" |
| 407 |
import pickle |
| 408 |
|
| 409 |
model = ModelFixture1(self.db) |
| 410 |
model.add(self.db) |
| 411 |
model.field_one = 123 |
| 412 |
|
| 413 |
model.store() |
| 414 |
assert model._db is not None |
| 415 |
|
| 416 |
pickle.dumps(model) |
| 417 |
|
| 418 |
|
| 419 |
class FormatTest(unittest.TestCase): |
| 420 |
def test_format_fixed_field_integer(self): |
| 421 |
model = ModelFixture1() |
| 422 |
model.field_one = 155 |
| 423 |
value = model.formatted().get("field_one") |
| 424 |
assert value == "155" |
| 425 |
|
| 426 |
def test_format_fixed_field_integer_normalized(self): |
| 427 |
"""The normalize method of the Integer class rounds floats""" |
| 428 |
model = ModelFixture1() |
| 429 |
model.field_one = 142.432 |
| 430 |
value = model.formatted().get("field_one") |
| 431 |
assert value == "142" |
| 432 |
|
| 433 |
model.field_one = 142.863 |
| 434 |
value = model.formatted().get("field_one") |
| 435 |
assert value == "143" |
| 436 |
|
| 437 |
def test_format_fixed_field_string(self): |
| 438 |
model = ModelFixture1() |
| 439 |
model.field_two = "caf\xe9" |
| 440 |
value = model.formatted().get("field_two") |
| 441 |
assert value == "caf\xe9" |
| 442 |
|
| 443 |
def test_format_flex_field(self): |
| 444 |
model = ModelFixture1() |
| 445 |
model.other_field = "caf\xe9" |
| 446 |
value = model.formatted().get("other_field") |
| 447 |
assert value == "caf\xe9" |
| 448 |
|
| 449 |
def test_format_flex_field_bytes(self): |
| 450 |
model = ModelFixture1() |
| 451 |
model.other_field = "caf\xe9".encode() |
| 452 |
value = model.formatted().get("other_field") |
| 453 |
assert isinstance(value, str) |
| 454 |
assert value == "caf\xe9" |
| 455 |
|
| 456 |
def test_format_unset_field(self): |
| 457 |
model = ModelFixture1() |
| 458 |
value = model.formatted().get("other_field") |
| 459 |
assert value == "" |
| 460 |
|
| 461 |
def test_format_typed_flex_field(self): |
| 462 |
model = ModelFixture1() |
| 463 |
model.some_float_field = 3.14159265358979 |
| 464 |
value = model.formatted().get("some_float_field") |
| 465 |
assert value == "3.1" |
| 466 |
|
| 467 |
|
| 468 |
class FormattedMappingTest(unittest.TestCase): |
| 469 |
def test_keys_equal_model_keys(self): |
| 470 |
model = ModelFixture1() |
| 471 |
formatted = model.formatted() |
| 472 |
assert set(model.keys(True)) == set(formatted.keys()) |
| 473 |
|
| 474 |
def test_get_unset_field(self): |
| 475 |
model = ModelFixture1() |
| 476 |
formatted = model.formatted() |
| 477 |
with pytest.raises(KeyError): |
| 478 |
formatted["other_field"] |
| 479 |
|
| 480 |
def test_get_method_with_default(self): |
| 481 |
model = ModelFixture1() |
| 482 |
formatted = model.formatted() |
| 483 |
assert formatted.get("other_field") == "" |
| 484 |
|
| 485 |
def test_get_method_with_specified_default(self): |
| 486 |
model = ModelFixture1() |
| 487 |
formatted = model.formatted() |
| 488 |
assert formatted.get("other_field", "default") == "default" |
| 489 |
|
| 490 |
|
| 491 |
class ParseTest(unittest.TestCase): |
| 492 |
def test_parse_fixed_field(self): |
| 493 |
value = ModelFixture1._parse("field_one", "2") |
| 494 |
assert isinstance(value, int) |
| 495 |
assert value == 2 |
| 496 |
|
| 497 |
def test_parse_flex_field(self): |
| 498 |
value = ModelFixture1._parse("some_float_field", "2") |
| 499 |
assert isinstance(value, float) |
| 500 |
assert value == 2.0 |
| 501 |
|
| 502 |
def test_parse_untyped_field(self): |
| 503 |
value = ModelFixture1._parse("field_nine", "2") |
| 504 |
assert value == "2" |
| 505 |
|
| 506 |
|
| 507 |
class TestModelTypeFallback: |
| 508 |
def test_album_type_falls_back_to_item_type(self): |
| 509 |
typ = Album._type("artists") |
| 510 |
assert isinstance(typ, types.DelimitedString) |
| 511 |
assert typ is types.MULTI_VALUE_DSV |
| 512 |
|
| 513 |
def test_album_type_falls_back_to_item_type_other_list_fields(self): |
| 514 |
for field in ["genres", "composers", "artists_sort"]: |
| 515 |
typ = Album._type(field) |
| 516 |
assert isinstance(typ, types.DelimitedString), field |
| 517 |
|
| 518 |
def test_item_type_does_not_change(self): |
| 519 |
typ = Item._type("artists") |
| 520 |
assert isinstance(typ, types.DelimitedString) |
| 521 |
|
| 522 |
def test_unknown_key_falls_through_to_default(self): |
| 523 |
typ = Album._type("nonexistent_field_xyz") |
| 524 |
assert isinstance(typ, types.Default) |
| 525 |
|
| 526 |
|
| 527 |
class ResultsIteratorTest(unittest.TestCase): |
| 528 |
def setUp(self): |
| 529 |
self.db = DatabaseFixture1(":memory:") |
| 530 |
model = ModelFixture1() |
| 531 |
model["foo"] = "baz" |
| 532 |
model.add(self.db) |
| 533 |
model = ModelFixture1() |
| 534 |
model["foo"] = "bar" |
| 535 |
model.add(self.db) |
| 536 |
|
| 537 |
def tearDown(self): |
| 538 |
self.db._connection().close() |
| 539 |
|
| 540 |
def test_iterate_once(self): |
| 541 |
objs = self.db._get_results(ModelFixture1) |
| 542 |
assert len(list(objs)) == 2 |
| 543 |
|
| 544 |
def test_iterate_twice(self): |
| 545 |
objs = self.db._get_results(ModelFixture1) |
| 546 |
list(objs) |
| 547 |
assert len(list(objs)) == 2 |
| 548 |
|
| 549 |
def test_concurrent_iterators(self): |
| 550 |
results = self.db._get_results(ModelFixture1) |
| 551 |
it1 = iter(results) |
| 552 |
it2 = iter(results) |
| 553 |
next(it1) |
| 554 |
list(it2) |
| 555 |
assert len(list(it1)) == 1 |
| 556 |
|
| 557 |
def test_slow_query(self): |
| 558 |
q = query.SubstringQuery("foo", "ba", False) |
| 559 |
objs = self.db._get_results(ModelFixture1, q) |
| 560 |
assert len(list(objs)) == 2 |
| 561 |
|
| 562 |
def test_slow_query_negative(self): |
| 563 |
q = query.SubstringQuery("foo", "qux", False) |
| 564 |
objs = self.db._get_results(ModelFixture1, q) |
| 565 |
assert len(list(objs)) == 0 |
| 566 |
|
| 567 |
def test_iterate_slow_sort(self): |
| 568 |
s = sort.SlowFieldSort("foo") |
| 569 |
res = self.db._get_results(ModelFixture1, sort=s) |
| 570 |
objs = list(res) |
| 571 |
assert objs[0].foo == "bar" |
| 572 |
assert objs[1].foo == "baz" |
| 573 |
|
| 574 |
def test_unsorted_subscript(self): |
| 575 |
objs = self.db._get_results(ModelFixture1) |
| 576 |
assert objs[0].foo == "baz" |
| 577 |
assert objs[1].foo == "bar" |
| 578 |
|
| 579 |
def test_slow_sort_subscript(self): |
| 580 |
s = sort.SlowFieldSort("foo") |
| 581 |
objs = self.db._get_results(ModelFixture1, sort=s) |
| 582 |
assert objs[0].foo == "bar" |
| 583 |
assert objs[1].foo == "baz" |
| 584 |
|
| 585 |
def test_negative_subscript(self): |
| 586 |
objs = self.db._get_results(ModelFixture1) |
| 587 |
assert objs[-1].foo == "bar" |
| 588 |
assert objs[-2].foo == "baz" |
| 589 |
|
| 590 |
def test_slice(self): |
| 591 |
objs = self.db._get_results(ModelFixture1) |
| 592 |
assert [obj.foo for obj in objs[:]] == ["baz", "bar"] |
| 593 |
assert [obj.foo for obj in objs[1:]] == ["bar"] |
| 594 |
assert [obj.foo for obj in objs[::-1]] == ["bar", "baz"] |
| 595 |
|
| 596 |
def test_length(self): |
| 597 |
objs = self.db._get_results(ModelFixture1) |
| 598 |
assert len(objs) == 2 |
| 599 |
|
| 600 |
def test_out_of_range(self): |
| 601 |
objs = self.db._get_results(ModelFixture1) |
| 602 |
with pytest.raises(IndexError): |
| 603 |
objs[100] |
| 604 |
|
| 605 |
def test_no_results(self): |
| 606 |
assert ( |
| 607 |
self.db._get_results(ModelFixture1, query.FalseQuery()).get() |
| 608 |
is None |
| 609 |
) |
| 610 |
|
| 611 |
|
| 612 |
class TestException: |
| 613 |
@pytest.mark.parametrize("model", [DatabaseFixture1]) |
| 614 |
@pytest.mark.filterwarnings( |
| 615 |
"ignore: .*plz_raise.*: pytest.PytestUnraisableExceptionWarning" |
| 616 |
) |
| 617 |
@pytest.mark.filterwarnings( |
| 618 |
"error: .*: pytest.PytestUnraisableExceptionWarning" |
| 619 |
) |
| 620 |
def test_custom_function_error(self, db: DatabaseFixture1): |
| 621 |
def plz_raise(): |
| 622 |
raise Exception("i haz raized") |
| 623 |
|
| 624 |
db._connection().create_function("plz_raise", 0, plz_raise) |
| 625 |
|
| 626 |
with db.transaction() as tx: |
| 627 |
tx.mutate("insert into test (field_one) values (1)") |
| 628 |
|
| 629 |
with pytest.raises(DBCustomFunctionError): |
| 630 |
with db.transaction() as tx: |
| 631 |
tx.query("select * from test where plz_raise()") |