Esempio n. 1
0
        public void AddAuthor_AuthorWithoutCountryId_AuthorHasBEAsCountryId()
        {
            // Arrange
            var connectionStringBuilder = new SqliteConnectionStringBuilder {
                DataSource = ":memory:"
            };

            var connection = new SqliteConnection(connectionStringBuilder.ToString());

            var loggerProvider = new LogToActionLoggerProvider((log) => { _output.WriteLine(log); });

            var options = new DbContextOptionsBuilder <CourseContext>()
                          .UseLoggerFactory
                          (
                new LoggerFactory(new[] { loggerProvider }))
                          .UseSqlite(connection)
                          .Options;

            using (var context = new CourseContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();

                context.Countries.Add(new Entities.Country()
                {
                    Id          = "BE",
                    Description = "Belgium"
                });

                context.SaveChanges();
            }

            using (var context = new CourseContext(options))
            {
                var authorRepository = new AuthorRepository(context);
                var authorToAdd      = new Author()
                {
                    FirstName = "Kevin",
                    LastName  = "Dockx",
                    Id        = Guid.Parse("d84d3d7e-3fbc-4956-84a5-5c57c2d86d7b")
                };

                // Act
                authorRepository.AddAuthor(authorToAdd);
                authorRepository.SaveChanges();
            }

            using (var context = new CourseContext(options))
            {
                // Assert
                var authorRepository = new AuthorRepository(context);

                var addedAuthor = authorRepository.GetAuthor(
                    Guid.Parse("d84d3d7e-3fbc-4956-84a5-5c57c2d86d7b"));
                Assert.Equal("BE", addedAuthor.CountryId);
            }
        }
Esempio n. 2
0
        public void AddAuthor_AuthorWithoutCountryId_AuthorHasBEAsCountryId()
        {
            // ARRANGE
            var logger        = new LogToActionLoggerProvider((log) => _ouput.WriteLine(log));
            var loggerFactory = new LoggerFactory(new[] { logger });

            var connectionStringBuilder = new SqliteConnectionStringBuilder {
                DataSource = ":memory:"
            };
            var connection = new SqliteConnection(connectionStringBuilder.ToString());
            var options    = new DbContextOptionsBuilder <CourseContext>()
                             .UseLoggerFactory(loggerFactory)
                             .UseSqlite(connection)
                             .Options;

            using (var ctx = new CourseContext(options))
            {
                ctx.Database.OpenConnection();
                ctx.Database.EnsureCreated();

                ICollection <Country> countriesLst = new List <Country>
                {
                    new Country {
                        Id = "BE", Description = "Belgium"
                    }
                };

                ctx.Countries.AddRange(countriesLst);

                ctx.SaveChanges();

                var authorRepository = new AuthorRepository(ctx);

                var authorToAdd = new Author
                {
                    FirstName = "adonis",
                    LastName  = "cruz v",
                    Id        = Guid.Parse("ec622423-d92b-430b-a4b9-b14caafdda6c")
                };

                // ACT
                authorRepository.AddAuthor(authorToAdd);
                authorRepository.SaveChanges();
            }

            using (var ctx = new CourseContext(options))
            {
                ctx.Database.OpenConnection();
                ctx.Database.EnsureCreated();

                // ASSERT
                var authorRepository = new AuthorRepository(ctx);
                var addedAuthor      = authorRepository.GetAuthor(Guid.Parse("ec622423-d92b-430b-a4b9-b14caafdda6c"));
                Assert.Equal("BE", addedAuthor.CountryId);
            }
        }