Ejemplo n.º 1
0
        public async void UpdateExistingEpisode()
        {
            // Given
            var newhope = await _episodeRepository.Get(4);

            newhope.Title = "Episode4";

            // When
            _episodeRepository.Update(newhope);
            var saved = await _episodeRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var episode = await db.Episodes.FindAsync(4);

                Assert.NotNull(episode);
                Assert.Equal(4, episode.Id);
                Assert.Equal("Episode4", episode.Title);

                // Cleanup
                episode.Title = "NEWHOPE";
                db.Episodes.Update(episode);
                await db.SaveChangesAsync();
            }
        }
Ejemplo n.º 2
0
        public async void DeleteExistingEpisode()
        {
            // Given
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var episode102 = new Episode {
                    Id = 102, Title = "Episode102"
                };
                await db.Episodes.AddAsync(episode102);

                await db.SaveChangesAsync();
            }

            // When
            _episodeRepository.Delete(102);
            var saved = await _episodeRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var deletedEpisode = await db.Episodes.FindAsync(102);

                Assert.Null(deletedEpisode);
            }
        }
Ejemplo n.º 3
0
        public async void AddNewEpisode()
        {
            // Given
            var episode101 = new Episode {
                Id = 101, Title = "Episode101"
            };

            // When
            _episodeRepository.Add(episode101);
            var saved = await _episodeRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var episode = await db.Episodes.FindAsync(101);

                Assert.NotNull(episode);
                Assert.Equal(101, episode.Id);
                Assert.Equal("Episode101", episode.Title);

                // Cleanup
                db.Episodes.Remove(episode);
                await db.SaveChangesAsync();
            }
        }
Ejemplo n.º 4
0
        //many to many relation is retrieved by post materialization
        private void Episode(StarWarsContext db)
        {
            Field <ListGraphType <EpisodeType> >("episode", resolve: context =>
            {
                var dtos = db
                           .Episodes
                           .Select(x => new EpisodeDto()
                {
                    Id    = x.Id,
                    Title = x.Title
                })
                           .ToList();

                var ids = dtos.Select(x => x.Id);

                var chars = db
                            .CharacterEpisodes
                            .Where(x => ids.Contains(x.EpisodeId))
                            .Select(x => new CharacterDto()
                {
                    Id        = x.Character.Id,
                    Name      = x.Character.Name,
                    EpisodeId = x.EpisodeId
                })
                            .ToList();

                foreach (var dto in dtos)
                {
                    dto.Characters = chars.Where(x => x.EpisodeId == dto.Id);
                }

                return(dtos);
            });
        }
 public void BeforeEachTest()
 {
     Connection = new SqliteConnection("DataSource=:memory:");
     Connection.Open();
     Context = CreateContext();
     Context.Database.EnsureCreated();
 }
Ejemplo n.º 6
0
        public async void AddNewHuman()
        {
            // Given
            var human10101 = new Human {
                Id = 10101, Name = "Human10101"
            };

            // When
            _humanRepository.Add(human10101);
            var saved = await _humanRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var human = await db.Humans.FindAsync(10101);

                Assert.NotNull(human);
                Assert.Equal(10101, human.Id);
                Assert.Equal("Human10101", human.Name);

                // Cleanup
                db.Humans.Remove(human);
                await db.SaveChangesAsync();
            }
        }
        /// <summary>
        /// Getting Species by URL
        /// </summary>

        public static Species GetSpecies(string url)
        {
            using (var context = new StarWarsContext())
            {
                return(context.Species.FirstOrDefault(x => x.Url == url));
            }
        }
Ejemplo n.º 8
0
        public async void AddNewDroid()
        {
            // Given
            var droid2101 = new Droid {
                Id = 2101, Name = "Droid2101", PrimaryFunction = "Function2101"
            };

            // When
            _droidRepository.Add(droid2101);
            var saved = await _droidRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var droid = await db.Droids.FindAsync(2101);

                Assert.NotNull(droid);
                Assert.Equal(2101, droid.Id);
                Assert.Equal("Droid2101", droid.Name);

                // Cleanup
                db.Droids.Remove(droid);
                await db.SaveChangesAsync();
            }
        }
