summaryrefslogtreecommitdiff
path: root/server/szurubooru/tests/func/test_posts.py
diff options
context:
space:
mode:
authorShyam Sunder <sgsunder1@gmail.com>2020-03-07 21:02:01 -0500
committerShyam Sunder <sgsunder1@gmail.com>2020-03-13 22:45:11 -0400
commitbd9284b7f8d809f122f68f04525244457c90c645 (patch)
treed2d144cadcbb09888557cd9a81232fc7493ed85b /server/szurubooru/tests/func/test_posts.py
parent4c78cf8c4792e3eba8508f607bc3a4544d1f666f (diff)
server/tests: update unit tests for elasticsearch removal
Diffstat (limited to 'server/szurubooru/tests/func/test_posts.py')
-rw-r--r--server/szurubooru/tests/func/test_posts.py52
1 files changed, 42 insertions, 10 deletions
diff --git a/server/szurubooru/tests/func/test_posts.py b/server/szurubooru/tests/func/test_posts.py
index fc4cfc9..097136c 100644
--- a/server/szurubooru/tests/func/test_posts.py
+++ b/server/szurubooru/tests/func/test_posts.py
@@ -354,6 +354,7 @@ def test_update_post_content_for_new_post(
'post_height': 300,
},
'secret': 'test',
+ 'allow_broken_uploads': False,
})
output_file_path = '{}/data/posts/{}'.format(tmpdir, output_file_name)
post = post_factory(id=1)
@@ -371,11 +372,9 @@ def test_update_post_content_for_new_post(
assert post.checksum == 'crc'
assert os.path.exists(output_file_path)
if post.type in (model.Post.TYPE_IMAGE, model.Post.TYPE_ANIMATION):
- image_hash.delete_image.assert_called_once_with(post.post_id)
- image_hash.add_image.assert_called_once_with(post.post_id, content)
+ assert db.session.query(model.PostSignature).count() == 1
else:
- image_hash.delete_image.assert_not_called()
- image_hash.add_image.assert_not_called()
+ assert db.session.query(model.PostSignature).count() == 0
def test_update_post_content_to_existing_content(
@@ -388,6 +387,7 @@ def test_update_post_content_to_existing_content(
'post_height': 300,
},
'secret': 'test',
+ 'allow_broken_uploads': False,
})
post = post_factory()
another_post = post_factory()
@@ -398,8 +398,10 @@ def test_update_post_content_to_existing_content(
posts.update_post_content(another_post, read_asset('png.png'))
+@pytest.mark.parametrize('allow_broken_uploads', [True, False])
def test_update_post_content_with_broken_content(
- tmpdir, config_injector, post_factory, read_asset):
+ tmpdir, config_injector, post_factory, read_asset,
+ allow_broken_uploads):
# the rationale behind this behavior is to salvage user upload even if the
# server software thinks it's broken. chances are the server is wrong,
# especially about flash movies.
@@ -410,18 +412,28 @@ def test_update_post_content_with_broken_content(
'post_height': 300,
},
'secret': 'test',
+ 'allow_broken_uploads': allow_broken_uploads,
})
post = post_factory()
another_post = post_factory()
db.session.add_all([post, another_post])
- posts.update_post_content(post, read_asset('png-broken.png'))
- db.session.flush()
- assert post.canvas_width is None
- assert post.canvas_height is None
+ if allow_broken_uploads:
+ posts.update_post_content(post, read_asset('png-broken.png'))
+ db.session.flush()
+ assert post.canvas_width is None
+ assert post.canvas_height is None
+ else:
+ with pytest.raises(posts.InvalidPostContentError):
+ posts.update_post_content(post, read_asset('png-broken.png'))
+ db.session.flush()
@pytest.mark.parametrize('input_content', [None, b'not a media file'])
-def test_update_post_content_with_invalid_content(input_content):
+def test_update_post_content_with_invalid_content(
+ config_injector, input_content):
+ config_injector({
+ 'allow_broken_uploads': True,
+ })
post = model.Post()
with pytest.raises(posts.InvalidPostContentError):
posts.update_post_content(post, input_content)
@@ -437,6 +449,7 @@ def test_update_post_thumbnail_to_new_one(
'post_height': 300,
},
'secret': 'test',
+ 'allow_broken_uploads': False,
})
post = post_factory(id=1)
db.session.add(post)
@@ -472,6 +485,7 @@ def test_update_post_thumbnail_to_default(
'post_height': 300,
},
'secret': 'test',
+ 'allow_broken_uploads': False,
})
post = post_factory(id=1)
db.session.add(post)
@@ -506,6 +520,7 @@ def test_update_post_thumbnail_with_broken_thumbnail(
'post_height': 300,
},
'secret': 'test',
+ 'allow_broken_uploads': False,
})
post = post_factory(id=1)
db.session.add(post)
@@ -544,6 +559,7 @@ def test_update_post_content_leaving_custom_thumbnail(
'post_height': 300,
},
'secret': 'test',
+ 'allow_broken_uploads': False,
})
post = post_factory(id=1)
db.session.add(post)
@@ -964,3 +980,19 @@ def test_merge_posts_replaces_content(
assert os.path.exists(source_path)
assert os.path.exists(target_path1)
assert not os.path.exists(target_path2)
+
+
+def test_search_by_image(post_factory, config_injector, read_asset):
+ config_injector({'allow_broken_uploads': False})
+ post = post_factory()
+ posts.generate_post_signature(post, read_asset('jpeg.jpg'))
+ db.session.flush()
+
+ result1 = posts.search_by_image(read_asset('jpeg-similar.jpg'))
+ assert len(result1) == 1
+ result1_distance, result1_post = result1[0]
+ assert abs(result1_distance - 0.20599895341812172) < 1e-8
+ assert result1_post.post_id == post.post_id
+
+ result2 = posts.search_by_image(read_asset('png.png'))
+ assert not result2