public async Task Upsert_ShouldNotInsert_WhenEntityIsAlreadyPresent()
        {
            // arrange

            await using var context = new TestDbContext();

            const int id = 1;

            var userToBeInserted = new UserStub
            {
                Id   = id,
                Name = "Captain Smeghead"
            };

            var userToBeUpserted = new UserStub
            {
                Id   = id,
                Name = "John Wick"
            };

            // act

            context.Add(userToBeInserted);
            await context.SaveChangesAsync();

            var value = await context.Users.UpsertAsync(id, () => userToBeUpserted);

            // assert

            Assert.AreEqual("Captain Smeghead", value.Name);
        }
        public async Task Upsert_ShouldInsert_WhenEntityIsNotPresent()
        {
            // arrange

            using var context = new TestDbContext();
            const int id = 3;

            var userToBeUpserted = new UserStub
            {
                Id   = id,
                Name = "John Wick"
            };

            // act

            var before = await context.Users.FindAsync(id);

            var after = await context.Users.UpsertAsync(id, () => userToBeUpserted);

            // assert

            Assert.IsNull(before);
            Assert.IsNotNull(after);
            Assert.AreEqual(id, after.Id);
            Assert.AreEqual("John Wick", after.Name);
        }
        public async void GetStationAsyncShouldReturnResourceDoesNotBelongToCurrentUserWhenNecessesary(string returnedUser, string currentUser, bool shouldMatch)
        {
            // Arrange
            // Configure GetStationAsync to return the provided returnedUser
            this.dataLayer.GetStationAsync(Arg.Any <int>()).Returns(new Station()
            {
                UserId = returnedUser
            });
            // Create the provided currentUser
            this.user = new UserStub(currentUser);
            // Re-initialize the business layer so that it takes the new user object
            this.businessLayer = new BusinessLayer(this.dataLayer, this.user, this.mapper);

            // Act: Run GetStationAsync
            var response = await this.businessLayer.GetStationAsync(1);

            /* Assert that the we're returning either ResourceDoesNotBelongToCurrentUser or a Station
             * based on the shouldMatch input. */
            if (!shouldMatch)
            {
                Assert.True(response.IsLeft);
                Assert.Equal(BusinessOperationFailureReason.ResourceDoesNotBelongToCurrentUser, response.LeftValue);
            }
            else
            {
                Assert.True(response.IsRight);
            }
        }
        public async void SouldGetAllStationsForCurrentUser(UserStub user)
        {
            // Arrange: Init the business layer with the provided user
            this.businessLayer = new BusinessLayer(this.dataLayer, user, this.mapper);
            // Act: Perform GetAllStations
            await this.businessLayer.GetAllStations();

            // Assert that the proper user id was passed to GetAllStationsForUserAsync
            await this.dataLayer.Received(1).GetAllStationsForUserAsync(user.UniqueId);
        }
        public BusinessLayerTests()
        {
            this.user = new UserStub("Dummy Test User");
            var mappingConfig = new MapperConfiguration(mc => {
                mc.AddProfile(new MappingProfile());
            });

            this.mapper        = mappingConfig.CreateMapper();
            this.dataLayer     = Substitute.For <IDataLayer>();
            this.businessLayer = new BusinessLayer(this.dataLayer, this.user, this.mapper);
        }
        public static UserRowViewModel MaptoUserRowViewModel(this UserStub usrStub)
        {
            var retModel = new UserRowViewModel
            {
                Id        = usrStub.Id,
                Company   = usrStub.CompanyName,
                FirstName = usrStub.FirstName,
                LastName  = usrStub.LastName,
                Active    = usrStub.IsaActive
            };

            return(retModel);
        }
