aboutsummaryrefslogtreecommitdiff
path: root/server/szurubooru/tests/api
diff options
context:
space:
mode:
authorrr-2016-08-14 11:51:08 +0200
committerrr-2016-08-14 16:43:35 +0200
commitc23c401c4d0e83a6c0391dd2a8d6f6906ad9c172 (patch)
tree581cc6da25196b0ee700667a921d4f63a47a41ee /server/szurubooru/tests/api
parent53e96ba41f7902d7052ab37d340cfe20e30ff020 (diff)
server/tests: add func.tags tests
Diffstat (limited to 'server/szurubooru/tests/api')
-rw-r--r--server/szurubooru/tests/api/test_tag_creating.py88
-rw-r--r--server/szurubooru/tests/api/test_tag_deleting.py15
-rw-r--r--server/szurubooru/tests/api/test_tag_export.py64
-rw-r--r--server/szurubooru/tests/api/test_tag_merging.py124
-rw-r--r--server/szurubooru/tests/api/test_tag_siblings_retrieving.py44
-rw-r--r--server/szurubooru/tests/api/test_tag_updating.py134
6 files changed, 0 insertions, 469 deletions
diff --git a/server/szurubooru/tests/api/test_tag_creating.py b/server/szurubooru/tests/api/test_tag_creating.py
index 6c48ab2..3112054 100644
--- a/server/szurubooru/tests/api/test_tag_creating.py
+++ b/server/szurubooru/tests/api/test_tag_creating.py
@@ -120,49 +120,6 @@ def test_omitting_optional_field(test_ctx, field):
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
assert result is not None
-def test_duplicating_names(test_ctx):
- result = test_ctx.api.post(
- test_ctx.context_factory(
- input={
- 'names': ['tag1', 'TAG1'],
- 'category': 'meta',
- 'suggestions': [],
- 'implications': [],
- },
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
- assert result['names'] == ['tag1']
- assert result['category'] == 'meta'
- tag = tags.get_tag_by_name('tag1')
- assert [tag_name.name for tag_name in tag.names] == ['tag1']
-
-def test_trying_to_use_existing_name(test_ctx):
- db.session.add_all([
- test_ctx.tag_factory(names=['used1']),
- test_ctx.tag_factory(names=['used2']),
- ])
- db.session.commit()
- with pytest.raises(tags.TagAlreadyExistsError):
- test_ctx.api.post(
- test_ctx.context_factory(
- input={
- 'names': ['used1', 'unused'],
- 'category': 'meta',
- 'suggestions': [],
- 'implications': [],
- },
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
- with pytest.raises(tags.TagAlreadyExistsError):
- test_ctx.api.post(
- test_ctx.context_factory(
- input={
- 'names': ['USED2', 'unused'],
- 'category': 'meta',
- 'suggestions': [],
- 'implications': [],
- },
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
- assert tags.try_get_tag_by_name('unused') is None
-
def test_creating_new_category(test_ctx):
with pytest.raises(tag_categories.TagCategoryNotFoundError):
test_ctx.api.post(
@@ -217,51 +174,6 @@ def test_creating_new_suggestions_and_implications(
for name in ['main'] + expected_suggestions + expected_implications:
assert tags.try_get_tag_by_name(name) is not None
-def test_reusing_suggestions_and_implications(test_ctx):
- db.session.add_all([
- test_ctx.tag_factory(names=['tag1', 'tag2']),
- test_ctx.tag_factory(names=['tag3']),
- ])
- db.session.commit()
- result = test_ctx.api.post(
- test_ctx.context_factory(
- input={
- 'names': ['new'],
- 'category': 'meta',
- 'suggestions': ['TAG2'],
- 'implications': ['tag1'],
- },
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
- # NOTE: it should export only the first name
- assert result['suggestions'] == ['tag1']
- assert result['implications'] == ['tag1']
- tag = tags.get_tag_by_name('new')
- assert_relations(tag.suggestions, ['tag1'])
- assert_relations(tag.implications, ['tag1'])
-
-@pytest.mark.parametrize('input', [
- {
- 'names': ['tag'],
- 'category': 'meta',
- 'suggestions': ['tag'],
- 'implications': [],
- },
- {
- 'names': ['tag'],
- 'category': 'meta',
- 'suggestions': [],
- 'implications': ['tag'],
- }
-])
-def test_tag_trying_to_relate_to_itself(test_ctx, input):
- with pytest.raises(tags.TagAlreadyExistsError):
- test_ctx.api.post(
- test_ctx.context_factory(
- input=input,
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
- db.session.rollback()
- assert tags.try_get_tag_by_name('tag') is None
-
def test_trying_to_create_tag_without_privileges(test_ctx):
with pytest.raises(errors.AuthError):
test_ctx.api.post(
diff --git a/server/szurubooru/tests/api/test_tag_deleting.py b/server/szurubooru/tests/api/test_tag_deleting.py
index eab7c17..350fc0a 100644
--- a/server/szurubooru/tests/api/test_tag_deleting.py
+++ b/server/szurubooru/tests/api/test_tag_deleting.py
@@ -32,21 +32,6 @@ def test_deleting(test_ctx):
assert db.session.query(db.Tag).count() == 0
assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json'))
-def test_deleting_used(test_ctx, post_factory):
- tag = test_ctx.tag_factory(names=['tag'])
- post = post_factory()
- post.tags.append(tag)
- db.session.add_all([tag, post])
- db.session.commit()
- test_ctx.api.delete(
- test_ctx.context_factory(
- input={'version': 1},
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
- 'tag')
- db.session.refresh(post)
- assert db.session.query(db.Tag).count() == 0
- assert post.tags == []
-
def test_trying_to_delete_non_existing(test_ctx):
with pytest.raises(tags.TagNotFoundError):
test_ctx.api.delete(
diff --git a/server/szurubooru/tests/api/test_tag_export.py b/server/szurubooru/tests/api/test_tag_export.py
deleted file mode 100644
index 29fb7e1..0000000
--- a/server/szurubooru/tests/api/test_tag_export.py
+++ /dev/null
@@ -1,64 +0,0 @@
-import datetime
-import os
-import json
-from szurubooru import config, db
-from szurubooru.func import tags
-
-def test_export(
- tmpdir,
- query_counter,
- config_injector,
- post_factory,
- tag_factory,
- tag_category_factory):
- config_injector({
- 'data_dir': str(tmpdir)
- })
- cat1 = tag_category_factory(name='cat1', color='black')
- cat2 = tag_category_factory(name='cat2', color='white')
- db.session.add_all([cat1, cat2])
- db.session.flush()
- sug1 = tag_factory(names=['sug1'], category=cat1)
- sug2 = tag_factory(names=['sug2'], category=cat1)
- imp1 = tag_factory(names=['imp1'], category=cat1)
- imp2 = tag_factory(names=['imp2'], category=cat1)
- tag = tag_factory(names=['alias1', 'alias2'], category=cat2)
- db.session.add_all([tag, sug1, sug2, imp1, imp2, cat1, cat2])
- post = post_factory()
- post.tags = [tag]
- db.session.flush()
- db.session.add_all([
- post,
- db.TagSuggestion(tag.tag_id, sug1.tag_id),
- db.TagSuggestion(tag.tag_id, sug2.tag_id),
- db.TagImplication(tag.tag_id, imp1.tag_id),
- db.TagImplication(tag.tag_id, imp2.tag_id),
- ])
- db.session.flush()
-
- with query_counter:
- tags.export_to_json()
- assert len(query_counter.statements) == 5
-
- export_path = os.path.join(config.config['data_dir'], 'tags.json')
- assert os.path.exists(export_path)
- with open(export_path, 'r') as handle:
- assert json.loads(handle.read()) == {
- 'tags': [
- {
- 'names': ['alias1', 'alias2'],
- 'usages': 1,
- 'category': 'cat2',
- 'suggestions': ['sug1', 'sug2'],
- 'implications': ['imp1', 'imp2'],
- },
- {'names': ['sug1'], 'usages': 0, 'category': 'cat1'},
- {'names': ['sug2'], 'usages': 0, 'category': 'cat1'},
- {'names': ['imp1'], 'usages': 0, 'category': 'cat1'},
- {'names': ['imp2'], 'usages': 0, 'category': 'cat1'},
- ],
- 'categories': [
- {'name': 'cat1', 'color': 'black'},
- {'name': 'cat2', 'color': 'white'},
- ]
- }
diff --git a/server/szurubooru/tests/api/test_tag_merging.py b/server/szurubooru/tests/api/test_tag_merging.py
index 3f82021..0a64240 100644
--- a/server/szurubooru/tests/api/test_tag_merging.py
+++ b/server/szurubooru/tests/api/test_tag_merging.py
@@ -26,40 +26,6 @@ def test_ctx(
ret.api = api.TagMergeApi()
return ret
-def test_merging_without_usages(test_ctx, fake_datetime):
- category = test_ctx.tag_category_factory(name='meta')
- source_tag = test_ctx.tag_factory(names=['source'])
- target_tag = test_ctx.tag_factory(names=['target'], category=category)
- db.session.add_all([source_tag, target_tag])
- db.session.commit()
- with fake_datetime('1997-12-01'):
- result = test_ctx.api.post(
- test_ctx.context_factory(
- input={
- 'removeVersion': 1,
- 'mergeToVersion': 1,
- 'remove': 'source',
- 'mergeTo': 'target',
- },
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
- assert 'snapshots' in result
- del result['snapshots']
- assert result == {
- 'names': ['target'],
- 'category': 'meta',
- 'description': None,
- 'suggestions': [],
- 'implications': [],
- 'creationTime': datetime.datetime(1996, 1, 1),
- 'lastEditTime': None,
- 'usages': 0,
- 'version': 2,
- }
- assert tags.try_get_tag_by_name('source') is None
- tag = tags.get_tag_by_name('target')
- assert tag is not None
- assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json'))
-
def test_merging_with_usages(test_ctx, fake_datetime, post_factory):
source_tag = test_ctx.tag_factory(names=['source'])
target_tag = test_ctx.tag_factory(names=['target'])
@@ -86,83 +52,6 @@ def test_merging_with_usages(test_ctx, fake_datetime, post_factory):
assert tags.try_get_tag_by_name('source') is None
assert tags.get_tag_by_name('target').post_count == 1
-def test_merging_when_related(test_ctx, fake_datetime):
- source_tag = test_ctx.tag_factory(names=['source'])
- target_tag = test_ctx.tag_factory(names=['target'])
- db.session.add_all([source_tag, target_tag])
- db.session.flush()
- referring_tag = test_ctx.tag_factory(names=['parent'])
- referring_tag.suggestions = [source_tag]
- referring_tag.implications = [source_tag]
- db.session.add(referring_tag)
- db.session.commit()
- assert tags.try_get_tag_by_name('parent').implications != []
- assert tags.try_get_tag_by_name('parent').suggestions != []
- with fake_datetime('1997-12-01'):
- result = test_ctx.api.post(
- test_ctx.context_factory(
- input={
- 'removeVersion': 1,
- 'mergeToVersion': 1,
- 'remove': 'source',
- 'mergeTo': 'target',
- },
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
- assert tags.try_get_tag_by_name('source') is None
- assert tags.try_get_tag_by_name('parent').implications == []
- assert tags.try_get_tag_by_name('parent').suggestions == []
-
-def test_merging_when_target_exists(test_ctx, fake_datetime, post_factory):
- source_tag = test_ctx.tag_factory(names=['source'])
- target_tag = test_ctx.tag_factory(names=['target'])
- db.session.add_all([source_tag, target_tag])
- db.session.flush()
- post1 = post_factory()
- post1.tags = [source_tag, target_tag]
- db.session.add_all([post1])
- db.session.commit()
- assert source_tag.post_count == 1
- assert target_tag.post_count == 1
- with fake_datetime('1997-12-01'):
- result = test_ctx.api.post(
- test_ctx.context_factory(
- input={
- 'removeVersion': 1,
- 'mergeToVersion': 1,
- 'remove': 'source',
- 'mergeTo': 'target',
- },
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
- assert tags.try_get_tag_by_name('source') is None
- assert tags.get_tag_by_name('target').post_count == 1
-
-@pytest.mark.parametrize('input,expected_exception', [
- ({'remove': None}, tags.TagNotFoundError),
- ({'remove': ''}, tags.TagNotFoundError),
- ({'remove': []}, tags.TagNotFoundError),
- ({'mergeTo': None}, tags.TagNotFoundError),
- ({'mergeTo': ''}, tags.TagNotFoundError),
- ({'mergeTo': []}, tags.TagNotFoundError),
-])
-def test_trying_to_pass_invalid_input(test_ctx, input, expected_exception):
- source_tag = test_ctx.tag_factory(names=['source'])
- target_tag = test_ctx.tag_factory(names=['target'])
- db.session.add_all([source_tag, target_tag])
- db.session.commit()
- real_input = {
- 'removeVersion': 1,
- 'mergeToVersion': 1,
- 'remove': 'source',
- 'mergeTo': 'target',
- }
- for key, value in input.items():
- real_input[key] = value
- with pytest.raises(expected_exception):
- test_ctx.api.post(
- test_ctx.context_factory(
- input=real_input,
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
-
@pytest.mark.parametrize(
'field', ['remove', 'mergeTo', 'removeVersion', 'mergeToVersion'])
def test_trying_to_omit_mandatory_field(test_ctx, field):
@@ -198,19 +87,6 @@ def test_trying_to_merge_non_existing(test_ctx):
input={'remove': 'bad', 'mergeTo': 'good'},
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
-def test_trying_to_merge_to_itself(test_ctx):
- db.session.add(test_ctx.tag_factory(names=['good']))
- db.session.commit()
- with pytest.raises(tags.InvalidTagRelationError):
- test_ctx.api.post(
- test_ctx.context_factory(
- input={
- 'removeVersion': 1,
- 'mergeToVersion': 1,
- 'remove': 'good',
- 'mergeTo': 'good'},
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
-
@pytest.mark.parametrize('input', [
{'names': 'whatever'},
{'category': 'whatever'},
diff --git a/server/szurubooru/tests/api/test_tag_siblings_retrieving.py b/server/szurubooru/tests/api/test_tag_siblings_retrieving.py
index cddb04a..f3d8e2c 100644
--- a/server/szurubooru/tests/api/test_tag_siblings_retrieving.py
+++ b/server/szurubooru/tests/api/test_tag_siblings_retrieving.py
@@ -28,23 +28,6 @@ def test_ctx(
ret.api = api.TagSiblingsApi()
return ret
-def test_unused(test_ctx):
- db.session.add(test_ctx.tag_factory(names=['tag']))
- result = test_ctx.api.get(
- test_ctx.context_factory(
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag')
- assert_results(result, [])
-
-def test_used_alone(test_ctx):
- tag = test_ctx.tag_factory(names=['tag'])
- post = test_ctx.post_factory()
- post.tags = [tag]
- db.session.add_all([post, tag])
- result = test_ctx.api.get(
- test_ctx.context_factory(
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag')
- assert_results(result, [])
-
def test_used_with_others(test_ctx):
tag1 = test_ctx.tag_factory(names=['tag1'])
tag2 = test_ctx.tag_factory(names=['tag2'])
@@ -60,33 +43,6 @@ def test_used_with_others(test_ctx):
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag2')
assert_results(result, [('tag1', 1)])
-def test_used_with_multiple_others(test_ctx):
- tag1 = test_ctx.tag_factory(names=['tag1'])
- tag2 = test_ctx.tag_factory(names=['tag2'])
- tag3 = test_ctx.tag_factory(names=['tag3'])
- post1 = test_ctx.post_factory()
- post2 = test_ctx.post_factory()
- post3 = test_ctx.post_factory()
- post4 = test_ctx.post_factory()
- post1.tags = [tag1, tag2, tag3]
- post2.tags = [tag1, tag3]
- post3.tags = [tag2]
- post4.tags = [tag2]
- db.session.add_all([post1, post2, post3, post4, tag1, tag2, tag3])
- result = test_ctx.api.get(
- test_ctx.context_factory(
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag1')
- assert_results(result, [('tag3', 2), ('tag2', 1)])
- result = test_ctx.api.get(
- test_ctx.context_factory(
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag2')
- assert_results(result, [('tag1', 1), ('tag3', 1)])
- result = test_ctx.api.get(
- test_ctx.context_factory(
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag3')
- # even though tag2 is used more widely, tag1 is more relevant to tag3
- assert_results(result, [('tag1', 2), ('tag2', 1)])
-
def test_trying_to_retrieve_non_existing(test_ctx):
with pytest.raises(tags.TagNotFoundError):
test_ctx.api.get(
diff --git a/server/szurubooru/tests/api/test_tag_updating.py b/server/szurubooru/tests/api/test_tag_updating.py
index 151f62b..8ec965f 100644
--- a/server/szurubooru/tests/api/test_tag_updating.py
+++ b/server/szurubooru/tests/api/test_tag_updating.py
@@ -124,140 +124,6 @@ def test_trying_to_update_non_existing(test_ctx):
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
'tag1')
-@pytest.mark.parametrize('dup_name', ['tag1', 'TAG1'])
-def test_reusing_own_name(test_ctx, dup_name):
- db.session.add(
- test_ctx.tag_factory(names=['tag1', 'tag2']))
- db.session.commit()
- result = test_ctx.api.put(
- test_ctx.context_factory(
- input={'names': [dup_name, 'tag3'], 'version': 1},
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
- 'tag1')
- assert result['names'] == [dup_name, 'tag3']
- assert tags.try_get_tag_by_name('tag2') is None
- tag1 = tags.get_tag_by_name('tag1')
- tag2 = tags.get_tag_by_name('tag3')
- assert tag1.tag_id == tag2.tag_id
- assert [name.name for name in tag1.names] == [dup_name, 'tag3']
-
-def test_duplicating_names(test_ctx):
- db.session.add(
- test_ctx.tag_factory(names=['tag1', 'tag2']))
- result = test_ctx.api.put(
- test_ctx.context_factory(
- input={'names': ['tag3', 'TAG3'], 'version': 1},
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
- 'tag1')
- assert result['names'] == ['tag3']
- assert tags.try_get_tag_by_name('tag1') is None
- assert tags.try_get_tag_by_name('tag2') is None
- tag = tags.get_tag_by_name('tag3')
- assert tag is not None
- assert [tag_name.name for tag_name in tag.names] == ['tag3']
-
-@pytest.mark.parametrize('dup_name', ['tag1', 'TAG1', 'tag2', 'TAG2'])
-def test_trying_to_use_existing_name(test_ctx, dup_name):
- db.session.add_all([
- test_ctx.tag_factory(names=['tag1', 'tag2']),
- test_ctx.tag_factory(names=['tag3', 'tag4'])])
- db.session.commit()
- with pytest.raises(tags.TagAlreadyExistsError):
- test_ctx.api.put(
- test_ctx.context_factory(
- input={'names': [dup_name], 'version': 1},
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
- 'tag3')
-
-@pytest.mark.parametrize('input,expected_suggestions,expected_implications', [
- # new relations
- ({
- 'suggestions': ['sug1', 'sug2'],
- 'implications': ['imp1', 'imp2'],
- }, ['sug1', 'sug2'], ['imp1', 'imp2']),
- # overlapping relations
- ({
- 'suggestions': ['sug', 'shared'],
- 'implications': ['shared', 'imp'],
- }, ['shared', 'sug'], ['imp', 'shared']),
- # duplicate relations
- ({
- 'suggestions': ['sug', 'SUG'],
- 'implications': ['imp', 'IMP'],
- }, ['sug'], ['imp']),
- # overlapping duplicate relations
- ({
- 'suggestions': ['shared1', 'shared2'],
- 'implications': ['SHARED1', 'SHARED2'],
- }, ['shared1', 'shared2'], ['shared1', 'shared2']),
-])
-def test_updating_new_suggestions_and_implications(
- test_ctx, input, expected_suggestions, expected_implications):
- db.session.add(
- test_ctx.tag_factory(names=['main']))
- db.session.commit()
- result = test_ctx.api.put(
- test_ctx.context_factory(
- input={**input, **{'version': 1}},
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
- 'main')
- assert result['suggestions'] == expected_suggestions
- assert result['implications'] == expected_implications
- tag = tags.get_tag_by_name('main')
- assert_relations(tag.suggestions, expected_suggestions)
- assert_relations(tag.implications, expected_implications)
- for name in ['main'] + expected_suggestions + expected_implications:
- assert tags.try_get_tag_by_name(name) is not None
-
-def test_reusing_suggestions_and_implications(test_ctx):
- db.session.add_all([
- test_ctx.tag_factory(names=['tag1', 'tag2']),
- test_ctx.tag_factory(names=['tag3']),
- test_ctx.tag_factory(names=['tag4']),
- ])
- db.session.commit()
- result = test_ctx.api.put(
- test_ctx.context_factory(
- input={
- 'names': ['new'],
- 'category': 'meta',
- 'suggestions': ['TAG2'],
- 'implications': ['tag1'],
- 'version': 1,
- },
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
- 'tag4')
- # NOTE: it should export only the first name
- assert result['suggestions'] == ['tag1']
- assert result['implications'] == ['tag1']
- tag = tags.get_tag_by_name('new')
- assert_relations(tag.suggestions, ['tag1'])
- assert_relations(tag.implications, ['tag1'])
-
-@pytest.mark.parametrize('input', [
- {
- 'names': ['tag1'],
- 'category': 'meta',
- 'suggestions': ['tag1'],
- 'implications': [],
- },
- {
- 'names': ['tag1'],
- 'category': 'meta',
- 'suggestions': [],
- 'implications': ['tag1'],
- }
-])
-def test_trying_to_relate_tag_to_itself(test_ctx, input):
- db.session.add(test_ctx.tag_factory(names=['tag1']))
- db.session.commit()
- with pytest.raises(tags.InvalidTagRelationError):
- test_ctx.api.put(
- test_ctx.context_factory(
- input={**input, **{'version': 1}},
- user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
- 'tag1')
-
@pytest.mark.parametrize('input', [
{'names': 'whatever'},
{'category': 'whatever'},

© 2015 - 2026 Jakob L. Kreuze