コード例 #1
0
        public async Task GetPersonalDataForUserJson_WithNullUserId_ReturnsNull()
        {
            // Arrange
            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var personalDataService =
                new PersonalDataService(new EfRepository <PoolItUser>(context), null, null, null, null);

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

            // Assert
            Assert.Null(result);
        }
コード例 #2
0
        public async Task GetPersonalDataForUserJson_WithCorrectUserId_WorksCorrectly()
        {
            // Arrange
            const string expectedJson =
                "{\"FirstName\": \"FirstName\",\"LastName\": \"LastName\",\"Email\": \"[email protected]\"," +
                "\"SentRequests\": [{\"Ride\": \"Test Ride for Join Request\",\"SentOn\": \"Thu, 01 Jan 2015 10:25:30 GMT\"," +
                "\"Message\": \"Test Message for Join Request\"}],\"Cars\": [{\"Manufacturer\": \"TestManufacturer\"," +
                "\"Model\": \"TestModel\",\"Colour\": \"TestColour\",\"Details\": null}],\"OrganisedRides\": " +
                "[{\"Title\": \"Test Ride\",\"Date\": \"Fri, 02 Jan 2015 10:25:30 GMT\",\"From\": \"Test From\"," +
                "\"To\": \"Test To\",\"AvailableSeats\": 3,\"PhoneNumber\": \"0123123123\",\"Notes\": null,\"Car\": " +
                "{\"Manufacturer\": \"TestManufacturer\",\"Model\": \"TestModel\",\"Colour\": \"TestColour\",\"Details\": null}," +
                "\"Participants\": [{\"FirstName\": \"Other0\",\"LastName\": \"User0\"},{\"FirstName\": \"FirstName\"," +
                "\"LastName\": \"LastName\"}],\"Invitations\": [{\"Key\": \"testKey\"}],\"JoinRequests\": " +
                "[{\"FirstName\": \"Other1\",\"LastName\": \"User1\",\"SentOn\": \"Wed, 31 Dec 2014 10:25:30 GMT\"," +
                "\"Message\": \"Test Message\"}]}],\"ParticipantInRides\": [{\"Title\": \"Test Ride\"," +
                "\"Date\": \"Fri, 02 Jan 2015 10:25:30 GMT\",\"From\": \"Test From\",\"To\": \"Test To\",\"Organiser\": " +
                "{\"FirstName\": \"FirstName\",\"LastName\": \"LastName\"},\"Car\": {\"Manufacturer\": \"TestManufacturer\"," +
                "\"Model\": \"TestModel\"},\"Participants\": [{\"FirstName\": \"Other0\",\"LastName\": \"User0\"}," +
                "{\"FirstName\": \"FirstName\",\"LastName\": \"LastName\"}],\"Conversation\": {\"Messages\": [{\"Author\": " +
                "{\"FirstName\": \"Other0\",\"LastName\": \"User0\"},\"SentOn\": \"Wed, 31 Dec 2014 10:25:30 GMT\"," +
                "\"Content\": \"Test Message Content 1\"},{\"Author\": {\"FirstName\": \"Other0\",\"LastName\": \"User0\"}," +
                "\"SentOn\": \"Thu, 01 Jan 2015 10:25:30 GMT\",\"Content\": \"Test Message Content 2\"}]}}]," +
                "\"ContactMessages\": [{\"Subject\": \"Test Subject\",\"Message\": \"Test Message\"}]}";

            var dateTime = new DateTime(2015, 1, 1, 10, 25, 30);

            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.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.AddDays(-1),
                        Message = "Test Message"
                    }
                },
                Conversation = new Conversation
                {
                    Messages = new List <Message>
                    {
                        new Message
                        {
                            Author  = otherUsers[0],
                            SentOn  = dateTime.AddDays(-1),
                            Content = "Test Message Content 1"
                        },
                        new Message
                        {
                            Author  = otherUsers[0],
                            SentOn  = dateTime,
                            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,
                        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), null, null, null, null);

            // Act
            var actualJson = await personalDataService.GetPersonalDataForUserJson(user.Id);

            // Assert
            var expectedResult = JToken.Parse(expectedJson);
            var actualResult   = JToken.Parse(actualJson);

            var equal = JToken.DeepEquals(expectedResult, actualResult);

            Assert.True(equal);
        }