def test_update_files_2(self, mock_blog_model): db.init_app(app) db.create_all() file_upload = FileUpload() file_upload.init_app(app, db) new_file = FileStorage( stream=open(self.my_video_update, "rb"), filename="my_video_updated.mp4", content_type="video/mpeg", ) model = mock_blog_model(**self.attrs) assert model.my_video__file_name == "my_video.mp4" assert model.my_video__mime_type == "video/mpeg" assert model.my_video__file_type == "mp4" result = file_upload.update_files( model, files={"my_video": new_file}, ) assert result.my_video__file_name == "my_video_updated.mp4" assert result.my_video__mime_type == "mp4" assert result.my_video__file_type == "video/mpeg"
def test_get_file_url(self, mock_blog_model): db.init_app(app) db.create_all() file_upload = FileUpload() file_upload.init_app(app, db) m = mock_blog_model(**self.attrs) with app.test_request_context(): url = file_upload.get_file_url(m, filename="my_video") assert url == "http://localhost/static/blogs/1/my_video.mp4"
def db(app, request): """ Returns session-wide initialised database. """ with app.app_context(): from .models import User import oauth.models _db.drop_all() _db.create_all() user = User() user.email = 'demo' user.username = '******' user.password = '******' _db.session.add(user) _db.session.commit()
def test_add_file_urls_to_models(self, create_app, mock_blog_model, mock_news_model): db.init_app(app) db.create_all() file_upload = FileUpload() file_upload.init_app(app, db) blog1 = mock_blog_model( name="hello", my_video__file_name="my_video.mp4", my_video__mime_type="video/mpeg", my_video__file_type="mp4", my_placeholder__file_name="my_placeholder1.png", my_placeholder__mime_type="image/png", my_placeholder__file_type="png", ) blog2 = mock_blog_model( name="hello2", my_video__file_name="my_video2.mp4", my_video__mime_type="video/mpeg", my_video__file_type="mp4", my_placeholder__file_name="my_placeholder2.png", my_placeholder__mime_type="image/png", my_placeholder__file_type="png", ) mock_news_model(title="news_1", blog_id=1) db.session.add_all([ blog1, blog2, mock_news_model( title="news_1", blog_id=1, news_video__file_name="news_video1.mp4", news_video__mime_type="video/mpeg", news_video__file_type="mp4", news_image__file_name="news_image.png", news_image__mime_type="image/png", news_image__file_type="png", ), mock_news_model( title="news_2", blog_id=1, news_video__file_name="news_video2.mp4", news_video__mime_type="video/mpeg", news_video__file_type="mp4", news_image__file_name="news_image.png", news_image__mime_type="image/png", news_image__file_type="png", ) ]) db.session.commit() rv = create_app.get("/blogs") assert "200" in rv.status assert rv.get_json()["results"][ "my_video_url"] == "http://localhost/static/blogs/1/my_video.mp4" assert rv.get_json()["results"][ "my_video_url_2"] == "http://localhost/static/blogs/2/my_video2.mp4" assert rv.get_json()["results"][ "my_placeholder_url"] == "http://localhost/static/blogs/1/my_placeholder1.png" assert rv.get_json()["results"][ "my_placeholder_url_2"] == "http://localhost/static/blogs/2/my_placeholder2.png" assert rv.get_json()["results"][ "news_image_url"] == "http://localhost/static/news/1/news_image.png" assert rv.get_json()["results"][ "news_image_url_2"] == "http://localhost/static/news/2/news_image.png" assert rv.get_json()["results"][ "news_video_url"] == "http://localhost/static/news/1/news_video1.mp4" assert rv.get_json()["results"][ "news_video_url_2"] == "http://localhost/static/news/2/news_video2.mp4"