Пример #1
0
        public async void ItemsController_GetSelectedItemDataAsync()
        {
            context    = GetInMemoryContext();
            controller = new ItemsController(context);
            Item newItem = new Item()
            {
                Id           = 1,
                Title        = "First",
                About        = "About",
                CurrentPrice = 222.22m,
                Image        = "image/href"
            };
            await context.AddAsync(newItem);

            await context.SaveChangesAsync();

            OkObjectResult itemRes = await controller.Details(newItem.Id) as OkObjectResult;

            Assert.NotNull(itemRes);
            Item item = itemRes.Value as Item;

            Assert.Equal(newItem.Title, item.Title);
            Assert.Equal(newItem.Image, item.Image);
            Assert.Equal(newItem.About, item.About);
            Assert.Equal(newItem.CurrentPrice, item.CurrentPrice);
            Assert.Equal(newItem.PriceHistory.Count, item.PriceHistory.Count);
        }
Пример #2
0
        public async void ItemsController_GetEmptyDataAsync()
        {
            context    = GetInMemoryContext();
            controller = new ItemsController(context);
            var items = await controller.Index();

            Assert.Empty(items);
        }
Пример #3
0
        public async void ItemsController_ParseDataNotEmptyAsync()
        {
            context    = GetInMemoryContext();
            controller = new ItemsController(context);

            var items = await controller.Parse();

            Assert.NotNull(items);
        }
Пример #4
0
        public async void ItemsController_ThrowNotFoundExceptionItemsEmptyAsync()
        {
            context    = GetInMemoryContext();
            controller = new ItemsController(context);

            NotFoundResult itemRes = await controller.Details(1) as NotFoundResult;

            Assert.NotNull(itemRes);
        }
Пример #5
0
        public async void ItemsController_ParseDataLength_BiggerThan20_Async()
        {
            context    = GetInMemoryContext();
            controller = new ItemsController(context);

            var items = await controller.Parse() as List <Item>;

            bool isBigger = items.Count > 20;

            Assert.True(isBigger);
        }
Пример #6
0
        private ShopAppContext GetInMemoryContext()
        {
            DbContextOptions <ShopAppContext> options;
            var builder = new DbContextOptionsBuilder <ShopAppContext>();

            builder.UseInMemoryDatabase("ShopApp");
            options = builder.Options;
            ShopAppContext shopDataContext = new ShopAppContext(options);

            shopDataContext.Database.EnsureDeleted();
            shopDataContext.Database.EnsureCreated();
            return(shopDataContext);
        }
