public async Task SaveTwoCountriesWithSameCodes()
        {
            var countriesList = new List <Country>();

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

            mockRepo.Setup(r => r.All()).Returns(countriesList.AsQueryable());
            mockRepo.Setup(r => r.AddAsync(It.IsAny <Country>())).Callback <Country>(country => countriesList.Add(country));

            var countryService = new CountryService(mockRepo.Object);

            var firstCountryViewModel = new CountryViewModel
            {
                Name = "FirstCountry",
                Code = "BG"
            };

            var secondCountryViewModel = new CountryViewModel
            {
                Name = "SecondCountry",
                Code = "BG"
            };

            await countryService.CreateAsync(firstCountryViewModel);

            await Assert.ThrowsAsync <Exception>(() => countryService.CreateAsync(secondCountryViewModel));
        }
        public async Task SaveAndDeleteCountry()
        {
            var countriesList = new List <Country>();

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

            mockRepo.Setup(r => r.All()).Returns(countriesList.AsQueryable());
            mockRepo.Setup(r => r.AddAsync(It.IsAny <Country>())).Callback <Country>(country => countriesList.Add(new Country
            {
                Id   = 1,
                Name = country.Name,
                Code = country.Code
            }));
            mockRepo.Setup(r => r.Delete(It.IsAny <Country>())).Callback <Country>(country => countriesList.Remove(country));

            var countryService = new CountryService(mockRepo.Object);

            var countryViewModel = new CountryViewModel
            {
                Name = "Norway",
                Code = "NO"
            };

            await countryService.CreateAsync(countryViewModel);

            await countryService.DeleteAsync(1);

            Assert.Empty(countryService.GetAll());
        }
        public async Task SaveAndLoadCountry()
        {
            var countriesList = new List <Country>();

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

            mockRepo.Setup(r => r.All()).Returns(countriesList.AsQueryable());
            mockRepo.Setup(r => r.AddAsync(It.IsAny <Country>())).Callback <Country>(country => countriesList.Add(country));
            mockRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var countryService = new CountryService(mockRepo.Object);

            var countryViewModel = new CountryViewModel
            {
                Name = "Australia",
                Code = "AU"
            };

            await countryService.CreateAsync(countryViewModel);

            var notExistingCountry = countryService.Get(50);
            var lastSavedCountry   = countryService.GetAll().LastOrDefault();

            Assert.Null(notExistingCountry);
            Assert.Equal("Australia", lastSavedCountry.Name);
            Assert.Equal("AU", lastSavedCountry.Code);
        }
        public async Task SaveAndUpdateCountryWithNameOfAnotherdExistingCountry()
        {
            var countriesList = new List <Country>();
            var id            = 1;

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

            mockRepo.Setup(r => r.All()).Returns(countriesList.AsQueryable());
            mockRepo.Setup(r => r.AddAsync(It.IsAny <Country>())).Callback <Country>(country => countriesList.Add(new Country
            {
                Id   = id++,
                Name = country.Name,
                Code = country.Code
            }));

            var countryService = new CountryService(mockRepo.Object);

            var firstCountryViewModel = new CountryViewModel
            {
                Name = "Switzerland",
                Code = "SW"
            };

            var secondCountryViewModel = new CountryViewModel
            {
                Name = "Scotland",
                Code = "SC"
            };

            await countryService.CreateAsync(firstCountryViewModel);

            await countryService.CreateAsync(secondCountryViewModel);

            var secondUpdatedViewModel = new CountryViewModel
            {
                Id   = 2,
                Name = "Switzerland",
                Code = "SC"
            };

            await Assert.ThrowsAsync <Exception>(() => countryService.UpdateAsync(secondUpdatedViewModel));
        }
        public async Task GetAllCountriesAsKeyValuePairs()
        {
            var countriesList = new List <Country>();

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

            mockRepo.Setup(r => r.All()).Returns(countriesList.AsQueryable());
            mockRepo.Setup(r => r.AddAsync(It.IsAny <Country>())).Callback <Country>(country => countriesList.Add(new Country
            {
                Id   = 1,
                Name = country.Name,
                Code = country.Code
            }));

            var countryService = new CountryService(mockRepo.Object);

            var firstCountryViewModel = new CountryViewModel
            {
                Name = "Romania",
                Code = "RO"
            };

            var secondCountryViewModel = new CountryViewModel
            {
                Name = "Greece",
                Code = "GR"
            };

            await countryService.CreateAsync(firstCountryViewModel);

            await countryService.CreateAsync(secondCountryViewModel);

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

            Assert.True(keyValuePairs.Count == 2);
        }
        public async Task CreateAsync_ValidObjectPassed_ReturnCreatedCountry()
        {
            // Arrange
            var country = new Country("Some name", 201);

            _repositoryMock
            .Setup(rep => rep.AddAsync(country))
            .ReturnsAsync(country);

            // Act
            var createdCountry = await _countryService.CreateAsync(country);

            // Assert
            Assert.Equal(country.Name, createdCountry.Name);
            Assert.Equal(country.Code, createdCountry.Code);
        }
Example #7
0
        public async Task <IActionResult> Create([FromForm] string name, [FromForm] string code)
        {
            var result = await _countryService.CreateAsync(new CreateCountryDto(name, code));

            if (result.IsFailed &&
                (result.Errors.Exists(e =>
                                      e.HasMetadata("errCode", e => (string)e == "errCountryAlreadyExistsByName")) ||
                 result.Errors.Exists(e =>
                                      e.HasMetadata("errCode", e => (string)e == "errCountryAlreadyExistsByName"))))
            {
                return(Conflict("The country already exists"));
            }

            if (result.IsFailed &&
                result.Errors.Exists(e => e.HasMetadata("errCode", e => (string)e == "errDbSaveFail")))
            {
                return(BadRequest("An error happened while trying to save the country into the database"));
            }

            return(Ok());
        }
        public async Task SaveAndUpdateCountry()
        {
            var countriesList = new List <Country>();

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

            mockRepo.Setup(r => r.All()).Returns(countriesList.AsQueryable());
            mockRepo.Setup(r => r.AddAsync(It.IsAny <Country>())).Callback <Country>(country => countriesList.Add(new Country
            {
                Id   = 1,
                Name = country.Name,
                Code = country.Code
            }));
            mockRepo.Setup(r => r.Get(It.IsAny <int>())).Returns <int>(id => countriesList.FirstOrDefault(c => c.Id == id));

            var countryService = new CountryService(mockRepo.Object);

            var countryViewModel = new CountryViewModel
            {
                Name = "Bulgariaaaa",
                Code = "BB"
            };

            await countryService.CreateAsync(countryViewModel);

            var updatedViewModel = new CountryViewModel
            {
                Id   = 1,
                Name = "Bulgaria",
                Code = "BG"
            };

            await countryService.UpdateAsync(updatedViewModel);

            var savedCountry = countryService.Get(1);

            Assert.Equal(1, savedCountry.Id);
            Assert.Equal("Bulgaria", savedCountry.Name);
            Assert.Equal("BG", savedCountry.Code);
        }
Example #9
0
 public async Task <Country> AddCountryAsync(InsertCountryDto input)
 {
     return(await _countryService.CreateAsync(input));
 }