Ejemplo n.º 9
0
        public async void UpdateExistingHuman()
        {
            // Given
            var vader = await _humanRepository.Get(1001);

            vader.Name = "Human1001";

            // When
            _humanRepository.Update(vader);
            var saved = await _humanRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var human = await db.Humans.FindAsync(1001);

                Assert.NotNull(human);
                Assert.Equal(1001, human.Id);
                Assert.Equal("Human1001", human.Name);

                // Cleanup
                human.Name = "Darth Vader";
                db.Humans.Update(human);
                await db.SaveChangesAsync();
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Getting Film item by URL
 /// </summary>
 /// <param name="url"></param>
 /// <returns></returns>
 public static Film GetFilm(string url)
 {
     using (var context = new StarWarsContext())
     {
         return(context.Film.FirstOrDefault(x => x.Url == url));
     }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Getting StarShip by URL
 /// </summary>
 public static StarShip GetStarShip(string url)
 {
     using (var context = new StarWarsContext())
     {
         return(context.StarShip.FirstOrDefault(x => x.Url == url));
     }
 }
Ejemplo n.º 12
0
        public async Task TestCreate()
        {
            Character newCharacter = new Character()
            {
                Name     = "Leia Organa",
                Episodes = new string[]
                {
                    "NEWHOPE",
                    "EMPIRE",
                    "JEDI"
                },
                Planet  = "Alderaan",
                Friends = new string[]
                {
                    "Luke Skywalker",
                    "Han Solo",
                    "C-3PO",
                    "R2-D2"
                }
            };

            using (StarWarsContext context = await CreateContext("TestCreate"))
            {
                Repository <Character> repository = new Repository <Character>(context);

                Character result = await repository.Create(newCharacter);

                Assert.AreEqual(3, context.Set <Character>().Count());
                Assert.IsTrue(newCharacter.IsDataEqual(result));
            }
        }
Ejemplo n.º 13
0
        public async void UpdateExistingPlanet()
        {
            // Given
            var alderaan = await _planetRepository.Get(2);

            alderaan.Name = "Planet2";

            // When
            _planetRepository.Update(alderaan);
            var saved = await _planetRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var planet = await db.Planets.FindAsync(2);

                Assert.NotNull(planet);
                Assert.Equal(2, planet.Id);
                Assert.Equal("Planet2", planet.Name);

                // Cleanup
                planet.Name = "Alderaan";
                db.Planets.Update(planet);
                await db.SaveChangesAsync();
            }
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Getting Vehicle by URL
 /// </summary>
 public static Vehicle GetVehicle(string url)
 {
     using (var context = new StarWarsContext())
     {
         return(context.Vehicle.FirstOrDefault(x => x.Url == url));
     }
 }
Ejemplo n.º 15
0
        public async void DeleteExistingPlanet()
        {
            // Given
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var planet102 = new Planet {
                    Id = 102, Name = "Planet102"
                };
                await db.Planets.AddAsync(planet102);

                await db.SaveChangesAsync();
            }

            // When
            _planetRepository.Delete(102);
            var saved = await _planetRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var deletedPlanet = await db.Planets.FindAsync(102);

                Assert.Null(deletedPlanet);
            }
        }
        public async void UpdateExistingCharacter()
        {
            // Given
            var threepio = await _characterRepository.Get(2000);

            threepio.Name = "Character2000";

            // When
            _characterRepository.Update(threepio);
            var saved = await _characterRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var character = await db.Characters.FindAsync(2000);

                Assert.NotNull(character);
                Assert.Equal(2000, character.Id);
                Assert.Equal("Character2000", character.Name);

                // Cleanup
                character.Name = "C-3PO";
                db.Characters.Update(character);
                await db.SaveChangesAsync();
            }
        }
