Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from Ivareh/226-update-the-pytests-backend-to…
Browse files Browse the repository at this point in the history
…-the-new-backend-setup

226 update the pytests backend to the new backend setup
bogadisa authored May 14, 2024
2 parents 8cc58d8 + df51935 commit 094d77d
Showing 8 changed files with 234 additions and 83 deletions.
26 changes: 18 additions & 8 deletions src/backend_api/app/app/tests/crud/crud_models/test_item.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import asyncio
from sqlalchemy.orm import Session
from typing import Callable, Dict, Tuple, List, Union
import pytest

@@ -8,9 +6,9 @@
CRUD_account,
CRUD_itemBaseType,
CRUD_currency,
CRUD_stash,
)
from app.core.models.database import engine
from app.core.models.models import Item, Account, ItemBaseType, Currency
from app.core.models.models import Account, Item, ItemBaseType, Currency, Stash
from app.crud.base import CRUDBase
import app.tests.crud.cascade_tests as cascade_test
from app.tests.utils.model_utils.item import generate_random_item
@@ -23,14 +21,25 @@ def object_generator_func() -> Callable[[], Dict]:

@pytest.fixture(scope="module")
def object_generator_func_w_deps() -> (
Callable[
[], Tuple[Dict, Item, List[Union[Dict, Account, ItemBaseType, Currency]]]
]
Callable[[], Tuple[Dict, Item, List[Union[Dict, Stash, ItemBaseType, Currency, Account]]]]
):
def generate_random_item_w_deps(
db,
) -> Callable[
[], Tuple[Dict, Item, List[Union[Dict, Account, ItemBaseType, Currency]]]
[],
Tuple[
Dict,
Item,
List[
Union[
Dict,
Stash,
ItemBaseType,
Currency,
Account
]
],
],
]:
return generate_random_item(db, retrieve_dependencies=True)

