server/tags: don't auto-create tag categories

This commit is contained in:
rr- 2016-08-13 22:29:12 +02:00
parent 6b77404ecf
commit 2b3d193b7c
9 changed files with 97 additions and 88 deletions

View file

@ -32,7 +32,7 @@ class TagListApi(BaseApi):
auth.verify_privilege(ctx.user, 'tags:create') auth.verify_privilege(ctx.user, 'tags:create')
names = ctx.get_param_as_list('names', required=True) names = ctx.get_param_as_list('names', required=True)
category = ctx.get_param_as_string('category', required=True) category = ctx.get_param_as_string('category', required=True) or ''
description = ctx.get_param_as_string( description = ctx.get_param_as_string(
'description', required=False, default=None) 'description', required=False, default=None)
suggestions = ctx.get_param_as_list( suggestions = ctx.get_param_as_list(
@ -67,7 +67,7 @@ class TagDetailApi(BaseApi):
if ctx.has_param('category'): if ctx.has_param('category'):
auth.verify_privilege(ctx.user, 'tags:edit:category') auth.verify_privilege(ctx.user, 'tags:edit:category')
tags.update_tag_category_name( tags.update_tag_category_name(
tag, ctx.get_param_as_string('category')) tag, ctx.get_param_as_string('category') or '')
if ctx.has_param('description'): if ctx.has_param('description'):
auth.verify_privilege(ctx.user, 'tags:edit:description') auth.verify_privilege(ctx.user, 'tags:edit:description')
tags.update_tag_description( tags.update_tag_description(

View file

@ -11,9 +11,7 @@ class TagAlreadyExistsError(errors.ValidationError): pass
class TagIsInUseError(errors.ValidationError): pass class TagIsInUseError(errors.ValidationError): pass
class InvalidTagNameError(errors.ValidationError): pass class InvalidTagNameError(errors.ValidationError): pass
class InvalidTagRelationError(errors.ValidationError): pass class InvalidTagRelationError(errors.ValidationError): pass
class InvalidTagCategoryError(errors.ValidationError): pass
DEFAULT_CATEGORY_NAME = 'Default'
DEFAULT_CATEGORY_COLOR = 'default'
def _verify_name_validity(name): def _verify_name_validity(name):
name_regex = config.config['tag_name_regex'] name_regex = config.config['tag_name_regex']
@ -29,13 +27,6 @@ def _lower_list(names):
def _check_name_intersection(names1, names2): def _check_name_intersection(names1, names2):
return len(set(_lower_list(names1)).intersection(_lower_list(names2))) > 0 return len(set(_lower_list(names1)).intersection(_lower_list(names2))) > 0
def _get_default_category_name():
tag_category = tag_categories.try_get_default_category()
if tag_category:
return tag_category.name
else:
return DEFAULT_CATEGORY_NAME
def sort_tags(tags): def sort_tags(tags):
default_category = tag_categories.try_get_default_category() default_category = tag_categories.try_get_default_category()
default_category_name = default_category.name if default_category else None default_category_name = default_category.name if default_category else None
@ -146,7 +137,7 @@ def get_or_create_tags_by_names(names):
_verify_name_validity(name) _verify_name_validity(name)
existing_tags = get_tags_by_names(names) existing_tags = get_tags_by_names(names)
new_tags = [] new_tags = []
tag_category_name = _get_default_category_name() tag_category_name = tag_categories.get_default_category().name
for name in names: for name in names:
found = False found = False
for existing_tag in existing_tags: for existing_tag in existing_tags:
@ -213,15 +204,7 @@ def create_tag(names, category_name, suggestions, implications):
return tag return tag
def update_tag_category_name(tag, category_name): def update_tag_category_name(tag, category_name):
category = db.session \ tag.category = tag_categories.get_category_by_name(category_name)
.query(db.TagCategory) \
.filter(db.TagCategory.name == category_name) \
.first()
if not category:
category = tag_categories.create_category(
category_name, DEFAULT_CATEGORY_COLOR)
db.session.add(category)
tag.category = category
def update_tag_names(tag, names): def update_tag_names(tag, names):
names = util.icase_unique([name for name in names if name]) names = util.icase_unique([name for name in names if name])

View file

@ -2,7 +2,7 @@ import datetime
import os import os
import pytest import pytest
from szurubooru import api, config, db, errors from szurubooru import api, config, db, errors
from szurubooru.func import util, tags, tag_categories from szurubooru.func import util, tags, tag_categories, cache
def assert_relations(relations, expected_tag_names): def assert_relations(relations, expected_tag_names):
actual_names = sorted([rel.names[0].name for rel in relations]) actual_names = sorted([rel.names[0].name for rel in relations])
@ -20,6 +20,7 @@ def test_ctx(
db.session.add_all([ db.session.add_all([
db.TagCategory(name) for name in ['meta', 'character', 'copyright']]) db.TagCategory(name) for name in ['meta', 'character', 'copyright']])
db.session.flush() db.session.flush()
cache.purge()
ret = util.dotdict() ret = util.dotdict()
ret.context_factory = context_factory ret.context_factory = context_factory
ret.user_factory = user_factory ret.user_factory = user_factory
@ -68,9 +69,9 @@ def test_creating_simple_tags(test_ctx, fake_datetime):
({'names': ['']}, tags.InvalidTagNameError), ({'names': ['']}, tags.InvalidTagNameError),
({'names': ['!bad']}, tags.InvalidTagNameError), ({'names': ['!bad']}, tags.InvalidTagNameError),
({'names': ['x' * 65]}, tags.InvalidTagNameError), ({'names': ['x' * 65]}, tags.InvalidTagNameError),
({'category': None}, tag_categories.InvalidTagCategoryNameError), ({'category': None}, tag_categories.TagCategoryNotFoundError),
({'category': ''}, tag_categories.InvalidTagCategoryNameError), ({'category': ''}, tag_categories.TagCategoryNotFoundError),
({'category': '!bad'}, tag_categories.InvalidTagCategoryNameError), ({'category': '!bad'}, tag_categories.TagCategoryNotFoundError),
({'suggestions': ['good', '!bad']}, tags.InvalidTagNameError), ({'suggestions': ['good', '!bad']}, tags.InvalidTagNameError),
({'implications': ['good', '!bad']}, tags.InvalidTagNameError), ({'implications': ['good', '!bad']}, tags.InvalidTagNameError),
]) ])
@ -136,8 +137,8 @@ def test_duplicating_names(test_ctx):
def test_trying_to_use_existing_name(test_ctx): def test_trying_to_use_existing_name(test_ctx):
db.session.add_all([ db.session.add_all([
test_ctx.tag_factory(names=['used1'], category_name='meta'), test_ctx.tag_factory(names=['used1']),
test_ctx.tag_factory(names=['used2'], category_name='meta'), test_ctx.tag_factory(names=['used2']),
]) ])
db.session.commit() db.session.commit()
with pytest.raises(tags.TagAlreadyExistsError): with pytest.raises(tags.TagAlreadyExistsError):
@ -163,6 +164,7 @@ def test_trying_to_use_existing_name(test_ctx):
assert tags.try_get_tag_by_name('unused') is None assert tags.try_get_tag_by_name('unused') is None
def test_creating_new_category(test_ctx): def test_creating_new_category(test_ctx):
with pytest.raises(tag_categories.TagCategoryNotFoundError):
test_ctx.api.post( test_ctx.api.post(
test_ctx.context_factory( test_ctx.context_factory(
input={ input={
@ -171,7 +173,6 @@ def test_creating_new_category(test_ctx):
'suggestions': [], 'suggestions': [],
'implications': [], 'implications': [],
}, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) }, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
assert tag_categories.try_get_category_by_name('new') is not None
@pytest.mark.parametrize('input,expected_suggestions,expected_implications', [ @pytest.mark.parametrize('input,expected_suggestions,expected_implications', [
# new relations # new relations
@ -218,8 +219,8 @@ def test_creating_new_suggestions_and_implications(
def test_reusing_suggestions_and_implications(test_ctx): def test_reusing_suggestions_and_implications(test_ctx):
db.session.add_all([ db.session.add_all([
test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta'), test_ctx.tag_factory(names=['tag1', 'tag2']),
test_ctx.tag_factory(names=['tag3'], category_name='meta'), test_ctx.tag_factory(names=['tag3']),
]) ])
db.session.commit() db.session.commit()
result = test_ctx.api.post( result = test_ctx.api.post(

View file

@ -6,7 +6,12 @@ from szurubooru.func import util, tags
@pytest.fixture @pytest.fixture
def test_ctx( def test_ctx(
tmpdir, config_injector, context_factory, user_factory, tag_factory): tmpdir,
config_injector,
context_factory,
user_factory,
tag_factory,
tag_category_factory):
config_injector({ config_injector({
'data_dir': str(tmpdir), 'data_dir': str(tmpdir),
'privileges': { 'privileges': {
@ -17,12 +22,14 @@ def test_ctx(
ret.context_factory = context_factory ret.context_factory = context_factory
ret.user_factory = user_factory ret.user_factory = user_factory
ret.tag_factory = tag_factory ret.tag_factory = tag_factory
ret.tag_category_factory = tag_category_factory
ret.api = api.TagMergeApi() ret.api = api.TagMergeApi()
return ret return ret
def test_merging_without_usages(test_ctx, fake_datetime): def test_merging_without_usages(test_ctx, fake_datetime):
source_tag = test_ctx.tag_factory(names=['source'], category_name='meta') category = test_ctx.tag_category_factory(name='meta')
target_tag = test_ctx.tag_factory(names=['target'], category_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.add_all([source_tag, target_tag])
db.session.commit() db.session.commit()
with fake_datetime('1997-12-01'): with fake_datetime('1997-12-01'):
@ -54,8 +61,8 @@ def test_merging_without_usages(test_ctx, fake_datetime):
assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json')) assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json'))
def test_merging_with_usages(test_ctx, fake_datetime, post_factory): def test_merging_with_usages(test_ctx, fake_datetime, post_factory):
source_tag = test_ctx.tag_factory(names=['source'], category_name='meta') source_tag = test_ctx.tag_factory(names=['source'])
target_tag = test_ctx.tag_factory(names=['target'], category_name='meta') target_tag = test_ctx.tag_factory(names=['target'])
db.session.add_all([source_tag, target_tag]) db.session.add_all([source_tag, target_tag])
db.session.flush() db.session.flush()
assert source_tag.post_count == 0 assert source_tag.post_count == 0
@ -80,8 +87,8 @@ def test_merging_with_usages(test_ctx, fake_datetime, post_factory):
assert tags.get_tag_by_name('target').post_count == 1 assert tags.get_tag_by_name('target').post_count == 1
def test_merging_when_related(test_ctx, fake_datetime): def test_merging_when_related(test_ctx, fake_datetime):
source_tag = test_ctx.tag_factory(names=['source'], category_name='meta') source_tag = test_ctx.tag_factory(names=['source'])
target_tag = test_ctx.tag_factory(names=['target'], category_name='meta') target_tag = test_ctx.tag_factory(names=['target'])
db.session.add_all([source_tag, target_tag]) db.session.add_all([source_tag, target_tag])
db.session.flush() db.session.flush()
referring_tag = test_ctx.tag_factory(names=['parent']) referring_tag = test_ctx.tag_factory(names=['parent'])
@ -106,8 +113,8 @@ def test_merging_when_related(test_ctx, fake_datetime):
assert tags.try_get_tag_by_name('parent').suggestions == [] assert tags.try_get_tag_by_name('parent').suggestions == []
def test_merging_when_target_exists(test_ctx, fake_datetime, post_factory): def test_merging_when_target_exists(test_ctx, fake_datetime, post_factory):
source_tag = test_ctx.tag_factory(names=['source'], category_name='meta') source_tag = test_ctx.tag_factory(names=['source'])
target_tag = test_ctx.tag_factory(names=['target'], category_name='meta') target_tag = test_ctx.tag_factory(names=['target'])
db.session.add_all([source_tag, target_tag]) db.session.add_all([source_tag, target_tag])
db.session.flush() db.session.flush()
post1 = post_factory() post1 = post_factory()
@ -138,8 +145,8 @@ def test_merging_when_target_exists(test_ctx, fake_datetime, post_factory):
({'mergeTo': []}, tags.TagNotFoundError), ({'mergeTo': []}, tags.TagNotFoundError),
]) ])
def test_trying_to_pass_invalid_input(test_ctx, input, expected_exception): def test_trying_to_pass_invalid_input(test_ctx, input, expected_exception):
source_tag = test_ctx.tag_factory(names=['source'], category_name='meta') source_tag = test_ctx.tag_factory(names=['source'])
target_tag = test_ctx.tag_factory(names=['target'], category_name='meta') target_tag = test_ctx.tag_factory(names=['target'])
db.session.add_all([source_tag, target_tag]) db.session.add_all([source_tag, target_tag])
db.session.commit() db.session.commit()
real_input = { real_input = {
@ -160,8 +167,8 @@ def test_trying_to_pass_invalid_input(test_ctx, input, expected_exception):
'field', ['remove', 'mergeTo', 'removeVersion', 'mergeToVersion']) 'field', ['remove', 'mergeTo', 'removeVersion', 'mergeToVersion'])
def test_trying_to_omit_mandatory_field(test_ctx, field): def test_trying_to_omit_mandatory_field(test_ctx, field):
db.session.add_all([ db.session.add_all([
test_ctx.tag_factory(names=['source'], category_name='meta'), test_ctx.tag_factory(names=['source']),
test_ctx.tag_factory(names=['target'], category_name='meta'), test_ctx.tag_factory(names=['target']),
]) ])
db.session.commit() db.session.commit()
input = { input = {
@ -178,7 +185,7 @@ def test_trying_to_omit_mandatory_field(test_ctx, field):
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
def test_trying_to_merge_non_existing(test_ctx): def test_trying_to_merge_non_existing(test_ctx):
db.session.add(test_ctx.tag_factory(names=['good'], category_name='meta')) db.session.add(test_ctx.tag_factory(names=['good']))
db.session.commit() db.session.commit()
with pytest.raises(tags.TagNotFoundError): with pytest.raises(tags.TagNotFoundError):
test_ctx.api.post( test_ctx.api.post(
@ -192,7 +199,7 @@ def test_trying_to_merge_non_existing(test_ctx):
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
def test_trying_to_merge_to_itself(test_ctx): def test_trying_to_merge_to_itself(test_ctx):
db.session.add(test_ctx.tag_factory(names=['good'], category_name='meta')) db.session.add(test_ctx.tag_factory(names=['good']))
db.session.commit() db.session.commit()
with pytest.raises(tags.InvalidTagRelationError): with pytest.raises(tags.InvalidTagRelationError):
test_ctx.api.post( test_ctx.api.post(
@ -212,8 +219,8 @@ def test_trying_to_merge_to_itself(test_ctx):
]) ])
def test_trying_to_merge_without_privileges(test_ctx, input): def test_trying_to_merge_without_privileges(test_ctx, input):
db.session.add_all([ db.session.add_all([
test_ctx.tag_factory(names=['source'], category_name='meta'), test_ctx.tag_factory(names=['source']),
test_ctx.tag_factory(names=['target'], category_name='meta'), test_ctx.tag_factory(names=['target']),
]) ])
db.session.commit() db.session.commit()
with pytest.raises(errors.AuthError): with pytest.raises(errors.AuthError):

View file

@ -4,7 +4,12 @@ from szurubooru import api, db, errors
from szurubooru.func import util, tags from szurubooru.func import util, tags
@pytest.fixture @pytest.fixture
def test_ctx(context_factory, config_injector, user_factory, tag_factory): def test_ctx(
context_factory,
config_injector,
user_factory,
tag_factory,
tag_category_factory):
config_injector({ config_injector({
'privileges': { 'privileges': {
'tags:list': db.User.RANK_REGULAR, 'tags:list': db.User.RANK_REGULAR,
@ -16,6 +21,7 @@ def test_ctx(context_factory, config_injector, user_factory, tag_factory):
ret.context_factory = context_factory ret.context_factory = context_factory
ret.user_factory = user_factory ret.user_factory = user_factory
ret.tag_factory = tag_factory ret.tag_factory = tag_factory
ret.tag_category_factory = tag_category_factory
ret.list_api = api.TagListApi() ret.list_api = api.TagListApi()
ret.detail_api = api.TagDetailApi() ret.detail_api = api.TagDetailApi()
return ret return ret
@ -42,14 +48,15 @@ def test_trying_to_retrieve_multiple_without_privileges(test_ctx):
user=test_ctx.user_factory(rank=db.User.RANK_ANONYMOUS))) user=test_ctx.user_factory(rank=db.User.RANK_ANONYMOUS)))
def test_retrieving_single(test_ctx): def test_retrieving_single(test_ctx):
db.session.add(test_ctx.tag_factory(names=['tag'])) category = test_ctx.tag_category_factory(name='meta')
db.session.add(test_ctx.tag_factory(names=['tag'], category=category))
result = test_ctx.detail_api.get( result = test_ctx.detail_api.get(
test_ctx.context_factory( test_ctx.context_factory(
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
'tag') 'tag')
assert result == { assert result == {
'names': ['tag'], 'names': ['tag'],
'category': 'dummy', 'category': 'meta',
'description': None, 'description': None,
'creationTime': datetime.datetime(1996, 1, 1), 'creationTime': datetime.datetime(1996, 1, 1),
'lastEditTime': None, 'lastEditTime': None,

View file

@ -2,7 +2,7 @@ import datetime
import os import os
import pytest import pytest
from szurubooru import api, config, db, errors from szurubooru import api, config, db, errors
from szurubooru.func import util, tags, tag_categories from szurubooru.func import util, tags, tag_categories, cache
def assert_relations(relations, expected_tag_names): def assert_relations(relations, expected_tag_names):
actual_names = sorted([rel.names[0].name for rel in relations]) actual_names = sorted([rel.names[0].name for rel in relations])
@ -26,7 +26,8 @@ def test_ctx(
}) })
db.session.add_all([ db.session.add_all([
db.TagCategory(name) for name in ['meta', 'character', 'copyright']]) db.TagCategory(name) for name in ['meta', 'character', 'copyright']])
db.session.flush() db.session.commit()
cache.purge()
ret = util.dotdict() ret = util.dotdict()
ret.context_factory = context_factory ret.context_factory = context_factory
ret.user_factory = user_factory ret.user_factory = user_factory
@ -35,7 +36,7 @@ def test_ctx(
return ret return ret
def test_simple_updating(test_ctx, fake_datetime): def test_simple_updating(test_ctx, fake_datetime):
tag = test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta') tag = test_ctx.tag_factory(names=['tag1', 'tag2'])
db.session.add(tag) db.session.add(tag)
db.session.commit() db.session.commit()
with fake_datetime('1997-12-01'): with fake_datetime('1997-12-01'):
@ -79,14 +80,14 @@ def test_simple_updating(test_ctx, fake_datetime):
({'names': ['']}, tags.InvalidTagNameError), ({'names': ['']}, tags.InvalidTagNameError),
({'names': ['!bad']}, tags.InvalidTagNameError), ({'names': ['!bad']}, tags.InvalidTagNameError),
({'names': ['x' * 65]}, tags.InvalidTagNameError), ({'names': ['x' * 65]}, tags.InvalidTagNameError),
({'category': None}, tag_categories.InvalidTagCategoryNameError), ({'category': None}, tag_categories.TagCategoryNotFoundError),
({'category': ''}, tag_categories.InvalidTagCategoryNameError), ({'category': ''}, tag_categories.TagCategoryNotFoundError),
({'category': '!bad'}, tag_categories.InvalidTagCategoryNameError), ({'category': '!bad'}, tag_categories.TagCategoryNotFoundError),
({'suggestions': ['good', '!bad']}, tags.InvalidTagNameError), ({'suggestions': ['good', '!bad']}, tags.InvalidTagNameError),
({'implications': ['good', '!bad']}, tags.InvalidTagNameError), ({'implications': ['good', '!bad']}, tags.InvalidTagNameError),
]) ])
def test_trying_to_pass_invalid_input(test_ctx, input, expected_exception): def test_trying_to_pass_invalid_input(test_ctx, input, expected_exception):
db.session.add(test_ctx.tag_factory(names=['tag1'], category_name='meta')) db.session.add(test_ctx.tag_factory(names=['tag1']))
db.session.commit() db.session.commit()
with pytest.raises(expected_exception): with pytest.raises(expected_exception):
test_ctx.api.put( test_ctx.api.put(
@ -98,7 +99,7 @@ def test_trying_to_pass_invalid_input(test_ctx, input, expected_exception):
@pytest.mark.parametrize( @pytest.mark.parametrize(
'field', ['names', 'category', 'description', 'implications', 'suggestions']) 'field', ['names', 'category', 'description', 'implications', 'suggestions'])
def test_omitting_optional_field(test_ctx, field): def test_omitting_optional_field(test_ctx, field):
db.session.add(test_ctx.tag_factory(names=['tag'], category_name='meta')) db.session.add(test_ctx.tag_factory(names=['tag']))
db.session.commit() db.session.commit()
input = { input = {
'names': ['tag1', 'tag2'], 'names': ['tag1', 'tag2'],
@ -126,7 +127,7 @@ def test_trying_to_update_non_existing(test_ctx):
@pytest.mark.parametrize('dup_name', ['tag1', 'TAG1']) @pytest.mark.parametrize('dup_name', ['tag1', 'TAG1'])
def test_reusing_own_name(test_ctx, dup_name): def test_reusing_own_name(test_ctx, dup_name):
db.session.add( db.session.add(
test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta')) test_ctx.tag_factory(names=['tag1', 'tag2']))
db.session.commit() db.session.commit()
result = test_ctx.api.put( result = test_ctx.api.put(
test_ctx.context_factory( test_ctx.context_factory(
@ -142,7 +143,7 @@ def test_reusing_own_name(test_ctx, dup_name):
def test_duplicating_names(test_ctx): def test_duplicating_names(test_ctx):
db.session.add( db.session.add(
test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta')) test_ctx.tag_factory(names=['tag1', 'tag2']))
result = test_ctx.api.put( result = test_ctx.api.put(
test_ctx.context_factory( test_ctx.context_factory(
input={'names': ['tag3', 'TAG3'], 'version': 1}, input={'names': ['tag3', 'TAG3'], 'version': 1},
@ -158,8 +159,8 @@ def test_duplicating_names(test_ctx):
@pytest.mark.parametrize('dup_name', ['tag1', 'TAG1', 'tag2', 'TAG2']) @pytest.mark.parametrize('dup_name', ['tag1', 'TAG1', 'tag2', 'TAG2'])
def test_trying_to_use_existing_name(test_ctx, dup_name): def test_trying_to_use_existing_name(test_ctx, dup_name):
db.session.add_all([ db.session.add_all([
test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta'), test_ctx.tag_factory(names=['tag1', 'tag2']),
test_ctx.tag_factory(names=['tag3', 'tag4'], category_name='meta')]) test_ctx.tag_factory(names=['tag3', 'tag4'])])
db.session.commit() db.session.commit()
with pytest.raises(tags.TagAlreadyExistsError): with pytest.raises(tags.TagAlreadyExistsError):
test_ctx.api.put( test_ctx.api.put(
@ -193,7 +194,7 @@ def test_trying_to_use_existing_name(test_ctx, dup_name):
def test_updating_new_suggestions_and_implications( def test_updating_new_suggestions_and_implications(
test_ctx, input, expected_suggestions, expected_implications): test_ctx, input, expected_suggestions, expected_implications):
db.session.add( db.session.add(
test_ctx.tag_factory(names=['main'], category_name='meta')) test_ctx.tag_factory(names=['main']))
db.session.commit() db.session.commit()
result = test_ctx.api.put( result = test_ctx.api.put(
test_ctx.context_factory( test_ctx.context_factory(
@ -210,9 +211,9 @@ def test_updating_new_suggestions_and_implications(
def test_reusing_suggestions_and_implications(test_ctx): def test_reusing_suggestions_and_implications(test_ctx):
db.session.add_all([ db.session.add_all([
test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta'), test_ctx.tag_factory(names=['tag1', 'tag2']),
test_ctx.tag_factory(names=['tag3'], category_name='meta'), test_ctx.tag_factory(names=['tag3']),
test_ctx.tag_factory(names=['tag4'], category_name='meta'), test_ctx.tag_factory(names=['tag4']),
]) ])
db.session.commit() db.session.commit()
result = test_ctx.api.put( result = test_ctx.api.put(
@ -248,7 +249,7 @@ def test_reusing_suggestions_and_implications(test_ctx):
} }
]) ])
def test_trying_to_relate_tag_to_itself(test_ctx, input): def test_trying_to_relate_tag_to_itself(test_ctx, input):
db.session.add(test_ctx.tag_factory(names=['tag1'], category_name='meta')) db.session.add(test_ctx.tag_factory(names=['tag1']))
db.session.commit() db.session.commit()
with pytest.raises(tags.InvalidTagRelationError): with pytest.raises(tags.InvalidTagRelationError):
test_ctx.api.put( test_ctx.api.put(
@ -264,7 +265,7 @@ def test_trying_to_relate_tag_to_itself(test_ctx, input):
{'implications': ['whatever']}, {'implications': ['whatever']},
]) ])
def test_trying_to_update_without_privileges(test_ctx, input): def test_trying_to_update_without_privileges(test_ctx, input):
db.session.add(test_ctx.tag_factory(names=['tag'], category_name='meta')) db.session.add(test_ctx.tag_factory(names=['tag']))
db.session.commit() db.session.commit()
with pytest.raises(errors.AuthError): with pytest.raises(errors.AuthError):
test_ctx.api.put( test_ctx.api.put(

View file

@ -106,18 +106,19 @@ def user_factory():
@pytest.fixture @pytest.fixture
def tag_category_factory(): def tag_category_factory():
def factory(name='dummy', color='dummy'): def factory(name=None, color='dummy', default=False):
category = db.TagCategory() category = db.TagCategory()
category.name = name category.name = name or get_unique_name()
category.color = color category.color = color
category.default = default
return category return category
return factory return factory
@pytest.fixture @pytest.fixture
def tag_factory(): def tag_factory():
def factory(names=None, category=None, category_name='dummy'): def factory(names=None, category=None):
if not category: if not category:
category = db.TagCategory(category_name) category = db.TagCategory(get_unique_name())
db.session.add(category) db.session.add(category)
tag = db.Tag() tag = db.Tag()
tag.names = [db.TagName(name) for name in (names or [get_unique_name()])] tag.names = [db.TagName(name) for name in (names or [get_unique_name()])]

View file

@ -127,6 +127,7 @@ def test_serialize_post(
db.session.flush() db.session.flush()
result = posts.serialize_post(post, auth_user) result = posts.serialize_post(post, auth_user)
result['tags'].sort()
assert result == { assert result == {
'id': 1, 'id': 1,

View file

@ -62,8 +62,9 @@ def test_serializing_post(post_factory, user_factory, tag_factory):
} }
def test_serializing_tag(tag_factory): def test_serializing_tag(tag_factory, tag_category_factory):
tag = tag_factory(names=['main_name', 'alias'], category_name='dummy') category = tag_category_factory(name='dummy')
tag = tag_factory(names=['main_name', 'alias'], category=category)
assert snapshots.get_tag_snapshot(tag) == { assert snapshots.get_tag_snapshot(tag) == {
'names': ['main_name', 'alias'], 'names': ['main_name', 'alias'],
'category': 'dummy', 'category': 'dummy',
@ -71,7 +72,7 @@ def test_serializing_tag(tag_factory):
'implications': [], 'implications': [],
} }
tag = tag_factory(names=['main_name', 'alias'], category_name='dummy') tag = tag_factory(names=['main_name', 'alias'], category=category)
imp1 = tag_factory(names=['imp1_main_name', 'imp1_alias']) imp1 = tag_factory(names=['imp1_main_name', 'imp1_alias'])
imp2 = tag_factory(names=['imp2_main_name', 'imp2_alias']) imp2 = tag_factory(names=['imp2_main_name', 'imp2_alias'])
sug1 = tag_factory(names=['sug1_main_name', 'sug1_alias']) sug1 = tag_factory(names=['sug1_main_name', 'sug1_alias'])
@ -196,8 +197,13 @@ def test_merging_resets_merging_time_window(
'initial_operation', 'initial_operation',
[snapshots.save_entity_creation, snapshots.save_entity_modification]) [snapshots.save_entity_creation, snapshots.save_entity_modification])
def test_merging_deletion_to_modification_or_creation( def test_merging_deletion_to_modification_or_creation(
fake_datetime, tag_factory, user_factory, initial_operation): fake_datetime,
tag = tag_factory(names=['dummy'], category_name='dummy') tag_factory,
tag_category_factory,
user_factory,
initial_operation):
category = tag_category_factory(name='dummy')
tag = tag_factory(names=['dummy'], category=category)
user = user_factory() user = user_factory()
db.session.add_all([tag, user]) db.session.add_all([tag, user])
db.session.flush() db.session.flush()
@ -241,8 +247,10 @@ def test_merging_deletion_all_the_way_deletes_all_snapshots(
snapshots.save_entity_deletion(tag, user) snapshots.save_entity_deletion(tag, user)
assert db.session.query(db.Snapshot).count() == 0 assert db.session.query(db.Snapshot).count() == 0
def test_get_serialized_history(fake_datetime, tag_factory, user_factory): def test_get_serialized_history(
tag = tag_factory(names=['dummy']) fake_datetime, tag_factory, tag_category_factory, user_factory):
category = tag_category_factory(name='dummy')
tag = tag_factory(names=['dummy'], category=category)
user = user_factory(name='the-user') user = user_factory(name='the-user')
db.session.add_all([tag, user]) db.session.add_all([tag, user])
db.session.flush() db.session.flush()