def test_invalid_input(self): with db_test_session() as session: with application.test_request_context(): nicoru = NicoruController() request.get_json = self.dummy_get_json result = nicoru.put('') assert result is None
def test_got_data_id_exists_in_db(self): with db_test_session() as session: # setup vid = TestData.VIDEO_ID_1 TestDataUtil.add_video(session, id=vid) video_api_info = VideoAPIInfo(video_id=vid, thread_id=1, user_id='aiueo700', ms='a', user_key='b') video_info = VideoInfo(TestDataLevel2.VideoObject.VO_1) assert video_api_info.video_id == video_info.video_id == vid with mock.patch('requests.Session', DummySession), \ mock.patch.object(NiconicoAPIConnector, 'get_video_info', return_value=video_info), \ mock.patch.object(NiconicoAPIConnector, 'get_video_api_info', return_value=video_api_info): # run IncompleteVideoDataGetter.get_and_register_data( session, TestData.VIDEO_ID_2) session.commit() # verify stored = session.query(Video).all() # type: List[Video] assert len(stored) == 2 assert stored[0].id == TestData.VIDEO_ID_2 assert stored[1].id == TestData.VIDEO_ID_1
def test_video_data_get_error(self): with db_test_session() as session: # setup vid = TestData.VIDEO_ID_1 TestDataUtil.add_video(session, id=vid) video_api_info = VideoAPIInfo(video_id=vid, thread_id=1, user_id='aiueo700', ms='a', user_key='b') video_info = VideoInfo(TestDataLevel2.VideoObject.VO_1) assert video_api_info.video_id == video_info.video_id == vid with mock.patch('requests.Session', DummySession), \ mock.patch.object(NiconicoAPIConnector, 'get_video_info', side_effect=VideoDataGetError), \ mock.patch.object(NiconicoAPIConnector, 'get_video_api_info', side_effect=VideoDataGetError): with pytest.raises(VideoDataGetError): # run IncompleteVideoDataGetter.get_and_register_data( session, vid) # verify stored = session.query( IrregularVideoId).all() # type: List[IrregularVideoId] assert len(stored) == 1 assert stored[0].video_id == TestData.VIDEO_ID_1 assert JobLogDAO(session).find_by_type( JobLogType.VIDEO).status == JobLogStatus.ABORTED
def get(self, nicorus: List[str], completed_video_ids: List[str], expected: Optional[str]): with db_test_session() as session: # setup dao = NicoruDAO(session) for vid in nicorus: dao.nicoru(video_id=vid, comment_id=TestData.COMMENT_ID_1) session.commit() # add videos v_dao = VideoDAO(session) for vid in completed_video_ids: v_dao.add( id=vid, title=TestData.Video.TITLE_1, thumbnail=TestData.Video.THUMBNAIL_1, posted_at=TestData.Video.POSTED_AT_1, length=TestData.Video.LENGTH_1, watch_url=TestData.Video.WATCH_URL_1, posted_by=TestData.Video.POSTED_BY_1, posted_by_name=TestData.Video.POSTED_BY_NAME_1, ) session.commit() # run result = dao.find_incomplete_video_id() # verify assert result == expected
def test_success(self): with db_test_session() as session: # setup NicoruDAO(session).nicoru(TestData.VIDEO_ID_1, TestData.COMMENT_ID_1) CommentDAO(session).add( id=TestData.COMMENT_ID_1, video_id=TestData.VIDEO_ID_1, text=TestData.Comment.TEXT_1, posted_at=TestData.Comment.POSTED_AT_1, posted_by=TestData.Comment.POSTED_BY_1, point=TestData.Comment.POINT_1, was_deleted=TestData.Comment.WAS_DELETED_1, official_nicoru=TestData.Comment.OFFICIAL_NICORU_1, ) session.commit() HardConstants.App = HardConstants.Test # run DBDataExporter.execute() # verify with open(HardConstants.App.REPORT_CSV, 'r') as f: assert f.readlines() == [ '"動画ID","コメ番","コメント","擬似ニコる","公式ニコる"\n', '"{vid}","{cid}","{c}","1","{o_n}"\n'.format( vid=TestData.VIDEO_ID_1, cid=TestData.COMMENT_ID_1, c=TestData.Comment.TEXT_1, o_n=TestData.Comment.OFFICIAL_NICORU_1), ] assert JobLogDAO(session).find_by_type( JobLogType.DB_DATA_EXPORT).status == JobLogStatus.DONE
def test(self): with db_test_session() as session: # setup nicoru = NicoruController() # run result = nicoru.get('sm9') # verify assert result == {}
def test_not_found(self): with db_test_session() as session: # setup dao = NicoruDAO(session) # run data = dao.get_nicorareta(TestData.Comment.POSTED_BY_1) # verify assert data == []
def test_not_found(self): with db_test_session() as session: # setup dao = NicoruDAO(session) # run stored = dao.get_nicoru_for_video(TestData.VIDEO_ID_1) # verify assert stored == {}
def test_success(self): with db_test_session() as session: # run with mock.patch.object(DropboxUploader, 'upload'): DropboxUploader.execute() # verify assert JobLogDAO(session).find_by_type( JobLogType.UPLOAD_TO_STORAGE).status == JobLogStatus.DONE
def test_found(self): with db_test_session() as session: # setup dao = NicoruDAO(session) dao.nicoru(video_id=TestData.VIDEO_ID_1, comment_id=TestData.COMMENT_ID_1) session.commit() # run stored = dao.get_nicoru_for_video(TestData.VIDEO_ID_1) # verify assert stored == {TestData.COMMENT_ID_1: 1}
def test_failure(self): with db_test_session() as session: # run with mock.patch.object(DropboxUploader, 'upload', side_effect=Exception): with pytest.raises(Exception): DropboxUploader.execute() # verify assert JobLogDAO(session).find_by_type( JobLogType.UPLOAD_TO_STORAGE ).status == JobLogStatus.ABORTED
def test_use_commit_no_error(self): with db_test_session() as session: # run with db_session() as session: dao = NicoruDAO(session) dao.nicoru('sm9', '1') # verify with db_session() as session: dao = NicoruDAO(session) stored = dao.find_by_video_id_and_comment_id('sm9', '1') assert stored.video_id == 'sm9' assert stored.comment_id == '1'
def test_1001_lines(self): with db_test_session() as session: # setup stub_io = mock.mock_open(read_data="\n".join(['"sm{}","1","3"'.format(i) for i in range(1001)])) with mock.patch("builtins.open", stub_io, create=True) as m: m.return_value.__iter__ = lambda self_: self_ m.return_value.__next__ = lambda self_: next(iter(self_.readline, '')) # run NicoruCSVImporter.execute() # verify stored = session.query(Nicoru).all() # type: List[Nicoru] assert len(stored) == 1001
def test_wait(self): with db_test_session() as session: with mock.patch.object(get_incomplete_data, 'sleep') as m_sleep: # setup job_log = JobLogDAO(session).add_or_update( JobLogType.VIDEO, JobLogStatus.DONE) session.commit() # run IncompleteDataGetter.wait_to_run_next_process(job_log) # verify m_sleep.assert_called_once()
def test_use_commit_error(self): with db_test_session() as session: # run try: with db_session() as session: dao = NicoruDAO(session) dao.nicoru('sm9', '1') raise Exception except: pass # verify with db_session() as session: dao = NicoruDAO(session) stored = dao.find_by_video_id_and_comment_id('sm9', '1') assert stored is None
def test_failure(self): with db_test_session() as session: # setup HardConstants.App = HardConstants.Test with mock.patch.object(DBDataExporter, 'export_public_data', side_effect=Exception): # run with pytest.raises(Exception): DBDataExporter.execute() # verify assert JobLogDAO(session).find_by_type( JobLogType.DB_DATA_EXPORT).status == JobLogStatus.ABORTED
def test_long_file(self): with db_test_session() as session: # setup HardConstants.App = HardConstants.Test TestDataUtil.make_test_file(HardConstants.App.DB_DUMP_ZIP, DropboxUploader.CHUNK_SIZE * 2 + 1) # run with mock.patch.object(Dropbox, 'files_upload'), \ mock.patch.object(Dropbox, 'files_upload_session_start'), \ mock.patch.object(Dropbox, 'files_upload_session_append_v2'), \ mock.patch.object(Dropbox, 'files_upload_session_finish', return_value=None), \ mock.patch.object(CommitInfo, '__init__', return_value=None), \ mock.patch.object(UploadSessionCursor, '__init__', return_value=None): DropboxUploader.upload()
def test(self): with db_test_session() as session: with mock.patch.object(mail, 'send', return_value=None): # setup file HardConstants.App = HardConstants.Test TestDataUtil.make_test_file(HardConstants.App.DB_DUMP_ZIP, DropboxUploader.CHUNK_SIZE) # setup DB dao = JobLogDAO(session) dao.add_or_update(JobLogType.VIDEO, JobLogStatus.DONE) dao.add_or_update(JobLogType.COMMENT, JobLogStatus.DONE) session.commit() # run JobLogMailer.execute()
def test(self): with db_test_session() as session: # run IrregularCommentIdDAO(session).add( video_id=TestData.VIDEO_ID_1, comment_ids=[TestData.COMMENT_ID_1, TestData.COMMENT_ID_2], ) session.commit() # verify stored = session.query(IrregularCommentId).all() # type: List[IrregularCommentId] assert len(stored) == 2 assert stored[0].video_id == TestData.VIDEO_ID_1 assert stored[0].comment_id == TestData.COMMENT_ID_1 assert stored[1].video_id == TestData.VIDEO_ID_1 assert stored[1].comment_id == TestData.COMMENT_ID_2
def test_add(self): with db_test_session() as session: # setup dao = NicoruDAO(session) # run new = dao.nicoru(video_id=TestData.VIDEO_ID_1, comment_id=TestData.COMMENT_ID_1) session.commit() # verify assert new.video_id == TestData.VIDEO_ID_1 assert new.comment_id == TestData.COMMENT_ID_1 assert new.nicoru == 1 stored = dao.find_by_video_id_and_comment_id( video_id=TestData.VIDEO_ID_1, comment_id=TestData.COMMENT_ID_1) assert stored.video_id == TestData.VIDEO_ID_1 assert stored.comment_id == TestData.COMMENT_ID_1 assert stored.nicoru == 1
def test(self): with db_test_session() as session: # run dao = CommentDAO(session) new = dao.add( id=TestData.COMMENT_ID_1, video_id=TestData.VIDEO_ID_1, text=TestData.Comment.TEXT_1, posted_at=TestData.Comment.POSTED_AT_1, posted_by=TestData.Comment.POSTED_BY_1, point=TestData.Comment.POINT_1, was_deleted=TestData.Comment.WAS_DELETED_1, official_nicoru=TestData.Comment.OFFICIAL_NICORU_1, ) session.commit() # verify assert new == dao.find(TestData.COMMENT_ID_1, TestData.VIDEO_ID_1)
def test_success_without_wait(self): with db_test_session() as session: # setup vid = TestData.VIDEO_ID_1 video_api_info = VideoAPIInfo(video_id=vid, thread_id=1, user_id='aiueo700', ms='a', user_key='b') video_info = VideoInfo(TestDataLevel2.VideoObject.VO_1) assert video_api_info.video_id == video_info.video_id == vid NicoruDAO(session).nicoru(video_id=vid, comment_id='1') session.commit() # run, verify with mock.patch('requests.Session', DummySession), \ mock.patch.object(NiconicoAPIConnector, 'get_video_info', return_value=video_info), \ mock.patch.object(NiconicoAPIConnector, 'get_video_api_info', return_value=video_api_info): assert IncompleteVideoDataGetter.execute( ) == IncompleteVideoDataGetter.ReturnCode.SUCCESS
def test(self): with db_test_session() as session: # setup dao = NicoruDAO(session) dao.nicoru(TestData.VIDEO_ID_1, TestData.COMMENT_ID_1) c_dao = CommentDAO(session) c_dao.add( id=TestData.COMMENT_ID_1, video_id=TestData.VIDEO_ID_1, text=TestData.Comment.TEXT_1, posted_at=TestData.Comment.POSTED_AT_1, posted_by=TestData.Comment.POSTED_BY_1, point=TestData.Comment.POINT_1, was_deleted=TestData.Comment.WAS_DELETED_1, official_nicoru=TestData.Comment.OFFICIAL_NICORU_1, ) v_dao = VideoDAO(session) v_dao.add( id=TestData.VIDEO_ID_1, title=TestData.Video.TITLE_1, thumbnail=TestData.Video.THUMBNAIL_1, posted_at=TestData.Video.POSTED_AT_1, length=TestData.Video.LENGTH_1, watch_url=TestData.Video.WATCH_URL_1, posted_by=TestData.Video.POSTED_BY_1, posted_by_name=TestData.Video.POSTED_BY_NAME_1, ) session.commit() obj = NicoraretaController() # run result = obj.get(TestData.Comment.POSTED_BY_1) result_json = json.loads(result) # verify assert result_json == [ [1, TestData.VIDEO_ID_1, TestData.Comment.TEXT_1, TestData.Comment.POSTED_AT_1.strftime("%Y/%m/%d %H:%M:%S"), TestData.Video.TITLE_1, TestData.Video.THUMBNAIL_1, TestData.Video.WATCH_URL_1] ]
def test_2_lines(self): with db_test_session() as session: # setup n_dao = NicoruDAO(session) n_dao.nicoru('sm9', '1') session.commit() stub_io = mock.mock_open(read_data='"sm9","1","3"\n"sm9","2","5"') with mock.patch("builtins.open", stub_io, create=True) as m: m.return_value.__iter__ = lambda self_: self_ m.return_value.__next__ = lambda self_: next(iter(self_.readline, '')) # run NicoruCSVImporter.execute() # verify stored = session.query(Nicoru).all() # type: List[Nicoru] assert stored[0].video_id == 'sm9' assert stored[0].comment_id == '1' assert stored[0].nicoru == 4 assert stored[1].video_id == 'sm9' assert stored[1].comment_id == '2' assert stored[1].nicoru == 5
def test_previous_process_running(self): with db_test_session() as session: # setup job_log = JobLogDAO(session).add_or_update( JobLogType.VIDEO, JobLogStatus.RUNNING) job_log.updated_at == datetime.now() - timedelta(seconds=5) session.commit() vid = TestData.VIDEO_ID_1 video_api_info = VideoAPIInfo(video_id=vid, thread_id=1, user_id='aiueo700', ms='a', user_key='b') video_info = VideoInfo(TestDataLevel2.VideoObject.VO_1) assert video_api_info.video_id == video_info.video_id == vid with mock.patch('requests.Session', DummySession), \ mock.patch.object(NiconicoAPIConnector, 'get_video_info', return_value=video_info), \ mock.patch.object(NiconicoAPIConnector, 'get_video_api_info', return_value=video_api_info): # run, verify assert IncompleteVideoDataGetter.execute( ) == IncompleteVideoDataGetter.ReturnCode.PREVIOUS_PROCESS_IS_RUNNING
def get(self, nicorus: List[Tuple], videos: List[str], expected: Tuple[Optional[str], Optional[List[str]]]): with db_test_session() as session: # insert nicorus dao = NicoruDAO(session) c_dao = CommentDAO(session) for vid, cid, is_completed in nicorus: dao.nicoru(video_id=vid, comment_id=cid) if is_completed: c_dao.add( id=cid, video_id=vid, text=TestData.Comment.TEXT_1, posted_at=TestData.Comment.POSTED_AT_1, posted_by=TestData.Comment.POSTED_BY_1, point=TestData.Comment.POINT_1, was_deleted=TestData.Comment.WAS_DELETED_1, official_nicoru=TestData.Comment.OFFICIAL_NICORU_1, ) session.commit() # insert comments v_dao = VideoDAO(session) for vid in videos: v_dao.add( id=vid, title=TestData.Video.TITLE_1, thumbnail=TestData.Video.THUMBNAIL_1, posted_at=TestData.Video.POSTED_AT_1, length=TestData.Video.LENGTH_1, watch_url=TestData.Video.WATCH_URL_1, posted_by=TestData.Video.POSTED_BY_1, posted_by_name=TestData.Video.POSTED_BY_NAME_1, ) session.commit() assert dao.find_incomplete_comment_records() == expected
def test(self): with db_test_session() as session: # setup dao = VideoDAO(session) # run new = dao.add( id=TestData.VIDEO_ID_1, title=TestData.Video.TITLE_1, thumbnail=TestData.Video.THUMBNAIL_1, posted_at=TestData.Video.POSTED_AT_1, length=TestData.Video.LENGTH_1, watch_url=TestData.Video.WATCH_URL_1, posted_by=TestData.Video.POSTED_BY_1, posted_by_name=TestData.Video.POSTED_BY_NAME_1, ) session.commit() # verify assert new.id == TestData.VIDEO_ID_1 assert new.title == TestData.Video.TITLE_1 assert new.thumbnail == TestData.Video.THUMBNAIL_1 assert new.posted_at == TestData.Video.POSTED_AT_1 assert new.length == TestData.Video.LENGTH_1 assert new.watch_url == TestData.Video.WATCH_URL_1 assert new.posted_by == TestData.Video.POSTED_BY_1 assert new.posted_by_name == TestData.Video.POSTED_BY_NAME_1 stored = dao.find(TestData.VIDEO_ID_1) assert stored.id == TestData.VIDEO_ID_1 assert stored.title == TestData.Video.TITLE_1 assert stored.thumbnail == TestData.Video.THUMBNAIL_1 assert stored.posted_at == TestData.Video.POSTED_AT_1 assert stored.length == TestData.Video.LENGTH_1 assert stored.watch_url == TestData.Video.WATCH_URL_1 assert stored.posted_by == TestData.Video.POSTED_BY_1 assert stored.posted_by_name == TestData.Video.POSTED_BY_NAME_1
def test_found(self): with db_test_session() as session: # setup dao = NicoruDAO(session) dao.nicoru(TestData.VIDEO_ID_1, TestData.COMMENT_ID_1) # v1, c1, 1 nicoru dao.nicoru(TestData.VIDEO_ID_1, TestData.COMMENT_ID_2) session.commit() dao.nicoru(TestData.VIDEO_ID_1, TestData.COMMENT_ID_2) # v1, c2, 2 nicoru dao.nicoru(TestData.VIDEO_ID_2, TestData.COMMENT_ID_1) # v2, c1, 1 nicoru c_dao = CommentDAO(session) c_dao.add( # v1, c1 id=TestData.COMMENT_ID_1, video_id=TestData.VIDEO_ID_1, text=TestData.Comment.TEXT_1, posted_at=TestData.Comment.POSTED_AT_1, posted_by=TestData.Comment.POSTED_BY_1, point=TestData.Comment.POINT_1, was_deleted=TestData.Comment.WAS_DELETED_1, official_nicoru=TestData.Comment.OFFICIAL_NICORU_1, ) c_dao.add( # v1, c2 id=TestData.COMMENT_ID_2, video_id=TestData.VIDEO_ID_1, text=TestData.Comment.TEXT_2, posted_at=TestData.Comment.POSTED_AT_2, posted_by=TestData.Comment.POSTED_BY_1, point=TestData.Comment.POINT_1, was_deleted=TestData.Comment.WAS_DELETED_1, official_nicoru=TestData.Comment.OFFICIAL_NICORU_1, ) c_dao.add( # v1, c3 id=TestData.COMMENT_ID_3, video_id=TestData.VIDEO_ID_1, text=TestData.Comment.TEXT_2, posted_at=TestData.Comment.POSTED_AT_3, posted_by=TestData.Comment.POSTED_BY_1, point=TestData.Comment.POINT_1, was_deleted=TestData.Comment.WAS_DELETED_1, official_nicoru=TestData.Comment.OFFICIAL_NICORU_1, ) c_dao.add( # v2, c1 id=TestData.COMMENT_ID_1, video_id=TestData.VIDEO_ID_2, text=TestData.Comment.TEXT_1, posted_at=TestData.Comment.POSTED_AT_3, posted_by=TestData.Comment.POSTED_BY_1, point=TestData.Comment.POINT_1, was_deleted=TestData.Comment.WAS_DELETED_1, official_nicoru=TestData.Comment.OFFICIAL_NICORU_1, ) v_dao = VideoDAO(session) v_dao.add( id=TestData.VIDEO_ID_1, title=TestData.Video.TITLE_1, thumbnail=TestData.Video.THUMBNAIL_1, posted_at=TestData.Video.POSTED_AT_1, length=TestData.Video.LENGTH_1, watch_url=TestData.Video.WATCH_URL_1, posted_by=TestData.Video.POSTED_BY_1, posted_by_name=TestData.Video.POSTED_BY_NAME_1, ) v_dao.add( id=TestData.VIDEO_ID_2, title=TestData.Video.TITLE_2, thumbnail=TestData.Video.THUMBNAIL_2, posted_at=TestData.Video.POSTED_AT_1, length=TestData.Video.LENGTH_1, watch_url=TestData.Video.WATCH_URL_2, posted_by=TestData.Video.POSTED_BY_1, posted_by_name=TestData.Video.POSTED_BY_NAME_1, ) session.commit() # run data = dao.get_nicorareta(TestData.Comment.POSTED_BY_1) # verify assert data == [ (1, TestData.VIDEO_ID_2, TestData.Comment.TEXT_1, TestData.Comment.POSTED_AT_3.strftime( "%Y/%m/%d %H:%M:%S"), TestData.Video.TITLE_2, TestData.Video.THUMBNAIL_2, TestData.Video.WATCH_URL_2), # v2, c1 (2, TestData.VIDEO_ID_1, TestData.Comment.TEXT_2, TestData.Comment.POSTED_AT_2.strftime( "%Y/%m/%d %H:%M:%S"), TestData.Video.TITLE_1, TestData.Video.THUMBNAIL_1, TestData.Video.WATCH_URL_1), # v1, c2 (1, TestData.VIDEO_ID_1, TestData.Comment.TEXT_1, TestData.Comment.POSTED_AT_1.strftime( "%Y/%m/%d %H:%M:%S"), TestData.Video.TITLE_1, TestData.Video.THUMBNAIL_1, TestData.Video.WATCH_URL_1), # v1, c1 ]
def test(self): with db_test_session() as session: with application.test_request_context(): obj = NicoraretaScreenController() result = obj.get(TestData.Comment.POSTED_BY_1) assert '<title>ニコられた</title>' in result
def test_no_incomplete_video(self): with db_test_session() as session: assert IncompleteVideoDataGetter.execute( ) == IncompleteVideoDataGetter.ReturnCode.NO_INCOMPLETE_DATA