public async Task DeleteUser_WithNullUserId_ReturnsFalse()
        {
            // Arrange
            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var personalDataService = new PersonalDataService(new EfRepository <PoolItUser>(context),
                                                              new EfRepository <JoinRequest>(context),
                                                              new EfRepository <UserRide>(context),
                                                              new EfRepository <Conversation>(context),
                                                              new EfRepository <ContactMessage>(context));

            // Act
            var result = await personalDataService.DeleteUser(null);

            // Assert
            Assert.False(result);
        }
        public async Task DeleteUser_WithExceptionOnSave_ReturnsFalse()
        {
            // Arrange
            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var user = new PoolItUser
            {
                FirstName = "FirstName",
                LastName  = "LastName",
                UserName  = "******",
                Email     = "*****@*****.**"
            };

            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            var usersRepository = new EfRepository <PoolItUser>(context);

            var mockUsersRepository = new Mock <IRepository <PoolItUser> >();

            mockUsersRepository.Setup(r => r.All()).Returns(usersRepository.All());
            mockUsersRepository.Setup(r => r.SaveChangesAsync()).Throws <InvalidOperationException>();

            var personalDataService = new PersonalDataService(mockUsersRepository.Object,
                                                              new EfRepository <JoinRequest>(context),
                                                              new EfRepository <UserRide>(context),
                                                              new EfRepository <Conversation>(context),
                                                              new EfRepository <ContactMessage>(context));

            // Act
            var result = await personalDataService.DeleteUser(user.Id);

            // Assert
            Assert.False(result);
        }
        public async Task DeleteUser_WithCorrectUserId_WorksCorrectly()
        {
            // Arrange
            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var otherUsers = new List <PoolItUser>
            {
                new PoolItUser
                {
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    FirstName = "Other0",
                    LastName  = "User0"
                },
                new PoolItUser
                {
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    FirstName = "Other1",
                    LastName  = "User1"
                }
            };

            var ride = new Ride
            {
                Title          = "Test Ride",
                Date           = DateTime.UtcNow.AddDays(1),
                From           = "Test From",
                To             = "Test To",
                AvailableSeats = 3,
                PhoneNumber    = "0123123123",
                Notes          = null,
                Participants   = new List <UserRide>
                {
                    new UserRide
                    {
                        User = otherUsers[0],
                    }
                },
                Invitations = new List <Invitation>
                {
                    new Invitation
                    {
                        Key = "testKey"
                    }
                },
                JoinRequests = new List <JoinRequest>
                {
                    new JoinRequest
                    {
                        User    = otherUsers[1],
                        SentOn  = DateTime.UtcNow.AddDays(-1),
                        Message = "Test Message"
                    }
                },
                Conversation = new Conversation
                {
                    Messages = new List <Message>
                    {
                        new Message
                        {
                            Author  = otherUsers[0],
                            SentOn  = DateTime.UtcNow.AddDays(-1),
                            Content = "Test Message Content 1"
                        },
                        new Message
                        {
                            Author  = otherUsers[0],
                            SentOn  = DateTime.UtcNow,
                            Content = "Test Message Content 2"
                        }
                    }
                }
            };

            var user = new PoolItUser
            {
                FirstName    = "FirstName",
                LastName     = "LastName",
                UserName     = "******",
                Email        = "*****@*****.**",
                SentRequests = new List <JoinRequest>
                {
                    new JoinRequest
                    {
                        Ride = new Ride
                        {
                            Title = "Test Ride for Join Request"
                        },
                        SentOn  = DateTime.UtcNow,
                        Message = "Test Message for Join Request"
                    }
                },
                Cars = new List <Car>
                {
                    new Car
                    {
                        Model = new CarModel
                        {
                            Manufacturer = new CarManufacturer
                            {
                                Name = "TestManufacturer"
                            },
                            Model = "TestModel"
                        },
                        Colour  = "TestColour",
                        Details = null,
                        Rides   = new List <Ride>
                        {
                            ride
                        }
                    }
                },
                UserRides = new List <UserRide>
                {
                    new UserRide
                    {
                        Ride = ride
                    }
                },
                ContactMessages = new List <ContactMessage>
                {
                    new ContactMessage
                    {
                        Subject = "Test Subject",
                        Message = "Test Message"
                    }
                }
            };

            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            var personalDataService = new PersonalDataService(new EfRepository <PoolItUser>(context),
                                                              new EfRepository <JoinRequest>(context),
                                                              new EfRepository <UserRide>(context),
                                                              new EfRepository <Conversation>(context),
                                                              new EfRepository <ContactMessage>(context));

            // Act
            var result = await personalDataService.DeleteUser(user.Id);

            // Assert
            Assert.True(result);

            var carCount = await context.Cars.CountAsync();

            Assert.Equal(0, carCount);

            var rideCount = await context.Rides.CountAsync();

            Assert.Equal(1, rideCount);

            var conversationCount = await context.Conversations.CountAsync();

            Assert.Equal(0, conversationCount);

            var invitationCount = await context.Invitations.CountAsync();

            Assert.Equal(0, invitationCount);

            var joinRequestCount = await context.JoinRequests.CountAsync();

            Assert.Equal(0, joinRequestCount);

            var messageCount = await context.Messages.CountAsync();

            Assert.Equal(0, messageCount);

            var userRideCount = await context.UserRides.CountAsync();

            Assert.Equal(0, userRideCount);

            var userCount = await context.Users.CountAsync();

            Assert.Equal(2, userCount);

            var contactMessageCount = await context.ContactMessages.CountAsync();

            Assert.Equal(0, contactMessageCount);
        }