<s> '''<STR_LIT>''' <EOL> import unittest <EOL> import time <EOL> from cola . core . opener import MechanizeOpener <EOL> from cola . core . unit import Bundle <EOL> from app . weibo import login_hook <EOL> from app . weibo . parsers import MicroBlogParser , ForwardCommentLikeParser , UserInfoParser , UserFriendParser <EOL> from app . weibo . conf import user_config <EOL> from app . weibo . bundle import WeiboUserBundle <EOL> from pymongo import Connection <EOL> class Test ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . test_uid = '<STR_LIT>' <EOL> self . bundle = WeiboUserBundle ( self . test_uid ) <EOL> self . opener = MechanizeOpener ( ) <EOL> self . conn = Connection ( ) <EOL> self . db = self . conn [ getattr ( user_config . job , '<STR_LIT>' ) ] <EOL> self . users_collection = self . db . weibo_user <EOL> self . weibos_collection = self . db . micro_blog <EOL> assert len ( user_config . job [ '<STR_LIT>' ] ) > <NUM_LIT:0> <EOL> login_hook ( self . opener , ** user_config . job [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> def _get_urls_and_bundles ( self , yields ) : <EOL> urls = [ ] <EOL> bundles = [ ] <EOL> for item in yields : <EOL> if isinstance ( item , Bundle ) : <EOL> bundles . append ( item ) <EOL> else : <EOL> urls . append ( item ) <EOL> return urls , bundles <EOL> def tearDown ( self ) : <EOL> self . users_collection . remove ( { '<STR_LIT>' : self . test_uid } ) <EOL> self . weibos_collection . remove ( { '<STR_LIT>' : self . test_uid } ) <EOL> self . conn . close ( ) <EOL> def testMicroBlogParser ( self ) : <EOL> test_url = '<STR_LIT>' % ( <EOL> self . test_uid , <EOL> int ( time . time ( ) * ( <NUM_LIT:10> ** <NUM_LIT:6> ) ) <EOL> ) <EOL> parser = MicroBlogParser ( opener = self . opener , <EOL> url = test_url , <EOL> bundle = self . bundle ) <EOL> _ , bundles = self . _get_urls_and_bundles ( parser . parse ( ) ) <EOL> self . assertEqual ( len ( bundles ) , <NUM_LIT:0> ) <EOL> size = self . weibos_collection . find ( { '<STR_LIT>' : self . test_uid } ) . count ( ) <EOL> self . assertAlmostEqual ( size , <NUM_LIT:15> , delta = <NUM_LIT:1> ) <EOL> def testMicroBlogForwardsParser ( self ) : <EOL> test_url = '<STR_LIT>' <EOL> parser = ForwardCommentLikeParser ( opener = self . opener , <EOL> url = test_url , <EOL> bundle = self . bundle ) <EOL> urls , _ = self . _get_urls_and_bundles ( parser . parse ( ) ) <EOL> self . assertEqual ( len ( urls ) , <NUM_LIT:1> ) <EOL> weibo = self . weibos_collection . find_one ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : self . test_uid } ) <EOL> self . assertLessEqual ( len ( weibo [ '<STR_LIT>' ] ) , <NUM_LIT:20> ) <EOL> self . assertGreater ( len ( weibo [ '<STR_LIT>' ] ) , <NUM_LIT:0> ) <EOL> parser . parse ( urls [ <NUM_LIT:0> ] ) <EOL> weibo = self . weibos_collection . find_one ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : self . test_uid } ) <EOL> self . assertLessEqual ( len ( weibo [ '<STR_LIT>' ] ) , <NUM_LIT> ) <EOL> self . assertGreater ( len ( weibo [ '<STR_LIT>' ] ) , <NUM_LIT:10> ) <EOL> self . assertNotEqual ( weibo [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> weibo [ '<STR_LIT>' ] [ <NUM_LIT:10> ] ) <EOL> def testMicroBlogLikesParser ( self ) : <EOL> test_url = '<STR_LIT>' <EOL> parser = ForwardCommentLikeParser ( opener = self . opener , <EOL> url = test_url , <EOL> bundle = self . bundle ) <EOL> urls , _ = self . _get_urls_and_bundles ( parser . parse ( ) ) <EOL> self . assertEqual ( len ( urls ) , <NUM_LIT:1> ) <EOL> weibo = self . weibos_collection . find_one ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : self . test_uid } ) <EOL> self . assertEqual ( len ( weibo [ '<STR_LIT>' ] ) , <NUM_LIT:30> ) <EOL> def testUserInfoParser ( self ) : <EOL> test_url = '<STR_LIT>' % self . test_uid <EOL> parser = UserInfoParser ( opener = self . opener , <EOL> url = test_url , <EOL> bundle = self . bundle ) <EOL> parser . parse ( ) <EOL> user = self . users_collection . find_one ( { '<STR_LIT>' : self . test_uid } ) <EOL> self . assertTrue ( '<STR_LIT:info>' in user ) <EOL> def testUserInfoParserForSite ( self ) : <EOL> test_uid = '<STR_LIT>' <EOL> test_url = '<STR_LIT>' % test_uid <EOL> bundle = WeiboUserBundle ( test_uid ) <EOL> parser = UserInfoParser ( opener = self . opener , <EOL> url = test_url , <EOL> bundle = bundle ) <EOL> parser . parse ( ) <EOL> def testFriendParser ( self ) : <EOL> test_url = '<STR_LIT>' % self . test_uid <EOL> parser = UserFriendParser ( opener = self . opener , <EOL> url = test_url , <EOL> bundle = self . bundle ) <EOL> urls , bundles = self . _get_urls_and_bundles ( parser . parse ( ) ) <EOL> self . assertEqual ( len ( urls ) , <NUM_LIT:1> ) <EOL> self . assertGreater ( bundles , <NUM_LIT:0> ) <EOL> user = self . users_collection . find_one ( { '<STR_LIT>' : self . test_uid } ) <EOL> self . assertEqual ( len ( bundles ) , len ( user [ '<STR_LIT>' ] ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
