1+ import uuid
2+
3+ import boto3
14import pytest
25from fastapi import status
6+ from moto import mock_dynamodb
37from starlette .testclient import TestClient
48
59from main import app
10+ from models import Task , TaskStatus
11+ from store import TaskStore
612
713
814@pytest .fixture
@@ -19,3 +25,72 @@ def test_health_check(client):
1925 response = client .get ("/api/health-check/" )
2026 assert response .status_code == status .HTTP_200_OK
2127 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" , "john@doe.com" )
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" , "john@doe.com" )
76+ closed_task = Task (
77+ uuid .uuid4 (), "Clean your office" , TaskStatus .CLOSED , "john@doe.com"
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" , "john@doe.com" )
89+ closed_task = Task (
90+ uuid .uuid4 (), "Clean your office" , TaskStatus .CLOSED , "john@doe.com"
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