@@ -46,6 +55,7 @@ def crud_instance() -> CRUDBase:
def crud_deps_instances() -> CRUDBase:
return [
CRUD_account,
CRUD_stash,
CRUD_itemBaseType,
CRUD_currency,
]
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
CRUD_currency,
CRUD_item,
CRUD_modifier,
CRUD_stash,
)
from app.core.models.models import (
ItemModifier,
@@ -17,6 +18,7 @@
Currency,
Item,
Modifier,
Stash,
)
from app.crud.base import CRUDBase
import app.tests.crud.cascade_tests as cascade_test
@@ -34,7 +36,7 @@ def object_generator_func_w_deps() -> Callable[
Tuple[
Dict,
ItemModifier,
List[Union[Dict, Account, ItemBaseType, Currency, Item, Modifier]],
List[Union[Dict, Item, Stash, Account, ItemBaseType, Currency, Modifier]],
],
]:
def generate_random_item_modifier_w_deps(
@@ -44,7 +46,7 @@ def generate_random_item_modifier_w_deps(
Tuple[
Dict,
ItemModifier,
List[Union[Dict, Item, Account, ItemBaseType, Currency, Modifier]],
List[Union[Dict, Item, Stash, Account, ItemBaseType, Currency, Modifier]],
],
]:
return generate_random_item_modifier(db, retrieve_dependencies=True)
@@ -61,6 +63,7 @@ def crud_instance() -> CRUDBase:
def crud_deps_instances() -> List[CRUDBase]:
return [
CRUD_account,
CRUD_stash,
CRUD_itemBaseType,
CRUD_currency,
CRUD_item,
134 changes: 68 additions & 66 deletions src/backend_api/app/app/tests/crud/crud_models/test_modifier.py
Original file line number Diff line number Diff line change
@@ -34,69 +34,71 @@ def crud_instance() -> CRUDBase:
return CRUD_modifier


class TestModifierCRUD(test_crud.TestCRUD):
@pytest.mark.asyncio
async def test_main_key_get(
self,
db: Session,
crud_instance: CRUDBase,
object_generator_func_w_main_key: Callable[[], Tuple[Dict, ModelType]],
*,
count: int = 5,
) -> None:
"""
A test function.
1. Creates multiple objects, which all have the same key
2. Creates a map, only containing the 'modifierId' key
3. Retrieves all objects with matching 'modifierId' key
4. Sorts the objects so that they line up
5. Tests if the retrieved objects are the same as the initial
"""
multiple_object_dict, multiple_object_out = await self._create_multiple_objects(
db, object_generator_func_w_main_key, count=count
)

self._test_object(multiple_object_out, multiple_object_dict)

modifier_map = {"modifierId": multiple_object_out[0].modifierId}
multiple_object_db = await crud_instance.get(
db, filter=modifier_map, sort_key="position", sort="asc"
)
dict_refrence = [item["position"] for item in multiple_object_dict]
multiple_object_dict = sort_with_refrence(multiple_object_dict, dict_refrence)

assert len(multiple_object_db) == count
self._test_object(multiple_object_db, multiple_object_dict)

@pytest.mark.asyncio
async def test_main_key_delete(
self,
db: Session,
crud_instance: CRUDBase,
object_generator_func_w_main_key: Callable[[], Tuple[Dict, ModelType]],
count: Optional[int] = 5,
) -> None:
"""
A test function.
1. Creates multiple objects, which all have the same key
2. Creates a map, only containing the 'modifierId' key
3. Deletes all objects with matching 'modifierId' key
4. Sorts the objects so that they line up
5. Tests if the deleted objects are the same as the initial
"""
multiple_object_dict, multiple_object_out = await self._create_multiple_objects(
db, object_generator_func_w_main_key, count=count
)
self._test_object(multiple_object_out, multiple_object_dict)

modifier_map = {"modifierId": multiple_object_out[0].modifierId}
deleted_objects = await crud_instance.remove(
db=db, filter=modifier_map, sort_key="position", sort="asc"
)

dict_refrence = [item["position"] for item in multiple_object_dict]
multiple_object_out = sort_with_refrence(multiple_object_out, dict_refrence)
assert deleted_objects
self._test_object(deleted_objects, multiple_object_out)
# Modifier has only one key, which is 'modifierId'. Therefore, the following tests are not needed.

# class TestModifierCRUD(test_crud.TestCRUD):
# @pytest.mark.asyncio
# async def test_main_key_get(
# self,
# db: Session,
# crud_instance: CRUDBase,
# object_generator_func_w_main_key: Callable[[], Tuple[Dict, ModelType]],
# *,
# count: int = 5,
# ) -> None:
# """
# A test function.

# 1. Creates multiple objects, which all have the same key
# 2. Creates a map, only containing the 'modifierId' key
# 3. Retrieves all objects with matching 'modifierId' key
# 4. Sorts the objects so that they line up
# 5. Tests if the retrieved objects are the same as the initial
# """
# multiple_object_dict, multiple_object_out = await self._create_multiple_objects(
# db, object_generator_func_w_main_key, count=count
# )

# self._test_object(multiple_object_out, multiple_object_dict)

# modifier_map = {"modifierId": multiple_object_out[0].modifierId}
# multiple_object_db = await crud_instance.get(
# db, filter=modifier_map, sort_key="position", sort="asc"
# )
# dict_refrence = [item["position"] for item in multiple_object_dict]
# multiple_object_dict = sort_with_refrence(multiple_object_dict, dict_refrence)

# assert len(multiple_object_db) == count
# self._test_object(multiple_object_db, multiple_object_dict)

# @pytest.mark.asyncio
# async def test_main_key_delete(
# self,
# db: Session,
# crud_instance: CRUDBase,
# object_generator_func_w_main_key: Callable[[], Tuple[Dict, ModelType]],
# count: Optional[int] = 5,
# ) -> None:
# """
# A test function.

# 1. Creates multiple objects, which all have the same key
# 2. Creates a map, only containing the 'modifierId' key
# 3. Deletes all objects with matching 'modifierId' key
# 4. Sorts the objects so that they line up
# 5. Tests if the deleted objects are the same as the initial
# """
# multiple_object_dict, multiple_object_out = await self._create_multiple_objects(
# db, object_generator_func_w_main_key, count=count
# )
# self._test_object(multiple_object_out, multiple_object_dict)

# modifier_map = {"modifierId": multiple_object_out[0].modifierId}
# deleted_objects = await crud_instance.remove(
# db=db, filter=modifier_map, sort_key="position", sort="asc"
# )

# dict_refrence = [item["position"] for item in multiple_object_dict]
# multiple_object_out = sort_with_refrence(multiple_object_out, dict_refrence)
# assert deleted_objects
# self._test_object(deleted_objects, multiple_object_out)
44 changes: 44 additions & 0 deletions src/backend_api/app/app/tests/crud/crud_models/test_stash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import Callable, Dict, Tuple, List, Union
import pytest

from app.crud import (
CRUD_stash,
CRUD_account,
)
from app.core.models.models import Stash, Account
from app.crud.base import CRUDBase
import app.tests.crud.cascade_tests as cascade_test
from app.tests.utils.model_utils.stash import generate_random_stash


@pytest.fixture(scope="module")
def object_generator_func() -> Callable[[], Dict]:
return generate_random_stash


@pytest.fixture(scope="module")
def object_generator_func_w_deps() -> (
Callable[[], Tuple[Dict, Stash, List[Union[Dict, Account]]]]
):
def generate_random_stash_w_deps(
db,
) -> Callable[[], Tuple[Dict, Stash, List[Union[Dict, Account]]]]:
return generate_random_stash(db, retrieve_dependencies=True)

return generate_random_stash_w_deps


@pytest.fixture(scope="module")
def crud_instance() -> CRUDBase:
return CRUD_stash


@pytest.fixture(scope="module")
def crud_deps_instances() -> CRUDBase:
return [
CRUD_account,
]


class TestStashCRUD(cascade_test.TestCascade):
pass
47 changes: 42 additions & 5 deletions src/backend_api/app/app/tests/utils/model_utils/item.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
from sqlalchemy.orm import Session

from app import crud
from app.core.models.models import Item, Account, Currency, ItemBaseType
from app.core.models.models import Stash, Account, Item, Currency, ItemBaseType
from app.core.schemas.item import ItemCreate
from app.tests.utils.utils import (
random_lower_string,
@@ -16,12 +16,25 @@

from app.tests.utils.model_utils.item_base_type import generate_random_item_base_type
from app.tests.utils.model_utils.currency import generate_random_currency
from app.tests.utils.model_utils.stash import generate_random_stash


async def create_random_item_dict(
db: Session, retrieve_dependencies: Optional[bool] = False
) -> Union[
Dict, Tuple[Dict, List[Union[Dict, Account, ItemBaseType, Currency]]]
Dict,
Tuple[
Dict,
List[
Union[
Dict,
Account,
Stash,
ItemBaseType,
Currency,
]
],
],
]:
gameItemId = random_lower_string()
changeId = random_lower_string()
@@ -42,10 +55,12 @@ async def create_random_item_dict(
elder = random_bool()
shaper = random_bool()
influences_type_dict = {
"shaper": "bool",
"elder": "bool",
"shaper": "bool",
"crusader": "bool",
"redeemer": "bool",
"hunter": "bool",
"warlord": "bool",
}
influences = random_json(influences_type_dict)
searing = random_bool()
@@ -55,14 +70,23 @@ async def create_random_item_dict(
suffixes = random_int(small_int=True)
foilVariation = random_int(small_int=True)

if not retrieve_dependencies:
stash_dict, stash = await generate_random_stash(db)
else:
stash_dict, stash, deps = await generate_random_stash(
db, retrieve_dependencies=retrieve_dependencies
)

stashId = stash.stashId
item_base_type_dict, item_base_type = await generate_random_item_base_type(db)
baseType = item_base_type.baseType
currency_dict, currency = await generate_random_currency(db)
currencyId = currency.currencyId

item = {
"stashId": stashId,
"gameItemId": gameItemId,
"changeId": changeId,
"changeId": changeId,
"name": name,
"iconUrl": iconUrl,
"league": league,
@@ -93,6 +117,7 @@ async def create_random_item_dict(
if not retrieve_dependencies:
return item
else:
deps += [stash_dict, stash]
deps += [item_base_type_dict, item_base_type]
deps += [currency_dict, currency]
return item, deps
@@ -101,7 +126,19 @@ async def create_random_item_dict(
async def generate_random_item(
db: Session, retrieve_dependencies: Optional[bool] = False
) -> Tuple[
Dict, Item, Optional[List[Union[Dict, Account, ItemBaseType, Currency]]]
Dict,
Item,
Optional[
List[
Union[
Dict,
Account,
Stash,
ItemBaseType,
Currency,
]
]
],
]:
output = await create_random_item_dict(db, retrieve_dependencies)
if not retrieve_dependencies:
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
from app import crud
from app.core.models.models import (
ItemModifier,
Stash,
Account,
ItemBaseType,
Currency,
@@ -25,7 +26,7 @@ async def create_random_item_modifier_dict(
Tuple[
Dict,
Optional[
List[Union[Dict, Account, ItemBaseType, Currency, Item, Modifier]]
List[Union[Dict, Stash, Account, ItemBaseType, Currency, Item, Modifier]]
],
],
]:
@@ -60,7 +61,7 @@ async def generate_random_item_modifier(
) -> Tuple[
Dict,
ItemModifier,
Optional[List[Union[Dict, Account, ItemBaseType, Currency, Item, Modifier]]],
Optional[List[Union[Dict, Stash, Account, ItemBaseType, Currency, Item, Modifier]]],
]:
output = await create_random_item_modifier_dict(db, retrieve_dependencies)
if not retrieve_dependencies:
2 changes: 2 additions & 0 deletions src/backend_api/app/app/tests/utils/model_utils/modifier.py
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ def create_random_modifier_dict() -> Dict:
delve = random_bool()
fractured = random_bool()
synthesized = random_bool()
unique = random_bool()
corrupted = random_bool()
enchanted = random_bool()
veiled = random_bool()
@@ -60,6 +61,7 @@ def create_random_modifier_dict() -> Dict:
"delve": delve,
"fractured": fractured,
"synthesized": synthesized,
"unique": unique,
"corrupted": corrupted,
"enchanted": enchanted,
"veiled": veiled,
52 changes: 52 additions & 0 deletions src/backend_api/app/app/tests/utils/model_utils/stash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import asyncio
from typing import Dict, List, Optional, Tuple, Union
from sqlalchemy.orm import Session

from app import crud
from app.tests.conftest import db
from app.core.models.models import Account, Stash
from app.core.schemas.stash import StashCreate
from app.tests.utils.utils import random_lower_string, random_bool
from app.tests.utils.model_utils.account import generate_random_account


async def create_random_stash_dict(
db: Session, retrieve_dependencies: Optional[bool] = False
) -> Union[Dict, Tuple[Dict, List[Union[Dict, Account]]]]:
stashId = random_lower_string()
public: bool = random_bool()
league = random_lower_string()

account_dict, account = await generate_random_account(db)
accountName = account.accountName

stash = {
"stashId": stashId,
"accountName": accountName,
"public": public,
"league": league,
}

if not retrieve_dependencies:
return stash
else:
deps = []
deps += [account_dict, account]
return stash, deps


async def generate_random_stash(
db: Session, retrieve_dependencies: Optional[bool] = False
) -> Tuple[Dict, Stash, Optional[List[Union[Dict, Account]]]]:
output = await create_random_stash_dict(db, retrieve_dependencies)
if not retrieve_dependencies:
stash_dict = output
else:
stash_dict, deps = output
stash_create = StashCreate(**stash_dict)
stash = await crud.CRUD_stash.create(db, obj_in=stash_create)

if not retrieve_dependencies:
return stash_dict, stash
else:
return stash_dict, stash, deps

0 comments on commit 094d77d

Please sign in to comment.