Skip to content

Commit 53eca41

Browse files
Fixed test errors on backend
Important changes to code: Relationships Modified Profile <---one to one -- profile picture ----> one to one ---> upload record Removed profile field from upload record as a result which affects existing tests and requests. I believe that I have fixed all of them but there might have been some that I might have missed Additionally, for the tests for uploads, I had to add jtw token for it to work.
1 parent fb26024 commit 53eca41

File tree

10 files changed

+51
-24
lines changed

10 files changed

+51
-24
lines changed

backend/apps/search/serializers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class Meta:
1616

1717
def get_image_url(self, result_data):
1818
# Ensure that the profile and profile_picture exist before accessing them
19-
if hasattr(result_data, "profile") and hasattr(result_data.profile, "upload_record"):
20-
return UploadRecord.objects.build_url(result_data.profile.upload_record)
19+
if hasattr(result_data, "profile") and hasattr(result_data.profile, "profile_picture") and hasattr(result_data.profile.profile_picture, "upload"):
20+
return UploadRecord.objects.build_url(result_data.profile.profile_picture.upload)
2121
return None # Return None if there's no profile picture
2222

2323
def get_subjects(self, result_data):

backend/apps/submissions/tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def setUp(self):
2929
"version": 1,
3030
"asset_id": "sample_asset_id"
3131
}
32-
self.upload_record = UploadRecord.objects.create(upload_data=self.upload_data, profile=self.profile)
32+
self.upload_record = UploadRecord.objects.create(upload_data=self.upload_data)
3333

3434
# Create submission
3535
self.submission = Submissions.objects.create(
@@ -426,7 +426,7 @@ def setUp(self):
426426
"version": 1,
427427
"asset_id": "sample_asset_id"
428428
}
429-
self.file_record = UploadRecord.objects.create(self.upload_data, self.profile)
429+
self.file_record = UploadRecord.objects.create(self.upload_data)
430430
self.quiz_submission = QuizSubmissions.objects.create(submission=self.submission)
431431

432432
self.login_url = "/users/login/"

backend/apps/uploads/managers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
from cloudinary import CloudinaryImage
66

77
class ProfilePictureManager(models.Manager):
8-
def create(self, upload):
8+
def create(self, profile, upload_record):
99
# Create a new UploadRecord instance with the provided data
1010
profile_picture = self.model(
11-
upload=upload
11+
profile=profile,
12+
upload=upload_record
1213
)
1314
# Save the instance to the database
1415
profile_picture.save()
@@ -26,7 +27,7 @@ def delete_upload(self, cloudinary_public_id):
2627
result = cloudinary.uploader.destroy(cloudinary_public_id, invalidate = True)
2728
return result
2829

29-
def create(self, upload_data, profile):
30+
def create(self, upload_data):
3031
# Replace 'Z' with '+00:00' for compatibility with fromisoformat
3132
compatible_created_at = upload_data['created_at'].replace('Z', '+00:00')
3233
# Convert to datetime object
@@ -42,7 +43,6 @@ def create(self, upload_data, profile):
4243
cloudinary_public_id=upload_data["public_id"],
4344
version=upload_data["version"],
4445
asset_id=upload_data["asset_id"],
45-
profile=profile
4646
)
4747
# Save the instance to the database
4848
upload_record.save()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generated by Django 5.1.6 on 2025-04-10 23:20
2+
3+
import django.db.models.deletion
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('uploads', '0008_alter_uploadrecord_profile'),
11+
('users', '0009_alter_tutorprofile_rating'),
12+
]
13+
14+
operations = [
15+
migrations.RemoveField(
16+
model_name='uploadrecord',
17+
name='profile',
18+
),
19+
migrations.AddField(
20+
model_name='profilepicture',
21+
name='profile',
22+
field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='profile_picture', to='users.profile'),
23+
),
24+
]

backend/apps/uploads/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ class UploadRecord(models.Model):
2121
version = models.PositiveBigIntegerField()
2222
asset_id = models.CharField(max_length=255)
2323

24-
# profile = models.OneToOneField(Profile, on_delete=models.CASCADE, related_name='upload_record')
25-
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='upload_record')
26-
2724
# default = 1 for first user (for now)
2825
description = models.TextField(default="", blank=True, null=False)
2926

@@ -35,6 +32,7 @@ def __str__(self):
3532

