public async Task SaveTwoClubsWithSameNames()
        {
            var stadiumsList = new List <Stadium>
            {
                new Stadium {
                    Id = 1, Name = "Old Trafford", Capacity = 76000
                }
            };
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "England", Code = "EN"
                }
            };
            var clubsList = new List <Club>();

            var mockStadiumRepo = new Mock <IRepository <Stadium> >();

            mockStadiumRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => stadiumsList.FirstOrDefault(c => c.Id == id));

            var mockCountryRepo = new Mock <IRepository <Country> >();

            mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var mockClubRepo = new Mock <IRepository <Club> >();

            mockClubRepo.Setup(r => r.All()).Returns(clubsList.AsQueryable());
            mockClubRepo.Setup(r => r.AddAsync(It.IsAny <Club>())).Callback <Club>(club => clubsList.Add(club));

            var clubService = new ClubService(mockClubRepo.Object, mockCountryRepo.Object, mockStadiumRepo.Object);

            var firstClubViewModel = new ClubViewModel
            {
                Name          = "Manchester United",
                CountryId     = 1,
                HomeStadiumId = 1
            };

            var secondClubViewModel = new ClubViewModel
            {
                Name          = "Manchester United",
                CountryId     = 1,
                HomeStadiumId = 1
            };

            await clubService.CreateAsync(firstClubViewModel);

            await Assert.ThrowsAsync <Exception>(() => clubService.CreateAsync(secondClubViewModel));
        }
        public async Task SaveAndUpdateClub()
        {
            var stadiumsList = new List <Stadium>
            {
                new Stadium {
                    Id = 1, Name = "Old Trafford", Capacity = 76000
                }
            };
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "England", Code = "EN"
                }
            };
            var clubsList = new List <Club>();

            var mockStadiumRepo = new Mock <IRepository <Stadium> >();

            mockStadiumRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => stadiumsList.FirstOrDefault(c => c.Id == id));

            var mockCountryRepo = new Mock <IRepository <Country> >();

            mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var mockClubRepo = new Mock <IRepository <Club> >();

            mockClubRepo.Setup(r => r.All()).Returns(clubsList.AsQueryable());
            mockClubRepo.Setup(r => r.AddAsync(It.IsAny <Club>())).Callback <Club>(club => clubsList.Add(new Club
            {
                Id          = 1,
                Name        = club.Name,
                Country     = club.Country,
                HomeStadium = club.HomeStadium
            }));

            var clubService = new ClubService(mockClubRepo.Object, mockCountryRepo.Object, mockStadiumRepo.Object);

            var clubViewModel = new ClubViewModel
            {
                Name          = "Manchester United",
                CountryId     = 1,
                HomeStadiumId = 1
            };

            await clubService.CreateAsync(clubViewModel);

            var updatedViewModel = new ClubViewModel
            {
                Id            = 1,
                Name          = "Newcastle United",
                CountryId     = 1,
                HomeStadiumId = 1
            };

            await clubService.UpdateAsync(updatedViewModel);

            var savedClub = clubService.Get(1);

            Assert.Equal(1, savedClub.Id);
            Assert.Equal("Newcastle United", savedClub.Name);
        }
        public async Task CreateAsync_WhenCalled_CreateNewClub()
        {
            var club = new Club
            {
                Id = 1
            };

            await _clubService.CreateAsync(club);

            _clubRepository.Verify(c => c.Add(club), Times.Once);
            _unitOfWork.Verify(u => u.CompleteAsync(), Times.Once);
        }
        public async Task SaveAndLoadClub()
        {
            var stadiumsList = new List <Stadium>
            {
                new Stadium {
                    Id = 1, Name = "Old Trafford", Capacity = 76000
                }
            };
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "England", Code = "EN"
                }
            };
            var clubsList = new List <Club>();

            var mockStadiumRepo = new Mock <IRepository <Stadium> >();

            mockStadiumRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => stadiumsList.FirstOrDefault(c => c.Id == id));

            var mockCountryRepo = new Mock <IRepository <Country> >();

            mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var mockClubRepo = new Mock <IRepository <Club> >();

            mockClubRepo.Setup(r => r.All()).Returns(clubsList.AsQueryable());
            mockClubRepo.Setup(r => r.AddAsync(It.IsAny <Club>())).Callback <Club>(club => clubsList.Add(club));
            mockClubRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => clubsList.FirstOrDefault(c => c.Id == id));

            var clubService = new ClubService(mockClubRepo.Object, mockCountryRepo.Object, mockStadiumRepo.Object);

            var clubViewModel = new ClubViewModel
            {
                Name            = "Manchester United",
                CountryId       = 1,
                HomeStadiumId   = 1,
                FoundedOn       = DateTime.Now,
                CountryName     = "England",
                HomeStadiumName = "Old Trafford"
            };

            await clubService.CreateAsync(clubViewModel);

            var savedClub     = clubService.Get(10, false);
            var lastSavedClub = clubService.GetAll().LastOrDefault();

            Assert.Null(savedClub);
            Assert.Equal("Manchester United", lastSavedClub.Name);
            Assert.Equal("England", clubViewModel.CountryName);
            Assert.Equal("Old Trafford", clubViewModel.HomeStadiumName);
            Assert.NotNull(lastSavedClub.Country);
            Assert.NotNull(lastSavedClub.HomeStadium);
        }