Ejemplo n.º 17
0
        public async void UpdateExistingDroid()
        {
            // Given
            var threepio = await _droidRepository.Get(2000);

            threepio.PrimaryFunction = "Function2000";

            // When
            _droidRepository.Update(threepio);
            var saved = await _droidRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var droid = await db.Droids.FindAsync(2000);

                Assert.NotNull(droid);
                Assert.Equal(2000, droid.Id);
                Assert.Equal("Function2000", droid.PrimaryFunction);

                // Cleanup
                droid.PrimaryFunction = "Protocol";
                db.Droids.Update(droid);
                await db.SaveChangesAsync();
            }
        }
Ejemplo n.º 18
0
        public IntegrationTests()
        {
            var optionsBuilder = new DbContextOptionsBuilder <StarWarsContext>();

            optionsBuilder.UseInMemoryDatabase("InMemory");
            _context = new StarWarsContext(optionsBuilder.Options);
        }
Ejemplo n.º 19
0
        public async void DeleteExistingDroid()
        {
            // Given
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var droid2100 = new Droid {
                    Id = 2100, Name = "Droid2100", PrimaryFunction = "Function2100"
                };
                await db.Droids.AddAsync(droid2100);

                await db.SaveChangesAsync();
            }

            // When
            _droidRepository.Delete(2100);
            var saved = await _droidRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var deletedDroid = await db.Droids.FindAsync(2100);

                Assert.Null(deletedDroid);
            }
        }
Ejemplo n.º 20
0
        public async void DeleteExistingHuman()
        {
            // Given
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var human10102 = new Human {
                    Id = 10102, Name = "Human10102"
                };
                await db.Humans.AddAsync(human10102);

                await db.SaveChangesAsync();
            }

            // When
            _humanRepository.Delete(10102);
            var saved = await _humanRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var deletedHuman = await db.Humans.FindAsync(10101);

                Assert.Null(deletedHuman);
            }
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Getting Planet Item by URL
 /// </summary>
 public static Planet GetPlanet(string url)
 {
     using (var context = new StarWarsContext())
     {
         return(context.Planet.FirstOrDefault(x => x.Url == url));
     }
 }
        public async void AddNewCharacter()
        {
            // Given
            var character3101 = new Character {
                Id = 3101, Name = "Character3101"
            };

            // When
            _characterRepository.Add(character3101);
            var saved = await _characterRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var character = await db.Characters.FindAsync(3101);

                Assert.NotNull(character);
                Assert.Equal(3101, character.Id);
                Assert.Equal("Character3101", character.Name);

                // Cleanup
                db.Characters.Remove(character);
                await db.SaveChangesAsync();
            }
        }
        public async void DeleteExistingCharacter()
        {
            // Given
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var character3100 = new Character {
                    Id = 3100, Name = "Character3100"
                };
                await db.Characters.AddAsync(character3100);

                await db.SaveChangesAsync();
            }

            // When
            _characterRepository.Delete(3100);
            var saved = await _characterRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var deletedCharacter = await db.Characters.FindAsync(3100);

                Assert.Null(deletedCharacter);
            }
        }
Ejemplo n.º 24
0
        void SeedData(StarWarsContext context)
        {
            var character = new[]
            {
                new Character {
                    Id = 1, Name = "Gandalf"
                },
                new Character {
                    Id = 2, Name = "Aragorn"
                },
                new Character {
                    Id = 3, Name = "Legolas"
                },
                new Character {
                    Id = 4, Name = "Biblo Baggins"
                },
                new Character {
                    Id = 5, Name = "Eragorn"
                },
                new Character {
                    Id = 6, Name = "Bron"
                },
            };

            context.Characters.AddRange(character);
            context.SaveChanges();
        }
