Skip to content

Commit 22c94cf

Browse files
author
Robert Segal
committed
Updated account seeding and tests
1 parent d927ac4 commit 22c94cf

File tree

26 files changed

+372
-684
lines changed

26 files changed

+372
-684
lines changed

e2e_config.test.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"accounts.account.id": "ACC-9042-0088",
33
"accounts.api_token.id": "TKN-8857-1729",
4-
"accounts.buyer.account.id": "ACC-1086-6867",
4+
"accounts.client_account.id": "ACC-1086-6867",
55
"accounts.buyer.id": "BUY-1591-2112",
6-
"accounts.licensee.account.id": "ACC-1086-6867",
76
"accounts.licensee.group.id": "UGR-2757-7226",
87
"accounts.licensee.id": "LCE-5758-4071-6507",
98
"accounts.module.id": "MOD-1756",

mpt_api_client/resources/accounts/account.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ class AccountsServiceConfig:
3939

4040

4141
class AccountsService(
42-
CreateFileMixin[Model],
43-
UpdateFileMixin[Model],
44-
ActivatableMixin[Model],
45-
EnablableMixin[Model],
46-
ValidateMixin[Model],
42+
CreateFileMixin[Account],
43+
UpdateFileMixin[Account],
44+
ActivatableMixin[Account],
45+
EnablableMixin[Account],
46+
ValidateMixin[Account],
4747
GetMixin[Account],
4848
CollectionMixin[Account],
4949
Service[Account],
@@ -59,11 +59,11 @@ def users(self, account_id: str) -> AccountsUsersService:
5959

6060

6161
class AsyncAccountsService(
62-
AsyncCreateFileMixin[Model],
63-
AsyncUpdateFileMixin[Model],
64-
AsyncActivatableMixin[Model],
65-
AsyncEnablableMixin[Model],
66-
AsyncValidateMixin[Model],
62+
AsyncCreateFileMixin[Account],
63+
AsyncUpdateFileMixin[Account],
64+
AsyncActivatableMixin[Account],
65+
AsyncEnablableMixin[Account],
66+
AsyncValidateMixin[Account],
6767
AsyncGetMixin[Account],
6868
AsyncCollectionMixin[Account],
6969
AsyncService[Account],

mpt_api_client/resources/accounts/buyers.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ class BuyersServiceConfig:
3838

3939

4040
class BuyersService(
41-
CreateFileMixin[Model],
42-
UpdateFileMixin[Model],
43-
ActivatableMixin[Model],
44-
EnablableMixin[Model],
45-
ValidateMixin[Model],
41+
CreateFileMixin[Buyer],
42+
UpdateFileMixin[Buyer],
43+
ActivatableMixin[Buyer],
44+
EnablableMixin[Buyer],
45+
ValidateMixin[Buyer],
4646
GetMixin[Buyer],
4747
DeleteMixin,
4848
CollectionMixin[Buyer],
@@ -71,11 +71,11 @@ def transfer(self, resource_id: str, resource_data: ResourceData | None = None)
7171

7272

7373
class AsyncBuyersService(
74-
AsyncCreateFileMixin[Model],
75-
AsyncUpdateFileMixin[Model],
76-
AsyncActivatableMixin[Model],
77-
AsyncEnablableMixin[Model],
78-
AsyncValidateMixin[Model],
74+
AsyncCreateFileMixin[Buyer],
75+
AsyncUpdateFileMixin[Buyer],
76+
AsyncActivatableMixin[Buyer],
77+
AsyncEnablableMixin[Buyer],
78+
AsyncValidateMixin[Buyer],
7979
AsyncGetMixin[Buyer],
8080
AsyncDeleteMixin,
8181
AsyncCollectionMixin[Buyer],

mpt_api_client/resources/accounts/licensees.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class LicenseesServiceConfig:
3333

3434

3535
class LicenseesService(
36-
CreateFileMixin[Model],
37-
UpdateFileMixin[Model],
38-
EnablableMixin[Model],
36+
CreateFileMixin[Licensee],
37+
UpdateFileMixin[Licensee],
38+
EnablableMixin[Licensee],
3939
GetMixin[Licensee],
4040
DeleteMixin,
4141
CollectionMixin[Licensee],
@@ -46,9 +46,9 @@ class LicenseesService(
4646

4747

4848
class AsyncLicenseesService(
49-
AsyncCreateFileMixin[Model],
50-
AsyncUpdateFileMixin[Model],
51-
AsyncEnablableMixin[Model],
49+
AsyncCreateFileMixin[Licensee],
50+
AsyncUpdateFileMixin[Licensee],
51+
AsyncEnablableMixin[Licensee],
5252
AsyncGetMixin[Licensee],
5353
AsyncDeleteMixin,
5454
AsyncCollectionMixin[Licensee],

seed/accounts/account.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import logging
2+
import os
3+
4+
from dependency_injector.wiring import Provide, inject
5+
6+
from mpt_api_client.resources.accounts.account import Account
7+
from seed.container import Container
8+
from seed.context import Context
9+
from seed.helper import init_resource
10+
11+
logger = logging.getLogger(__name__)
12+
13+
14+
@inject
15+
async def init_vendor_account_id(context: Context = Provide[Container.context]) -> Account: # noqa: RUF029
16+
"""Initialize account ID in context."""
17+
account_id = os.getenv("VENDOR_ACCOUNT_ID")
18+
if not account_id:
19+
raise ValueError("VENDOR_ACCOUNT_ID environment variable is required")
20+
context["accounts.account.id"] = account_id
21+
return Account({"id": account_id})
22+
23+
24+
@inject
25+
async def init_client_account_id(context: Context = Provide[Container.context]) -> Account: # noqa: RUF029
26+
"""Initialize client account ID in context."""
27+
account_id = os.getenv("CLIENT_ACCOUNT_ID")
28+
if not account_id:
29+
raise ValueError("CLIENT_ACCOUNT_ID environment variable is required")
30+
context["accounts.client_account.id"] = account_id
31+
return Account({"id": account_id})
32+
33+
34+
async def seed_account_ids() -> None:
35+
"""Seed account."""
36+
logger.debug("Seeding vendor account ...")
37+
await init_resource("accounts.account.id", init_vendor_account_id)
38+
await init_resource("accounts.client_account.id", init_client_account_id)
39+
logger.debug("Seeding account completed.")

seed/accounts/accounts.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
from seed.accounts.account import seed_account_ids
34
from seed.accounts.api_tokens import seed_api_token
45
from seed.accounts.buyer import seed_buyer
56
from seed.accounts.licensee import seed_licensee
@@ -10,9 +11,12 @@
1011
logger = logging.getLogger(__name__)
1112

1213

13-
async def seed_accounts() -> None: # noqa: WPS217
14+
async def seed_accounts() -> None: # noqa: WPS217, WPS213
1415
"""Seed accounts data including account."""
1516
logger.debug("Seeding accounts ...")
17+
18+
await seed_account_ids()
19+
1620
await seed_seller()
1721
await seed_buyer()
1822
await seed_module()

seed/accounts/api_tokens.py

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,23 @@
11
import logging
2-
import os
32

43
from dependency_injector.wiring import Provide, inject
54

65
from mpt_api_client import AsyncMPTClient
76
from mpt_api_client.resources.accounts.api_tokens import ApiToken
87
from seed.container import Container
98
from seed.context import Context
9+
from seed.helper import init_resource, require_context_id
1010

1111
logger = logging.getLogger(__name__)
1212

1313

14-
@inject
15-
async def get_api_token(
16-
context: Context = Provide[Container.context],
17-
mpt_ops: AsyncMPTClient = Provide[Container.mpt_operations],
18-
) -> ApiToken | None:
19-
"""Get API token from context or fetch from API."""
20-
api_token_id = context.get_string("accounts.api_token.id")
21-
if not api_token_id:
22-
return None
23-
try:
24-
api_token = context.get_resource("accounts.api_token", api_token_id)
25-
except ValueError:
26-
api_token = None
27-
if not isinstance(api_token, ApiToken):
28-
api_token = await mpt_ops.accounts.api_tokens.get(api_token_id)
29-
context.set_resource("accounts.api_token", api_token)
30-
context["accounts.api_token.id"] = api_token.id
31-
return api_token
32-
return api_token
33-
34-
3514
@inject
3615
def build_api_token_data(
3716
context: Context = Provide[Container.context],
3817
) -> dict[str, object]:
3918
"""Get API token data dictionary for creation."""
40-
account_id = os.getenv("CLIENT_ACCOUNT_ID")
41-
module_id = context.get_string("accounts.module.id")
19+
account_id = require_context_id(context, "accounts.client_account.id", "creating API token")
20+
module_id = require_context_id(context, "accounts.module.id", "creating API token")
4221
return {
4322
"account": {"id": account_id},
4423
"name": "E2E Seeded API Token",
@@ -49,27 +28,18 @@ def build_api_token_data(
4928

5029

5130
@inject
52-
async def init_api_token(
31+
async def create_api_token(
5332
context: Context = Provide[Container.context],
5433
mpt_ops: AsyncMPTClient = Provide[Container.mpt_operations],
5534
) -> ApiToken:
56-
"""Get or create API token."""
57-
api_token = await get_api_token(context=context, mpt_ops=mpt_ops)
58-
if api_token is None:
59-
logger.debug("Creating API token ...")
60-
api_token_data = build_api_token_data(context=context)
61-
api_token = await mpt_ops.accounts.api_tokens.create(api_token_data)
62-
context.set_resource("accounts.api_token", api_token)
63-
context["accounts.api_token.id"] = api_token.id
64-
logger.info("API token created: %s", api_token.id)
65-
else:
66-
logger.info("API token found: %s", api_token.id)
67-
return api_token
35+
"""Creates an API token."""
36+
logger.debug("Creating API token ...")
37+
api_token_data = build_api_token_data(context=context)
38+
return await mpt_ops.accounts.api_tokens.create(api_token_data)
6839

6940

70-
@inject
7141
async def seed_api_token() -> None:
7242
"""Seed API token."""
7343
logger.debug("Seeding API token ...")
74-
await init_api_token()
44+
await init_resource("accounts.api_token.id", create_api_token)
7545
logger.debug("Seeding API token completed.")

seed/accounts/buyer.py

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,26 @@
11
import logging
2-
import os
32

43
from dependency_injector.wiring import Provide, inject
54

65
from mpt_api_client import AsyncMPTClient
76
from mpt_api_client.resources.accounts.buyers import Buyer
87
from seed.container import Container
98
from seed.context import Context
9+
from seed.helper import init_resource, require_context_id
1010
from seed.static.static import ICON
1111

1212
logger = logging.getLogger(__name__)
1313

1414

15-
@inject
16-
async def get_buyer(
17-
context: Context = Provide[Container.context],
18-
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
19-
) -> Buyer | None:
20-
"""Get buyer from context or fetch from API."""
21-
buyer_id = context.get_string("accounts.buyer.id")
22-
if not buyer_id:
23-
return None
24-
try:
25-
buyer = context.get_resource("accounts.buyer", buyer_id)
26-
except ValueError:
27-
buyer = None
28-
if not isinstance(buyer, Buyer):
29-
buyer = await mpt_operations.accounts.buyers.get(buyer_id)
30-
context.set_resource("accounts.buyer", buyer)
31-
context["accounts.buyer.id"] = buyer.id
32-
return buyer
33-
return buyer
34-
35-
3615
@inject
3716
def build_buyer_data(context: Context = Provide[Container.context]) -> dict[str, object]:
3817
"""Build buyer data dictionary for creation."""
39-
buyer_account_id = os.getenv("CLIENT_ACCOUNT_ID")
40-
if not buyer_account_id:
41-
raise ValueError("CLIENT_ACCOUNT_ID environment variable is required")
42-
seller_id = context.get_string("accounts.seller.id")
43-
if not seller_id:
44-
raise ValueError("accounts.seller.id missing from context; seed seller before buyer.")
18+
client_account_id = require_context_id(context, "accounts.client_account.id", "creating buyer")
19+
seller_id = require_context_id(context, "accounts.seller.id", "creating buyer")
20+
4521
return {
4622
"name": "E2E Seeded Buyer",
47-
"account": {"id": buyer_account_id},
23+
"account": {"id": client_account_id},
4824
"sellers": [{"id": seller_id}],
4925
"contact": {
5026
"firstName": "first",
@@ -62,31 +38,19 @@ def build_buyer_data(context: Context = Provide[Container.context]) -> dict[str,
6238

6339

6440
@inject
65-
async def init_buyer(
41+
async def create_buyer(
6642
context: Context = Provide[Container.context],
6743
mpt_operations: AsyncMPTClient = Provide[Container.mpt_operations],
6844
) -> Buyer:
69-
"""Get or create buyer."""
70-
buyer = await get_buyer(context=context, mpt_operations=mpt_operations)
71-
if buyer is None:
72-
buyer_data = build_buyer_data(context=context)
73-
logger.debug("Creating buyer ...")
74-
with ICON.open("rb") as icon_fd:
75-
created = await mpt_operations.accounts.buyers.create(buyer_data, file=icon_fd)
76-
if isinstance(created, Buyer):
77-
context.set_resource("accounts.buyer", created)
78-
context["accounts.buyer.id"] = created.id
79-
logger.info("Buyer created: %s", created.id)
80-
return created
81-
logger.warning("Buyer creation failed")
82-
raise ValueError("Buyer creation failed")
83-
logger.info("Buyer found: %s", buyer.id)
84-
return buyer
45+
"""Creates a buyer."""
46+
buyer_data = build_buyer_data(context=context)
47+
logger.debug("Creating buyer ...")
48+
with ICON.open("rb") as icon_fd:
49+
return await mpt_operations.accounts.buyers.create(buyer_data, file=icon_fd)
8550

8651

87-
@inject
8852
async def seed_buyer() -> None:
8953
"""Seed buyer."""
9054
logger.debug("Seeding buyer ...")
91-
await init_buyer()
55+
await init_resource("accounts.buyer.id", create_buyer)
9256
logger.debug("Seeding buyer completed.")

0 commit comments

Comments
 (0)