Skip to content

Commit a1416e9

Browse files
committed
Fix subject deletion when deleting user, add unit tests
1 parent f923b60 commit a1416e9

3 files changed

Lines changed: 127 additions & 4 deletions

File tree

Refresh.Database/GameDatabaseContext.Users.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public void DeleteUser(GameUser user)
401401
user.PsnAuthenticationAllowed = false;
402402
user.RpcnAuthenticationAllowed = false;
403403

404-
foreach (GamePhotoSubject subject in this.GamePhotoSubjects.Where(s => s.UserId == user.UserId))
404+
foreach (GamePhotoSubject subject in this.GamePhotoSubjects.Where(s => s.UserId == user.UserId).ToList())
405405
{
406406
subject.UserId = null;
407407
}
@@ -410,7 +410,6 @@ public void DeleteUser(GameUser user)
410410
this.FavouriteUserRelations.RemoveRange(r => r.UserToFavourite == user);
411411
this.FavouriteUserRelations.RemoveRange(r => r.UserFavouriting == user);
412412
this.QueueLevelRelations.RemoveRange(r => r.User == user);
413-
this.GamePhotoSubjects.RemoveRange(s => s.Photo.PublisherId == user.UserId);
414413
this.GamePhotos.RemoveRange(p => p.Publisher == user);
415414
this.GameUserVerifiedIpRelations.RemoveRange(p => p.User == user);
416415

RefreshTests.GameServer/TestContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public GameLevel CreateLevel(GameUser author, string title, string description,
145145
return level;
146146
}
147147

