public async Task GetBirdsAsync_PageSizeTheory_ReturnsPageSize(int pageSize) { // Arrange //var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" }; //var connection = new SqliteConnection(connectionStringBuilder.ToString()); //var options = new DbContextOptionsBuilder<ApplicationDbContext>() // .UseSqlite(connection) // .Options; var options = SqliteInMemory.CreateOptions <ApplicationDbContext>(); using var context = new ApplicationDbContext(options); context.Database.EnsureCreated(); context.ChangeTracker.Clear(); //NEW LINE ADDED //using (var context = new ApplicationDbContext(options)) //{ context.Database.OpenConnection(); context.Database.EnsureCreated(); //context.ConservationStatuses.Add(new ConservationStatus() { ConservationStatusId = 1, ConservationList = "Red", Description = "", CreationDate = DateTime.Now, LastUpdateDate = DateTime.Now }); //context.ConservationStatuses.Add(new ConservationStatus() { ConservationStatusId = 2, ConservationList = "Amber", Description = "", CreationDate = DateTime.Now, LastUpdateDate = DateTime.Now }); //context.ConservationStatuses.Add(new ConservationStatus() { ConservationStatusId = 3, ConservationList = "Green", Description = "", CreationDate = DateTime.Now, LastUpdateDate = DateTime.Now }); for (int i = 1; i < 30; i++) { Random r = new Random(); context.Birds.Add(new Bird() { BirdId = i, Class = $"Class {i}", Order = $"Order {i}", Family = $"Family {i}", Genus = $"Genus {i}", Species = $"Species {i}", EnglishName = $"Name {i}", ConservationStatusId = r.Next(1, 3), CreationDate = DateTime.Now, LastUpdateDate = DateTime.Now }); } context.SaveChanges(); //} //using (var context = new ApplicationDbContext(options)) //{ var birdRepository = new BirdRepository(context); // Act var birds = await birdRepository.GetBirdsAsync(1, pageSize, BirderStatus.Common); // Assert Assert.Equal(pageSize, birds.Items.Count()); Assert.IsType <ConservationStatus>(birds.Items.First().BirdConservationStatus); }
public void RemoveBirdTest() { var mockRepository = new BirdRepository(); Bird bird = mockRepository.GetSpecificBird(1); List <Bird> birds = mockRepository.RemoveBird(bird.Id).ToList(); if (birds == null) { Assert.Fail(); } }
public void GetBirdsTest() { var mockRepository = new BirdRepository(); AddBirdTest(); var birdsCollection = mockRepository.GetBirds(); if (birdsCollection == null) { Assert.Fail(); } }
public void GetSpecificBirdTest() { var mockRepository = new BirdRepository(); AddBirdTest(); Bird bird = mockRepository.GetSpecificBird(1); if (bird == null) { Assert.Fail(); } }
public void PagingAndRepeater(int id) { var repos = new BirdRepository(); IList <Bird> birds = repos.GetBirds(); IPaginatedPage items = PaginationHelper.CreatePagination( birds, // list 10, // number of items per page id ); PropertyBag["items"] = items; }
static void TestBirds() { var birds = BirdRepository.LoadBirds(); var sightings = birds.SelectMany(b => b.Sightings); var avgS = birds.Select(b => b.Sightings.Count()).Average(); var sightingsByCountry = sightings .GroupBy(s => s.Place.Country) .Select(g => new { Country = g.Key, Sightings = g.Count() }); var birdCountByStatus = birds .Where(b => b.ConservationStatus != "LeastConcern" && b.ConservationStatus != "NearThreatened") .GroupBy(b => b.ConservationStatus) .Select(selector: g => new { Status = g.Key, Count = g.Count(), Sightings = g.Sum(b => b.Sightings.Count) }); var statuses = birds .Select(b => b.ConservationStatus) .Where(s => s != "LeastConcern" && s != "NearThreatened") .Distinct(); var endangeredSightings = birds .Join(statuses, b => b.ConservationStatus, s => s, (b, s) => new { Status = s, Sigthings = b.Sightings }) .GroupBy(a => a.Status) .Select(g => new { Status = g.Key, Sightings = g.Sum(selector: s => s.Sigthings.Count) }); var importedBirds = BirdRepository.LoadImportedBirds(); var matchingBirds = birds.Intersect(importedBirds, new BirdComparer()); // Perform outer join to get elements that exist in one enumerable and not in the other var newBirds = importedBirds.GroupJoin(birds, ib => ib.CommonName, b => b.CommonName, (ib, b) => new { ImportedBird = ib, Birds = b }) // Variant 1: flatten the grouping first, providing default value for elements with no matches, then filter records //.SelectMany(gb => gb.Birds.DefaultIfEmpty(), (gb, b) => new { ImportedBird = gb.ImportedBird, Bird = b }) //.Where(a => a.Bird == null) // Variant 2: filter the grouping directly - my option .Where(a => a.Birds.Count() == 0) .Select(a => a.ImportedBird) .ToList(); var searchParams = new BirdSearch { Country = "United States", Colors = new List <string> { "White", "Brown", "Black" }, Page = 0, PageSize = 5 }; var foundBirds = birds.Search(searchParams); }
public void UpdateBirdTest() { var mockRepository = new BirdRepository(); AddBirdTest(); Bird bird = mockRepository.GetSpecificBird(1); bird.BirdType = BirdType.Trex; List <Bird> birds = mockRepository.UpdateBird(bird).ToList(); if (birds == null) { Assert.Fail(); } }
public void RepositoryTest() { var mockRepository = new BirdRepository(); AddBirdTest(); GetSpecificBirdTest(); RemoveBirdTest(); UpdateBirdTest(); var finalBirdsCollection = mockRepository.GetBirds(); if (finalBirdsCollection == null) { Assert.Fail(); } }
public ActionResult PagingAndRepeater(int?id) { var pageNumber = id ?? 1; var pageSize = 10; var repos = new BirdRepository(); var allBirds = repos.GetBirds(); var showBirds = allBirds.Skip((pageNumber - 1) * pageSize).Take(pageSize); var birdCount = allBirds.Count(); return(View(new Page <Bird> { Items = showBirds, ItemCount = birdCount, CurrentPage = pageNumber, PageCount = (birdCount + pageSize - 1) / pageSize, FirstItemIndex = (pageNumber - 1) * pageSize })); }
public void AddBirdTest() { var mockRepository = new BirdRepository(); mockRepository.AddBird(new Bird { BirdType = BirdType.Archie, Girth = 3.21, Id = 1, Height = 3.3, Length = 4.3, SpecimenName = "Archie", Weight = 45.4, Location = new Location { Altitude = "3453", Latitude = "23423", Longitude = "12341", Name = "Cypress" }, Skeleton = new Skeleton() }); mockRepository.AddBird(new Bird { BirdType = BirdType.Archie, Girth = 1.21, Id = 2, Height = 2.3, Length = 3.3, SpecimenName = "Archie 2", Weight = 15.4, Location = new Location { Altitude = "3453", Latitude = "23423", Longitude = "12341", Name = "Cypress" }, Skeleton = new Skeleton() }); if (mockRepository.GetBirds() == null) { Assert.Fail(); } }
public async Task GetBird_EmptyId_ThrowsArgumentException() { // Arrange var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" }; var connection = new SqliteConnection(connectionStringBuilder.ToString()); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(connection) .Options; using (var context = new ApplicationDbContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); var authorRepository = new BirdRepository(context); // Assert await Assert.ThrowsAsync <ArgumentException>( // Act () => authorRepository.GetBirdAsync(0)); } }