Пример #7
0
        /// <summary>
        /// Adding parsed items in context or update them with new price
        /// </summary>
        /// <param name="context"> context from ItemsController </param>
        /// <param name="titles"> all items titles </param>
        /// <param name="abouts"> all items about infos </param>
        /// <param name="images"> all items image hrefs </param>
        /// <param name="prices"> all item prices </param>
        /// <returns></returns>
        private async Task HandleParsedDataAsync(ShopAppContext context,
                                                 List <HtmlNode> titles,
                                                 List <HtmlNode> abouts,
                                                 List <HtmlNode> images,
                                                 List <HtmlNode> prices)
        {
            IEnumerable <Item> items    = context.Items;
            List <Item>        newItems = new List <Item>();

            for (var i = 0; i < titles.Count(); i++)
            {
                var  imageHref = images[i].Attributes["src"].Value;
                Item newItem   = new Item()
                {
                    Title = titles[i].InnerText,
                    About = abouts[i].InnerText,
                    Image = $"https://hotline.ua{imageHref}"
                };
                decimal price;
                string  replacedPrice = prices[i].InnerText.Replace("&nbsp;", "");
                decimal.TryParse(replacedPrice, out price);
                newItem.CurrentPrice = price;
                Item oldItem = null;
                if (items.Any())
                {
                    oldItem = items.FirstOrDefault(item => item.Title == newItem.Title);
                }

                if (!(oldItem is null))
                {
                    if (oldItem.CurrentPrice != price)
                    {
                        var priceInHistory = new PriceInfo()
                        {
                            Price = price,
                            Date  = DateTime.Now,
                            Item  = oldItem
                        };
                        oldItem.PriceHistory.Add(priceInHistory);
                        oldItem.Image        = newItem.Image;
                        oldItem.About        = newItem.About;
                        oldItem.CurrentPrice = price;

                        context.Items.Update(oldItem);
                        await context.SaveChangesAsync();
                    }
                }
Пример #8
0
        public async void ItemsController_GetNotEmptyDataAsync()
        {
            context    = GetInMemoryContext();
            controller = new ItemsController(context);
            Item newItem = new Item()
            {
                Id           = 1,
                Title        = "First",
                About        = "About",
                CurrentPrice = 222.22m,
                Image        = "image/href"
            };
            await context.AddAsync(newItem);

            await context.SaveChangesAsync();

            var items = await controller.Index();

            Assert.NotEmpty(items);
        }
Пример #9
0
        public async void ItemsController_ThrowNotFoundExceptionItemNullIdAsync()
        {
            context    = GetInMemoryContext();
            controller = new ItemsController(context);
            Item newItem = new Item()
            {
                Id           = 1,
                Title        = "First",
                About        = "About",
                CurrentPrice = 222.22m,
                Image        = "image/href"
            };
            await context.AddAsync(newItem);

            await context.SaveChangesAsync();

            NotFoundResult itemRes = await controller.Details(null) as NotFoundResult;

            Assert.NotNull(itemRes);
        }
Пример #10
0
        /// <summary>
        /// Parsing html using HtmlAgilityPack and updating given context
        /// </summary>
        /// <param name="context"> context of current data from ItemsController </param>
        /// <returns></returns>
        public async Task Parse(ShopAppContext context)
        {
            HttpClient          hc     = new HttpClient();
            HttpResponseMessage result = await hc.GetAsync("https://hotline.ua/mobile/umnye-chasy-smartwatch/");

            Stream stream = await result.Content.ReadAsStreamAsync();

            HtmlDocument doc = new HtmlDocument();

            doc.Load(stream);



            var titles = doc.DocumentNode.SelectNodes("//div[@class='item-info']/p/a").ToList();
            var abouts = doc.DocumentNode.SelectNodes("//div[@class='text']").ToList();
            var images = doc.DocumentNode.SelectNodes("//div[@class='item-img']/a/img").ToList();
            var prices = doc.DocumentNode.SelectNodes("//div[@class='price-md']/span").ToList();



            await HandleParsedDataAsync(context, titles, abouts, images, prices);
        }
Пример #11
0
 public BaseController(ShopAppContext context)
 {
     _context = context;
 }
Пример #12
0
 public ItemsController(ShopAppContext context)
 {
     _context = context;
 }
 public SeafreshCartController(ShopAppContext context)
 {
     _context = context;
 }
 public ProductController(ILogger <ProductController> logger, ShopAppContext context)
     : base(context)
 {
     _logger  = logger;
     _context = context;
 }
Пример #15
0
 public UserController(ShopAppContext shopAppContext)
     : base(shopAppContext)
 {
     _shopAppContext = shopAppContext;
 }
Пример #16
0
 public NotebooksController(ShopAppContext context)
 {
     _context = context;
 }
Пример #17
0
 public CartController(ShopAppContext context)
     : base(context)
 {
     _context = context;
 }
Пример #18
0
 public HomeController(ILogger <HomeController> logger, ShopAppContext context)
     : base(context)
 {
     _logger  = logger;
     _context = context;
 }
 public SeafreshHomeController(ShopAppContext context)
 {
     _context = context;
 }
Пример #20
0
 public HtmlParserTests()
 {
     parser  = new HtmlParser();
     context = GetInMemoryContext();
 }