def test_set_model_attrs(self, mock_model): file_upload = FileUpload() file_upload.file_data = self.file_data file_upload._set_model_attrs(mock_model) assert mock_model.my_video__file_name == "my_video.mp4" assert mock_model.my_video__mime_type == "video/mpeg" assert mock_model.my_video__file_type == "mp4" assert mock_model.my_placeholder__file_name == "my_placeholder.png" assert mock_model.my_placeholder__mime_type == "image/png" assert mock_model.my_placeholder__file_type == "jpg" with pytest.raises(AttributeError): file_upload.file_data[0]["bananas"] = "bananas" file_upload._set_model_attrs(mock_model)
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 blog(): from tests.fixtures.models import MockBlogModel if request.method == "GET": blog_post = MockBlogModel( id=1, name="My Blog Post", my_video__file_name="my_video.mp4", my_video__mime_type="video/mpeg", my_video__file_type="mp4", ) file_upload = FileUpload(app, db) # Warning - The UPLOAD_FOLDER - only needs to be reset for testing! current_app.config["UPLOAD_FOLDER"] = "test_path" file_upload.init_app(app, db) return file_upload.stream_file(blog_post, filename="my_video") if request.method == "POST": my_video = request.files["my_video"] my_placeholder = request.files["my_placeholder"] blog_post = MockBlogModel(id=2, name="My Blog Post") file_upload = FileUpload(app, db) blog = file_upload.save_files(blog_post, files={ "my_video": my_video, "my_placeholder": my_placeholder, }) blog_post = MockBlogModel() blog_data = blog_post.get_blog() return {"blog": f"{blog_data}"}, 200
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"
def test_init_app(self, create_app, mock_blog_model, flask_app): file_upload = FileUpload() file_upload.init_app(flask_app, db) assert isinstance(file_upload.app, Flask)
import pytest from flask import Flask, request, send_from_directory, current_app from flask_sqlalchemy import SQLAlchemy from flask_file_upload.file_upload import FileUpload from flask_file_upload.file_utils import FileUtils from flask_file_upload._config import Config app = Flask(__name__) app.config["SERVERNAME"] = "127.0.0.1:5000" app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" app.config["UPLOAD_FOLDER"] = "tests/test_path" app.config["ALLOWED_EXTENSIONS"] = ["jpg", "png", "mov", "mp4", "mpg"] app.config["MAX_CONTENT_LENGTH"] = 1000 * 1024 * 1024 db = SQLAlchemy(app) file_upload = FileUpload(app, db) @app.route("/config_test", methods=["POST"]) def config_test(): from tests.fixtures.models import MockBlogModel file = request.files["file"] current_app.config["UPLOAD_FOLDER"] = "tests/test_path" config = Config() config.init_config(app) file_util = FileUtils(MockBlogModel(name="test_save"), config) file_util.save_file(file, 1) return {"data": "hello"}, 200
from flask import (Flask, url_for, render_template, json, request, redirect) from flask_file_upload.file_upload import FileUpload from flask_user import UserManager, roles_required, current_user import os from flask_sqlalchemy import SQLAlchemy #from flask.ext.mobility import Mobility db = SQLAlchemy() file_upload = FileUpload(db=db) def create_app(test_config=None): # create and configure the app app = Flask(__name__, instance_relative_config=True) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.from_mapping(test_config) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass from . import database with app.app_context(): db.init_app(app)