# Path: performanceplatform/collector/piwik/realtime.py
# def main(credentials, data_set_config, query, options, start_at, end_at):
#     if start_at or end_at:
#         print 'Cannot backfill realtime collectors'
#         sys.exit(1)
# 
#     data = Fetcher(credentials, query).fetch()
# 
#     parsed_data = Parser().parse(data)
# 
#     Pusher(data_set_config, options).push(parsed_data)
# 
# class Fetcher(BaseFetcher):
#     def __init__(self, credentials, query):
#         super(Fetcher, self).__init__(credentials, query)
#         self.last_minutes = query.get('minutes', 2)
# 
#     def _request_params(self):
#         params = super(Fetcher, self)._request_params()
#         params.update({'lastMinutes': self.last_minutes})
#         return params
# 
# class Parser():
#     def _timestamp(self):
#         timezone = pytz.UTC
#         timestamp = datetime.now(timezone).replace(microsecond=0)
#         return timestamp.isoformat()
# 
#     def parse(self, data):
#         timestamp = self._timestamp()
#         visitor_count = int(data[0]["visitors"])
#         return [{
#             "_timestamp": timestamp,
#             "_id": timestamp,
#             "unique_visitors": visitor_count,
#             "for_url": ""}]
# 
# Path: tests/performanceplatform/collector/piwik/test_core.py
# def query():
#     return {
#         'site_id': '7',
#         'api_method': 'SomeNamespace.someMethod'
#     }
# 
# def credentials():
#     return {
#         'token_auth': 'foo',
#         'url': 'http://bar.com'
#     }


import unittest
import json
from mock import patch
from hamcrest import assert_that, equal_to
from freezegun import freeze_time
from performanceplatform.collector.piwik.realtime import main, Fetcher, Parser
from tests.performanceplatform.collector.piwik.test_core import(
    query, credentials)
class TestFetcher(unittest.TestCase):
    def test_all_core_request_params_are_present_and_correct(self, mock_get):
        fetcher = build_fetcher()
        fetcher.fetch()
        mock_get.assert_called_once_with(
            url='http://bar.com?module=API',
            params={
                'idSite': '7',
                'token_auth': 'foo',
                'format': 'json',
                'method': 'SomeNamespace.someMethod',
                'lastMinutes': 2
            }
        )

    def test_minutes_option_sets_last_minutes_request_param(self, mock_get):
        fetcher = build_fetcher({'minutes': 30})
        fetcher.fetch()
        minutes = mock_get.call_args[1]['params']['lastMinutes']
        assert_that(minutes, equal_to(30))


class TestParser(unittest.TestCase):
    def test_parses_piwik_visitors_metric(self):
        parser = Parser()
        parsed_data = parser.parse(get_piwik_data())
        assert_that(parsed_data[0]['unique_visitors'], equal_to(53))

    @freeze_time("2015-02-16 12:00:01")
    def test_sets_timestamp_to_now(self):

# Path: performanceplatform/collector/piwik/realtime.py
# def main(credentials, data_set_config, query, options, start_at, end_at):
#     if start_at or end_at:
#         print 'Cannot backfill realtime collectors'
#         sys.exit(1)
# 
#     data = Fetcher(credentials, query).fetch()
# 
#     parsed_data = Parser().parse(data)
# 
#     Pusher(data_set_config, options).push(parsed_data)
# 
# class Fetcher(BaseFetcher):
#     def __init__(self, credentials, query):
#         super(Fetcher, self).__init__(credentials, query)
#         self.last_minutes = query.get('minutes', 2)
# 
#     def _request_params(self):
#         params = super(Fetcher, self)._request_params()
#         params.update({'lastMinutes': self.last_minutes})
#         return params
# 
# class Parser():
#     def _timestamp(self):
#         timezone = pytz.UTC
#         timestamp = datetime.now(timezone).replace(microsecond=0)
#         return timestamp.isoformat()
# 
#     def parse(self, data):
#         timestamp = self._timestamp()
#         visitor_count = int(data[0]["visitors"])
#         return [{
#             "_timestamp": timestamp,
#             "_id": timestamp,
#             "unique_visitors": visitor_count,
#             "for_url": ""}]
# 
# Path: tests/performanceplatform/collector/piwik/test_core.py
# def query():
#     return {
#         'site_id': '7',
#         'api_method': 'SomeNamespace.someMethod'
#     }
# 
# def credentials():
#     return {
#         'token_auth': 'foo',
#         'url': 'http://bar.com'
#     }


# Path: tests/performanceplatform/collector/piwik/test_realtime.py
import unittest
import json
from mock import patch
from hamcrest import assert_that, equal_to
from freezegun import freeze_time
from performanceplatform.collector.piwik.realtime import main, Fetcher, Parser
from tests.performanceplatform.collector.piwik.test_core import(
    query, credentials)

class TestFetcher(unittest.TestCase):
    def test_all_core_request_params_are_present_and_correct(self, mock_get):
        fetcher = build_fetcher()
        fetcher.fetch()
        mock_get.assert_called_once_with(
            url='http://bar.com?module=API',
            params={
                'idSite': '7',
                'token_auth': 'foo',
                'format': 'json',
                'method': 'SomeNamespace.someMethod',
                'lastMinutes': 2
            }
        )

    def test_minutes_option_sets_last_minutes_request_param(self, mock_get):
        fetcher = build_fetcher({'minutes': 30})
        fetcher.fetch()
        minutes = mock_get.call_args[1]['params']['lastMinutes']
        assert_that(minutes, equal_to(30))


class TestParser(unittest.TestCase):
    def test_parses_piwik_visitors_metric(self):
        parser = Parser()
        parsed_data = parser.parse(get_piwik_data())
        assert_that(parsed_data[0]['unique_visitors'], equal_to(53))

    @freeze_time("2015-02-16 12:00:01")
    def test_sets_timestamp_to_now(self):

        parser = Parser()