server/tests: use transactional db for faster unit tests
* `test_modify_saves_non_empty_diffs` needs non-transactional db, so moved to seperate file * Replaced incompatable usage of `db.session.rollback()` with parametrerized function calls * xfail conditionals for search removed, as we can no longer get current driver with binds * Also remove usage of deprecated `pytest.yield_fixture`
This commit is contained in:
parent
ad9d3599bc
commit
d083084407
8 changed files with 91 additions and 75 deletions
|
@ -145,8 +145,9 @@ def test_trying_to_update_without_privileges(
|
|||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("type", ["suggestions", "implications"])
|
||||
def test_trying_to_create_tags_without_privileges(
|
||||
config_injector, context_factory, tag_factory, user_factory
|
||||
config_injector, context_factory, tag_factory, user_factory, type
|
||||
):
|
||||
tag = tag_factory(names=["tag"])
|
||||
db.session.add(tag)
|
||||
|
@ -165,16 +166,7 @@ def test_trying_to_create_tags_without_privileges(
|
|||
with pytest.raises(errors.AuthError):
|
||||
api.tag_api.update_tag(
|
||||
context_factory(
|
||||
params={"suggestions": ["tag1", "tag2"], "version": 1},
|
||||
user=user_factory(rank=model.User.RANK_REGULAR),
|
||||
),
|
||||
{"tag_name": "tag"},
|
||||
)
|
||||
db.session.rollback()
|
||||
with pytest.raises(errors.AuthError):
|
||||
api.tag_api.update_tag(
|
||||
context_factory(
|
||||
params={"implications": ["tag1", "tag2"], "version": 1},
|
||||
params={type: ["tag1", "tag2"], "version": 1},
|
||||
user=user_factory(rank=model.User.RANK_REGULAR),
|
||||
),
|
||||
{"tag_name": "tag"},
|
||||
|
|
|
@ -43,14 +43,26 @@ def query_logger(pytestconfig):
|
|||
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
|
||||
|
||||
|
||||
@pytest.yield_fixture(scope="function", autouse=True)
|
||||
def session(query_logger, postgresql_db):
|
||||
@pytest.fixture(scope="function", autouse=True)
|
||||
def session(query_logger, transacted_postgresql_db):
|
||||
db.session = transacted_postgresql_db.session
|
||||
transacted_postgresql_db.create_table(*model.Base.metadata.sorted_tables)
|
||||
try:
|
||||
yield transacted_postgresql_db.session
|
||||
finally:
|
||||
transacted_postgresql_db.reset_db()
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def nontransacted_session(query_logger, postgresql_db):
|
||||
old_db_session = db.session
|
||||
db.session = postgresql_db.session
|
||||
postgresql_db.create_table(*model.Base.metadata.sorted_tables)
|
||||
try:
|
||||
yield postgresql_db.session
|
||||
finally:
|
||||
postgresql_db.reset_db()
|
||||
db.session = old_db_session
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import pytest # noqa: F401
|
||||
|
||||
from szurubooru import db, model
|
||||
from szurubooru.func import snapshots, users
|
||||
|
@ -144,46 +144,6 @@ def test_create(tag_factory, user_factory):
|
|||
assert results[0].data == "mocked"
|
||||
|
||||
|
||||
def test_modify_saves_non_empty_diffs(post_factory, user_factory):
|
||||
if "sqlite" in db.session.get_bind().driver:
|
||||
pytest.xfail(
|
||||
"SQLite doesn't support transaction isolation, "
|
||||
"which is required to retrieve original entity"
|
||||
)
|
||||
post = post_factory()
|
||||
post.notes = [model.PostNote(polygon=[(0, 0), (0, 1), (1, 1)], text="old")]
|
||||
user = user_factory()
|
||||
db.session.add_all([post, user])
|
||||
db.session.commit()
|
||||
post.source = "new source"
|
||||
post.notes = [model.PostNote(polygon=[(0, 0), (0, 1), (1, 1)], text="new")]
|
||||
db.session.flush()
|
||||
with patch("szurubooru.func.snapshots._post_to_webhooks"):
|
||||
snapshots.modify(post, user)
|
||||
db.session.flush()
|
||||
results = db.session.query(model.Snapshot).all()
|
||||
assert len(results) == 1
|
||||
assert results[0].data == {
|
||||
"type": "object change",
|
||||
"value": {
|
||||
"source": {
|
||||
"type": "primitive change",
|
||||
"old-value": None,
|
||||
"new-value": "new source",
|
||||
},
|
||||
"notes": {
|
||||
"type": "list change",
|
||||
"removed": [
|
||||
{"polygon": [[0, 0], [0, 1], [1, 1]], "text": "old"}
|
||||
],
|
||||
"added": [
|
||||
{"polygon": [[0, 0], [0, 1], [1, 1]], "text": "new"}
|
||||
],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_modify_doesnt_save_empty_diffs(tag_factory, user_factory):
|
||||
tag = tag_factory(names=["dummy"])
|
||||
user = user_factory()
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from szurubooru import db, model
|
||||
from szurubooru.func import snapshots
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def session(query_logger, postgresql_db):
|
||||
"""
|
||||
Override db session for this specific test section only
|
||||
"""
|
||||
db.session = postgresql_db.session
|
||||
postgresql_db.create_table(*model.Base.metadata.sorted_tables)
|
||||
try:
|
||||
yield postgresql_db.session
|
||||
finally:
|
||||
postgresql_db.reset_db()
|
||||
|
||||
|
||||
def test_modify_saves_non_empty_diffs(post_factory, user_factory):
|
||||
if "sqlite" in db.session.get_bind().driver:
|
||||
pytest.xfail(
|
||||
"SQLite doesn't support transaction isolation, "
|
||||
"which is required to retrieve original entity"
|
||||
)
|
||||
post = post_factory()
|
||||
post.notes = [model.PostNote(polygon=[(0, 0), (0, 1), (1, 1)], text="old")]
|
||||
user = user_factory()
|
||||
db.session.add_all([post, user])
|
||||
db.session.commit()
|
||||
post.source = "new source"
|
||||
post.notes = [model.PostNote(polygon=[(0, 0), (0, 1), (1, 1)], text="new")]
|
||||
db.session.flush()
|
||||
with patch("szurubooru.func.snapshots._post_to_webhooks"):
|
||||
snapshots.modify(post, user)
|
||||
db.session.flush()
|
||||
results = db.session.query(model.Snapshot).all()
|
||||
assert len(results) == 1
|
||||
assert results[0].data == {
|
||||
"type": "object change",
|
||||
"value": {
|
||||
"source": {
|
||||
"type": "primitive change",
|
||||
"old-value": None,
|
||||
"new-value": "new source",
|
||||
},
|
||||
"notes": {
|
||||
"type": "list change",
|
||||
"removed": [
|
||||
{"polygon": [[0, 0], [0, 1], [1, 1]], "text": "old"}
|
||||
],
|
||||
"added": [
|
||||
{"polygon": [[0, 0], [0, 1], [1, 1]], "text": "new"}
|
||||
],
|
||||
},
|
||||
},
|
||||
}
|
|
@ -107,17 +107,16 @@ def test_update_category_name_reusing_other_name(
|
|||
tag_categories.update_category_name(category, "NAME")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", ["name", "NAME"])
|
||||
def test_update_category_name_reusing_own_name(
|
||||
config_injector, tag_category_factory
|
||||
config_injector, tag_category_factory, name
|
||||
):
|
||||
config_injector({"tag_category_name_regex": ".*"})
|
||||
for name in ["name", "NAME"]:
|
||||
category = tag_category_factory(name="name")
|
||||
db.session.add(category)
|
||||
db.session.flush()
|
||||
tag_categories.update_category_name(category, name)
|
||||
assert category.name == name
|
||||
db.session.rollback()
|
||||
|
||||
|
||||
def test_update_category_color_with_empty_string(tag_category_factory):
|
||||
|
|
|
@ -513,15 +513,14 @@ def test_update_tag_names_trying_to_use_taken_name(
|
|||
tags.update_tag_names(tag, ["A"])
|
||||
|
||||
|
||||
def test_update_tag_names_reusing_own_name(config_injector, tag_factory):
|
||||
@pytest.mark.parametrize("name", list("aA"))
|
||||
def test_update_tag_names_reusing_own_name(config_injector, tag_factory, name):
|
||||
config_injector({"tag_name_regex": "^[a-zA-Z]*$"})
|
||||
for name in list("aA"):
|
||||
tag = tag_factory(names=["a"])
|
||||
db.session.add(tag)
|
||||
db.session.flush()
|
||||
tags.update_tag_names(tag, [name])
|
||||
assert [tag_name.name for tag_name in tag.names] == [name]
|
||||
db.session.rollback()
|
||||
|
||||
|
||||
def test_update_tag_names_changing_primary_name(config_injector, tag_factory):
|
||||
|
@ -533,7 +532,6 @@ def test_update_tag_names_changing_primary_name(config_injector, tag_factory):
|
|||
db.session.flush()
|
||||
db.session.refresh(tag)
|
||||
assert [tag_name.name for tag_name in tag.names] == ["b", "a"]
|
||||
db.session.rollback()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("attempt", ["name", "NAME", "alias", "ALIAS"])
|
||||
|
|
|
@ -136,8 +136,6 @@ def test_escaping(
|
|||
)
|
||||
db.session.flush()
|
||||
|
||||
if db_driver and db.session.get_bind().driver != db_driver:
|
||||
pytest.xfail()
|
||||
if expected_pool_names is None:
|
||||
with pytest.raises(errors.SearchError):
|
||||
executor.execute(input, offset=0, limit=100)
|
||||
|
|
|
@ -134,8 +134,6 @@ def test_escaping(executor, tag_factory, input, expected_tag_names, db_driver):
|
|||
)
|
||||
db.session.flush()
|
||||
|
||||
if db_driver and db.session.get_bind().driver != db_driver:
|
||||
pytest.xfail()
|
||||
if expected_tag_names is None:
|
||||
with pytest.raises(errors.SearchError):
|
||||
executor.execute(input, offset=0, limit=100)
|
||||
|
|
Loading…
Reference in a new issue