Ejemplo n.º 25
0
        public async void AddNewPlanet()
        {
            // Given
            var planet101 = new Planet {
                Id = 101, Name = "Planet101"
            };

            // When
            _planetRepository.Add(planet101);
            var saved = await _planetRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new StarWarsContext(_options, _dbLogger.Object))
            {
                var planet = await db.Planets.FindAsync(101);

                Assert.NotNull(planet);
                Assert.Equal(101, planet.Id);
                Assert.Equal("Planet101", planet.Name);

                // Cleanup
                db.Planets.Remove(planet);
                await db.SaveChangesAsync();
            }
        }
Ejemplo n.º 26
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory loggerFactory, StarWarsContext starWarsContext)
        {
            loggerFactory.AddNLog();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync(
                            "Unexcepted fault happend. Try again later.");
                    });
                });
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            AutoMapper.Mapper.Initialize(config =>
            {
                //config.CreateMap<Author, AuthorDto>()
                //.ForMember(dest => dest.Name, opt => opt.MapFrom(src =>
                //$"{src.LastName} {src.FirstName}"))
                //.ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
                //src.DateOfBirth.GetCurrentAge()));

                config.CreateMap <Character, CharacterDto>();
                //.ForMember(dest => dest.Friends, opt => opt.MapFrom(src =>
                //new List<string>(src.Episodes.Select(e => e.Name))));

                config.CreateMap <Character, CharacterForCreationDto>();
                config.CreateMap <CharacterForCreationDto, Character>();
                config.CreateMap <CharacterForUpdateDto, Character>();
                config.CreateMap <CharacterForUpdateDto, CharacterDto>();
                config.CreateMap <Character, CharacterForUpdateDto>();

                config.CreateMap <EpisodeDto, Episode>();
                config.CreateMap <Episode, EpisodeDto>();

                config.CreateMap <EpisodeForCreationDto, Episode>();
                config.CreateMap <EpisodeForUpdateDto, Episode>();
            });

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "StarWars.API V1");
            });

            starWarsContext.StartWithFreshData();
            app.UseHttpsRedirection();
            app.UseMvc();
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Adding new Starship items
 /// </summary>
 public static void Insert(List <StarShip> entities)
 {
     using (var context = new StarWarsContext())
     {
         context.StarShip.AddRange(entities);
         context.SaveChanges();
     }
 }
Ejemplo n.º 28
0
        public static void Main(string[] args)
        {
            using (var db = new StarWarsContext())
            {
                // insert some testing data into database
                if (db.Humans.Count() == 0)
                {
                    InsertData(db);
                }

                var container = new UnityContainer();

                container.RegisterInstance(db);

                container.RegisterType <StarWarsQuery>();
                container.RegisterType <DroidType>();
                container.RegisterType <HumanType>();

                var schema = new StarWarsSchema((t) => container.Resolve(t) as GraphType);

                var query = @"
                    query AllHumansQuery {
                        humans {
                            name
                            friends {
                                id
                                name
                                ...on Droid {
                                    primaryFunction
                                }
                            }
                        }
                    }";

                Console.WriteLine("Run AllHumansQuery");

                var result = Execute(schema, null, query);

                Console.WriteLine(result.Result);
                Console.WriteLine();

                query = @"
                    query AllCharactersQuery {
                        characters {
                            name
                            friends {
                                name
                            }
                        }
                    }";

                Console.WriteLine("Run AllCharactersQuery");

                result = Execute(schema, null, query);

                Console.WriteLine(result.Result);
            }
        }
Ejemplo n.º 29
0
 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
 public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                       ILoggerFactory loggerFactory, StarWarsContext db)
 {
     if (env.IsDevelopment())
     {
         app.UseDeveloperExceptionPage();
     }
     app.UseMvc();
 }
        private void RemoveCharacterByName(StarWarsContext context, string name)
        {
            var character = context.Character.FirstOrDefault(c => c.Name == name);

            if (character != null)
            {
                context.Remove(character);
            }
        }