Skip to content

Commit

Permalink
♻️ - refactor: refactor gherkin.py
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvandescheur committed Dec 10, 2024
1 parent 546e245 commit d5df99f
Showing 1 changed file with 19 additions and 45 deletions.
64 changes: 19 additions & 45 deletions backend/src/openarchiefbeheer/utils/tests/gherkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,6 @@ async def user_exists(self, **kwargs):
)

async def list_exists(self, **kwargs):
@sync_to_async()
def add_items(destruction_list, assignees=[], items=[]):
if assignees:
destruction_list.assignees.set(assignees)

if items:
destruction_list.items.set(items)

record_manager = await self.record_manager_exists()
base_kwargs = {
"name": "My First Destruction List",
Expand All @@ -171,7 +163,13 @@ def add_items(destruction_list, assignees=[], items=[]):
)
for zaak in zaken
]
await add_items(destruction_list, assignees, items)

if assignees:
await destruction_list.assignees.aset(assignees)

if items:
await destruction_list.items.aset(items)

return destruction_list

async def list_item_exists(self, **kwargs):
Expand All @@ -188,11 +186,6 @@ async def list_item_exists(self, **kwargs):
)

async def review_exists(self, **kwargs):
@sync_to_async()
def add_items(review, items=None):
if items:
review.item_reviews.set(items)

base_kwargs = {
"destruction_list": (
await self.list_exists() if "destruction_list" not in kwargs else {}
Expand All @@ -211,18 +204,19 @@ def add_items(review, items=None):
review = await self._get_or_create(
DestructionListReviewFactory, **merged_kwargs
)
await add_items(review, items)

if items:
await review.item_reviews.aset(items)

return review

async def review_item_exists(self, **kwargs):
@sync_to_async()
def get_item(merged_kwargs):
return merged_kwargs["destruction_list"].items.first()

destruction_list = await self.list_exists()
base_kwargs = {"destruction_list": destruction_list}
merged_kwargs = {**base_kwargs, **kwargs}
item = merged_kwargs.pop("items", await get_item(merged_kwargs))
item = merged_kwargs.pop(
"items", await merged_kwargs["destruction_list"].items.afirst()
)

return await self._get_or_create(
DestructionListItemReviewFactory,
Expand Down Expand Up @@ -252,10 +246,6 @@ async def zaak_selection_api_is_empty(self):
await SelectionItem.objects.all().adelete()
await AllSelectedToggle.objects.all().adelete()

@sync_to_async
def _orm_get(self, model, **kwargs):
return model.objects.get(**kwargs)

@sync_to_async
def _orm_filter(self, model, **kwargs):
return (
Expand Down Expand Up @@ -283,7 +273,7 @@ async def _get_or_create(self, factory, **kwargs):
if key not in factory._meta.parameters
and not key.startswith("post__")
}
return await self._orm_get(factory._meta.model, **orm_params)
return await factory._meta.model.objects.aget(**orm_params)
except factory._meta.model.DoesNotExist:
return await self._factory_create(factory, **kwargs)

Expand Down Expand Up @@ -475,41 +465,25 @@ async def inverted_method(*args, **kwargs):
return InvertedThen(self)

async def list_should_exist(self, page, name):
@sync_to_async()
def get_list():
return DestructionList.objects.get(name=name)

return await get_list()
return await DestructionList.objects.aget(name=name)

async def list_should_have_assignee(self, page, destruction_list, assignee):
@sync_to_async()
def refresh_list():
destruction_list.refresh_from_db()

@sync_to_async()
def get_assignee():
return destruction_list.assignee

await refresh_list()
await destruction_list.arefresh_from_db()
list_assignee = await get_assignee()
self.testcase.assertEqual(list_assignee, assignee)

async def list_should_have_status(self, page, destruction_list, status):
@sync_to_async()
def refresh_list():
destruction_list.refresh_from_db()

await refresh_list()
await destruction_list.arefresh_from_db()
self.testcase.assertEqual(destruction_list.status, status)

async def list_should_have_number_of_items(
self, destruction_list, number_of_items
):
@sync_to_async()
def get_number_of_items():
return destruction_list.items.count()

count = await get_number_of_items()
count = await destruction_list.items.acount()
self.testcase.assertEqual(number_of_items, count)

async def page_should_contain_text(self, page, text, timeout=None):
Expand Down

0 comments on commit d5df99f

Please sign in to comment.