Skip to content

Commit

Permalink
refactor: ♻️ change GQL custom test class to use transation
Browse files Browse the repository at this point in the history
  • Loading branch information
delcroip committed Jan 23, 2025
1 parent db5b410 commit 2c71c4b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
7 changes: 3 additions & 4 deletions core/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.apps import apps
from django.utils import timezone
from django.dispatch import receiver
from core.models import InteractiveUser
import logging
import uuid
from datetime import datetime
Expand Down Expand Up @@ -93,10 +94,8 @@ def get_jwt_key(encode=True, context=None, payload=None):

def extract_private_key_from_payload(payload):
# Get user private key from payload. This covers the refresh token mutation
from core.models import User

if "username" in payload:
return User.objects.get(username=payload["username"]).private_key
if "username" in payload:
return InteractiveUser.objects.get(login_name=payload["username"]).private_key


def extract_private_key_from_context(context):
Expand Down
65 changes: 64 additions & 1 deletion core/models/openimis_graphql_test_case.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,92 @@
import json
from django.conf import settings
from graphene_django.utils.testing import GraphQLTestCase
from django.core.management import call_command
from django.apps import apps
from django.db import transaction, connection

from django.test import TransactionTestCase
from simple_history.models import HistoricalChanges
import uuid
import logging
from graphene import Schema
from graphene.test import Client
import datetime
import time
from core.models import ObjectMutation, MutationLog


logger = logging.getLogger(__name__)

MODEL_NOT_TO_FLUSH = [
'gender',
'role',
'roleright',
'profession',
'familytype',
'location',
'healthfacility',
'permission'

]
TABLE_TO_FLUSH = ['tblLogins', 'tblHealthStatus']

class openIMISGraphQLTestCase(GraphQLTestCase):
class openIMISGraphQLTestCase(TransactionTestCase):
GRAPHQL_URL = f"/{settings.SITE_ROOT()}graphql"
GRAPHQL_SCHEMA = True

def query(self, query, **kwargs):
"""
Execute a GraphQL query using the client.
"""
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()



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)


@classmethod
def setUpClass(cls):
# cls.client=Client(cls.schema)
super(openIMISGraphQLTestCase, cls).setUpClass()


def assertResponseNoErrors(self, resp, **kwargs):
return GraphQLTestCase.assertResponseNoErrors(self, resp, kwargs)

def assertResponseHasErrors(self, resp, **kwargs):
return GraphQLTestCase.assertResponseHasErrors(self, resp, kwargs)

def get_mutation_result(self, mutation_uuid, token, internal=False):
content = None
while True:
Expand Down
3 changes: 3 additions & 0 deletions core/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class Meta:


class InteractiveUser(VersionedModel):

USERNAME_FIELD = 'login_name'

id = models.AutoField(db_column="UserID", primary_key=True)
uuid = models.CharField(db_column="UserUUID", max_length=36, default=uuid.uuid4, unique=True)
language = models.ForeignKey(Language, models.DO_NOTHING, db_column="LanguageID")
Expand Down
9 changes: 9 additions & 0 deletions core/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ def create_test_interactive_user(username='TestInteractiveTest', password="S\\:\
}
)


if not user:
user = User.objects.filter(
username=username,
).first()
if user:
user.i_user = i_user
user.save()

if not user:
user = User.objects.create(
username=username,
i_user=i_user,
Expand Down

0 comments on commit 2c71c4b

Please sign in to comment.