3633
# A model that acts as a container for the profile picture of user
3734
class ProfilePicture(models.Model):
35+
profile = models.OneToOneField(Profile, on_delete=models.CASCADE, related_name = "profile_picture", null = True) #changed
3836
upload = models.OneToOneField(UploadRecord, on_delete=models.CASCADE)
3937

4038
# Link the custom manager to the model

backend/apps/uploads/serializers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ def get_file_url(self, upload):
1515
return UploadRecord.objects.build_url(upload)
1616

1717
class UploadListSerializer(serializers.ModelSerializer):
18-
assignment_id = serializers.PrimaryKeyRelatedField(source='assignment.id', read_only=True)
1918

2019
class Meta:
2120
model = UploadRecord
22-
fields = ['file_name', 'file_format', 'cloudinary_public_id', 'assignment_id']
21+
fields = ['file_name', 'file_format', 'cloudinary_public_id']

backend/apps/uploads/tests.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from apps.users.models import Profile
66
from django.contrib.auth import get_user_model
77
from .serializers import UploadListSerializer
8+
from rest_framework_simplejwt.tokens import RefreshToken
89

910
class UploadRecordViewTest(APITestCase):
1011
def setUp(self):
@@ -17,8 +18,6 @@ def setUp(self):
1718
self.version = 1234567890
1819
self.asset_id = "test-asset-id"
1920

20-
self.url = reverse('upload-list')
21-
2221
self.fake_user = get_user_model().objects.create_user(
2322
username='testuser',
2423
password='password',
@@ -27,17 +26,25 @@ def setUp(self):
2726
email='testuser@example.com'
2827
)
2928
self.profile = Profile.objects.create(self.fake_user, 2)
29+
self.token = self.get_jwt_token(self.fake_user)
30+
self.client.credentials(HTTP_AUTHORIZATION=f'Bearer {self.token}')
31+
32+
def get_jwt_token(self, user):
33+
refresh = RefreshToken.for_user(user)
34+
return str(refresh.access_token)
3035

3136
# Ensure that if there are no uploads,
3237
# the API returns a 404 error with the proper message.
3338
def test_get_uploads_empty(self):
34-
response = self.client.get(self.url)
39+
url = reverse('upload-list')
40+
response = self.client.get(url)
3541
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
3642
self.assertEqual(response.data, {'error': 'No uploads found'})
3743

3844
# Test that the API returns a 200 status and
3945
# the correct serialized data when uploads exist.
4046
def test_get_uploads_success(self):
47+
url = reverse('upload-list')
4148
# Create an example UploadRecord instance.
4249
upload_record = UploadRecord.objects.create(
4350
upload_data={
@@ -50,11 +57,10 @@ def test_get_uploads_success(self):
5057
"version": self.version,
5158
"asset_id": self.asset_id,
5259
},
53-
profile=self.profile
5460
)
5561

5662
# Perform the GET request
57-
response = self.client.get(self.url)
63+
response = self.client.get(url)
5864
self.assertEqual(response.status_code, status.HTTP_200_OK)
5965

6066
# Serialize the created object using the serializer

backend/apps/uploads/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def post(self, request):
119119
upload_data = UploadRecord.objects.upload(file)
120120

121121
# Use the manager method to save relevant metadata into database
122-
UploadRecord.objects.create(upload_data, profile)
122+
UploadRecord.objects.create(upload_data)
123123

124124
return Response({'status': 'success'}, status=status.HTTP_200_OK)
125125
# Note: Tested this POST request by entering this into the command line

backend/apps/users/managers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ def get_result_data(self, search_results):
8787
'hourly_rate',
8888
'state',
8989
'city',
90-
'profile__upload_record'
90+
'profile__profile_picture__upload' #changed
9191
)
9292
return search_results

backend/apps/users/views.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ def register_profile(request):
8484
if image:
8585
# Use the manager method to handle file upload
8686
upload_data = UploadRecord.objects.upload(image)
87-
88-
# Use the manager method to save relevant metadata into database
89-
upload_record = UploadRecord.objects.create(upload_data, profile)
90-
ProfilePicture.objects.create(upload_record)
87+
# Use manager method to create and save metadata
88+
upload_record = UploadRecord.objects.create(upload_data)
89+
# Use the manager method to create and save profile picture
90+
ProfilePicture.objects.create(profile, upload_record)
9191

9292
# Create Tutor Profile if role is Tutor
9393
if int(profile.role) == Profile.TUTOR:

0 commit comments

Comments
 (0)