Exemple #7
0
 internal void SetRevisions(Revision firstRevision, Revision lastRevision)
 {
     Debug.Assert(lastRevision != null);
     if (firstRevision != null)
     {
         Debug.Assert(lastRevision.TimeStamp >= firstRevision.TimeStamp);
     }
     Id        = lastRevision.Page.Id;
     Exists    = true;
     TimeStamp = DateTime.MinValue;
     Content   = lastRevision.Content;
     if (firstRevision == null)
     {
         // Need to infer from the page title
         // Assuming the author hasn't changed the user name
         var match = PermalinkTimeStampMatcher.Match(lastRevision.Page.Title);
         if (match.Success)
         {
             Author    = new UserStub(match.Groups["UserName"].Value, 0);
             TimeStamp = DateTime.ParseExact(match.Groups["TimeStamp"].Value,
                                             "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
             if (TimeStamp != lastRevision.TimeStamp)
             {
                 LastUpdated = lastRevision.TimeStamp;
             }
             else
             {
                 LastUpdated = null;
             }
         }
         else
         {
             Site.Logger.LogWarning("Cannot infer author from comment page title: {PageStub}.", lastRevision.Page);
         }
     }
     else
     {
         Author    = firstRevision.UserStub;
         TimeStamp = firstRevision.TimeStamp;
         if (lastRevision.Id != firstRevision.Id)
         {
             LastUpdated = lastRevision.TimeStamp;
         }
         else
         {
             LastUpdated = null;
         }
     }
     LastEditor   = lastRevision.UserStub;
     LastRevision = lastRevision;
 }
Exemple #8
0
        public void FindThreeUsers()
        {
            UserStub           user = new UserStub();
            UserRepositoryStub repo = new UserRepositoryStub();

            repo.users = new List <IUser>()
            {
                user, user, user
            };

            searchEngine.SetRepository(repo);
            List <IUser> users = searchEngine.Search("Delft");

            Assert.AreEqual(3, users.Count);
        }
Exemple #9
0
        public void FindSingleUser()
        {
            UserStub           user = new UserStub();
            UserRepositoryStub repo = new UserRepositoryStub();

            repo.users = new List <IUser>()
            {
                user
            };

            searchEngine.SetRepository(repo);
            List <IUser> users = searchEngine.Search("Francine C# Developer Delft");

            Assert.AreEqual(1, users.Count);
        }
        public async void ShouldApplyTheCurrentUserIdToNewStations(UserStub user)
        {
            // Arrange: Init the businesslayer with the provided user & create a dummy StationDTO
            this.businessLayer = new BusinessLayer(this.dataLayer, user, this.mapper);
            var newStation = new StationCreateUpdateDto()
            {
                Title = "New Station"
            };

            // Act: Create the station
            await this.businessLayer.CreateStationAsync(newStation);

            // Assert that the station was created with the provided UserId
            await this.dataLayer.Received(1).CreateStationAsync(Arg.Is <Station>(s => s.UserId == user.UniqueId));
        }
Exemple #11
0
        public void FindUserByName()
        {
            UserStub           user = new UserStub();
            UserRepositoryStub repo = new UserRepositoryStub();

            user.SetName("Bram");
            repo.users = new List <IUser>()
            {
                user
            };

            searchEngine.SetRepository(repo);
            List <IUser> users = searchEngine.Search("Bram");

            Assert.AreEqual(1, users.Count);
            Assert.AreEqual("Bram", users.FirstOrDefault().Name());
        }
        PagedList <UserStub> IUserRepository.GetUsersPage(DataItemPageRequest dataItemPageRequest)
        {
            var retPagedList = new PagedList <UserStub>();

            using (var cn = new SqlConnection(_connStr))
            {
                using (var cmd = new SqlCommand("uspGetAspNetIdentityUsersPaged", cn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@PageNum", dataItemPageRequest.PageNumber);
                    cmd.Parameters.AddWithValue("@PageSize", dataItemPageRequest.PageSize);
                    cmd.Parameters.AddWithValue("@SortOrder", dataItemPageRequest.SortBy);

                    cn.Open();
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var usrStub = new UserStub()
                            {
                                Id          = reader.GetInt64(0),
                                FirstName   = reader.GetString(2),
                                LastName    = reader.GetString(3),
                                CompanyName = reader.GetString(5),
                                IsaActive   = reader.GetBoolean(6)
                            };
                            retPagedList.ItemList.Add(usrStub);
                        }
                        reader.NextResult();
                        if (reader.Read())
                        {
                            var pagedInfo = new PagedInfo()
                            {
                                TotalPages  = reader.GetInt32(0),
                                CurrentPage = reader.GetInt32(1),
                                TotalItems  = reader.GetInt32(2),
                                PageSize    = reader.GetInt32(3)
                            };
                            retPagedList.PagedListInfo = pagedInfo;
                        }
                    }
                }
            }
            return(retPagedList);
        }
        public void FromJson_When_Model_Deserialized_Then_Returns_Model_Of_Matching_Type()
        {
            // Arrange
            var response = new HttpResponseMessage();
            var entity   = new UserStub
            {
                Name         = "andculture engineering",
                EmailAddress = "*****@*****.**"
            };

            var jsonEntity = Newtonsoft.Json.JsonConvert.SerializeObject(entity);

            response.Content = new StringContent(jsonEntity);

            // Act
            var result = response.FromJson <UserStub>();

            // Assert
            result.ShouldBeOfType(typeof(UserStub));
            result.Name.ShouldBe(entity.Name);
            result.EmailAddress.ShouldBe(entity.EmailAddress);
        }
        public async void DeleteStationAsyncShouldReturnTheStationIdWhenAllIsWell()
        {
            // Arrange
            // Configure GetStationAsync to return a Station for user "user"
            var station = new Station()
            {
                UserId = "user"
            };

            this.dataLayer.GetStationAsync(Arg.Any <int>()).Returns(station);
            // Set the current user
            this.user = new UserStub("user");
            // Re-initialize the business layer so that it takes the new user object
            this.businessLayer = new BusinessLayer(this.dataLayer, this.user, this.mapper);

            // Act: Run DeleteStationAsync
            var response = await this.businessLayer.DeleteStationAsync(8675309);

            // Assert that the station id is returned, denoting a successful deletion
            Assert.True(response.IsRight);
            Assert.Equal(8675309, response.RightValue);
        }
        public async void GetStationAsyncShouldReturnTheStationWhenAllIsWell()
        {
            // Arrange
            // Configure GetStationAsync to return a Station for user "user"
            var station = new Station()
            {
                UserId = "user"
            };

            this.dataLayer.GetStationAsync(Arg.Any <int>()).Returns(station);
            // Set the current user
            this.user = new UserStub("user");
            // Re-initialize the business layer so that it takes the new user object
            this.businessLayer = new BusinessLayer(this.dataLayer, this.user, this.mapper);

            // Act: Run GetStationAsync
            var response = await this.businessLayer.GetStationAsync(1);

            // Assert that the station is successfully mapped and returned
            Assert.True(response.IsRight);
            response.RightValue.ShouldDeepEqual(this.mapper.Map <StationDto>(station));
        }
Exemple #16
0
        public void FindUsersFromAmersfoort()
        {
            // stub
            UserStub user = new UserStub();

            user.SetProfile(new List <string> {
                "Bram", "Junior Developer", "Amersfoort"
            });
            UserRepositoryStub repo = new UserRepositoryStub();

            repo.users = new List <IUser>()
            {
                user, user, user
            };

            searchEngine.SetRepository(repo);
            List <IUser> users = searchEngine.Search("Amersfoort");

            Assert.AreEqual(3, users.Count);
            foreach (UserStub currentUser in users)
            {
                Assert.IsTrue(currentUser.Profile().Contains("Amersfoort"));
            }
        }