Exemplo n.º 1
0
    def test_should_query_webdriver(self):
        import os
        # import pandas as pd
        from products import Product
        ProductDatabase.clear_database()
        filename = 'test.json'

        extractor = PageExtractor('submarino')
        products_list = extractor.query_webdriver("goblet of fire")
        products = [Product(item_attrs) for item_attrs in products_list]
        extractor.store_products_on_json(products, filename)
        # import pdb; pdb.set_trace()
        filtered_products = ProductDatabase.filter(price__gte=700,
                                                   price__lt=1000)
        extractor.store_products_on_json(filtered_products,
                                         'test_filtered.json')

        # df = pd.DataFrame({
        #     'name': [prod.name for prod in filtered_products],
        #     'price': [prod.price for prod in filtered_products]
        # })

        assert os.path.exists(filename) == True
        # We cant check it like this since the add_product on ProductDatabase checks whether the product was already inserted
        # assert len(products_list) == ProductDatabase.get_products_total()
        assert ProductDatabase.get_products_total() != 0
 def test_should_populate_products_database_from_json_and_return_list(self):
     ProductDatabase.clear_database()
     # import pdb; pdb.set_trace()
     previous_total = ProductDatabase.get_products_total()
     products_list = populate_products_database_from_json_and_return_list()
     assert ProductDatabase.get_products_total(
     ) == previous_total + len(products_list)
     assert type(products_list[0]) == Product
 def test_should_get_info_list_about_products(self):
     parsed_html = ParsedPage.parsed_html
     previous_total = ProductDatabase.get_products_total()
     products = get_info_list_about_products(parsed_html)
     # import pdb; pdb.set_trace()
     assert type(products) == list
     assert ProductDatabase.get_products_total(
     ) == len(products) + previous_total
Exemplo n.º 4
0
    def test_should_retrieve_products_from_query(self, store, query):
        ProductDatabase.clear_database()

        extractor = PageExtractor(store)
        products_list = extractor.retrieve_products_from_query(query)

        assert type(products_list) == list
        # Cannot check for equal because there might be items repited which were not inserted in the database
        assert len(products_list) >= ProductDatabase.get_products_total()
Exemplo n.º 5
0
    def test_should_store_products_on_json(self, store, query):
        import os
        ProductDatabase.clear_database()
        filename = 'test.json'

        extractor = PageExtractor(store)
        products_list = extractor.retrieve_products_from_query(query)
        extractor.store_products_on_json(products_list, filename)

        # import pdb; pdb.set_trace()
        assert os.path.exists(filename) == True
        # Cannot check for equal because there might be items repited which were not inserted in the database
        assert len(products_list) >= ProductDatabase.get_products_total()
Exemplo n.º 6
0
 def test_should_query_for(self):
     retriever = DataRetriever(['submarino'])
     products_dicts = retriever.query_for('iphone')
     # import pdb; pdb.set_trace()
     products = [Product(item_attrs) for item_attrs in products_dicts]
     DataRetriever.store_products_on_json(products, 'test.json')
     # import pdb; pdb.set_trace()
     filtered_products = ProductDatabase.filter(price__gte=3000,
                                                price__lt=4000)
     DataRetriever.store_products_on_json(filtered_products,
                                          'test_filtered.json')
     assert type(products_dicts) == list
     assert type(products_dicts[0]) == dict
Exemplo n.º 7
0
def render_main(request):
    from .services import retrieve_products_from_json

    products = retrieve_products_from_json('search_products.json')

    query_keys_list = [*request.GET]
    filtered_products = products
    # If it comes with params then we need to filter before sending response

    if len(query_keys_list) > 0:
        parsed_query = {}
        for key, value in request.GET.items():
            if 'order_by_' not in key:
                parsed_query[key] = float(
                    value) if 'contains' not in key else value
                attribute = None
                order = None
            else:
                # If there is an order_by params we get the attribute its sorting and order
                attribute = key.split('order_by_')[1]
                order = value

        filtered_products = ProductDatabase.filter(**parsed_query)
        print(filtered_products)
        # If there is an order_by params we get the attribute its sorting and order
        if attribute and order:
            reversed = order == 'desc'
            filtered_products.sort(key=lambda prod: getattr(prod, attribute),
                                   reverse=reversed)
        print(filtered_products)

    context = {
        'products': filtered_products,
        'products_total': len(filtered_products)
    }
    return render(request, 'finder/main.html', context)
Exemplo n.º 8
0
def filter_and_save_products_on_json(**kwargs_query):
    ''' Filter the ProdcutDatabase with the query kwargs passed and save it as JSON in filtered_products.json '''
    filtered_products = ProductDatabase.filter(**kwargs_query)
    print(filtered_products)
    store_products_on_json(filtered_products, 'filtered_products.json')
Exemplo n.º 9
0
 def test_should_get_products_from_json(self):
     ProductDatabase.clear_database()
     products = DataRetriever.get_products_from_json('test.json')
     assert len(products) == len(ProductDatabase.products)
     assert products[0] == ProductDatabase.products[0]