# 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)
        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()
        parsed_data = parser.parse(get_piwik_data())
        assert_that(parsed_data[0]['_timestamp'],
                    equal_to('2015-02-16T12:00:01+00:00'))


class TestMain(unittest.TestCase):
    def test_main_disallows_start_at_and_end_at_date_arguments(self):
        with self.assertRaises(SystemExit) as cm:
            main(credentials(),
                 {'data-type': 'realtime'},
                 query(),
                 {},
                 '2015-01-21',
                 '2015-01-22')
        self.assertEqual(cm.exception.code, 1)

    @patch("performanceplatform.collector.piwik.realtime.Pusher")
    @patch("performanceplatform.collector.piwik.base."
           "requests_with_backoff.get")
    def test_main_fetches_and_pushes_parsed_data(self, mock_get, mock_pusher):
        mock_get().json.return_value = get_piwik_data()
        parser = Parser()
        parsed_data = parser.parse(get_piwik_data())
        main(credentials(),

# 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)

        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()
        parsed_data = parser.parse(get_piwik_data())
        assert_that(parsed_data[0]['_timestamp'],
                    equal_to('2015-02-16T12:00:01+00:00'))


class TestMain(unittest.TestCase):
    def test_main_disallows_start_at_and_end_at_date_arguments(self):
        with self.assertRaises(SystemExit) as cm:
            main(credentials(),
                 {'data-type': 'realtime'},
                 query(),
                 {},
                 '2015-01-21',
                 '2015-01-22')
        self.assertEqual(cm.exception.code, 1)

    @patch("performanceplatform.collector.piwik.realtime.Pusher")
    @patch("performanceplatform.collector.piwik.base."
           "requests_with_backoff.get")
    def test_main_fetches_and_pushes_parsed_data(self, mock_get, mock_pusher):
        mock_get().json.return_value = get_piwik_data()
        parser = Parser()
        parsed_data = parser.parse(get_piwik_data())
        main(credentials(),

             {'data-type': 'realtime'}, query(), {}, None, None)