public async Task SingleByIdAsync_should_return_expected_superheroes(
            IEnumerable <Superhero> superheroes,
            IAppLogger <EfRepository <Superhero, int> > logger)
        {
            // Arrange
            var options = new DbContextOptionsBuilder <ShbContext>()
                          .UseInMemoryDatabase(databaseName: "ShbDb")
                          .Options;

            using (var dbContext = new ShbContext(options))
            {
                dbContext.AddRange(superheroes);
                dbContext.SaveChanges();
            }

            using (var dbContext = new ShbContext(options))
            {
                SuperheroRepository repository = new SuperheroRepository(dbContext, logger);

                //Act
                Superhero result = await repository.SingleByIdAsync(superheroes.First().Id);

                //Assert
                result.Should().NotBeNull();
                result.Id.Should().Be(superheroes.First().Id);
            }
        }
Example #2
0
        private void ComposeObjects()
        {
            SuperheroRepository repository = new SuperheroRepository();
            SuperheroViewModel  viewModel  = new SuperheroViewModel(repository);

            Application.Current.MainWindow = new SuperheroesViewerWindow(viewModel);
        }
        public void Update_given_existing_entity_uses_existing_city()
        {
            var builder = new DbContextOptionsBuilder <SuperheroContext>().UseInMemoryDatabase(nameof(Update_given_no_entity_returns_NotFound));
            var context = new SuperheroContext(builder.Options);
            var entity  = new Superhero
            {
                Name     = "Bruce Wayne",
                AlterEgo = "Batman"
            };

            context.Superheroes.Add(entity);
            var entityCity = new City {
                Name = "Metropolis"
            };

            context.Cities.Add(entityCity);
            context.SaveChanges();
            var repository = new SuperheroRepository(context);

            var superhero = new SuperheroUpdateDTO
            {
                Id       = entity.Id,
                Name     = "Clark Kent",
                AlterEgo = "Superman",
                CityName = "Metropolis"
            };

            var response = repository.Update(superhero);

            var updated = context.Superheroes.Find(entity.Id);

            Assert.Equal("Clark Kent", updated.Name);
            Assert.Equal("Superman", updated.AlterEgo);
            Assert.Equal(entityCity.Id, updated.CityId);
        }
Example #4
0
        private static void CompositionObjects()
        {
            //ISuperheroRepository repository = new CSVRepository();
            var decoratedRepository      = new SuperheroRepository();
            var repository               = new CachingRepository(decoratedRepository);
            SuperheroViewModel viewModel = new SuperheroViewModel(repository);

            Application.Current.MainWindow = new SuperheroesViewerWindow(viewModel);
        }
        public SuperheroRepositoryTests()
        {
            _connection = new SqliteConnection("DataSource=:memory:");
            _connection.Open();
            var builder = new DbContextOptionsBuilder <SuperheroContext>().UseSqlite(_connection);

            _context = new SuperheroTestContext(builder.Options);
            _context.Database.EnsureCreated();
            _repository = new SuperheroRepository(_context);
        }
Example #6
0
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            SuperheroRepository repository = new SuperheroRepository();
            SuperheroViewModel  viewModel  = new SuperheroViewModel(repository);

            Application.Current.MainWindow       = new SuperheroesViewerWindow(viewModel);
            Application.Current.MainWindow.Title = "Loose Coupling - Superheroes";
            Application.Current.MainWindow.Show();
        }
