<s> import os , collections , simplejson <EOL> from totalimpact import db , app <EOL> from totalimpact . providers import pmc <EOL> from test . unit_tests . providers import common <EOL> from test . unit_tests . providers . common import ProviderTestCase <EOL> from totalimpact . providers . provider import Provider , ProviderContentMalformedError , ProviderFactory <EOL> from totalimpact import provider_batch_data <EOL> from test . utils import http <EOL> from test . utils import setup_postgres_for_unittests , teardown_postgres_for_unittests <EOL> from nose . tools import assert_equals , raises , nottest , assert_items_equal <EOL> datadir = os . path . join ( os . path . split ( __file__ ) [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> SAMPLE_EXTRACT_METRICS_PAGE = os . path . join ( datadir , "<STR_LIT>" ) <EOL> SAMPLE_EXTRACT_METRICS_PAGE_DIFFERENT_MONTH = os . path . join ( datadir , "<STR_LIT>" ) <EOL> TEST_PMID = "<STR_LIT>" <EOL> class TestPmc ( ProviderTestCase ) : <EOL> provider_name = "<STR_LIT>" <EOL> testitem_aliases = ( "<STR_LIT>" , TEST_PMID ) <EOL> testitem_metrics = ( "<STR_LIT>" , TEST_PMID ) <EOL> def setUp ( self ) : <EOL> ProviderTestCase . setUp ( self ) <EOL> self . db = setup_postgres_for_unittests ( db , app ) <EOL> sample_data_dump = open ( SAMPLE_EXTRACT_METRICS_PAGE , "<STR_LIT:r>" ) . read ( ) <EOL> sample_data_dump_different_month = open ( SAMPLE_EXTRACT_METRICS_PAGE_DIFFERENT_MONTH , "<STR_LIT:r>" ) . read ( ) <EOL> test_monthly_data = [ <EOL> { "<STR_LIT>" : "<STR_LIT:abc>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : sample_data_dump , <EOL> "<STR_LIT>" : <NUM_LIT:1.0> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] } , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : sample_data_dump_different_month , <EOL> "<STR_LIT>" : <NUM_LIT:1.0> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : [ "<STR_LIT>" ] } , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> } , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> ] <EOL> for doc in test_monthly_data : <EOL> new_object = provider_batch_data . create_objects_from_doc ( doc ) <EOL> print new_object <EOL> self . provider = pmc . Pmc ( ) <EOL> print "<STR_LIT>" <EOL> def tearDown ( self ) : <EOL> teardown_postgres_for_unittests ( self . db ) <EOL> def test_has_applicable_batch_data_true ( self ) : <EOL> response = self . provider . has_applicable_batch_data ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> assert_equals ( response , True ) <EOL> def test_has_applicable_batch_data_false ( self ) : <EOL> response = self . provider . has_applicable_batch_data ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> assert_equals ( response , False ) <EOL> def test_build_batch_data_dict ( self ) : <EOL> response = self . provider . build_batch_data_dict ( ) <EOL> print response . keys ( ) <EOL> expected = [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> assert_items_equal ( response . keys ( ) , expected ) <EOL> def test_is_relevant_alias ( self ) : <EOL> assert_equals ( self . provider . is_relevant_alias ( self . testitem_aliases ) , True ) <EOL> def test_extract_metrics_success ( self ) : <EOL> f = open ( SAMPLE_EXTRACT_METRICS_PAGE , "<STR_LIT:r>" ) <EOL> good_page = f . read ( ) <EOL> metrics_dict = self . provider . _extract_metrics ( good_page , id = "<STR_LIT>" ) <EOL> print metrics_dict <EOL> expected = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:9> } <EOL> assert_equals ( metrics_dict , expected ) <EOL> def test_provider_metrics_500 ( self ) : <EOL> pass <EOL> def test_provider_metrics_400 ( self ) : <EOL> pass <EOL> def test_provider_metrics_nonsense_xml ( self ) : <EOL> pass <EOL> def test_provider_metrics_nonsense_txt ( self ) : <EOL> pass <EOL> def test_provider_metrics_empty ( self ) : <EOL> pass <EOL> @ http <EOL> def test_metrics ( self ) : <EOL> metrics_dict = self . provider . metrics ( [ ( "<STR_LIT>" , "<STR_LIT>" ) ] ) <EOL> expected = { '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' : ( <NUM_LIT:9> , '<STR_LIT>' ) } <EOL> print metrics_dict <EOL> for key in expected : <EOL> assert metrics_dict [ key ] [ <NUM_LIT:0> ] >= expected [ key ] [ <NUM_LIT:0> ] , [ key , metrics_dict [ key ] , expected [ key ] ] <EOL> assert metrics_dict [ key ] [ <NUM_LIT:1> ] == expected [ key ] [ <NUM_LIT:1> ] , [ key , metrics_dict [ key ] , expected [ key ] ] <EOL> @ http <EOL> def test_metrics_multiple_months ( self ) : <EOL> metrics_dict = self . provider . metrics ( [ ( "<STR_LIT>" , "<STR_LIT>" ) ] ) <EOL> expected = { '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) } <EOL> print metrics_dict <EOL> for key in expected : <EOL> assert metrics_dict [ key ] [ <NUM_LIT:0> ] >= expected [ key ] [ <NUM_LIT:0> ] , [ key , metrics_dict [ key ] , expected [ key ] ] <EOL> assert metrics_dict [ key ] [ <NUM_LIT:1> ] == expected [ key ] [ <NUM_LIT:1> ] , [ key , metrics_dict [ key ] , expected [ key ] ] <EOL> @ http <EOL> def test_metrics_real ( self ) : <EOL> metrics_dict = self . provider . metrics ( [ ( "<STR_LIT>" , "<STR_LIT>" ) ] ) <EOL> expected = { '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' : ( <NUM_LIT> , '<STR_LIT>' ) } <EOL> print metrics_dict <EOL> for key in expected : <EOL> assert metrics_dict [ key ] [ <NUM_LIT:0> ] >= expected [ key ] [ <NUM_LIT:0> ] , [ key , metrics_dict [ key ] , expected [ key ] ] <EOL> assert metrics_dict [ key ] [ <NUM_LIT:1> ] == expected [ key ] [ <NUM_LIT:1> ] , [ key , metrics_dict [ key ] , expected [ key ] ] </s>
