diff --git a/backend/dataall/modules/vote/db/vote_repositories.py b/backend/dataall/modules/vote/db/vote_repositories.py index 339922439..88971a23d 100644 --- a/backend/dataall/modules/vote/db/vote_repositories.py +++ b/backend/dataall/modules/vote/db/vote_repositories.py @@ -15,6 +15,7 @@ def get_vote(session, targetUri, targetType) -> [models.Vote]: .filter( models.Vote.targetUri == targetUri, models.Vote.targetType == targetType, + models.Vote.username == get_context().username, ) .first() ) @@ -24,6 +25,7 @@ def upvote(session, targetUri: str, targetType: str, upvote: bool) -> [models.Vo vote: models.Vote = ( session.query(models.Vote) .filter( + models.Vote.username == get_context().username, models.Vote.targetUri == targetUri, models.Vote.targetType == targetType, ) diff --git a/tests_new/integration_tests/modules/vote/conftest.py b/tests_new/integration_tests/modules/vote/conftest.py new file mode 100644 index 000000000..16781af4e --- /dev/null +++ b/tests_new/integration_tests/modules/vote/conftest.py @@ -0,0 +1,11 @@ +import pytest +from integration_tests.modules.vote.queries import upvote, get_vote + + +S3_DATASET_TARGET_TYPE = 'dataset' + + +@pytest.fixture(scope='session') +def vote1(client1, session_s3_dataset1): + upvote(client1, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, True) + yield get_vote(client1, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE) diff --git a/tests_new/integration_tests/modules/vote/queries.py b/tests_new/integration_tests/modules/vote/queries.py new file mode 100644 index 000000000..1912bb79e --- /dev/null +++ b/tests_new/integration_tests/modules/vote/queries.py @@ -0,0 +1,55 @@ +# TODO: This file will be replaced by using the SDK directly + + +def upvote(client, uri, target_type, vote): + query = { + 'operationName': 'upVote', + 'variables': {'input': {'targetUri': uri, 'targetType': target_type, 'upvote': vote}}, + 'query': """ + mutation upVote($input:VoteInput!){ + upVote(input:$input){ + voteUri + targetUri + targetType + upvote + } + } + """, + } + + response = client.query(query=query) + return response.data.upVote + + +def count_upvotes(client, uri, target_type): + query = { + 'operationName': 'countUpVotes', + 'variables': {'targetUri': uri, 'targetType': target_type}, + 'query': """ + query countUpVotes($targetUri:String!, $targetType:String!){ + countUpVotes(targetUri:$targetUri, targetType:$targetType) + } + """, + } + response = client.query(query=query) + return response.data.countUpVotes + + +def get_vote(client, uri, target_type): + query = { + 'operationName': 'getVote', + 'variables': {'targetUri': uri, 'targetType': target_type}, + 'query': """ + query getVote($targetUri:String!, $targetType:String!){ + getVote(targetUri:$targetUri, targetType:$targetType){ + upvote + voteUri + targetUri + targetType + } + } + """, + } + response = client.query(query=query) + print(response.data.getVote) + return response.data.getVote diff --git a/tests_new/integration_tests/modules/vote/test_vote.py b/tests_new/integration_tests/modules/vote/test_vote.py new file mode 100644 index 000000000..49c657bbf --- /dev/null +++ b/tests_new/integration_tests/modules/vote/test_vote.py @@ -0,0 +1,51 @@ +from assertpy import assert_that + +from integration_tests.errors import GqlError + +from integration_tests.modules.vote.queries import upvote, count_upvotes, get_vote +from integration_tests.modules.vote.conftest import S3_DATASET_TARGET_TYPE + + +def test_upvote(client1, vote1): + assert_that(vote1).is_not_none() + assert_that(vote1.voteUri).is_not_none() + assert_that(vote1.upvote).is_true() + + +def test_upvote_invalid(client1, vote1, session_s3_dataset1): + assert_that(upvote).raises(GqlError).when_called_with(client1, session_s3_dataset1.datasetUri, None, True).contains( + 'targetType', 'not to be None' + ) + assert_that(upvote).raises(GqlError).when_called_with(client1, None, S3_DATASET_TARGET_TYPE, True).contains( + 'targetUri', 'not to be None' + ) + + +def test_get_vote_invalid(client1, vote1, session_s3_dataset1): + assert_that(get_vote).raises(GqlError).when_called_with(client1, session_s3_dataset1.datasetUri, None).contains( + 'targetType', 'must not be null' + ) + assert_that(get_vote).raises(GqlError).when_called_with(client1, None, S3_DATASET_TARGET_TYPE).contains( + 'targetUri', 'must not be null' + ) + + +def test_count_upvote_invalid(client1, vote1, session_s3_dataset1): + assert_that(count_upvotes).raises(GqlError).when_called_with( + client1, session_s3_dataset1.datasetUri, None + ).contains('targetType', 'must not be null') + assert_that(count_upvotes).raises(GqlError).when_called_with(client1, None, S3_DATASET_TARGET_TYPE).contains( + 'targetUri', 'must not be null' + ) + + +def test_count_votes(client2, vote1, session_s3_dataset1): + count = count_upvotes(client2, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE) + + # Assert incremeent by 1 + upvote(client2, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, True) + assert_that(count_upvotes(client2, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE)).is_equal_to(count + 1) + + # Assert decrement by 1 + upvote(client2, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, False) + assert_that(count_upvotes(client2, session_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE)).is_equal_to(count)