aboutsummaryrefslogtreecommitdiff
path: root/server/szurubooru/tests
diff options
context:
space:
mode:
authorHunternif2025-02-15 18:12:57 +0000
committerHunternif2025-02-15 18:12:57 +0000
commit6a5300502df8b4de9c8f269b444cd309f02b28ad (patch)
tree0cf2ba56bce802c94ad324f9eda7a31d37a42e2e /server/szurubooru/tests
parent34e7c883808738871aed371948a3c9896a95905c (diff)
parent376f687c386f65522b2f65e98b998b21af26ee29 (diff)
Merge branch 'master' into hunternif
Diffstat (limited to 'server/szurubooru/tests')
-rw-r--r--server/szurubooru/tests/api/test_tag_updating.py14
-rw-r--r--server/szurubooru/tests/assets/mov.movbin0 -> 844 bytes
-rw-r--r--server/szurubooru/tests/conftest.py16
-rw-r--r--server/szurubooru/tests/func/test_mime.py4
-rw-r--r--server/szurubooru/tests/func/test_net.py21
-rw-r--r--server/szurubooru/tests/func/test_snapshots.py42
-rw-r--r--server/szurubooru/tests/func/test_snapshots_transactional_isolation.py59
-rw-r--r--server/szurubooru/tests/func/test_tag_categories.py15
-rw-r--r--server/szurubooru/tests/func/test_tags.py16
-rw-r--r--server/szurubooru/tests/search/configs/test_pool_search_config.py2
-rw-r--r--server/szurubooru/tests/search/configs/test_post_search_config.py53
-rw-r--r--server/szurubooru/tests/search/configs/test_tag_search_config.py2
12 files changed, 167 insertions, 77 deletions
diff --git a/server/szurubooru/tests/api/test_tag_updating.py b/server/szurubooru/tests/api/test_tag_updating.py
index 9112c29..66939a4 100644
--- a/server/szurubooru/tests/api/test_tag_updating.py
+++ b/server/szurubooru/tests/api/test_tag_updating.py
@@ -167,8 +167,9 @@ def test_trying_to_create_metric_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)
@@ -187,16 +188,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"},
diff --git a/server/szurubooru/tests/assets/mov.mov b/server/szurubooru/tests/assets/mov.mov
new file mode 100644
index 0000000..911ee85
--- /dev/null
+++ b/server/szurubooru/tests/assets/mov.mov
Binary files differ
diff --git a/server/szurubooru/tests/conftest.py b/server/szurubooru/tests/conftest.py
index 50cbf7d..45113f5 100644
--- a/server/szurubooru/tests/conftest.py
+++ b/server/szurubooru/tests/conftest.py
@@ -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
diff --git a/server/szurubooru/tests/func/test_mime.py b/server/szurubooru/tests/func/test_mime.py
index b33746b..551ba7c 100644
--- a/server/szurubooru/tests/func/test_mime.py
+++ b/server/szurubooru/tests/func/test_mime.py
@@ -7,6 +7,7 @@ from szurubooru.func import mime
"input_path,expected_mime_type",
[
("mp4.mp4", "video/mp4"),
+ ("mov.mov", "video/quicktime"),
("webm.webm", "video/webm"),
("flash.swf", "application/x-shockwave-flash"),
("png.png", "image/png"),
@@ -35,6 +36,7 @@ def test_get_mime_type_for_empty_file():
[
("video/mp4", "mp4"),
("video/webm", "webm"),
+ ("video/quicktime", "mov"),
("application/x-shockwave-flash", "swf"),
("image/png", "png"),
("image/jpeg", "jpg"),
@@ -70,6 +72,8 @@ def test_is_flash(input_mime_type, expected_state):
("VIDEO/WEBM", True),
("video/mp4", True),
("VIDEO/MP4", True),
+ ("video/quicktime", True),
+ ("VIDEO/QUICKTIME", True),
("video/anything_else", False),
("application/ogg", True),
("not a video", False),
diff --git a/server/szurubooru/tests/func/test_net.py b/server/szurubooru/tests/func/test_net.py
index c5b4c73..be2f3c9 100644
--- a/server/szurubooru/tests/func/test_net.py
+++ b/server/szurubooru/tests/func/test_net.py
@@ -1,3 +1,5 @@
+import os
+
import pytest
from szurubooru import errors
@@ -16,6 +18,9 @@ def inject_config(tmpdir, config_injector):
)
+@pytest.mark.skipif(
+ "TEST_NET" not in os.environ, reason="Network tests skipped by default."
+)
def test_download():
url = "http://info.cern.ch/hypertext/WWW/TheProject.html"
@@ -62,6 +67,9 @@ def test_download():
assert actual_content == expected_content
+@pytest.mark.skipif(
+ "TEST_NET" not in os.environ, reason="Network tests skipped by default."
+)
@pytest.mark.parametrize(
"url",
[
@@ -74,6 +82,9 @@ def test_too_large_download(url):
net.download(url, use_video_downloader=True)
+@pytest.mark.skipif(
+ "TEST_NET" not in os.environ, reason="Network tests skipped by default."
+)
@pytest.mark.parametrize(
"url,expected_sha1",
[
@@ -96,6 +107,9 @@ def test_content_download(url, expected_sha1):
assert get_sha1(actual_content) == expected_sha1
+@pytest.mark.skipif(
+ "TEST_NET" not in os.environ, reason="Network tests skipped by default."
+)
def test_bad_content_downlaod():
url = "http://info.cern.ch/hypertext/WWW/TheProject.html"
with pytest.raises(errors.ThirdPartyError):
@@ -108,11 +122,13 @@ def test_no_webhooks(config_injector):
assert len(res) == 0
+@pytest.mark.skipif(
+ "TEST_NET" not in os.environ, reason="Network tests skipped by default."
+)
@pytest.mark.parametrize(
"webhook,status_code",
[
("https://postman-echo.com/post", 200),
- ("http://localhost/", 400),
("https://postman-echo.com/get", 400),
],
)
@@ -121,6 +137,9 @@ def test_single_webhook(config_injector, webhook, status_code):
assert ret == status_code
+@pytest.mark.skipif(
+ "TEST_NET" not in os.environ, reason="Network tests skipped by default."
+)
def test_multiple_webhooks(config_injector):
config_injector(
{
diff --git a/server/szurubooru/tests/func/test_snapshots.py b/server/szurubooru/tests/func/test_snapshots.py
index da93530..dc68ff0 100644
--- a/server/szurubooru/tests/func/test_snapshots.py
+++ b/server/szurubooru/tests/func/test_snapshots.py
@@ -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()
diff --git a/server/szurubooru/tests/func/test_snapshots_transactional_isolation.py b/server/szurubooru/tests/func/test_snapshots_transactional_isolation.py
new file mode 100644
index 0000000..b98cea7
--- /dev/null
+++ b/server/szurubooru/tests/func/test_snapshots_transactional_isolation.py
@@ -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"}
+ ],
+ },
+ },
+ }
diff --git a/server/szurubooru/tests/func/test_tag_categories.py b/server/szurubooru/tests/func/test_tag_categories.py
index 11300cf..9e649a3 100644
--- a/server/szurubooru/tests/func/test_tag_categories.py
+++ b/server/szurubooru/tests/func/test_tag_categories.py
@@ -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()
+ category = tag_category_factory(name="name")
+ db.session.add(category)
+ db.session.flush()
+ tag_categories.update_category_name(category, name)
+ assert category.name == name
def test_update_category_color_with_empty_string(tag_category_factory):
diff --git a/server/szurubooru/tests/func/test_tags.py b/server/szurubooru/tests/func/test_tags.py
index c938e68..79376f4 100644
--- a/server/szurubooru/tests/func/test_tags.py
+++ b/server/szurubooru/tests/func/test_tags.py
@@ -541,15 +541,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()
+ 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]
def test_update_tag_names_changing_primary_name(config_injector, tag_factory):
@@ -561,7 +560,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"])
diff --git a/server/szurubooru/tests/search/configs/test_pool_search_config.py b/server/szurubooru/tests/search/configs/test_pool_search_config.py
index 202635c..1103ec4 100644
--- a/server/szurubooru/tests/search/configs/test_pool_search_config.py
+++ b/server/szurubooru/tests/search/configs/test_pool_search_config.py
@@ -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)
diff --git a/server/szurubooru/tests/search/configs/test_post_search_config.py b/server/szurubooru/tests/search/configs/test_post_search_config.py
index c9f2408..6ab5d52 100644
--- a/server/szurubooru/tests/search/configs/test_post_search_config.py
+++ b/server/szurubooru/tests/search/configs/test_post_search_config.py
@@ -1003,7 +1003,6 @@ def test_filter_by_similar(
verify_unpaged(input, expected_post_ids, True)
-
@pytest.mark.parametrize("input,expected_post_ids", [
("similar:1", [3, 1, 2]),
("similar:2", [3, 2, 1]),
@@ -1023,3 +1022,55 @@ def test_sort_by_similar(
)
db.session.flush()
verify_unpaged(input, expected_post_ids, True)
+
+
+@pytest.mark.parametrize(
+ "input,expected_post_ids",
+ [
+ ("category:cat1", [1, 2, 3]),
+ ("category:cat2", [3, 4]),
+ ],
+)
+def test_search_by_tag_category(
+ verify_unpaged,
+ post_factory,
+ tag_factory,
+ tag_category_factory,
+ input,
+ expected_post_ids,
+):
+ cat1 = tag_category_factory(name="cat1")
+ cat2 = tag_category_factory(name="cat2")
+ tag1 = tag_factory(names=["t1"], category=cat1)
+ tag2 = tag_factory(names=["t2"], category=cat1)
+ tag3 = tag_factory(names=["t3"], category=cat2)
+
+ post1 = post_factory(id=1)
+ post1.tags.append(tag1)
+
+ post2 = post_factory(id=2)
+ post2.tags.append(tag2)
+
+ post3 = post_factory(id=3)
+ post3.tags.append(tag1)
+ post3.tags.append(tag3)
+
+ post4 = post_factory(id=4)
+ post4.tags.append(tag3)
+
+ post5 = post_factory(id=5)
+
+ db.session.add_all(
+ [
+ tag1,
+ tag2,
+ tag3,
+ post1,
+ post2,
+ post3,
+ post4,
+ post5,
+ ]
+ )
+ db.session.flush()
+ verify_unpaged(input, expected_post_ids)
diff --git a/server/szurubooru/tests/search/configs/test_tag_search_config.py b/server/szurubooru/tests/search/configs/test_tag_search_config.py
index 8175b73..9fe9a80 100644
--- a/server/szurubooru/tests/search/configs/test_tag_search_config.py
+++ b/server/szurubooru/tests/search/configs/test_tag_search_config.py
@@ -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)

© 2015 - 2026 Jakob L. Kreuze