private static async Task TestUserProjectRepositoryManyToMany() { IUserRepository userRepo = new UserRepository(_tableName); var u1 = new User { Id = "U1", Name = "User 1", FirstName = "User", LastName = "A", Email = "*****@*****.**" }; await userRepo.AddUser(u1); IUserProjectRepository repo = new UserProjectRepository(_tableName); var u1p1 = new UserProject { UserId = u1.Id, ProjectId = "P1", Role = "owner" }; await repo.AddProjectToUser(u1p1); var u1p2 = new UserProject { UserId = u1.Id, ProjectId = "P2", Role = "member" }; await repo.AddProjectToUser(u1p2); Console.WriteLine("Getting projects by user U1"); var allUPU1 = await repo.GetProjectsByUserAsync(u1.Id); foreach (var item in allUPU1) { Console.WriteLine(JsonSerializer.Serialize(item)); } Console.WriteLine("Getting users by project P1"); var usersP1 = await repo.GetUsersByProjectAsync("P1"); foreach (var item in usersP1) { Console.WriteLine(JsonSerializer.Serialize(item)); } Console.WriteLine("Getting users by project P2"); var usersP2 = await repo.GetUsersByProjectAsync("P2"); foreach (var item in usersP2) { Console.WriteLine(JsonSerializer.Serialize(item)); } Console.WriteLine("Deleting projects P1 and P2 for user U1"); await repo.RemoveProjectFromUser(u1p1.UserId, u1p1.ProjectId); await repo.RemoveProjectFromUser(u1p2.UserId, u1p2.ProjectId); Console.WriteLine("Getting projects by user U1 - should be empty"); var deletedUPU1 = await repo.GetProjectsByUserAsync(u1.Id); foreach (var item in deletedUPU1) { Console.WriteLine(JsonSerializer.Serialize(item)); } }
public async void TestRepo_UserProjectRepository() { var u1 = new User { Id = "U1", Name = "User 1", FirstName = "User", LastName = "A", Email = "*****@*****.**" }; IUserProjectRepository repo = new UserProjectRepository(_tableName, _serviceUrl); var allUPU1 = await repo.GetProjectsByUserAsync(u1.Id); Assert.Equal(0, allUPU1.Count); var u1p1 = new UserProject { UserId = u1.Id, ProjectId = "P1", Role = "owner" }; await repo.AddProjectToUser(u1p1); allUPU1 = await repo.GetProjectsByUserAsync(u1.Id); Assert.Equal(1, allUPU1.Count); var u1p2 = new UserProject { UserId = u1.Id, ProjectId = "P2", Role = "member" }; await repo.AddProjectToUser(u1p2); allUPU1 = await repo.GetProjectsByUserAsync(u1.Id); Assert.Equal(2, allUPU1.Count); Assert.Equal(u1.Id, allUPU1[0].UserId); Assert.Equal("P1", allUPU1[0].ProjectId); Assert.Equal("owner", allUPU1[0].Role); Assert.Equal(u1.Id, allUPU1[1].UserId); Assert.Equal("P2", allUPU1[1].ProjectId); Assert.Equal("member", allUPU1[1].Role); var usersP1 = await repo.GetUsersByProjectAsync("P1"); Assert.Equal(1, usersP1.Count); Assert.Equal(u1.Id, usersP1[0].UserId); Assert.Equal("P1", usersP1[0].ProjectId); Assert.Equal("owner", usersP1[0].Role); var usersP2 = await repo.GetUsersByProjectAsync("P2"); Assert.Equal(1, usersP2.Count); Assert.Equal(u1.Id, usersP2[0].UserId); Assert.Equal("P2", usersP2[0].ProjectId); Assert.Equal("member", usersP2[0].Role); await repo.RemoveProjectFromUser(u1p1.UserId, u1p1.ProjectId); allUPU1 = await repo.GetProjectsByUserAsync(u1.Id); Assert.Equal(1, allUPU1.Count); await repo.RemoveProjectFromUser(u1p2.UserId, u1p2.ProjectId); allUPU1 = await repo.GetProjectsByUserAsync(u1.Id); Assert.Equal(0, allUPU1.Count); }