def test_simple_updating(test_ctx, fake_datetime): tag = test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta') db.session.add(tag) db.session.commit() with fake_datetime('1997-12-01'): result = test_ctx.api.put( test_ctx.context_factory( input={ 'names': ['tag3'], 'category': 'character', }, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag1') assert result['tag'] == { 'names': ['tag3'], 'category': 'character', 'suggestions': [], 'implications': [], 'creationTime': datetime.datetime(1996, 1, 1), 'lastEditTime': datetime.datetime(1997, 12, 1), 'usages': 0, } assert len(result['snapshots']) == 1 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'] assert tag.category.name == 'character' assert tag.suggestions == [] assert tag.implications == [] assert os.path.exists(os.path.join(config.config['data_dir'], 'tags.json'))
def test_try_get_tag_by_name(name_to_search, expected_to_find, tag_factory): tag = tag_factory(names=['name', 'ALIAS']) db.session.add(tag) db.session.flush() if expected_to_find: assert tags.try_get_tag_by_name(name_to_search) == tag else: assert tags.try_get_tag_by_name(name_to_search) is None
def test_duplicating_names(test_ctx): db.session.add( test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta')) result = test_ctx.api.put( test_ctx.context_factory( input={'names': ['tag3', 'TAG3']}, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag1') assert result['tag']['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']
def test_trying_to_use_existing_name(test_ctx): db.session.add_all([ test_ctx.tag_factory(names=['used1'], category_name='meta'), test_ctx.tag_factory(names=['used2'], category_name='meta'), ]) 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_tag_trying_to_relate_to_itself(test_ctx, input): with pytest.raises(tags.InvalidTagRelationError): test_ctx.api.post( test_ctx.context_factory( input=input, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) assert tags.try_get_tag_by_name('tag') is None
def test_merging_without_usages(test_ctx, fake_datetime): source_tag = test_ctx.tag_factory(names=['source'], category_name='meta') target_tag = test_ctx.tag_factory(names=['target'], category_name='meta') 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={ 'remove': 'source', 'mergeTo': 'target', }, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) assert result['tag'] == { 'names': ['target'], 'category': 'meta', 'suggestions': [], 'implications': [], 'creationTime': datetime.datetime(1996, 1, 1), 'lastEditTime': None, 'usages': 0, } assert 'snapshots' in result 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_merge_tags_deletes_source_tag(tag_factory): source_tag = tag_factory(names=['source']) target_tag = tag_factory(names=['target']) db.session.add_all([source_tag, target_tag]) db.session.flush() tags.merge_tags(source_tag, target_tag) db.session.flush() assert tags.try_get_tag_by_name('source') is None tag = tags.get_tag_by_name('target') assert tag is not None
def test_creating_new_suggestions_and_implications( test_ctx, input, expected_suggestions, expected_implications): result = test_ctx.api.post( test_ctx.context_factory( input=input, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) assert result['tag']['suggestions'] == expected_suggestions assert result['tag']['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_merge_tags_moves_usages(tag_factory, post_factory): source_tag = tag_factory(names=['source']) target_tag = tag_factory(names=['target']) post = post_factory() post.tags = [source_tag] db.session.add_all([source_tag, target_tag, post]) db.session.commit() assert source_tag.post_count == 1 assert target_tag.post_count == 0 tags.merge_tags(source_tag, target_tag) db.session.commit() assert tags.try_get_tag_by_name('source') is None assert tags.get_tag_by_name('target').post_count == 1
def test_merge_tags_doesnt_duplicate_usages(tag_factory, post_factory): source_tag = tag_factory(names=['source']) target_tag = tag_factory(names=['target']) post = post_factory() post.tags = [source_tag, target_tag] db.session.add_all([source_tag, target_tag, post]) db.session.flush() assert source_tag.post_count == 1 assert target_tag.post_count == 1 tags.merge_tags(source_tag, target_tag) db.session.flush() 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'], category_name='meta') target_tag = test_ctx.tag_factory(names=['target'], category_name='meta') 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={ '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_reusing_own_name(test_ctx, dup_name): db.session.add( test_ctx.tag_factory(names=['tag1', 'tag2'], category_name='meta')) db.session.commit() result = test_ctx.api.put( test_ctx.context_factory( input={'names': [dup_name, 'tag3']}, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'tag1') assert result['tag']['names'] == ['tag1', '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] == ['tag1', 'tag3']
def test_creating_new_suggestions_and_implications(test_ctx, input, expected_suggestions, expected_implications): result = test_ctx.api.post( test_ctx.context_factory( input=input, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR))) assert result['tag']['suggestions'] == expected_suggestions assert result['tag']['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_merge_tags_doesnt_create_relation_loop_for_parents(tag_factory): source_tag = tag_factory(names=['source']) target_tag = tag_factory(names=['target']) target_tag.suggestions = [source_tag] target_tag.implications = [source_tag] db.session.add_all([source_tag, target_tag]) db.session.commit() assert source_tag.suggestion_count == 0 assert source_tag.implication_count == 0 assert target_tag.suggestion_count == 1 assert target_tag.implication_count == 1 tags.merge_tags(source_tag, target_tag) db.session.commit() assert tags.try_get_tag_by_name('source') is None assert tags.get_tag_by_name('target').suggestion_count == 0 assert tags.get_tag_by_name('target').implication_count == 0
def test_merge_tags_doesnt_create_relation_loop_for_children(tag_factory): source_tag = tag_factory(names=["source"]) target_tag = tag_factory(names=["target"]) source_tag.suggestions = [target_tag] source_tag.implications = [target_tag] db.session.add_all([source_tag, target_tag]) db.session.commit() assert source_tag.suggestion_count == 1 assert source_tag.implication_count == 1 assert target_tag.suggestion_count == 0 assert target_tag.implication_count == 0 tags.merge_tags(source_tag, target_tag) db.session.commit() assert tags.try_get_tag_by_name("source") is None assert tags.get_tag_by_name("target").suggestion_count == 0 assert tags.get_tag_by_name("target").implication_count == 0
def test_updating_new_suggestions_and_implications( test_ctx, input, expected_suggestions, expected_implications): db.session.add( test_ctx.tag_factory(names=['main'], category_name='meta')) db.session.commit() result = test_ctx.api.put( test_ctx.context_factory( input=input, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'main') assert result['tag']['suggestions'] == expected_suggestions assert result['tag']['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_updating_new_suggestions_and_implications(test_ctx, input, expected_suggestions, expected_implications): db.session.add(test_ctx.tag_factory(names=['main'], category_name='meta')) db.session.commit() result = test_ctx.api.put( test_ctx.context_factory( input=input, user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)), 'main') assert result['tag']['suggestions'] == expected_suggestions assert result['tag']['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_merge_tags_moves_child_relations(tag_factory): source_tag = tag_factory(names=['source']) target_tag = tag_factory(names=['target']) related_tag = tag_factory() source_tag.suggestions = [related_tag] source_tag.implications = [related_tag] db.session.add_all([source_tag, target_tag, related_tag]) db.session.commit() assert source_tag.suggestion_count == 1 assert source_tag.implication_count == 1 assert target_tag.suggestion_count == 0 assert target_tag.implication_count == 0 tags.merge_tags(source_tag, target_tag) db.session.commit() assert tags.try_get_tag_by_name('source') is None assert tags.get_tag_by_name('target').suggestion_count == 1 assert tags.get_tag_by_name('target').implication_count == 1
def test_merge_tags_doesnt_duplicate_child_relations(tag_factory): source_tag = tag_factory(names=["source"]) target_tag = tag_factory(names=["target"]) related_tag = tag_factory() source_tag.suggestions = [related_tag] source_tag.implications = [related_tag] target_tag.suggestions = [related_tag] target_tag.implications = [related_tag] db.session.add_all([source_tag, target_tag, related_tag]) db.session.commit() assert source_tag.suggestion_count == 1 assert source_tag.implication_count == 1 assert target_tag.suggestion_count == 1 assert target_tag.implication_count == 1 tags.merge_tags(source_tag, target_tag) db.session.commit() assert tags.try_get_tag_by_name("source") is None assert tags.get_tag_by_name("target").suggestion_count == 1 assert tags.get_tag_by_name("target").implication_count == 1
def test_merge_tags_moves_parent_relations(tag_factory): source_tag = tag_factory(names=["source"]) target_tag = tag_factory(names=["target"]) related_tag = tag_factory(names=["related"]) related_tag.suggestions = [related_tag] related_tag.implications = [related_tag] db.session.add_all([source_tag, target_tag, related_tag]) db.session.commit() assert source_tag.suggestion_count == 0 assert source_tag.implication_count == 0 assert target_tag.suggestion_count == 0 assert target_tag.implication_count == 0 tags.merge_tags(source_tag, target_tag) db.session.commit() assert tags.try_get_tag_by_name("source") is None assert tags.get_tag_by_name("related").suggestion_count == 1 assert tags.get_tag_by_name("related").suggestion_count == 1 assert tags.get_tag_by_name("target").suggestion_count == 0 assert tags.get_tag_by_name("target").implication_count == 0
def test_merging_with_usages(test_ctx, fake_datetime, post_factory): source_tag = test_ctx.tag_factory(names=['source'], category_name='meta') target_tag = test_ctx.tag_factory(names=['target'], category_name='meta') db.session.add_all([source_tag, target_tag]) db.session.flush() assert source_tag.post_count == 0 assert target_tag.post_count == 0 post = post_factory() post.tags = [source_tag] db.session.add(post) db.session.commit() assert source_tag.post_count == 1 assert target_tag.post_count == 0 with fake_datetime('1997-12-01'): result = test_ctx.api.post( test_ctx.context_factory( input={ '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