148-
public GamePhoto CreatePhotoWithSubject(GameUser author, string imageHash, GameLevel? level = null)
148+
public GamePhoto CreatePhotoWithSubject(GameUser author, string imageHash, GameLevel? level = null, List<SerializedPhotoSubject>? subjects = null)
149149
{
150150
// TODO: Return newly created GamePhoto
151151
if (this.Database.GetAssetFromHash(imageHash) == null)
@@ -164,7 +164,7 @@ public GamePhoto CreatePhotoWithSubject(GameUser author, string imageHash, GameL
164164
LargeHash = imageHash,
165165

166166
};
167-
IEnumerable<SerializedPhotoSubject> subjects =
167+
subjects ??=
168168
[
169169
new()
170170
{
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using Refresh.Database.Helpers;
2+
using Refresh.Database.Models.Levels;
3+
using Refresh.Database.Models.Photos;
4+
using Refresh.Database.Models.Users;
5+
6+
namespace RefreshTests.GameServer.Tests.Photos;
7+
8+
public class PhotoTests : GameServerTest
9+
{
10+
private const string TEST_IMAGE_HASH = "0ec63b140374ba704a58fa0c743cb357683313dd";
11+
12+
[Test]
13+
public void CreateAndGetPhotoWithSubjects()
14+
{
15+
using TestContext context = this.GetServer();
16+
GameUser publisher = context.CreateUser();
17+
GameUser player2 = context.CreateUser();
18+
GamePhoto createdPhoto = context.CreatePhotoWithSubject(publisher, TEST_IMAGE_HASH, subjects:
19+
[
20+
new()
21+
{
22+
Username = publisher.Username,
23+
DisplayName = publisher.Username,
24+
BoundsList = "1,2,3,4",
25+
},
26+
new()
27+
{
28+
Username = player2.Username,
29+
DisplayName = player2.Username,
30+
BoundsList = "4,5,6,7",
31+
}
32+
]);
33+
34+
List<GamePhotoSubject> separateSubjects = context.Database.GetSubjectsInPhoto(createdPhoto).ToList();
35+
GamePhoto? fetchedPhoto = context.Database.GetRecentPhotos(10, 0).Items.FirstOrDefault();
36+
Assert.That(fetchedPhoto, Is.Not.Null);
37+
Assert.That(fetchedPhoto!.PhotoId, Is.EqualTo(createdPhoto.PhotoId));
38+
39+
// these 3 subject lists are fetched/created in different ways, so ensure they all contain the correct data
40+
Assert.That(separateSubjects.Count, Is.EqualTo(2));
41+
Assert.That(createdPhoto.Subjects.Count, Is.EqualTo(2));
42+
Assert.That(fetchedPhoto.Subjects.Count, Is.EqualTo(2));
43+
44+
// Assert first subject on all lists
45+
Assert.That(separateSubjects[0].PlayerId, Is.EqualTo(1));
46+
Assert.That(createdPhoto.Subjects[0].PlayerId, Is.EqualTo(1));
47+
Assert.That(fetchedPhoto.Subjects[0].PlayerId, Is.EqualTo(1));
48+
49+
Assert.That(separateSubjects[0].DisplayName, Is.EqualTo(publisher.Username));
50+
Assert.That(createdPhoto.Subjects[0].DisplayName, Is.EqualTo(publisher.Username));
51+
Assert.That(fetchedPhoto.Subjects[0].DisplayName, Is.EqualTo(publisher.Username));
52+
53+
Assert.That(separateSubjects[0].User, Is.Not.Null);
54+
Assert.That(createdPhoto.Subjects[0].User, Is.Not.Null);
55+
Assert.That(fetchedPhoto.Subjects[0].User, Is.Not.Null);
56+
57+
Assert.That(separateSubjects[0].User!.UserId, Is.EqualTo(publisher.UserId));
58+
Assert.That(createdPhoto.Subjects[0].User!.UserId, Is.EqualTo(publisher.UserId));
59+
Assert.That(fetchedPhoto.Subjects[0].User!.UserId, Is.EqualTo(publisher.UserId));
60+
61+
float[] subject1Bounds = PhotoHelper.ParseBoundsList("1,2,3,4");
62+
Assert.That(separateSubjects[0].Bounds, Is.EqualTo(subject1Bounds));
63+
Assert.That(createdPhoto.Subjects[0].Bounds, Is.EqualTo(subject1Bounds));
64+
Assert.That(fetchedPhoto.Subjects[0].Bounds, Is.EqualTo(subject1Bounds));
65+
66+
// Assert second subject on all lists
67+
Assert.That(separateSubjects[1].PlayerId, Is.EqualTo(2));
68+
Assert.That(createdPhoto.Subjects[1].PlayerId, Is.EqualTo(2));
69+
Assert.That(fetchedPhoto.Subjects[1].PlayerId, Is.EqualTo(2));
70+
71+
Assert.That(separateSubjects[1].DisplayName, Is.EqualTo(player2.Username));
72+
Assert.That(createdPhoto.Subjects[1].DisplayName, Is.EqualTo(player2.Username));
73+
Assert.That(fetchedPhoto.Subjects[1].DisplayName, Is.EqualTo(player2.Username));
74+
75+
Assert.That(separateSubjects[1].User, Is.Not.Null);
76+
Assert.That(createdPhoto.Subjects[1].User, Is.Not.Null);
77+
Assert.That(fetchedPhoto.Subjects[1].User, Is.Not.Null);
78+
79+
Assert.That(separateSubjects[1].User!.UserId, Is.EqualTo(player2.UserId));
80+
Assert.That(createdPhoto.Subjects[1].User!.UserId, Is.EqualTo(player2.UserId));
81+
Assert.That(fetchedPhoto.Subjects[1].User!.UserId, Is.EqualTo(player2.UserId));
82+
83+
float[] subject2Bounds = PhotoHelper.ParseBoundsList("4,5,6,7");
84+
Assert.That(separateSubjects[1].Bounds, Is.EqualTo(subject2Bounds));
85+
Assert.That(createdPhoto.Subjects[1].Bounds, Is.EqualTo(subject2Bounds));
86+
Assert.That(fetchedPhoto.Subjects[1].Bounds, Is.EqualTo(subject2Bounds));
87+
}
88+
89+
[Test]
90+
public void DeletePhotoAndSubjectsWhenDeletingUser()
91+
{
92+
using TestContext context = this.GetServer();
93+
GameUser publisher = context.CreateUser();
94+
GameUser subject = context.CreateUser();
95+
GamePhoto photo = context.CreatePhotoWithSubject(publisher, TEST_IMAGE_HASH, subjects:
96+
[
97+
new()
98+
{
99+
Username = publisher.Username,
100+
DisplayName = publisher.Username,
101+
BoundsList = "1,2,3,4",
102+
},
103+
new()
104+
{
105+
Username = subject.Username,
106+
DisplayName = subject.Username,
107+
BoundsList = "4,5,6,7",
108+
}
109+
]);
110+
111+
// Initial subjects checks
112+
List<GamePhotoSubject> subjects = context.Database.GetSubjectsInPhoto(photo).ToList();
113+
Assert.That(subjects.Count, Is.EqualTo(2));
114+
Assert.That(photo.Subjects.Count, Is.EqualTo(2));
115+
116+
// Delete publisher and re-check
117+
context.Database.DeleteUser(publisher);
118+
context.Database.Refresh();
119+
120+
// GetSubjectsInPhoto only compares the photo IDs, so if the subjects weren't cascade-deleted, they should still be found
121+
subjects = context.Database.GetSubjectsInPhoto(photo).ToList();
122+
Assert.That(subjects.Count, Is.Zero);
123+
}
124+
}

0 commit comments

Comments
 (0)