Example #7
0
        public void TestSuperHeroCount()
        {
            int expected = 5;

            // TODO: we should use the interface and MOQ.
            var repo = new SuperheroRepository();

            // check to see if we getting 4 heros back
            var results = repo.Get();
            int actual  = results.Count;

            Assert.Equal(expected, actual);
        }
        public SuperheroRepositoryTests()
        {
            // Arrange
            _connection = new SqliteConnection("Filename=:memory:");
            _connection.Open();
            var builder = new DbContextOptionsBuilder <SuperheroContext>().UseSqlite(_connection);

            _context = new SuperheroContext(builder.Options);
            _context.Database.EnsureCreated();
            _context.GenerateTestData();

            _repository = new SuperheroRepository(_context);
        }
        public void Delete_given_existing_id_calls_SaveChanges()
        {
            // Arrange
            var context = new Mock <ISuperheroContext>();

            context.Setup(s => s.Superheroes.Find(42)).Returns(new Superhero());
            var repository = new SuperheroRepository(context.Object);

            // Act
            repository.Delete(42);

            // Assert
            context.Verify(c => c.SaveChanges());
        }
        public void Given_existing_id_when_Delete_it_returns_Deleted()
        {
            var entity = new Superhero();

            var context = new Mock <ISuperheroContext>();

            context.Setup(s => s.Superheroes.Find(42)).Returns(entity);

            var repository = new SuperheroRepository(context.Object);

            var result = repository.Delete(42);

            Assert.Equal(Deleted, result);
        }
        public void Given_existing_id_when_Delete_it_saves_changes()
        {
            var entity = new Superhero();

            var context = new Mock <ISuperheroContext>();

            context.Setup(s => s.Superheroes.Find(42)).Returns(entity);

            var repository = new SuperheroRepository(context.Object);

            repository.Delete(42);

            context.Verify(s => s.SaveChanges());
        }
        public void Delete_given_id_when_exists_returns_Deleted()
        {
            // Arrange
            var context = new Mock <ISuperheroContext>();

            context.Setup(s => s.Superheroes.Find(42)).Returns(new Superhero());
            var repository = new SuperheroRepository(context.Object);

            // Act
            var response = repository.Delete(42);

            // Assert
            Assert.Equal(Deleted, response);
        }
        public void Delete_given_id_when_not_does_not_exist_does_not_call_SaveChanges()
        {
            // Arrange
            var context = new Mock <ISuperheroContext>();

            context.Setup(s => s.Superheroes.Find(42)).Returns(default(Superhero));
            var repository = new SuperheroRepository(context.Object);

            // Act
            repository.Delete(42);

            // Assert
            context.Verify(c => c.SaveChanges(), Times.Never);
        }
        public void Update_given_no_entity_returns_NotFound()
        {
            var builder    = new DbContextOptionsBuilder <SuperheroContext>().UseInMemoryDatabase(nameof(Update_given_no_entity_returns_NotFound));
            var context    = new SuperheroContext(builder.Options);
            var repository = new SuperheroRepository(context);

            var superhero = new SuperheroUpdateDTO {
                Id = 42
            };

            var response = repository.Update(superhero);

            Assert.Equal(NotFound, response);
        }
        public void Delete_given_id_when_not_does_not_exist_returns_NotFound()
        {
            // Arrange
            var context = new Mock <ISuperheroContext>();

            context.Setup(s => s.Superheroes.Find(42)).Returns(default(Superhero));
            var repository = new SuperheroRepository(context.Object);

            // Act
            var response = repository.Delete(42);

            // Assert
            Assert.Equal(NotFound, response);
        }
        public void Delete_given_existing_entity_removes_it()
        {
            // Arrange
            var hero    = new Superhero();
            var context = new Mock <ISuperheroContext>();

            context.Setup(s => s.Superheroes.Find(42)).Returns(hero);
            var repository = new SuperheroRepository(context.Object);

            // Act
            repository.Delete(42);

            // Assert
            context.Verify(c => c.Superheroes.Remove(hero));
        }
Example #17
0
        static void Main(string[] args)
        {
            using var context = new SuperheroContext();
            var repository = new SuperheroRepository(context);

            foreach (var hero in repository.Read())
            {
                Console.WriteLine($"{hero.Id}: {hero.Name} aka {hero.AlterEgo}");
            }

            Console.Write("Enter superhero id: ");
            var id = int.Parse(Console.ReadLine());

            var superhero = repository.Read(id);

            Console.WriteLine($"Name: {superhero.Name}");
            Console.WriteLine($"Alter Ego: {superhero.AlterEgo}");
            Console.WriteLine($"Occupation: {superhero.Occupation}");
            Console.WriteLine($"City: {superhero.CityName}");
            Console.WriteLine($"Gender: {superhero.Gender}");
            Console.WriteLine($"First Appearance: {superhero.FirstAppearance}");
            Console.WriteLine($"Powers: {string.Join(", ", superhero.Powers)}");
        }
Example #18
0
 public SuperheroViewModel()
 {
     _repository = new SuperheroRepository();
 }