Beispiel #5
0
        public void CreateAsync_InvalidOperationException()
        {
            // Arrange
            ClubService clubService = CreateClubService();
            ClubDTO     clubDto     = new ClubDTO
            {
                ID = 0
            };

            _repoWrapper.Setup(r => r.Club.GetFirstOrDefaultAsync(It.IsAny <Expression <Func <DataAccessClub.Club, bool> > >(), null))
            .ReturnsAsync(new DataAccessClub.Club());

            // Act // Assert
            Assert.ThrowsAsync <InvalidOperationException>(async() => await clubService.CreateAsync(clubDto));
        }
Beispiel #6
0
        public async Task CreateTest()
        {
            ClubService    ClubService    = CreateClubService();
            ClubProfileDTO ClubProfileDto = new ClubProfileDTO
            {
                Club = new ClubDTO
                {
                    ID = 0
                }
            };

            var result = await ClubService.CreateAsync(ClubProfileDto, null);

            Assert.Equal(ClubProfileDto.Club.ID, result);
        }
Beispiel #7
0
        public async Task CreateAsync_WhereFormFileIsNull_ReturnClubDtoID()
        {
            // Arrange
            ClubService    clubService    = CreateClubService();
            ClubProfileDTO clubProfileDto = new ClubProfileDTO
            {
                Club = new ClubDTO
                {
                    ID = 0
                }
            };

            // Act
            var result = await clubService.CreateAsync(clubProfileDto, null);

            // Assert
            Assert.AreEqual(clubProfileDto.Club.ID, result);
        }
Beispiel #8
0
        public async Task CreateAsync_ReturnClubDtoID()
        {
            // Arrange
            ClubService clubService = CreateClubService();
            ClubDTO     clubDto     = new ClubDTO
            {
                ID = 0
            };

            _clubBlobStorage.Setup(b => b.UploadBlobForBase64Async(It.IsAny <string>(), It.IsAny <string>()));
            _repoWrapper.Setup(r => r.Club.GetFirstOrDefaultAsync(It.IsAny <Expression <Func <DataAccessClub.Club, bool> > >(), null))
            .ReturnsAsync((DataAccessClub.Club)null);

            // Act
            var result = await clubService.CreateAsync(clubDto);

            // Assert
            Assert.AreEqual(clubDto.ID, result);
            _clubBlobStorage.Verify(b => b.UploadBlobForBase64Async(It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }
Beispiel #9
0
        public async Task CreateAsync_WithOldImageName_WhereFormFileIsNull_ReturnClubDtoID()
        {
            // Arrange
            ClubService    clubService    = CreateClubService();
            ClubProfileDTO clubProfileDto = new ClubProfileDTO
            {
                Club = new ClubDTO
                {
                    ID = 0
                }
            };

            _repoWrapper.Setup(r => r.Club.GetFirstOrDefaultAsync(It.IsAny <Expression <Func <DataAccessClub.Club, bool> > >(), null))
            .ReturnsAsync((DataAccessClub.Club)null);

            // Act
            var result = await clubService.CreateAsync(clubProfileDto, null);

            // Assert
            Assert.AreEqual(clubProfileDto.Club.ID, result);
        }
        public async Task GetAllClubsAsKeyValuePairs()
        {
            var stadiumsList = new List <Stadium>
            {
                new Stadium {
                    Id = 1, Name = "Old Trafford", Capacity = 76000
                }
            };
            var countriesList = new List <Country> {
                new Country {
                    Id = 1, Name = "England", Code = "EN"
                }
            };
            var clubsList = new List <Club>();

            var mockStadiumRepo = new Mock <IRepository <Stadium> >();

            mockStadiumRepo.Setup(r => r.All()).Returns(stadiumsList.AsQueryable());
            mockStadiumRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => stadiumsList.FirstOrDefault(c => c.Id == id));

            var mockCountryRepo = new Mock <IRepository <Country> >();

            mockCountryRepo.Setup(r => r.All()).Returns(countriesList.AsQueryable());
            mockCountryRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var mockClubRepo = new Mock <IRepository <Club> >();

            mockClubRepo.Setup(r => r.All()).Returns(clubsList.AsQueryable());
            mockClubRepo.Setup(r => r.AddAsync(It.IsAny <Club>())).Callback <Club>(club => clubsList.Add(new Club
            {
                Id          = 1,
                Name        = club.Name,
                Country     = club.Country,
                HomeStadium = club.HomeStadium
            }));

            var clubService = new ClubService(mockClubRepo.Object, mockCountryRepo.Object, mockStadiumRepo.Object);

            var firstClubViewModel = new ClubViewModel
            {
                Name          = "Manchester United",
                CountryId     = 1,
                HomeStadiumId = 1,
                StadiumsItems = new StadiumService(
                    mockStadiumRepo.Object,
                    mockCountryRepo.Object)
                                .GetAllAsKeyValuePairs()
            };

            var secondClubViewModel = new ClubViewModel
            {
                Name           = "Newcastle United",
                CountryId      = 1,
                HomeStadiumId  = 1,
                CountriesItems = new CountryService(mockCountryRepo.Object)
                                 .GetAllAsKeyValuePairs()
            };

            await clubService.CreateAsync(firstClubViewModel);

            await clubService.CreateAsync(secondClubViewModel);

            var keyValuePairs = clubService.GetAllAsKeyValuePairs().ToList();

            Assert.True(keyValuePairs.Count == 2);
            Assert.True(firstClubViewModel.StadiumsItems.Count() == 1);
            Assert.True(secondClubViewModel.CountriesItems.Count() == 1);
        }