# Path: collectors/hra/collector.py
# def collect(conf, conn, date_from=None, date_to=None):
# 
#     # Start collector
#     date_from = _get_date_from(conn, date_from)
#     date_to = _get_date_to(conn, date_to)
#     base.helpers.start(conf, 'hra', {'date_from': date_from, 'date_to': date_to})
# 
#     # Get parameters
#     URL = conf['HRA_URL']
#     USER = conf['HRA_USER']
#     PASS = conf['HRA_PASS']
# 
#     count = 0
#     chunk_days = 100
#     session = requests.Session()
#     loop_date_from = date_from
#     while True:
#         if loop_date_from > date_to:
#             break
#         loop_date_to = min(loop_date_from + datetime.timedelta(days=chunk_days), date_to)
#         url = _make_request_url(URL, loop_date_from, loop_date_to)
#         response = session.get(url, auth=(USER, PASS))
#         response.raise_for_status()
#         base.config.SENTRY.extra_context({
#             'url': response.url,
#         })
#         for item in response.json():
#             record = parse_record(response.url, item)
#             if not record:
#                 continue
#             record.write(conf, conn)
#             count += 1
#             if not count % 100:
#                 logger.info('Collected "%s" hra records', count)
#         loop_date_from = loop_date_to + datetime.timedelta(days=1)
#         time.sleep(1)
# 
#     # Stop collector
#     base.helpers.stop(conf, 'hra', {'collected': count})
# 
# def _make_request_url(prefix, date_from, date_to):
#     query = OrderedDict()
#     query['datePublishedFrom'] = date_from.strftime('%Y-%m-%d')
#     query['datePublishedTo'] = date_to.strftime('%Y-%m-%d')
#     url = '%s?%s' % (prefix, urlencode(query))
#     return url


import mock
import pytest
import datetime
import requests
from collections import defaultdict
from collectors.hra.collector import collect, _make_request_url
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals



class TestHRACollector(object):
    def test_make_request_url(self):
        date_from = datetime.date(2015, 1, 1)
        date_to = datetime.date(2015, 12, 31)
        actual = _make_request_url('prefix', date_from, date_to)
        expect = 'prefix?datePublishedFrom=2015-01-01&datePublishedTo=2015-12-31'
        assert actual == expect

    @mock.patch('requests.Session.get')
    def test_collect_skips_deffered_records(self, session_get_mock, conn, conf, deferred_item_stub):
        response_mock = mock.Mock()
        response_mock.json.return_value = [
            deferred_item_stub
        ]
        session_get_mock.return_value = response_mock

# Path: collectors/hra/collector.py
# def collect(conf, conn, date_from=None, date_to=None):
# 
#     # Start collector
#     date_from = _get_date_from(conn, date_from)
#     date_to = _get_date_to(conn, date_to)
#     base.helpers.start(conf, 'hra', {'date_from': date_from, 'date_to': date_to})
# 
#     # Get parameters
#     URL = conf['HRA_URL']
#     USER = conf['HRA_USER']
#     PASS = conf['HRA_PASS']
# 
#     count = 0
#     chunk_days = 100
#     session = requests.Session()
#     loop_date_from = date_from
#     while True:
#         if loop_date_from > date_to:
#             break
#         loop_date_to = min(loop_date_from + datetime.timedelta(days=chunk_days), date_to)
#         url = _make_request_url(URL, loop_date_from, loop_date_to)
#         response = session.get(url, auth=(USER, PASS))
#         response.raise_for_status()
#         base.config.SENTRY.extra_context({
#             'url': response.url,
#         })
#         for item in response.json():
#             record = parse_record(response.url, item)
#             if not record:
#                 continue
#             record.write(conf, conn)
#             count += 1
#             if not count % 100:
#                 logger.info('Collected "%s" hra records', count)
#         loop_date_from = loop_date_to + datetime.timedelta(days=1)
#         time.sleep(1)
# 
#     # Stop collector
#     base.helpers.stop(conf, 'hra', {'collected': count})
# 
# def _make_request_url(prefix, date_from, date_to):
#     query = OrderedDict()
#     query['datePublishedFrom'] = date_from.strftime('%Y-%m-%d')
#     query['datePublishedTo'] = date_to.strftime('%Y-%m-%d')
#     url = '%s?%s' % (prefix, urlencode(query))
#     return url


# Path: tests/collectors/hra/test_collector.py
import mock
import pytest
import datetime
import requests
from collections import defaultdict
from collectors.hra.collector import collect, _make_request_url

# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals



class TestHRACollector(object):
    def test_make_request_url(self):
        date_from = datetime.date(2015, 1, 1)
        date_to = datetime.date(2015, 12, 31)
        actual = _make_request_url('prefix', date_from, date_to)
        expect = 'prefix?datePublishedFrom=2015-01-01&datePublishedTo=2015-12-31'
        assert actual == expect

    @mock.patch('requests.Session.get')
    def test_collect_skips_deffered_records(self, session_get_mock, conn, conf, deferred_item_stub):
        response_mock = mock.Mock()
        response_mock.json.return_value = [
            deferred_item_stub
        ]
        session_get_mock.return_value = response_mock

        collect(conf, conn, '2015-01-01', '2015-01-01')