From 336f626a88910e8ed63e167e6dc7a37940348693 Mon Sep 17 00:00:00 2001 From: Noah Paige Date: Wed, 25 Sep 2024 11:16:28 -0700 Subject: [PATCH 1/4] add integration tests votes --- .../modules/vote/db/vote_repositories.py | 2 + .../modules/vote/conftest.py | 11 ++++ .../integration_tests/modules/vote/queries.py | 55 +++++++++++++++++++ .../modules/vote/test_vote.py | 24 ++++++++ 4 files changed, 92 insertions(+) create mode 100644 tests_new/integration_tests/modules/vote/conftest.py create mode 100644 tests_new/integration_tests/modules/vote/queries.py create mode 100644 tests_new/integration_tests/modules/vote/test_vote.py 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..19711d892 --- /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, persistent_s3_dataset1): + upvote(client1, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, True) + yield get_vote(client1, persistent_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..d2daa4421 --- /dev/null +++ b/tests_new/integration_tests/modules/vote/test_vote.py @@ -0,0 +1,24 @@ +from assertpy import assert_that + +from integration_tests.modules.vote.queries import upvote, count_upvotes +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_count_votes(client2, vote1, persistent_s3_dataset1): + count = count_upvotes(client2, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE) + + # Assert incremeent by 1 + upvote(client2, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, True) + assert_that(count_upvotes(client2, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE)).is_equal_to( + count + 1 + ) + + # Assert decrement by 1 + upvote(client2, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, False) + assert_that(count_upvotes(client2, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE)).is_equal_to(count) From 765d497b39aa7c86297840d896dd7f9ba2fbbaa1 Mon Sep 17 00:00:00 2001 From: Noah Paige Date: Wed, 25 Sep 2024 13:00:57 -0700 Subject: [PATCH 2/4] Switc persistent dataset to session dataset --- tests_new/integration_tests/modules/vote/conftest.py | 6 +++--- .../integration_tests/modules/vote/test_vote.py | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests_new/integration_tests/modules/vote/conftest.py b/tests_new/integration_tests/modules/vote/conftest.py index 19711d892..16781af4e 100644 --- a/tests_new/integration_tests/modules/vote/conftest.py +++ b/tests_new/integration_tests/modules/vote/conftest.py @@ -6,6 +6,6 @@ @pytest.fixture(scope='session') -def vote1(client1, persistent_s3_dataset1): - upvote(client1, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, True) - yield get_vote(client1, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE) +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/test_vote.py b/tests_new/integration_tests/modules/vote/test_vote.py index d2daa4421..ee929cb74 100644 --- a/tests_new/integration_tests/modules/vote/test_vote.py +++ b/tests_new/integration_tests/modules/vote/test_vote.py @@ -10,15 +10,15 @@ def test_upvote(client1, vote1): assert_that(vote1.upvote).is_true() -def test_count_votes(client2, vote1, persistent_s3_dataset1): - count = count_upvotes(client2, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE) +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, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, True) - assert_that(count_upvotes(client2, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE)).is_equal_to( + 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, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE, False) - assert_that(count_upvotes(client2, persistent_s3_dataset1.datasetUri, S3_DATASET_TARGET_TYPE)).is_equal_to(count) + 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) From 3f6c491c250535d9f4b739b75b191315414175d8 Mon Sep 17 00:00:00 2001 From: Noah Paige Date: Wed, 25 Sep 2024 16:52:08 -0700 Subject: [PATCH 3/4] Ruff --- tests_new/integration_tests/modules/vote/test_vote.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests_new/integration_tests/modules/vote/test_vote.py b/tests_new/integration_tests/modules/vote/test_vote.py index ee929cb74..33a8a0867 100644 --- a/tests_new/integration_tests/modules/vote/test_vote.py +++ b/tests_new/integration_tests/modules/vote/test_vote.py @@ -15,9 +15,7 @@ def test_count_votes(client2, vote1, session_s3_dataset1): # 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_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) From cc3716b4a1ce89a1daa942b945fb544ad52bb1de Mon Sep 17 00:00:00 2001 From: Noah Paige Date: Thu, 26 Sep 2024 10:45:00 -0700 Subject: [PATCH 4/4] Add invalid input unhappy path --- .../modules/vote/test_vote.py | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/tests_new/integration_tests/modules/vote/test_vote.py b/tests_new/integration_tests/modules/vote/test_vote.py index 33a8a0867..49c657bbf 100644 --- a/tests_new/integration_tests/modules/vote/test_vote.py +++ b/tests_new/integration_tests/modules/vote/test_vote.py @@ -1,6 +1,8 @@ from assertpy import assert_that -from integration_tests.modules.vote.queries import upvote, count_upvotes +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 @@ -10,6 +12,33 @@ def test_upvote(client1, vote1): 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)