server/tests: test image hash
This commit is contained in:
parent
b21ffac820
commit
894cd29511
7 changed files with 40 additions and 13 deletions
|
@ -36,6 +36,7 @@ smtp:
|
||||||
elasticsearch:
|
elasticsearch:
|
||||||
host: localhost
|
host: localhost
|
||||||
port: 9200
|
port: 9200
|
||||||
|
index: szurubooru
|
||||||
|
|
||||||
limits:
|
limits:
|
||||||
users_per_page: 20
|
users_per_page: 20
|
||||||
|
|
|
@ -12,7 +12,10 @@ es = elasticsearch.Elasticsearch([{
|
||||||
'host': config.config['elasticsearch']['host'],
|
'host': config.config['elasticsearch']['host'],
|
||||||
'port': config.config['elasticsearch']['port'],
|
'port': config.config['elasticsearch']['port'],
|
||||||
}])
|
}])
|
||||||
session = SignatureES(es, index='szurubooru')
|
|
||||||
|
|
||||||
|
def _get_session():
|
||||||
|
return SignatureES(es, index=config.config['elasticsearch']['index'])
|
||||||
|
|
||||||
|
|
||||||
def _safe_blanket(default_param_factory):
|
def _safe_blanket(default_param_factory):
|
||||||
|
@ -48,6 +51,7 @@ class Lookalike:
|
||||||
def add_image(path, image_content):
|
def add_image(path, image_content):
|
||||||
if not path or not image_content:
|
if not path or not image_content:
|
||||||
return
|
return
|
||||||
|
session = _get_session()
|
||||||
session.add_image(path=path, img=image_content, bytestream=True)
|
session.add_image(path=path, img=image_content, bytestream=True)
|
||||||
|
|
||||||
|
|
||||||
|
@ -55,6 +59,7 @@ def add_image(path, image_content):
|
||||||
def delete_image(path):
|
def delete_image(path):
|
||||||
if not path:
|
if not path:
|
||||||
return
|
return
|
||||||
|
session = _get_session()
|
||||||
es.delete_by_query(
|
es.delete_by_query(
|
||||||
index=session.index,
|
index=session.index,
|
||||||
doc_type=session.doc_type,
|
doc_type=session.doc_type,
|
||||||
|
@ -64,6 +69,7 @@ def delete_image(path):
|
||||||
@_safe_blanket(lambda: [])
|
@_safe_blanket(lambda: [])
|
||||||
def search_by_image(image_content):
|
def search_by_image(image_content):
|
||||||
ret = []
|
ret = []
|
||||||
|
session = _get_session()
|
||||||
for result in session.search_image(
|
for result in session.search_image(
|
||||||
path=image_content, # sic
|
path=image_content, # sic
|
||||||
bytestream=True):
|
bytestream=True):
|
||||||
|
@ -76,6 +82,7 @@ def search_by_image(image_content):
|
||||||
|
|
||||||
@_safe_blanket(lambda: None)
|
@_safe_blanket(lambda: None)
|
||||||
def purge():
|
def purge():
|
||||||
|
session = _get_session()
|
||||||
es.delete_by_query(
|
es.delete_by_query(
|
||||||
index=session.index,
|
index=session.index,
|
||||||
doc_type=session.doc_type,
|
doc_type=session.doc_type,
|
||||||
|
@ -84,6 +91,7 @@ def purge():
|
||||||
|
|
||||||
@_safe_blanket(lambda: set())
|
@_safe_blanket(lambda: set())
|
||||||
def get_all_paths():
|
def get_all_paths():
|
||||||
|
session = _get_session()
|
||||||
search = (
|
search = (
|
||||||
elasticsearch_dsl.Search(
|
elasticsearch_dsl.Search(
|
||||||
using=es, index=session.index, doc_type=session.doc_type)
|
using=es, index=session.index, doc_type=session.doc_type)
|
||||||
|
|
|
@ -258,7 +258,8 @@ def test_omitting_optional_field(
|
||||||
|
|
||||||
|
|
||||||
def test_errors_not_spending_ids(
|
def test_errors_not_spending_ids(
|
||||||
config_injector, tmpdir, context_factory, read_asset, user_factory):
|
config_injector, tmpdir, context_factory, read_asset, user_factory,
|
||||||
|
skip_post_hashing):
|
||||||
config_injector({
|
config_injector({
|
||||||
'data_dir': str(tmpdir.mkdir('data')),
|
'data_dir': str(tmpdir.mkdir('data')),
|
||||||
'data_url': 'example.com',
|
'data_url': 'example.com',
|
||||||
|
|
BIN
server/szurubooru/tests/assets/jpeg-similar.jpg
Normal file
BIN
server/szurubooru/tests/assets/jpeg-similar.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
|
@ -155,7 +155,7 @@ def tag_factory():
|
||||||
return factory
|
return factory
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture(autouse=True)
|
@pytest.yield_fixture
|
||||||
def skip_post_hashing():
|
def skip_post_hashing():
|
||||||
with patch('szurubooru.func.image_hash.add_image'), \
|
with patch('szurubooru.func.image_hash.add_image'), \
|
||||||
patch('szurubooru.func.image_hash.delete_image'):
|
patch('szurubooru.func.image_hash.delete_image'):
|
||||||
|
@ -163,7 +163,7 @@ def skip_post_hashing():
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def post_factory():
|
def post_factory(skip_post_hashing):
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
def factory(
|
def factory(
|
||||||
id=None,
|
id=None,
|
||||||
|
|
24
server/szurubooru/tests/func/test_image_hash.py
Normal file
24
server/szurubooru/tests/func/test_image_hash.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
from time import sleep
|
||||||
|
from szurubooru.func import image_hash
|
||||||
|
|
||||||
|
|
||||||
|
def test_hashing(read_asset, config_injector):
|
||||||
|
config_injector({'elasticsearch': {'index': 'szurubooru_test'}})
|
||||||
|
image_hash.purge()
|
||||||
|
image_hash.add_image('test', read_asset('jpeg.jpg'))
|
||||||
|
|
||||||
|
sleep(0.1)
|
||||||
|
|
||||||
|
paths = image_hash.get_all_paths()
|
||||||
|
results_exact = image_hash.search_by_image(read_asset('jpeg.jpg'))
|
||||||
|
results_similar = image_hash.search_by_image(read_asset('jpeg-similar.jpg'))
|
||||||
|
|
||||||
|
assert len(paths) == 1
|
||||||
|
assert len(results_exact) == 1
|
||||||
|
assert len(results_similar) == 1
|
||||||
|
assert results_exact[0].path == 'test'
|
||||||
|
assert results_exact[0].score == 63
|
||||||
|
assert results_exact[0].distance == 0
|
||||||
|
assert results_similar[0].path == 'test'
|
||||||
|
assert results_similar[0].score == 26
|
||||||
|
assert abs(results_similar[0].distance - 0.189390583) < 1e-8
|
|
@ -290,15 +290,8 @@ def test_update_post_source_with_too_long_string():
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
def test_update_post_content_for_new_post(
|
def test_update_post_content_for_new_post(
|
||||||
tmpdir,
|
tmpdir, config_injector, post_factory, read_asset, is_existing,
|
||||||
config_injector,
|
input_file, expected_mime_type, expected_type, output_file_name):
|
||||||
post_factory,
|
|
||||||
read_asset,
|
|
||||||
is_existing,
|
|
||||||
input_file,
|
|
||||||
expected_mime_type,
|
|
||||||
expected_type,
|
|
||||||
output_file_name):
|
|
||||||
with patch('szurubooru.func.util.get_sha1'):
|
with patch('szurubooru.func.util.get_sha1'):
|
||||||
util.get_sha1.return_value = 'crc'
|
util.get_sha1.return_value = 'crc'
|
||||||
config_injector({
|
config_injector({
|
||||||
|
|
Loading…
Reference in a new issue