Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ♻️ change GQL custom test class to use transation #361

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

delcroip
Copy link
Member

No description provided.

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
2 Security Hotspots
56.4% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Copy link
Contributor

@sniedzielski sniedzielski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sniedzielski
Copy link
Contributor

sniedzielski commented Jan 24, 2025

@delcroip Just only to double check, could you verify two security issues reported by SONAR?

Comment on lines +55 to +75
def _fixture_teardown(self):
with transaction.atomic(), connection.cursor() as cursor:
for table_name in TABLE_TO_FLUSH:
cursor.execute(f'TRUNCATE TABLE "{table_name}"')

for app in apps.get_app_configs():
models_dict = app.models
# Print model names and their classes
for model_name, model_class in models_dict.items():
if (
model_name.lower() not in MODEL_NOT_TO_FLUSH
and model_class._meta.db_table
and not 'View' in model_class._meta.db_table
and not model_class._meta.swapped
):
savepoint = transaction.savepoint()
try:
cursor.execute(f'TRUNCATE TABLE "{model_class._meta.db_table}"')
except Exception as e:
# Roll back to the savepoint (only this operation will be undone)
transaction.savepoint_rollback(savepoint)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a hack. Can we run tests without --keepdb but instead setup db in a custom class SeededTestRunner(DiscoverRunner) and inside setup_databases setup MODEL_NOT_TO_FLUSH using fixtures?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but we will need a proper way to init the db, we are working on that with solution building

Comment on lines +42 to +51
return GraphQLTestCase.query.__call__(self, query, **kwargs)

class BaseTestContext:
def __init__(self, user):
self.user = user
# client = None
def setUp(self):
# cls.client=Client(cls.schema)
GraphQLTestCase.setUp.__call__(self)
super().setUp()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that this implementation is inheriting from one class and reaching out to another class that is meant to be inherited from with __call__ multiple times, creates a code smell.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that the only way I found in python to call an external with another context, inheritance don't work because GraphQLTestCase inherite for TestCase, which inherite from TransactionTestCase .... yes it smell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants