Пример #1
0
        public void EngineFactory_CreateSiteEngine()
        {
            var factory = new EngineFactory();

            // tests for each handled site
            Assert.AreNotEqual(null, factory.CreateSiteEngine <ISearchLinkParserEngine>(WebstoreSite.Amazon));
            Assert.AreNotEqual(null, factory.CreateSiteEngine <IProductParserEngine>(WebstoreSite.Amazon));
            Assert.AreNotEqual(null, factory.CreateSiteEngine <ISearchLinkParserEngine>(WebstoreSite.AMain));
            Assert.AreNotEqual(null, factory.CreateSiteEngine <IProductParserEngine>(WebstoreSite.AMain));

            // tests for bad parameters

            // the following will throw an argument exception due to bad interface
            try
            {
                Assert.AreEqual(null, factory.CreateSiteEngine <IFormatProvider>(WebstoreSite.Amazon));
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("This interface is not supported by this factory", ex.Message);
            }
            catch (Exception)
            {
                Assert.Fail("unexpected exception");
            }
        }
Пример #2
0
        public void AMainSearchLinkParserEngine_GetSearchUrl()
        {
            var factory = new EngineFactory();
            var engine  = factory.CreateSiteEngine <ISearchLinkParserEngine>(WebstoreSite.AMain);

            Assert.AreEqual(@"http://www.amain.com/search?cID=&s=BAC00691", engine.GetSearchUrl("Bachmann", "00691", "BAC00691"));
        }
Пример #3
0
        public void AmazonSearchLinkParserEngine_GetSearchUrl()
        {
            var factory = new EngineFactory();
            var engine  = factory.CreateSiteEngine <ISearchLinkParserEngine>(WebstoreSite.Amazon);

            Assert.AreEqual(@"http://www.amazon.com/gp/search/ref=sr_adv_toys/?search-alias=toys-and-games&unfiltered=1&field-keywords=00691&field-brand=Bachmann&node=&field-price=&field-age_range=&sort=relevancerank&Adv-Srch-Toys-Submit.x=23&Adv-Srch-Toys-Submit.y=8", engine.GetSearchUrl("Bachmann", "00691", "BAC00691"));
        }
Пример #4
0
        public void AMainSearchLinkParserEngine_GetProductUrls()
        {
            var factory = new EngineFactory();
            var engine  = factory.CreateSiteEngine <ISearchLinkParserEngine>(WebstoreSite.AMain);

            Assert.IsTrue(File.Exists("AMainSingleSearchResult.txt"));

            var urls = engine.GetProductUrls(File.ReadAllText("AMainSingleSearchResult.txt"));

            Assert.AreEqual(1, urls.Length);
            Assert.AreEqual(@"http://www.amain.com/bachmann-ho-scale-thoroughbred-train-set-norfolk-southern/p47395", urls[0]);
        }
