<s> import unittest <EOL> import json <EOL> from mock import patch <EOL> from hamcrest import assert_that , equal_to <EOL> from freezegun import freeze_time <EOL> from performanceplatform . collector . piwik . realtime import main , Fetcher , Parser <EOL> from tests . performanceplatform . collector . piwik . test_core import ( <EOL> query , credentials ) <EOL> def get_piwik_data ( ) : <EOL> with open ( "<STR_LIT>" , "<STR_LIT:r>" ) as f : <EOL> return json . loads ( f . read ( ) ) <EOL> def build_fetcher ( query_params = { } ) : <EOL> q = query ( ) <EOL> q . update ( query_params ) <EOL> return Fetcher ( credentials ( ) , q ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> class TestFetcher ( unittest . TestCase ) : <EOL> def test_all_core_request_params_are_present_and_correct ( self , mock_get ) : <EOL> fetcher = build_fetcher ( ) <EOL> fetcher . fetch ( ) <EOL> mock_get . assert_called_once_with ( <EOL> url = '<STR_LIT>' , <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:2> <EOL> } <EOL> ) <EOL> def test_minutes_option_sets_last_minutes_request_param ( self , mock_get ) : <EOL> fetcher = build_fetcher ( { '<STR_LIT>' : <NUM_LIT:30> } ) <EOL> fetcher . fetch ( ) <EOL> minutes = mock_get . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> assert_that ( minutes , equal_to ( <NUM_LIT:30> ) ) <EOL> class TestParser ( unittest . TestCase ) : <EOL> def test_parses_piwik_visitors_metric ( self ) : <EOL> parser = Parser ( ) <EOL> parsed_data = parser . parse ( get_piwik_data ( ) ) <EOL> assert_that ( parsed_data [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , equal_to ( <NUM_LIT> ) ) <EOL> @ freeze_time ( "<STR_LIT>" ) <EOL> def test_sets_timestamp_to_now ( self ) : <EOL> parser = Parser ( ) <EOL> parsed_data = parser . parse ( get_piwik_data ( ) ) <EOL> assert_that ( parsed_data [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> equal_to ( '<STR_LIT>' ) ) <EOL> class TestMain ( unittest . TestCase ) : <EOL> def test_main_disallows_start_at_and_end_at_date_arguments ( self ) : <EOL> with self . assertRaises ( SystemExit ) as cm : <EOL> main ( credentials ( ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> query ( ) , <EOL> { } , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( cm . exception . code , <NUM_LIT:1> ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_main_fetches_and_pushes_parsed_data ( self , mock_get , mock_pusher ) : <EOL> mock_get ( ) . json . return_value = get_piwik_data ( ) <EOL> parser = Parser ( ) <EOL> parsed_data = parser . parse ( get_piwik_data ( ) ) <EOL> main ( credentials ( ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } , query ( ) , { } , None , None ) <EOL> second_call = mock_pusher . mock_calls [ <NUM_LIT:1> ] <EOL> argument = second_call [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> assert_that ( argument , equal_to ( parsed_data ) ) </s>
