1
+ #!/usr/bin/env python3
2
+ """
3
+ Test script to create sample data and test the assignee functionality.
4
+ This will help verify that the assignee details are working correctly.
5
+ """
6
+
7
+ import os
8
+ import sys
9
+ import django
10
+ from bson import ObjectId
11
+ from datetime import datetime , timezone
12
+
13
+ # Add the project root to the Python path
14
+ sys .path .append (os .path .dirname (os .path .abspath (__file__ )))
15
+
16
+ # Setup Django
17
+ os .environ .setdefault ('DJANGO_SETTINGS_MODULE' , 'todo_project.settings.development' )
18
+ django .setup ()
19
+
20
+ from todo .models .user import UserModel
21
+ from todo .models .team import TeamModel
22
+ from todo .models .task import TaskModel
23
+ from todo .models .task_assignment import TaskAssignmentModel
24
+ from todo .models .watchlist import WatchlistModel
25
+ from todo .repositories .user_repository import UserRepository
26
+ from todo .repositories .team_repository import TeamRepository
27
+ from todo .repositories .task_repository import TaskRepository
28
+ from todo .repositories .task_assignment_repository import TaskAssignmentRepository
29
+ from todo .repositories .watchlist_repository import WatchlistRepository
30
+
31
+
32
+ def create_sample_data ():
33
+ """Create sample data for testing assignee functionality"""
34
+
35
+ print ("=== Creating Sample Data ===\n " )
36
+
37
+ # 1. Create a sample user
38
+ print ("1. Creating sample user..." )
39
+ user_data = {
40
+ "google_id" : "test_google_id_123" ,
41
+
42
+ "name" : "Test User" ,
43
+ "picture" : "https://example.com/picture.jpg"
44
+ }
45
+ user = UserRepository .create_or_update (user_data )
46
+ print (f" Created user: { user .name } ({ user .email_id } )" )
47
+
48
+ # 2. Create a sample team
49
+ print ("\n 2. Creating sample team..." )
50
+ team = TeamModel (
51
+ name = "Test Team" ,
52
+ description = "A test team for assignee testing" ,
53
+ invite_code = "TEST123" ,
54
+ created_by = user .id ,
55
+ updated_by = user .id
56
+ )
57
+ team = TeamRepository .create (team )
58
+ print (f" Created team: { team .name } " )
59
+
60
+ # 3. Create a sample task
61
+ print ("\n 3. Creating sample task..." )
62
+ task = TaskModel (
63
+ title = "Test Task with Assignee" ,
64
+ description = "This is a test task to verify assignee functionality" ,
65
+ priority = "HIGH" ,
66
+ status = "TODO" ,
67
+ created_by = user .id
68
+ )
69
+ task = TaskRepository .create (task )
70
+ print (f" Created task: { task .title } " )
71
+
72
+ # 4. Create a task assignment (assign task to user)
73
+ print ("\n 4. Creating task assignment (user assignee)..." )
74
+ assignment = TaskAssignmentModel (
75
+ task_id = task .id ,
76
+ assignee_id = user .id ,
77
+ user_type = "user" ,
78
+ created_by = user .id
79
+ )
80
+ assignment = TaskAssignmentRepository .create (assignment )
81
+ print (f" Assigned task to user: { user .name } " )
82
+
83
+ # 5. Create another task assigned to team
84
+ print ("\n 5. Creating task assigned to team..." )
85
+ team_task = TaskModel (
86
+ title = "Team Task" ,
87
+ description = "This task is assigned to a team" ,
88
+ priority = "MEDIUM" ,
89
+ status = "IN_PROGRESS" ,
90
+ created_by = user .id
91
+ )
92
+ team_task = TaskRepository .create (team_task )
93
+ print (f" Created team task: { team_task .title } " )
94
+
95
+ team_assignment = TaskAssignmentModel (
96
+ task_id = team_task .id ,
97
+ assignee_id = team .id ,
98
+ user_type = "team" ,
99
+ created_by = user .id
100
+ )
101
+ team_assignment = TaskAssignmentRepository .create (team_assignment )
102
+ print (f" Assigned task to team: { team .name } " )
103
+
104
+ # 6. Create an unassigned task
105
+ print ("\n 6. Creating unassigned task..." )
106
+ unassigned_task = TaskModel (
107
+ title = "Unassigned Task" ,
108
+ description = "This task has no assignee" ,
109
+ priority = "LOW" ,
110
+ status = "TODO" ,
111
+ created_by = user .id
112
+ )
113
+ unassigned_task = TaskRepository .create (unassigned_task )
114
+ print (f" Created unassigned task: { unassigned_task .title } " )
115
+
116
+ # 7. Add tasks to watchlist
117
+ print ("\n 7. Adding tasks to watchlist..." )
118
+
119
+ # Add user-assigned task to watchlist
120
+ user_watchlist = WatchlistModel (
121
+ taskId = str (task .id ),
122
+ userId = str (user .id ),
123
+ createdBy = str (user .id )
124
+ )
125
+ user_watchlist = WatchlistRepository .create (user_watchlist )
126
+ print (f" Added user task to watchlist" )
127
+
128
+ # Add team-assigned task to watchlist
129
+ team_watchlist = WatchlistModel (
130
+ taskId = str (team_task .id ),
131
+ userId = str (user .id ),
132
+ createdBy = str (user .id )
133
+ )
134
+ team_watchlist = WatchlistRepository .create (team_watchlist )
135
+ print (f" Added team task to watchlist" )
136
+
137
+ # Add unassigned task to watchlist
138
+ unassigned_watchlist = WatchlistModel (
139
+ taskId = str (unassigned_task .id ),
140
+ userId = str (user .id ),
141
+ createdBy = str (user .id )
142
+ )
143
+ unassigned_watchlist = WatchlistRepository .create (unassigned_watchlist )
144
+ print (f" Added unassigned task to watchlist" )
145
+
146
+ return user .id , task .id , team_task .id , unassigned_task .id
147
+
148
+
149
+ def test_assignee_functionality (user_id , task_id , team_task_id , unassigned_task_id ):
150
+ """Test the assignee functionality with the created data"""
151
+
152
+ print ("\n === Testing Assignee Functionality ===\n " )
153
+
154
+ # Test the watchlist endpoint
155
+ print ("1. Testing watchlist with assignee details..." )
156
+ try :
157
+ count , tasks = WatchlistRepository .get_watchlisted_tasks (1 , 10 , str (user_id ))
158
+ print (f" Found { count } watchlisted tasks" )
159
+
160
+ for i , task in enumerate (tasks , 1 ):
161
+ print (f"\n Task { i } :" )
162
+ print (f" Title: { task .title } " )
163
+ print (f" Task ID: { task .taskId } " )
164
+ print (f" Assignee: { task .assignee } " )
165
+
166
+ if task .assignee :
167
+ print (f" Assignee Type: { task .assignee .type } " )
168
+ print (f" Assignee Name: { task .assignee .name } " )
169
+ print (f" Assignee Email: { task .assignee .email } " )
170
+ else :
171
+ print (f" Assignee: None (unassigned task)" )
172
+
173
+ except Exception as e :
174
+ print (f" Error testing watchlist: { e } " )
175
+
176
+ # Test the fallback method
177
+ print ("\n 2. Testing fallback method..." )
178
+ try :
179
+ user_assignee = WatchlistRepository ._get_assignee_for_task (str (task_id ))
180
+ print (f" User task assignee: { user_assignee } " )
181
+
182
+ team_assignee = WatchlistRepository ._get_assignee_for_task (str (team_task_id ))
183
+ print (f" Team task assignee: { team_assignee } " )
184
+
185
+ unassigned_assignee = WatchlistRepository ._get_assignee_for_task (str (unassigned_task_id ))
186
+ print (f" Unassigned task assignee: { unassigned_assignee } " )
187
+
188
+ except Exception as e :
189
+ print (f" Error testing fallback: { e } " )
190
+
191
+
192
+ def cleanup_sample_data ():
193
+ """Clean up the sample data"""
194
+ print ("\n === Cleaning Up Sample Data ===\n " )
195
+
196
+ try :
197
+ # Clean up watchlist
198
+ watchlist_collection = WatchlistRepository .get_collection ()
199
+ watchlist_collection .delete_many ({"userId" : {"$regex" : "test" }})
200
+ print (" Cleaned up watchlist entries" )
201
+
202
+ # Clean up task assignments
203
+ task_details_collection = TaskAssignmentRepository .get_collection ()
204
+ task_details_collection .delete_many ({"created_by" : {"$regex" : "test" }})
205
+ print (" Cleaned up task assignments" )
206
+
207
+ # Clean up tasks
208
+ task_collection = TaskRepository .get_collection ()
209
+ task_collection .delete_many ({"title" : {"$regex" : "Test" }})
210
+ print (" Cleaned up tasks" )
211
+
212
+ # Clean up teams
213
+ team_collection = TeamRepository .get_collection ()
214
+ team_collection .delete_many ({"name" : "Test Team" })
215
+ print (" Cleaned up teams" )
216
+
217
+ # Clean up users
218
+ user_collection = UserRepository ._get_collection ()
219
+ user_collection .
delete_many ({
"email_id" :
"[email protected] " })
220
+ print (" Cleaned up users" )
221
+
222
+ except Exception as e :
223
+ print (f" Error during cleanup: { e } " )
224
+
225
+
226
+ if __name__ == "__main__" :
227
+ try :
228
+ # Create sample data
229
+ user_id , task_id , team_task_id , unassigned_task_id = create_sample_data ()
230
+
231
+ # Test the functionality
232
+ test_assignee_functionality (user_id , task_id , team_task_id , unassigned_task_id )
233
+
234
+ # Ask if user wants to clean up
235
+ response = input ("\n Do you want to clean up the sample data? (y/n): " )
236
+ if response .lower () == 'y' :
237
+ cleanup_sample_data ()
238
+ print (" Cleanup completed!" )
239
+ else :
240
+ print (" Sample data left in database for further testing" )
241
+
242
+ except Exception as e :
243
+ print (f"Error: { e } " )
244
+ import traceback
245
+ traceback .print_exc ()
0 commit comments