Пример #5
0
        //TODO: make this class private and implement an interface while still supporting unit testing and mocking
        public Product[] GetProductPricingUsingSearch(string brand, string productCode, string industryCode, bool useProxy, string connString)
        {
            // TODO: Need to refactor this so it is not so repetitive. Possibly pass in references to the engines and make the workflow here generic

            // TODO: Once we have searched and identified a product page url we should not need to search for subsequent price refreshes.
            // TODO: Consider breaking these calls out into discrete site-specific calls.
            var products = new List <Product>();

            // Get the amazon product pricing
            var pageEngine        = EngineFactory.CreateEngine <IWebPageEngine>();
            var amznSearchParser  = EngineFactory.CreateSiteEngine <ISearchLinkParserEngine>(WebstoreSite.Amazon);
            var amznProductParser = EngineFactory.CreateSiteEngine <IProductParserEngine>(WebstoreSite.Amazon);
            var productAccessor   = AccessorFactory.CreateAccessor <IProductAccessor>();

            string[] urls = amznSearchParser.GetProductUrls(pageEngine.GetWebPageContents(amznSearchParser.GetSearchUrl(brand, productCode, industryCode), useProxy));
            // NOTE: for Amazon we are assuming that if more than 1 result returned then exact match not found so skip
            if (urls.Length > 0)
            {
                // Assume the first product in the result is the product in question
                // TODO: make this a little more robust in handling multiple search results coming back
                var product = amznProductParser.GetProductInfo(pageEngine.GetWebPageContents(urls[0], useProxy), brand, productCode, industryCode);
                if (product != null)
                {
                    product.ProductUrl  = urls[0];
                    product.ProductCode = industryCode;
                    product.Brand       = brand;
                    products.Add(product);
                    productAccessor.Save(connString, product);
                }
            }

            // Get the amain product pricing
            var amainSearchParser  = EngineFactory.CreateSiteEngine <ISearchLinkParserEngine>(WebstoreSite.AMain);
            var amainProductParser = EngineFactory.CreateSiteEngine <IProductParserEngine>(WebstoreSite.AMain);

            urls = amainSearchParser.GetProductUrls(pageEngine.GetWebPageContents(amainSearchParser.GetSearchUrl(brand, productCode, industryCode), useProxy));
            if (urls.Length > 0)
            {
                // Assume the first product in the result is the product in question
                // TODO: make this a little more robust in handling multiple search results coming back
                var product = amainProductParser.GetProductInfo(pageEngine.GetWebPageContents(urls[0], useProxy), brand, productCode, industryCode);
                if (product != null)
                {
                    product.ProductUrl  = urls[0];
                    product.ProductCode = industryCode;
                    product.Brand       = brand;
                    products.Add(product);
                    productAccessor.Save(connString, product);
                }
            }

            return(products.ToArray());
        }
Пример #6
0
        public void AmazonSearchLinkParserEngine_GetProductUrls()
        {
            var factory = new EngineFactory();
            var engine  = factory.CreateSiteEngine <ISearchLinkParserEngine>(WebstoreSite.Amazon);

            Assert.IsTrue(File.Exists("AmazonSearchResults.txt"));

            var urls = engine.GetProductUrls(File.ReadAllText("AmazonSearchResults.txt"));

            Assert.AreEqual(24, urls.Length);
            Assert.AreEqual(@"http://www.amazon.com/gp/product/B001RG7LDU/", urls[0]);
            Assert.AreEqual(@"http://www.amazon.com/gp/product/B0000CGB0P/", urls[23]);
        }
Пример #7
0
        public void AMainProductParserEngine_GetProductInfo()
        {
            var factory = new EngineFactory();
            var engine  = factory.CreateSiteEngine <IProductParserEngine>(WebstoreSite.AMain);

            Assert.IsTrue(File.Exists("AMainProductPage.txt"));

            var product = engine.GetProductInfo(File.ReadAllText("AMainProductPage.txt"), "Bachmann", "00691", "BAC00691");

            // TODO: Add tests for the other attributes parsed from the page
            Assert.AreEqual("$82.99", product.Price);
            Assert.AreEqual("Bachmann HO-Scale Thoroughbred Train Set (Norfolk Southern)", product.ProductName);
            Assert.AreEqual("AMain", product.Site);
        }
Пример #8
0
        public void AmazonProductParserEngine_GetProductInfo()
        {
            var factory = new EngineFactory();
            var engine  = factory.CreateSiteEngine <IProductParserEngine>(WebstoreSite.Amazon);

            Assert.IsTrue(File.Exists("AmazonProductPage.txt"));

            var product = engine.GetProductInfo(File.ReadAllText("AmazonProductPage.txt"), "Bachmann", "00691", "BAC00691");

            // TODO: Add tests for the other attributes parsed from the page
            Assert.AreEqual("$64.50", product.Price);
            Assert.AreEqual("Bachmann Trains Thoroughbred Ready-to-Run HO Scale Train Set", product.ProductName);
            Assert.AreEqual("Amazon", product.Site);
        }