Skip to content

Commit 9211af4

Browse files
committed
Implement docstrings for tests
1 parent 26f28c3 commit 9211af4

File tree

4 files changed

+184
-1
lines changed

4 files changed

+184
-1
lines changed

tests/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88

99

1010
class User(schemas.BaseUser):
11+
"""A base class representing an `User`."""
1112
first_name: str | None
1213

1314

1415
class UserCreate(schemas.BaseUserCreate):
16+
"""A base class representing the creation of an `User`."""
1517
first_name: str | None
1618

1719

1820
class UserUpdate(schemas.BaseUserUpdate):
21+
"""A base class representing the update of an `User`."""
1922
pass
2023

2124

2225
class UserOAuth(User, schemas.BaseOAuthAccountMixin):
26+
"""A base class representing an `User` with linked `OAuth` accounts."""
2327
pass
2428

2529

@@ -37,6 +41,11 @@ def global_moto_mock():
3741

3842
@pytest.fixture
3943
def oauth_account1() -> dict[str, Any]:
44+
"""Return a fake `OAuth` account for testing.
45+
46+
Returns:
47+
dict[str, Any]: A `dict` object representing the `OAuth` account.
48+
"""
4049
return {
4150
"oauth_name": "service1",
4251
"access_token": "TOKEN",
@@ -48,6 +57,11 @@ def oauth_account1() -> dict[str, Any]:
4857

4958
@pytest.fixture
5059
def oauth_account2() -> dict[str, Any]:
60+
"""Return a fake `OAuth` account for testing.
61+
62+
Returns:
63+
dict[str, Any]: A `dict` object representing the `OAuth` account.
64+
"""
5165
return {
5266
"oauth_name": "service2",
5367
"access_token": "TOKEN",
@@ -59,4 +73,9 @@ def oauth_account2() -> dict[str, Any]:
5973

6074
@pytest.fixture
6175
def user_id() -> UUID4:
76+
"""Return a randomly generated UUIDv4.
77+
78+
Returns:
79+
UUID4: The random UUIDv4 user id.
80+
"""
6281
return uuid.uuid4()

tests/test_access_token.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,55 @@
1919

2020

2121
class Base(Model):
22+
"""A base class representing a PynamoDB `Model`.
23+
24+
Args:
25+
Model (_type_): The PynamoDB base class definition.
26+
"""
2227
pass
2328

2429

2530
class AccessToken(DynamoDBBaseAccessTokenTableUUID, Base):
31+
"""A class representing an `AccessToken` object.
32+
33+
Args:
34+
DynamoDBBaseAccessTokenTableUUID (_type_): The underlying table object.
35+
Base (_type_): The PynamoDB base class definition.
36+
"""
2637
__tablename__: str = config.get("DATABASE_TOKENTABLE_NAME") + "_test"
2738

2839
class Meta:
40+
"""The required `Meta` definitions for PynamoDB.
41+
42+
Args:
43+
table_name (str): The name of the table.
44+
region (str): The AWS region string where the table should be created.
45+
billing_mode (str): The billing mode to use when creating the table. \
46+
Currently only supports `PAY_PER_REQUEST`.
47+
"""
2948
table_name: str = config.get("DATABASE_TOKENTABLE_NAME") + "_test"
3049
region: str = config.get("DATABASE_REGION")
3150
billing_mode: str = config.get("DATABASE_BILLING_MODE").value
3251

3352

3453
class User(DynamoDBBaseUserTableUUID, Base):
54+
"""A class representing a `User` obejct.
55+
56+
Args:
57+
DynamoDBBaseUserTableUUID (_type_): The underlying table object.
58+
Base (_type_): The PynamoDB base class definition.
59+
"""
3560
__tablename__: str = config.get("DATABASE_USERTABLE_NAME") + "_test"
3661

3762
class Meta:
63+
"""The required `Meta` definitions for PynamoDB.
64+
65+
Args:
66+
table_name (str): The name of the table.
67+
region (str): The AWS region string where the table should be created.
68+
billing_mode (str): The billing mode to use when creating the table. \
69+
Currently only supports `PAY_PER_REQUEST`.
70+
"""
3871
table_name: str = config.get("DATABASE_USERTABLE_NAME") + "_test"
3972
region: str = config.get("DATABASE_REGION")
4073
billing_mode: str = config.get("DATABASE_BILLING_MODE").value
@@ -44,6 +77,17 @@ class Meta:
4477
async def dynamodb_access_token_db(
4578
user_id: UUID4,
4679
) -> AsyncGenerator[DynamoDBAccessTokenDatabase[AccessToken]]:
80+
"""Create a new `AccessToken` Database and yield it to other tests.
81+
82+
Args:
83+
user_id (UUID4): The user id for the default user.
84+
85+
Returns:
86+
AsyncGenerator[DynamoDBAccessTokenDatabase[AccessToken]]: The `AccessToken` Database instance.
87+
88+
Yields:
89+
Iterator[AsyncGenerator[DynamoDBAccessTokenDatabase[AccessToken]]]: The `AccessToken` Database instance.
90+
"""
4791
user_db = DynamoDBUserDatabase(User)
4892
user = await user_db.create(
4993
User(
@@ -65,6 +109,12 @@ async def test_queries(
65109
dynamodb_access_token_db: DynamoDBAccessTokenDatabase[AccessToken],
66110
user_id: UUID4,
67111
):
112+
"""Test default queries to the `AccessToken` Database.
113+
114+
Args:
115+
dynamodb_access_token_db (DynamoDBAccessTokenDatabase[AccessToken]): The database instance to use.
116+
user_id (UUID4): The default user id to use.
117+
"""
68118
access_token_create = {"token": "TOKEN", "user_id": user_id}
69119

70120
# Create
@@ -130,6 +180,12 @@ async def test_insert_existing_token(
130180
dynamodb_access_token_db: DynamoDBAccessTokenDatabase[AccessToken],
131181
user_id: UUID4,
132182
):
183+
"""Test function that creates and saves an already existing `AccessToken`.
184+
185+
Args:
186+
dynamodb_access_token_db (DynamoDBAccessTokenDatabase[AccessToken]): The database instance to use.
187+
user_id (UUID4): The default user id to use.
188+
"""
133189
access_token_create = {"token": "TOKEN", "user_id": user_id}
134190

135191
token = await dynamodb_access_token_db.get_by_token(access_token_create["token"])

tests/test_misc.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,38 @@
77

88

99
class NotAModel:
10+
"""A class representing an invalid `Model`."""
1011
pass
1112

1213

1314
class IncompleteModel(Model):
15+
"""A class representing an incomplete `Model`, which misses required functions."""
1416
pass
1517

1618

1719
class ValidModel(Model):
20+
"""A class representing a valid `Model`."""
1821
class Meta:
22+
"""The required `Meta` definitions for PynamoDB.
23+
24+
Args:
25+
table_name (str): The name of the table.
26+
region (str): The AWS region string where the table should be created.
27+
billing_mode (str): The billing mode to use when creating the table. \
28+
Currently only supports `PAY_PER_REQUEST`.
29+
"""
1930
table_name: str = "valid_model_test"
2031
region: str = config.get("DATABASE_REGION")
2132
billing_mode: str = config.get("DATABASE_BILLING_MODE").value
2233

2334

2435
@pytest.mark.asyncio
2536
async def test_tables_invalid_models(monkeypatch):
37+
"""Test table creation on various `Model` instances.
38+
39+
Args:
40+
monkeypatch (_type_): The `pytest` monkeypatcher which is used to modify models.
41+
"""
2642
with pytest.raises(TypeError, match="must be a subclass of Model"):
2743
await ensure_tables_exist(NotAModel) # type: ignore
2844

@@ -43,6 +59,11 @@ async def test_tables_invalid_models(monkeypatch):
4359

4460

4561
def test_config(monkeypatch):
62+
"""Test config settings and changes.
63+
64+
Args:
65+
monkeypatch (_type_): The `pytest` monkeypatcher which is used to modify models.
66+
"""
4667
billing_mode = config.BillingMode.PAY_PER_REQUEST
4768
assert billing_mode.value == str(billing_mode)
4869

@@ -62,6 +83,11 @@ def test_config(monkeypatch):
6283

6384

6485
def test_attributes(user_id):
86+
"""Test serialization and deserialization of `Attribute` instances.
87+
88+
Args:
89+
user_id (_type_): The default user id to use for testing.
90+
"""
6591
id = GUID()
6692
assert id.serialize(None) is None
6793

tests/test_users.py

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,32 @@
1717

1818

1919
class Base(Model):
20+
"""A base class representing a PynamoDB `Model`.
21+
22+
Args:
23+
Model (_type_): The PynamoDB base class definition.
24+
"""
2025
pass
2126

2227

2328
class User(DynamoDBBaseUserTableUUID, Base):
29+
"""A class representing an `User` object.
30+
31+
Args:
32+
DynamoDBBaseUserTableUUID (_type_): The underlying table object.
33+
Base (_type_): The PynamoDB base class definition.
34+
"""
2435
__tablename__: str = config.get("DATABASE_USERTABLE_NAME") + "_test"
2536

2637
class Meta:
38+
"""The required `Meta` definitions for PynamoDB.
39+
40+
Args:
41+
table_name (str): The name of the table.
42+
region (str): The AWS region string where the table should be created.
43+
billing_mode (str): The billing mode to use when creating the table. \
44+
Currently only supports `PAY_PER_REQUEST`.
45+
"""
2746
table_name: str = config.get("DATABASE_USERTABLE_NAME") + "_test"
2847
region: str = config.get("DATABASE_REGION")
2948
billing_mode: str = config.get("DATABASE_BILLING_MODE").value
@@ -35,17 +54,42 @@ class Meta:
3554

3655

3756
class OAuthBase(Model):
57+
"""The base class representing `OAuth` related models.
58+
59+
Args:
60+
Model (_type_): The PynamoDB base class definition.
61+
"""
3862
pass
3963

4064

4165
class OAuthAccount(DynamoDBBaseOAuthAccountTableUUID, OAuthBase):
66+
"""A class representing an `OAuthAccount` object.
67+
68+
Args:
69+
DynamoDBBaseOAuthAccountTableUUID (_type_): The underlying table object.
70+
OAuthBase (_type_): The base class for `OAuth`.
71+
"""
4272
pass
4373

4474

4575
class UserOAuth(DynamoDBBaseUserTableUUID, OAuthBase):
76+
"""A class representing an `UserOAuth` object.
77+
78+
Args:
79+
DynamoDBBaseUserTableUUID (_type_): The underlying table object.
80+
OAuthBase (_type_): The base class representing `OAuth` related models.
81+
"""
4682
__tablename__: str = config.get("DATABASE_OAUTHTABLE_NAME") + "_test"
4783

4884
class Meta:
85+
"""The required `Meta` definitions for PynamoDB.
86+
87+
Args:
88+
table_name (str): The name of the table.
89+
region (str): The AWS region string where the table should be created.
90+
billing_mode (str): The billing mode to use when creating the table. \
91+
Currently only supports `PAY_PER_REQUEST`.
92+
"""
4993
table_name: str = config.get("DATABASE_OAUTHTABLE_NAME") + "_test"
5094
region: str = config.get("DATABASE_REGION")
5195
billing_mode: str = config.get("DATABASE_BILLING_MODE").value
@@ -60,18 +104,39 @@ class Meta:
60104

61105
@pytest_asyncio.fixture
62106
async def dynamodb_user_db() -> AsyncGenerator[DynamoDBUserDatabase, None]:
107+
"""Create and yield a new `User` database instance for other tests to use.
108+
109+
Returns:
110+
AsyncGenerator[DynamoDBUserDatabase, None]: The `User` database instance.
111+
112+
Yields:
113+
Iterator[AsyncGenerator[DynamoDBUserDatabase, None]]: The `User` database instance.
114+
"""
63115
db = DynamoDBUserDatabase(User)
64116
yield db
65117

66118

67119
@pytest_asyncio.fixture
68120
async def dynamodb_user_db_oauth() -> AsyncGenerator[DynamoDBUserDatabase, None]:
121+
"""Create and yield a new `OAuth` database instance for other tests to use.
122+
123+
Returns:
124+
AsyncGenerator[DynamoDBUserDatabase, None]: The `OAuth` database instance.
125+
126+
Yields:
127+
Iterator[AsyncGenerator[DynamoDBUserDatabase, None]]: The `OAuth` database instance.
128+
"""
69129
db = DynamoDBUserDatabase(UserOAuth, OAuthAccount)
70130
yield db
71131

72132

73133
@pytest.mark.asyncio
74134
async def test_queries(dynamodb_user_db: DynamoDBUserDatabase[User, UUID_ID]):
135+
"""Test basic **CRUD** operations on a `User`.
136+
137+
Args:
138+
dynamodb_user_db (DynamoDBUserDatabase[User, UUID_ID]): The `User` database instance to use.
139+
"""
75140
user_create = {"email": "lancelot@camelot.bt", "hashed_password": "guinevere"}
76141

77142
# Create user
@@ -134,6 +199,11 @@ async def test_queries(dynamodb_user_db: DynamoDBUserDatabase[User, UUID_ID]):
134199
async def test_insert_existing_email(
135200
dynamodb_user_db: DynamoDBUserDatabase[User, UUID_ID],
136201
):
202+
"""Test inserting an email, which already exists.
203+
204+
Args:
205+
dynamodb_user_db (DynamoDBUserDatabase[User, UUID_ID]): The `User` database instance to use.
206+
"""
137207
user_create = {
138208
"email": "lancelot@camelot.bt",
139209
"hashed_password": "guinevere",
@@ -157,7 +227,11 @@ async def test_insert_existing_email(
157227
async def test_queries_custom_fields(
158228
dynamodb_user_db: DynamoDBUserDatabase[User, UUID_ID],
159229
):
160-
"""It should output custom fields in query result."""
230+
"""Test basic **CRUD** operations (especially querying) on custom fields.
231+
232+
Args:
233+
dynamodb_user_db (DynamoDBUserDatabase[User, UUID_ID]): The `User` database instance to use.
234+
"""
161235
user_create = {
162236
"email": "lancelot@camelot.bt",
163237
"hashed_password": "guinevere",
@@ -178,6 +252,14 @@ async def test_queries_oauth(
178252
oauth_account2: dict[str, Any],
179253
user_id: UUID_ID,
180254
):
255+
"""Test `OAuth` implemenatation and basic **CRUD** operations.
256+
257+
Args:
258+
dynamodb_user_db_oauth (DynamoDBUserDatabase[UserOAuth, UUID_ID]): The `OAuth` database instance to use.
259+
oauth_account1 (dict[str, Any]): A fake `OAuth` account for testing.
260+
oauth_account2 (dict[str, Any]): Another fake `OAuth` account for testing.
261+
user_id (UUID_ID): The default user id to use.
262+
"""
181263
# Test OAuth accounts
182264
user_create = {"email": "lancelot@camelot.bt", "hashed_password": "guinevere"}
183265

0 commit comments

Comments
 (0)