Example #1
0
def test_incorrect_target_domain():
    with pytest.raises(ValueError, match=INCORRECT_TARGET_DOMAIN_ERROR) as excinfo:
        build_statistics(data=[],
                         shop_domain="shop.ru",
                         finish_url="http://shop.ru/checkout",
                         target_domain="",
                         another_domains=[])
Example #2
0
def test_incorrect_mix_shop_and_another_domains(shop, another):
    with pytest.raises(ValueError, match=MIX_ANOTHER_AND_SHOP_DOMAINS_ERROR) as excinfo:
        build_statistics(data=[],
                         shop_domain=shop,
                         finish_url="http://shop.ru/checkout",
                         target_domain="our.com",
                         another_domains=another)
Example #3
0
def test_incorrect_finish_url():
    with pytest.raises(ValueError, match=INCORRECT_FINISH_URL_ERROR) as excinfo:
        build_statistics(data=[],
                         shop_domain="shop.ru",
                         finish_url="",
                         target_domain="our.com",
                         another_domains=[])
Example #4
0
def test_incorrect_mix_shop_and_target_domains(shop, target):
    with pytest.raises(ValueError, match=MIX_TARGET_AND_SHOP_DOMAINS_ERROR) as excinfo:
        build_statistics(data=[],
                         shop_domain=shop,
                         finish_url="http://shop.ru/checkout",
                         target_domain=target,
                         another_domains=["their1.com", ""])
Example #5
0
def test_incorrect_mix_target_and_another_domains(target, another):
    with pytest.raises(ValueError, match=MIX_TARGET_AND_ANOTHER_DOMAINS_ERROR) as excinfo:
        build_statistics(data=[],
                         shop_domain="shop.ru",
                         finish_url="http://shop.ru/checkout",
                         target_domain=target,
                         another_domains=another)
Example #6
0
def test_incorrect_sorted_data():
    logs = [
        Log(client_id="user1", location="http://shop.ru", referer="http://shop.ru", request_datetime=datetime(2018, 1, 2, 20, 3, 4)),
        Log(client_id="user1", location="http://shop.ru", referer="http://shop.ru", request_datetime=datetime(2018, 1, 1, 20, 3, 4))
    ]

    with pytest.raises(ValueError, match=INCORRECT_SORTED_ERROR) as excinfo:
        build_statistics(data=logs,
                         shop_domain="shop.ru",
                         finish_url="http://shop.ru/checkout",
                         target_domain="our.com",
                         another_domains=[])
Example #7
0
def test_processor(filename):
    input_data = {}

    with open(filename) as json_file:
        input_data = json.load(json_file)

    # build available
    available = []

    for result_item in input_data['result']:
        item = StatItem(result_item['url'])
        item.bill_count = result_item['count']
        available.append(item)

    available = sorted(available, key=lambda x: x.url)

    # build input data
    logs = []

    for result_item in input_data['logs']:
        logs.append(Log(client_id=result_item['client_id'],
                        location=result_item['location'],
                        referer=result_item['referer'],
                        request_datetime=datetime.strptime(result_item['datetime'], '%Y-%m-%dT%H:%M:%S.%f')))

    result = build_statistics(data=logs,
                              shop_domain=input_data['shop_domain'],
                              finish_url=input_data['finish_url'],
                              target_domain=input_data['target_domain'],
                              another_domains=input_data['another_domains'])

    result = sorted(result, key=lambda x: x.url)
    assert result == available
Example #8
0
                        nargs='?',
                        required=False,
                        default="shop.com",
                        help='Shop domain')  # shop.com
    parser.add_argument('--another_domains',
                        nargs='+',
                        required=False,
                        default=["theirs1.com", "theirs2.com"],
                        help='Our competitors partner domains')
    parser.add_argument('--finish_url',
                        nargs='?',
                        required=False,
                        default="https://shop.com/checkout",
                        help='URL where user trasmits after purchase')
    parser.add_argument('--ignore-errors',
                        required=False,
                        default=False,
                        action="store_true",
                        help='Ignore validation errors')

    args = parser.parse_args()

    # load data
    loader = FileLoader(args.filename, args.ignore_errors)
    data = loader.load()
    data = sorted(data, key=lambda x: x.request_datetime)
    # process data
    stat = build_statistics(data, args.shop_domain, args.finish_url,
                            args.target_domain, args.another_domains)
    print("\n".join(map(str, stat)))