示例#1
0
        public void Add_sale()
        {
            var conn = new SqliteConnection("DataSource=:memory:");

            conn.Open();

            try
            {
                var options = new DbContextOptionsBuilder <DatabaseContext>()
                              .UseSqlite(conn)
                              .Options;

                using (var context = new DatabaseContext(options))
                {
                    context.Database.EnsureCreated();
                }

                using (var context = new DatabaseContext(options))
                {
                    CashbackService cashbackService = new CashbackService(new CashbackRepository(context));
                    cashbackService.InitializeCashbackDatabase();

                    AlbumService albumService = new AlbumService(new AlbumRepository(context));

                    DataMock
                    .GenerateAlbums()
                    .ToList()
                    .ForEach(album => albumService.Add(album));

                    var albums = albumService.GetPaged(1, 5).ToList();

                    List <AlbumDTO> dtos = new List <AlbumDTO>();

                    albums.ForEach(x => dtos.Add(x.ConvertTo(typeof(AlbumDTO))));

                    SaleService saleService = new SaleService(new SaleRepository(context), cashbackService, albumService);

                    int insertedId = saleService.RegisterSale(dtos);

                    Assert.Equal(1, insertedId);
                }
            }
            finally
            {
                conn.Close();
            }
        }
        public void Get_albums_through_pagination()
        {
            var conn = new SqliteConnection("DataSource=:memory:");

            conn.Open();

            try
            {
                var options = new DbContextOptionsBuilder <DatabaseContext>()
                              .UseSqlite(conn)
                              .Options;

                using (var context = new DatabaseContext(options))
                {
                    context.Database.EnsureCreated();
                }

                using (var context = new DatabaseContext(options))
                {
                    var repository = new AlbumRepository(context);

                    DataMock
                    .GenerateAlbums()
                    .ToList()
                    .ForEach(album => repository.Insert(album));
                }

                using (var context = new DatabaseContext(options))
                {
                    AlbumService service = new AlbumService(new AlbumRepository(context));
                    Assert.Equal(10, service.GetPaged(2, 10, "rock").Count());
                    Assert.Equal(10, service.GetPaged(2, 10).Count());
                }
            }
            finally
            {
                conn.Close();
            }
        }
        public void Get_albums_through_id(int id, int no_id)
        {
            var conn = new SqliteConnection("DataSource=:memory:");

            conn.Open();

            try
            {
                var options = new DbContextOptionsBuilder <DatabaseContext>()
                              .UseSqlite(conn)
                              .Options;

                using (var context = new DatabaseContext(options))
                {
                    context.Database.EnsureCreated();
                }

                using (var context = new DatabaseContext(options))
                {
                    var repository = new AlbumRepository(context);

                    DataMock
                    .GenerateAlbums()
                    .ToList()
                    .ForEach(album => repository.Insert(album));
                }

                using (var context = new DatabaseContext(options))
                {
                    AlbumService service = new AlbumService(new AlbumRepository(context));
                    Assert.NotNull(service.FindById(id));
                    Assert.Null(service.FindById(no_id));
                }
            }
            finally
            {
                conn.Close();
            }
        }