public ActionResult Edit() { CountriesService countriesService = new CountriesService(); CountriesEditVM model = new CountriesEditVM(); TryUpdateModel(model); if (!ModelState.IsValid) { return View(model); } Country country; if (model.ID == 0) { country = new Country(); } else { country = countriesService.GetByID(model.ID); if (country == null) { return RedirectToAction("List"); } } country.ID = model.ID; country.Name = model.Name; country.Population = model.Population; country.FoundationDate = model.FoundationDate; countriesService.Save(country); return RedirectToAction("List"); }
public ActionResult Edit(int? id) { CountriesService countriesService = new CountriesService(); CountriesEditVM model = new CountriesEditVM(); Country country; if (!id.HasValue) { country = new Country(); } else { country = countriesService.GetByID(id.Value); if (country == null) { return RedirectToAction("List"); } } model.ID = country.ID; model.Name = country.Name; model.Population = country.Population; model.FoundationDate = country.FoundationDate; return View(model); }
public async Task DeleteAsyncWorksCorrectlyWhenCountryExists() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var addressesServiceMock = new Mock <IAddressesService>(); var citiesRepository = new EfDeletableEntityRepository <City>(dbContext); var repository = new EfDeletableEntityRepository <Country>(dbContext); var citiesServiceMock = new Mock <CitiesService>(citiesRepository, addressesServiceMock.Object); var service = new CountriesService(repository, citiesServiceMock.Object); var country = new Country() { Id = 1, }; dbContext.Add(country); await dbContext.SaveChangesAsync(); await service.DeleteAsync(1); var result = dbContext.Countries.Count(); Assert.Equal(0, result); }
protected override async Task OnAfterRenderAsync(bool firstRender) { if (_loaded) { return; } _loaded = true; _creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/people/create", StringComparison.InvariantCulture); if (Id <= 0 && !_creating) { return; } _countries = await CountriesService.GetAsync(); _model = _creating ? new PersonViewModel() : await Service.GetAsync(Id); _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/people/edit/", StringComparison.InvariantCulture); if (_editing) { SetCheckboxes(); } StateHasChanged(); }
public async Task GetAllShouldWorkCorrectlyUsingDbContext() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "GetAllShouldWorkCorrectlyUsingDbContextCountriesServiceTests").Options; using var dbContext = new ApplicationDbContext(options); await dbContext.Countries.AddAsync(new Country { Name = "TestCountry1" }); await dbContext.Countries.AddAsync(new Country { Name = "TestCountry2" }); await dbContext.Countries.AddAsync(new Country { Name = "TestCountry3" }); await dbContext.SaveChangesAsync(); using var repository = new EfRepository <Country>(dbContext); var service = new CountriesService(repository); Assert.Equal(3, service.GetAll().Count()); Assert.Equal(1, service.GetAll().FirstOrDefault().Id); Assert.Equal("TestCountry1", service.GetAll().FirstOrDefault().Name); Assert.Equal(2, service.GetAll().ElementAt(1).Id); Assert.Equal("TestCountry2", service.GetAll().ElementAt(1).Name); Assert.Equal(3, service.GetAll().ElementAt(2).Id); Assert.Equal("TestCountry3", service.GetAll().ElementAt(2).Name); }
public void GetAllShouldWorkCorrectlyUsingMoq() { var repository = new Mock <IRepository <Country> >(); var countriesList = new List <Country> { new Country { Id = 1, CreatedOn = DateTime.UtcNow, Name = "TestCountry1" }, new Country { Id = 1, CreatedOn = DateTime.UtcNow, Name = "TestCountry2" }, new Country { Id = 1, CreatedOn = DateTime.UtcNow, Name = "TestCountry3" }, }; repository.Setup(r => r.AllAsNoTracking()).Returns(countriesList.AsQueryable()); var service = new CountriesService(repository.Object); Assert.Equal(countriesList, service.GetAll()); repository.Verify(x => x.AllAsNoTracking(), Times.Once); }
public void TestMethod1() { List <CountryResponse> response = new CountriesService().GetAllCountries().Result; response.ShouldNotBeNull(); response.Count.ShouldBeGreaterThan(1); }
public async Task DeleteAsync_ShouldDeleteCountryIfExist() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldDeleteCountryIfExist"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria" }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new CountriesService(context); await sut.DeleteAsync(1); var dbresult = await context.Countries.FirstOrDefaultAsync(c => c.Name == "Bulgaria"); //Assert Assert.AreEqual(dbresult.Name, "Bulgaria"); Assert.AreEqual(dbresult.DeletedOn, dbresult.ModifiedOn); Assert.AreEqual(dbresult.IsDeleted, true); } }
public async Task GetAllAsKeyValuePairsShouldWorkCorrect() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; using var db = new ApplicationDbContext(options); var repository = new EfRepository <Country>(db); await repository.AddAsync(new Country { Id = 1, Name = "Bulgaria" }); await repository.AddAsync(new Country { Id = 2, Name = "Nigeria" }); await repository.AddAsync(new Country { Id = 3, Name = "Germany" }); await repository.SaveChangesAsync(); var service = new CountriesService(repository); var result = service.GetAllAsKeyValuePairs() .Select(x => x.Key + "=>" + x.Value) .ToList(); Assert.Equal(3, result.Count()); // order should be by name Assert.Equal("1=>Bulgaria", result[0]); Assert.Equal("2=>Nigeria", result[2]); Assert.Equal("3=>Germany", result[1]); }
public SubmitModel(ILogger <SubmitModel> logger, CountriesService counServ, CasesService caseServ, UsersService usersServ) { _logger = logger; MyCountriesService = counServ; MyCasesService = caseServ; MyUsersService = usersServ; }
public async Task GetCountryShouldReturnCorrectCountry() { var list = new List <Country>() { new Country { Id = 1, Name = "Bulgaria", }, new Country { Id = 2, Name = "Germany", }, new Country { Id = 3, Name = "Greece", }, }; var repository = new Mock <IDeletableEntityRepository <Country> >(); repository.Setup(r => r.All()).Returns(() => list.AsQueryable()); var service = new CountriesService(repository.Object); var county = await service.GetCountry("Germany"); Assert.Equal("Germany", county.Name); Assert.Equal(2, county.Id); repository.Verify(x => x.All(), Times.Once); }
public async Task UpdateAsync_ShouldChangeNameOfCountryAsync() { //Arrange var options = InMemory.GetOptions("UpdateAsync_ShouldChangeNameOfCountryAsync"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria" }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { var countryDTO = new CountryDTO() { Name = "Belgium" }; //Act var sut = new CountriesService(context); var result = await sut.UpdateAsync(1, countryDTO); var dbresult = await context.Countries.FindAsync(1); //Assert Assert.AreEqual(dbresult.Name, "Belgium"); } }
public async Task DeleteAsync_ShouldReturnTrueIfSucceded() { //Arrange var options = InMemory.GetOptions("DeleteAsync_ShouldReturnTrueIfSucceded"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria" }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new CountriesService(context); var result = await sut.DeleteAsync(1); //Assert Assert.AreEqual(result, true); } }
public async Task CreateAsync_ShouldUndeleteRecordIfExist() { //Arrange var options = InMemory.GetOptions("CreateAsync_ShouldUndeleteRecordIfExist"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria", DeletedOn = DateTime.UtcNow, IsDeleted = true }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { var countryDTO = new CountryDTO() { Name = "Bulgaria" }; //Act var sut = new CountriesService(context); await sut.CreateAsync(countryDTO); var dbresult = await context.Countries.FirstOrDefaultAsync(c => c.Name == "Bulgaria"); //Assert Assert.AreEqual(dbresult.Name, "Bulgaria"); Assert.AreEqual(dbresult.DeletedOn, null); Assert.AreEqual(dbresult.IsDeleted, false); } }
public DashboardModel(ILogger <DashboardModel> logger, CountriesService counServ, CasesService caseServ, UsersService usersServ) { _logger = logger; MyCountriesService = counServ; MyCasesService = caseServ; MyUsersService = usersServ; }
public async Task GetAllAsync_ShouldReturnIEnumerableCountryDTOAsync() { //Arrange var options = InMemory.GetOptions("GetAllAsync_ShouldReturnIEnumerableCountryDTOAsync"); using (var context = new BOContext(options)) { var country = new Country() { Name = "Bulgaria" }; context.Countries.Add(country); await context.SaveChangesAsync(); } using (var context = new BOContext(options)) { //Act var sut = new CountriesService(context); var result = await sut.GetAllAsync(); //Assert Assert.IsInstanceOfType(result, typeof(IEnumerable <CountryDTO>)); } }
public void CheckIfGettingAllAsKvpWorksCorrectly() { var list = new List <Country>() { new Country() { Id = 1, Name = "Bulgaria", }, new Country() { Id = 2, Name = "Germany", }, }; var mockRepo = new Mock <IRepository <Country> >(); mockRepo.Setup(x => x.AllAsNoTracking()).Returns(list.AsQueryable()); var service = new CountriesService(mockRepo.Object); var actualResult = service.GetAllAsKvp(); Assert.Equal(2, actualResult.Count()); Assert.Equal("Bulgaria", actualResult.First().Value); }
public async Task GetById() { var fakeRepository = Mock.Of <ICountriesRepository>(); var countriesService = new CountriesService(fakeRepository); await countriesService.GetById(1); }
public async Task GetAllTest() { var countries = new List <Country> { new Country() { Name = "Country-1" }, new Country() { Name = "Country-2" }, }; var fakeRepositoryMock = new Mock <ICountriesRepository>(); fakeRepositoryMock.Setup(x => x.GetAll()).ReturnsAsync(countries); var countriesService = new CountriesService(fakeRepositoryMock.Object); var result = await countriesService.GetAll(); Assert.Collection(result, country => { Assert.Equal("Country-1", country.Name); }, country => { Assert.Equal("Country-2", country.Name); }); }
public async Task CreateAsync_ShouldReturnModifiedCountryDTOAsync() { //Arrange var options = InMemory.GetOptions("CreateAsync_ShouldReturnModifiedCountryDTOAsync"); using (var context = new BOContext(options)) { } using (var context = new BOContext(options)) { var countryDTO = new CountryDTO() { Name = "Bulgaria" }; //Act var sut = new CountriesService(context); var result = await sut.CreateAsync(countryDTO); var dbresult = await context.Countries.FirstOrDefaultAsync(c => c.Name == "Bulgaria"); //Assert Assert.AreEqual(result.ID, dbresult.ID); Assert.AreEqual(result.Name, dbresult.Name); } }
public async Task <IActionResult> PutCountry(int id, Country country) { var _countriesService = new CountriesService(_context); if (id != country.Id) { return(BadRequest()); } var CountryValidater = new CountryValidator(); var resultValidation = CountryValidater.Validate(country); if (!resultValidation.IsValid) { return(BadRequest(resultValidation.Errors)); } var result = await _countriesService.PutCountry(id, country); if (result) { return(Ok()); } return(NoContent()); }
public async Task CheckIfCountryExistsReturnsFalseIfCountryDoesntExists() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var addressesServiceMock = new Mock <IAddressesService>(); var citiesRepository = new EfDeletableEntityRepository <City>(dbContext); var repository = new EfDeletableEntityRepository <Country>(dbContext); var citiesServiceMock = new Mock <CitiesService>(citiesRepository, addressesServiceMock.Object); var service = new CountriesService(repository, citiesServiceMock.Object); var country = new Country() { Id = 1, }; dbContext.Add(country); await dbContext.SaveChangesAsync(); var result = await service.CheckIfCountryExists(2); Assert.False(result); }
public async Task GetByIdAsyncShouldReturnNullIfCountryDoesntExists() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var addressesServiceMock = new Mock <IAddressesService>(); var citiesRepository = new EfDeletableEntityRepository <City>(dbContext); var repository = new EfDeletableEntityRepository <Country>(dbContext); var citiesServiceMock = new Mock <CitiesService>(citiesRepository, addressesServiceMock.Object); var service = new CountriesService(repository, citiesServiceMock.Object); var country1 = new Country() { Id = 1, }; var country2 = new Country() { Id = 2, }; var country3 = new Country() { Id = 3, }; dbContext.Add(country1); dbContext.Add(country2); dbContext.Add(country3); await dbContext.SaveChangesAsync(); var result = await service.GetByIdAsync <CountryServiceModel>(7); Assert.Null(result); }
public ActionResult Index() { var countriesService = new CountriesService(); ViewBag.CountryData = countriesService.AllCountries(); ViewBag.Service = new CountriesService(); return(View()); }
public AgenciesController(IUnitOfWork unitOfWork, AgenciesService service, CountriesService countriesService, AgencyUsersService agencyUsersService, AgencyPacksService agencyPackagesService, CitiesService citiesService) : base(unitOfWork, service) { _countriesService = countriesService; _agencyUsersService = agencyUsersService; _agencyPackagesService = agencyPackagesService; _citiesService = citiesService; }
public WorldViewer() { countries = new List <Country>(); cities = new List <City>(); streets = new List <Street>(); citiesService = new CitiesService(); countriesService = new CountriesService(); streetService = new StreetService(); }
private async void CountriesBtn_Click(object sender, RoutedEventArgs e) { var cts = new CancellationTokenSource(TimeSpan.FromMinutes(CTS_TIMEOUT_DEFAULT)); var ct = cts.Token; this.CountriesList.ItemsSource = new string[] { "...loading..." }; var data = await CountriesService.GetCountriesAsync($"{App.API_BASE_URL}/countries?delay=5s", ct, captureContext : false); this.CountriesList.ItemsSource = data; }
private async Task <ICountriesService> GetCountriesService() { var dbContext = await this.GetUseInMemoryDbContext(); var repository = new EfRepository <Country>(dbContext); var service = new CountriesService(repository); return(service); }
public CountriesServiceTests() { long Id = singleEntity.Id; Mock = DefaultContextMock.GetMock(); MockSet = SetUpMock.SetUpFor(testEntities); Mock.Setup(c => c.Set <Country>()).Returns(MockSet.Object); Mock.Setup(c => c.Country).Returns(MockSet.Object); testedService = new CountriesService(Mock.Object); }
public void Should_return_list_of_supported_countries() { var isNotfied = false; var mockCountriesRepository = new Mocks.MockCountriesRepository(); mockCountriesRepository.OnGetAll += () => { isNotfied = true; }; var listOfCountries = new CountriesService(mockCountriesRepository).GetAllCountries(); Assert.IsTrue(isNotfied); }
public async Task DestroyTest() { var fakeRepository = Mock.Of <ICountriesRepository>(); var countriesService = new CountriesService(fakeRepository); var country = new Country() { Id = 1, Name = "DestroyedCountry" }; await countriesService.Destroy(country.Id); }
/// <summary>Initializes a new instance of the <see cref="FilterCarsForm"/> class.</summary> /// <param name="returnCriteria">The return criteria the parent form is currently set to.</param> /// <param name="con">The database connection.</param> /// <exception cref="ArgumentNullException">returnCriteria or con</exception> public FilterCarsForm(CarSearchCriteria returnCriteria, NpgsqlConnection con) { this.returnCriteria = returnCriteria ?? throw new ArgumentNullException(nameof(returnCriteria)); this.con = con ?? throw new ArgumentNullException(nameof(con)); manufacturersService = new ManufacturersService(con); countiresService = new CountriesService(con); regionsService = new RegionsService(con); InitializeComponent(); }
public async Task UpdateTest() { var fakeRepository = Mock.Of <ICountriesRepository>(); var countriesService = new CountriesService(fakeRepository); var country = new Country() { Id = 1, Name = "UpdatedCountry" }; await countriesService.Update(country); }
public ActionResult Delete(int? id) { CountriesService countriesService = new CountriesService(); if (!id.HasValue) { return RedirectToAction("List"); } countriesService.Delete(id.Value); return RedirectToAction("List"); }
public ActionResult List() { CountriesService countriesService = new CountriesService(); CountriesListVM model = new CountriesListVM(); TryUpdateModel(model); model.Countries = countriesService.GetAll(); if (!String.IsNullOrEmpty(model.Search)) { model.Countries = model.Countries.Where(c => c.Name.ToLower().Contains(model.Search.ToLower())).ToList(); } switch (model.SortOrder) { case "population_asc": model.Countries = model.Countries.OrderBy(c => c.Population).ToList(); break; case "population_desc": model.Countries = model.Countries.OrderByDescending(c => c.Population).ToList(); break; case "date_asc": model.Countries = model.Countries.OrderBy(c => c.FoundationDate).ToList(); break; case "date_desc": model.Countries = model.Countries.OrderByDescending(c => c.FoundationDate).ToList(); break; case "name_desc": model.Countries = model.Countries.OrderByDescending(c => c.Name).ToList(); break; case "name_asc": default: model.Countries = model.Countries.OrderBy(c => c.Name).ToList(); break; } return View(model); }