From 16d4d3ca68964eb7759b629ec84eb6b14d9d7cdb Mon Sep 17 00:00:00 2001 From: rr- Date: Sat, 21 May 2016 22:29:31 +0200 Subject: [PATCH] server/favorites: favoriting sets score to 1 --- server/szurubooru/func/favorites.py | 11 +++++++++-- server/szurubooru/func/scores.py | 7 ++++--- server/szurubooru/tests/api/test_comment_rating.py | 4 ++-- server/szurubooru/tests/api/test_post_favoriting.py | 7 ++++++- server/szurubooru/tests/api/test_post_rating.py | 4 ++-- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/server/szurubooru/func/favorites.py b/server/szurubooru/func/favorites.py index 65adc47..1cb0a3c 100644 --- a/server/szurubooru/func/favorites.py +++ b/server/szurubooru/func/favorites.py @@ -1,11 +1,14 @@ import datetime -from szurubooru import db +from szurubooru import db, errors +from szurubooru.func import scores + +class InvalidFavoriteTargetError(errors.ValidationError): pass def _get_table_info(entity): resource_type, _, _ = db.util.get_resource_info(entity) if resource_type == 'post': return db.PostFavorite, lambda table: table.post_id - assert False + raise InvalidFavoriteTargetError() def _get_fav_entity(entity, user): return db.util.get_aux_entity(db.session, _get_table_info, entity, user) @@ -19,6 +22,10 @@ def unset_favorite(entity, user): db.session.delete(fav_entity) def set_favorite(entity, user): + try: + scores.set_score(entity, user, 1) + except scores.InvalidScoreTargetError: + pass fav_entity = _get_fav_entity(entity, user) if not fav_entity: table, get_column = _get_table_info(entity) diff --git a/server/szurubooru/func/scores.py b/server/szurubooru/func/scores.py index 03600dd..f475a1e 100644 --- a/server/szurubooru/func/scores.py +++ b/server/szurubooru/func/scores.py @@ -1,7 +1,8 @@ import datetime from szurubooru import db, errors -class InvalidScoreError(errors.ValidationError): pass +class InvalidScoreTargetError(errors.ValidationError): pass +class InvalidScoreValueError(errors.ValidationError): pass def _get_table_info(entity): resource_type, _, _ = db.util.get_resource_info(entity) @@ -9,7 +10,7 @@ def _get_table_info(entity): return db.PostScore, lambda table: table.post_id elif resource_type == 'comment': return db.CommentScore, lambda table: table.comment_id - assert False + raise InvalidScoreTargetError() def _get_score_entity(entity, user): return db.util.get_aux_entity(db.session, _get_table_info, entity, user) @@ -31,7 +32,7 @@ def set_score(entity, user, score): delete_score(entity, user) return if score not in (-1, 1): - raise InvalidScoreError( + raise InvalidScoreValueError( 'Score %r is invalid. Valid scores: %r.' % (score, (-1, 1))) score_entity = _get_score_entity(entity, user) if score_entity: diff --git a/server/szurubooru/tests/api/test_comment_rating.py b/server/szurubooru/tests/api/test_comment_rating.py index 2632dc0..c0a00cc 100644 --- a/server/szurubooru/tests/api/test_comment_rating.py +++ b/server/szurubooru/tests/api/test_comment_rating.py @@ -110,8 +110,8 @@ def test_ratings_from_multiple_users(test_ctx, fake_datetime): @pytest.mark.parametrize('input,expected_exception', [ ({'score': None}, errors.ValidationError), ({'score': ''}, errors.ValidationError), - ({'score': -2}, scores.InvalidScoreError), - ({'score': 2}, scores.InvalidScoreError), + ({'score': -2}, scores.InvalidScoreValueError), + ({'score': 2}, scores.InvalidScoreValueError), ({'score': [1]}, errors.ValidationError), ]) def test_trying_to_pass_invalid_input(test_ctx, input, expected_exception): diff --git a/server/szurubooru/tests/api/test_post_favoriting.py b/server/szurubooru/tests/api/test_post_favoriting.py index f4e0aa0..00a1035 100644 --- a/server/szurubooru/tests/api/test_post_favoriting.py +++ b/server/szurubooru/tests/api/test_post_favoriting.py @@ -23,10 +23,11 @@ def test_ctx( ret.api = api.PostFavoriteApi() return ret -def test_simple_rating(test_ctx, fake_datetime): +def test_adding_to_favorites(test_ctx, fake_datetime): post = test_ctx.post_factory() db.session.add(post) db.session.commit() + assert post.score == 0 with fake_datetime('1997-12-01'): result = test_ctx.api.post( test_ctx.context_factory(user=test_ctx.user_factory()), @@ -37,21 +38,25 @@ def test_simple_rating(test_ctx, fake_datetime): assert db.session.query(db.PostFavorite).count() == 1 assert post is not None assert post.favorite_count == 1 + assert post.score == 1 def test_removing_from_favorites(test_ctx, fake_datetime): user = test_ctx.user_factory() post = test_ctx.post_factory() db.session.add(post) db.session.commit() + assert post.score == 0 with fake_datetime('1997-12-01'): result = test_ctx.api.post( test_ctx.context_factory(user=user), post.post_id) + assert post.score == 1 with fake_datetime('1997-12-02'): result = test_ctx.api.delete( test_ctx.context_factory(user=user), post.post_id) post = db.session.query(db.Post).one() + assert post.score == 1 assert db.session.query(db.PostFavorite).count() == 0 assert post.favorite_count == 0 diff --git a/server/szurubooru/tests/api/test_post_rating.py b/server/szurubooru/tests/api/test_post_rating.py index b8c9cb2..a48830e 100644 --- a/server/szurubooru/tests/api/test_post_rating.py +++ b/server/szurubooru/tests/api/test_post_rating.py @@ -106,8 +106,8 @@ def test_ratings_from_multiple_users(test_ctx, fake_datetime): @pytest.mark.parametrize('input,expected_exception', [ ({'score': None}, errors.ValidationError), ({'score': ''}, errors.ValidationError), - ({'score': -2}, scores.InvalidScoreError), - ({'score': 2}, scores.InvalidScoreError), + ({'score': -2}, scores.InvalidScoreValueError), + ({'score': 2}, scores.InvalidScoreValueError), ({'score': [1]}, errors.ValidationError), ]) def test_trying_to_pass_invalid_input(test_ctx, input, expected_exception):