1
+ import uuid
2
+
3
+ import boto3
1
4
import pytest
2
5
from fastapi import status
6
+ from moto import mock_dynamodb
3
7
from starlette .testclient import TestClient
4
8
5
9
from main import app
10
+ from models import Task , TaskStatus
11
+ from store import TaskStore
6
12
7
13
8
14
@pytest .fixture
@@ -19,3 +25,72 @@ def test_health_check(client):
19
25
response = client .get ("/api/health-check/" )
20
26
assert response .status_code == status .HTTP_200_OK
21
27
assert response .json () == {"message" : "OK" }
28
+
29
+
30
+ @pytest .fixture
31
+ def dynamodb_table ():
32
+ with mock_dynamodb ():
33
+ client = boto3 .client ("dynamodb" )
34
+ table_name = "test-table"
35
+ client .create_table (
36
+ AttributeDefinitions = [
37
+ {"AttributeName" : "PK" , "AttributeType" : "S" },
38
+ {"AttributeName" : "SK" , "AttributeType" : "S" },
39
+ {"AttributeName" : "GS1PK" , "AttributeType" : "S" },
40
+ {"AttributeName" : "GS1SK" , "AttributeType" : "S" },
41
+ ],
42
+ TableName = table_name ,
43
+ KeySchema = [
44
+ {"AttributeName" : "PK" , "KeyType" : "HASH" },
45
+ {"AttributeName" : "SK" , "KeyType" : "RANGE" },
46
+ ],
47
+ BillingMode = "PAY_PER_REQUEST" ,
48
+ GlobalSecondaryIndexes = [
49
+ {
50
+ "IndexName" : "GS1" ,
51
+ "KeySchema" : [
52
+ {"AttributeName" : "GS1PK" , "KeyType" : "HASH" },
53
+ {"AttributeName" : "GS1SK" , "KeyType" : "RANGE" },
54
+ ],
55
+ "Projection" : {
56
+ "ProjectionType" : "ALL" ,
57
+ },
58
+ },
59
+ ],
60
+ )
61
+ yield table_name
62
+
63
+
64
+ def test_added_task_retrieved_by_id (dynamodb_table ):
65
+ repository = TaskStore (table_name = dynamodb_table )
66
+ task = Task .
create (
uuid .
uuid4 (),
"Clean your office" ,
"[email protected] " )
67
+
68
+ repository .add (task )
69
+
70
+ assert repository .get_by_id (task_id = task .id , owner = task .owner ) == task
71
+
72
+
73
+ def test_open_tasks_listed (dynamodb_table ):
74
+ repository = TaskStore (table_name = dynamodb_table )
75
+ open_task = Task .
create (
uuid .
uuid4 (),
"Clean your office" ,
"[email protected] " )
76
+ closed_task = Task (
77
+ uuid .
uuid4 (),
"Clean your office" ,
TaskStatus .
CLOSED ,
"[email protected] "
78
+ )
79
+
80
+ repository .add (open_task )
81
+ repository .add (closed_task )
82
+
83
+ assert repository .list_open (owner = open_task .owner ) == [open_task ]
84
+
85
+
86
+ def test_closed_tasks_listed (dynamodb_table ):
87
+ repository = TaskStore (table_name = dynamodb_table )
88
+ open_task = Task .
create (
uuid .
uuid4 (),
"Clean your office" ,
"[email protected] " )
89
+ closed_task = Task (
90
+ uuid .
uuid4 (),
"Clean your office" ,
TaskStatus .
CLOSED ,
"[email protected] "
91
+ )
92
+
93
+ repository .add (open_task )
94
+ repository .add (closed_task )
95
+
96
+ assert repository .list_closed (owner = open_task .owner ) == [closed_task ]
0 commit comments