Tests use tempfiles and some new tests

This commit is contained in:
2016-01-09 13:55:21 +08:00
parent 0c2207894e
commit 76e2a9a43d
5 changed files with 99 additions and 51 deletions
+1
View File
@@ -16,3 +16,4 @@ class LocalConfig(object):
STORAGE_PROVIDER = "LOCAL"
STORAGE_CONTAINER = 'container_1'
STORAGE_ALLOWED_EXTENSIONS = []
STORAGE_SERVER = True
View File
-1
View File
@@ -1 +0,0 @@
// This is the javascript file
View File
+98 -50
View File
@@ -1,16 +1,17 @@
import os
from os.path import join, dirname
import pytest
from tests.config import LocalConfig as config
from flask_cloudy import (InvalidExtensionError, Object, get_driver_class,
get_file_extension, get_file_extension_type,
get_file_name, get_provider_name)
from libcloud.storage.base import Container, StorageDriver
CWD = os.path.dirname(__file__)
CWD = dirname(__file__)
CONTAINER = "%s/%s" % (CWD, config.STORAGE_CONTAINER) if config.STORAGE_PROVIDER == "LOCAL" else config.STORAGE_CONTAINER
CONTAINER = "%s/%s" % (
CWD, config.STORAGE_CONTAINER
) if config.STORAGE_PROVIDER == "LOCAL" else config.STORAGE_CONTAINER
class TestUtilities:
@@ -31,6 +32,7 @@ class TestUtilities:
def test_get_provider_name(self):
class GoogleStorageDriver(object):
pass
driver = GoogleStorageDriver()
assert get_provider_name(driver) == "google_storage"
@@ -64,6 +66,22 @@ class TestLocalStorage:
o = storage.create(object_name)
assert isinstance(o, Object)
def test_empty_storage_object_evaluates_false(self, storage):
"""Check that storage object eval false if they are empty."""
object_name = "hello.txt"
o = storage.create(object_name)
if o:
raise ValueError("Object evaluated to false")
def test_storage_object_evaluates_true(self, storage, temp_txt_file):
"""Check that storage object eval true if they are not empty."""
o = storage.upload(temp_txt_file.name)
o1 = storage.get(o.name)
if not o:
raise ValueError("Storage upload object evaluated to false")
if not o1:
raise ValueError("Storage get object evaluated to false")
def test_object_type_extension(self, storage):
object_name = "hello.jpg"
o = storage.create(object_name)
@@ -81,20 +99,42 @@ class TestLocalStorage:
p = "%s/%s" % (o.container.name, o.name)
assert o.path.endswith(p)
def test_storage_upload_invalid(self, storage):
def test_storage_upload_invalid(self, storage, temp_js_file):
"""Check .js extensions are not allowed by default."""
object_name = "my-js/hello.js"
with pytest.raises(InvalidExtensionError):
storage.upload(CWD + "/data/hello.js", name=object_name)
storage.upload(temp_js_file.name, name=object_name)
def test_storage_upload_ovewrite(self, storage):
object_name = "my-txt-hello.txt"
o = storage.upload(CWD + "/data/hello.txt", name=object_name, overwrite=True)
assert isinstance(o, Object)
assert o.name == object_name
def test_storage_upload_overwrite(self, storage, temp_txt_file,
temp_js_file):
object_name = "hello.txt"
o = storage.upload(temp_js_file.name,
overwrite=True,
allowed_extensions=["js"])
o_ow = storage.upload(temp_txt_file.name,
name=object_name,
overwrite=True)
assert isinstance(o_ow, Object)
assert o_ow.name == o.name
assert o_ow.name == object_name
def test_storage_get(self, storage):
object_name = "my-txt-helloIII.txt"
o = storage.upload(CWD + "/data/hello.txt", name=object_name, overwrite=True)
def test_storage_upload_no_overwrite(self, storage, temp_txt_file,
temp_js_file):
object_name = "hello.txt"
o = storage.upload(temp_js_file.name,
overwrite=True,
allowed_extensions=["js"])
o_no_ow = storage.upload(temp_txt_file.name,
name=object_name,
overwrite=False)
assert isinstance(o_no_ow, Object)
assert o.name != o_no_ow.name
def test_storage_get(self, storage, temp_txt_file):
object_name = "test_storage_get.txt"
o = storage.upload(temp_txt_file.name,
name=object_name,
overwrite=True)
o2 = storage.get(o.name)
assert isinstance(o2, Object)
@@ -102,58 +142,66 @@ class TestLocalStorage:
o2 = storage.get("idonexist")
assert o2 is None
def test_storage_upload(self, storage):
object_name = "my-txt-hello2.txt"
storage.upload(CWD + "/data/hello.txt", name=object_name)
o = storage.upload(CWD + "/data/hello.txt", name=object_name)
def test_storage_upload(self, storage, temp_txt_file):
object_name = "test_storage_upload.txt"
storage.upload(temp_txt_file.name, name=object_name)
o = storage.upload(temp_txt_file.name, name=object_name)
assert isinstance(o, Object)
assert o.name != object_name
def test_storage_upload_use_filename_name(self, storage):
object_name = "hello.js"
o = storage.upload(CWD + "/data/hello.js", overwrite=True, allowed_extensions=["js"])
def test_storage_upload_use_filename_name(self, storage, temp_js_file):
"""Check that uploaded files retain thier name."""
object_name = os.path.basename(temp_js_file.name)
o = storage.upload(temp_js_file.name,
overwrite=True,
allowed_extensions=["js"])
assert o.name == object_name
def test_storage_upload_append_extension(self, storage):
object_name = "my-txt-hello-hello"
o = storage.upload(CWD + "/data/hello.txt", object_name, overwrite=True)
def test_storage_upload_append_extension(self, storage, temp_txt_file):
"""Check that uploaded names get an appended extension."""
object_name = "test_storage_upload_append_extension"
o = storage.upload(temp_txt_file.name, object_name, overwrite=True)
assert get_file_extension(o.name) == "txt"
def test_storage_upload_with_prefix(self, storage):
object_name = "my-txt-hello-hello"
def test_storage_upload_with_prefix(self, storage, temp_txt_file):
object_name = os.path.splitext(os.path.basename(temp_txt_file.name))[0]
prefix = "dir1/dir2/dir3/"
full_name = "%s%s.%s" % (prefix, object_name, "txt")
o = storage.upload(CWD + "/data/hello.txt", name=object_name, prefix=prefix, overwrite=True)
o = storage.upload(temp_txt_file.name,
name=object_name,
prefix=prefix,
overwrite=True)
assert full_name in storage
assert o.name == full_name
def test_save_to(self, storage):
object_name = "my-txt-hello-to-save.txt"
o = storage.upload(CWD + "/data/hello.txt", name=object_name)
file = o.save_to(CWD + "/data", overwrite=True)
file2 = o.save_to(CWD + "/data", name="my_new_file", overwrite=True)
def test_save_to(self, storage, temp_dir, temp_txt_file):
object_name = "test_save_to.txt"
o = storage.upload(temp_txt_file.name, name=object_name)
file = o.save_to(join(CWD, "data"), overwrite=True)
file2 = o.save_to(
join(CWD, "data"),
name="my_new_file",
overwrite=True)
assert os.path.isfile(file)
assert file2 == CWD + "/data/my_new_file.txt"
assert file2 == join(CWD, "data", "my_new_file.txt")
def test_werkzeug_upload(self, storage):
try:
import werkzeug
except ImportError:
return
object_name = "my-txt-hello.txt"
filepath = CWD + "/data/hello.txt"
file = None
with open(filepath, 'rb') as fp:
file = werkzeug.datastructures.FileStorage(fp)
file.filename = object_name
o = storage.upload(file, overwrite=True)
assert isinstance(o, Object)
assert o.name == object_name
def test_local_server(self, storage):
def test_local_server(self, storage, temp_txt_file):
"""Test the local server function."""
object_name = "test_local_server.txt"
storage.upload(CWD + "/data/hello.txt", name=object_name, overwrite=True)
storage.upload(temp_txt_file.name, name=object_name, overwrite=True)
print(storage.app.view_functions)
file_server = storage.app.view_functions['FLASK_CLOUDY_SERVER']
response = file_server(object_name)
assert response.status_code == 200
# @pytest.mark.timeout(30)
# def test_werkzeug_upload(self, storage, temp_png_file):
# try:
# import werkzeug
# except ImportError:
# return
# object_name = "test_werkzeug_upload.txt"
# file = werkzeug.datastructures.FileStorage(temp_png_file)
# o = storage.upload(file, overwrite=True)
# assert isinstance(o, Object)
# assert o.name == object_name