Skip to content

Commit 6eafd67

Browse files
committed
test bugfixes
1 parent e63c7be commit 6eafd67

File tree

6 files changed

+172
-60
lines changed

6 files changed

+172
-60
lines changed

.gitignore

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,7 @@ docs/_build*
3333

3434
.tox
3535

36-
tests/*
3736
!tests/__init__.py
3837
!tests/config.py
3938
!tests/test_cloudy.py
40-
41-
tests/data/*
42-
!tests/data/hello.txt
43-
!tests/data/hello.js
44-
45-
tests/container_1/*
46-
!tests/container_1/empty
39+
!tests/testconf.py

tests/conftest.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""Defines fixtures available to all tests."""
2+
3+
import pytest
4+
from tempfile import TemporaryDirectory, NamedTemporaryFile
5+
import os
6+
from flask import Flask
7+
from flask_cloudy import Storage
8+
from tests.config import LocalConfig
9+
import random
10+
11+
12+
@pytest.yield_fixture(scope='function')
13+
def temp_dir():
14+
"""A temporary directory for each test."""
15+
with TemporaryDirectory() as temp_dir:
16+
yield temp_dir
17+
18+
@pytest.yield_fixture(scope='function')
19+
def temp_container():
20+
"""A temporary container directory for each test."""
21+
with TemporaryDirectory() as temp_dir:
22+
yield temp_dir
23+
24+
@pytest.yield_fixture(scope='function')
25+
def temp_txt_file():
26+
"""A temporary txt file with random contents for each test."""
27+
with NamedTemporaryFile(mode="w+", suffix=".txt") as temp_file:
28+
temp_file.write('%s' % random.random())
29+
temp_file.file.seek(0)
30+
yield temp_file
31+
32+
33+
@pytest.yield_fixture(scope='function')
34+
def temp_png_file():
35+
"""A temporary txt file with random contents for each test."""
36+
with NamedTemporaryFile(suffix=".png") as temp_file:
37+
yield temp_file
38+
39+
40+
@pytest.yield_fixture(scope='function')
41+
def temp_js_file():
42+
"""A temporary js file with random contents for each test."""
43+
with NamedTemporaryFile(mode="w+", suffix=".js") as temp_file:
44+
temp_file.write('%s' % random.random())
45+
temp_file.file.seek(0)
46+
yield temp_file
47+
48+
49+
@pytest.yield_fixture(scope='function')
50+
def app(temp_container):
51+
"""An application for the tests."""
52+
app = Flask(__name__)
53+
LocalConfig.STORAGE_CONTAINER = temp_container
54+
app.config.from_object(LocalConfig)
55+
56+
ctx = app.test_request_context()
57+
ctx.push()
58+
59+
yield app
60+
61+
ctx.pop()
62+
63+
64+
@pytest.fixture(scope='function')
65+
def storage(app):
66+
"""A flask-cloudy storage instance for tests."""
67+
storage = Storage(app=app)
68+
storage.app = app
69+
return storage

tests/container_1/empty

Whitespace-only changes.

tests/data/hello.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/data/hello.txt

Whitespace-only changes.

tests/test_cloudy.py

Lines changed: 102 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
21
import os
2+
from os.path import join, dirname
33
import pytest
44
from tests.config import LocalConfig as config
5-
65
from flask_cloudy import (InvalidExtensionError, Object, get_driver_class,
76
get_file_extension, get_file_extension_type,
87
get_file_name, get_provider_name)
98
from libcloud.storage.base import Container, StorageDriver
109

11-
CWD = os.path.dirname(__file__)
10+
CWD = dirname(__file__)
1211

13-
CONTAINER = "%s/%s" % (CWD, config.STORAGE_CONTAINER) if config.STORAGE_PROVIDER == "LOCAL" else config.STORAGE_CONTAINER
12+
CONTAINER = "%s/%s" % (
13+
CWD, config.STORAGE_CONTAINER
14+
) if config.STORAGE_PROVIDER == "LOCAL" else config.STORAGE_CONTAINER
1415

1516

1617
class TestUtilities:
@@ -31,6 +32,7 @@ def test_get_file_name(self):
3132
def test_get_provider_name(self):
3233
class GoogleStorageDriver(object):
3334
pass
35+
3436
driver = GoogleStorageDriver()
3537
assert get_provider_name(driver) == "google_storage"
3638

@@ -64,6 +66,22 @@ def test_storage_object(self, storage):
6466
o = storage.create(object_name)
6567
assert isinstance(o, Object)
6668

69+
def test_empty_storage_object_evaluates_false(self, storage):
70+
"""Check that storage object eval false if they are empty."""
71+
object_name = "hello.txt"
72+
o = storage.create(object_name)
73+
if o:
74+
raise ValueError("Object evaluated to false")
75+
76+
def test_storage_object_evaluates_true(self, storage, temp_txt_file):
77+
"""Check that storage object eval true if they are not empty."""
78+
o = storage.upload(temp_txt_file.name)
79+
o1 = storage.get(o.name)
80+
if not o:
81+
raise ValueError("Storage upload object evaluated to false")
82+
if not o1:
83+
raise ValueError("Storage get object evaluated to false")
84+
6785
def test_object_type_extension(self, storage):
6886
object_name = "hello.jpg"
6987
o = storage.create(object_name)
@@ -81,79 +99,112 @@ def test_object_object_path(self, storage):
8199
p = "%s/%s" % (o.container.name, o.name)
82100
assert o.path.endswith(p)
83101

84-
def test_storage_upload_invalid(self, storage):
102+
def test_storage_upload_invalid(self, storage, temp_js_file):
103+
"""Check .js extensions are not allowed by default."""
85104
object_name = "my-js/hello.js"
86105
with pytest.raises(InvalidExtensionError):
87-
storage.upload(CWD + "/data/hello.js", name=object_name)
106+
storage.upload(temp_js_file.name, name=object_name)
88107

89-
def test_storage_upload_ovewrite(self, storage):
90-
object_name = "my-txt-hello.txt"
91-
o = storage.upload(CWD + "/data/hello.txt", name=object_name, overwrite=True)
92-
assert isinstance(o, Object)
93-
assert o.name == object_name
94-
95-
def test_storage_get(self, storage):
96-
object_name = "my-txt-helloIII.txt"
97-
o = storage.upload(CWD + "/data/hello.txt", name=object_name, overwrite=True)
108+
def test_storage_upload_overwrite(self, storage, temp_txt_file,
109+
temp_js_file):
110+
object_name = "hello.txt"
111+
o = storage.upload(temp_js_file.name,
112+
name=object_name,
113+
overwrite=True,
114+
allowed_extensions=["js"])
115+
o_ow = storage.upload(temp_txt_file.name,
116+
name=object_name,
117+
overwrite=True)
118+
assert isinstance(o_ow, Object)
119+
assert o_ow.name == o.name
120+
assert o_ow.name == object_name
121+
122+
def test_storage_upload_no_overwrite(self, storage, temp_txt_file,
123+
temp_js_file):
124+
object_name = "hello.txt"
125+
o = storage.upload(temp_js_file.name,
126+
overwrite=True,
127+
allowed_extensions=["js"])
128+
o_no_ow = storage.upload(temp_txt_file.name,
129+
name=object_name,
130+
overwrite=False)
131+
assert isinstance(o_no_ow, Object)
132+
assert o.name != o_no_ow.name
133+
134+
def test_storage_get(self, storage, temp_txt_file):
135+
object_name = "test_storage_get.txt"
136+
o = storage.upload(temp_txt_file.name,
137+
name=object_name,
138+
overwrite=True)
98139
o2 = storage.get(o.name)
99140
assert isinstance(o2, Object)
100141

101142
def test_storage_get_none(self, storage):
102143
o2 = storage.get("idonexist")
103144
assert o2 is None
104145

105-
def test_storage_upload(self, storage):
106-
object_name = "my-txt-hello2.txt"
107-
storage.upload(CWD + "/data/hello.txt", name=object_name)
108-
o = storage.upload(CWD + "/data/hello.txt", name=object_name)
146+
def test_storage_upload(self, storage, temp_txt_file):
147+
object_name = "test_storage_upload.txt"
148+
storage.upload(temp_txt_file.name, name=object_name)
149+
o = storage.upload(temp_txt_file.name, name=object_name)
109150
assert isinstance(o, Object)
110151
assert o.name != object_name
111152

112-
def test_storage_upload_use_filename_name(self, storage):
113-
object_name = "hello.js"
114-
o = storage.upload(CWD + "/data/hello.js", overwrite=True, allowed_extensions=["js"])
153+
def test_storage_upload_use_filename_name(self, storage, temp_js_file):
154+
"""Check that uploaded files retain thier name."""
155+
object_name = os.path.basename(temp_js_file.name)
156+
o = storage.upload(temp_js_file.name,
157+
overwrite=True,
158+
allowed_extensions=["js"])
115159
assert o.name == object_name
116160

117-
def test_storage_upload_append_extension(self, storage):
118-
object_name = "my-txt-hello-hello"
119-
o = storage.upload(CWD + "/data/hello.txt", object_name, overwrite=True)
161+
def test_storage_upload_append_extension(self, storage, temp_txt_file):
162+
"""Check that uploaded names get an appended extension."""
163+
object_name = "test_storage_upload_append_extension"
164+
o = storage.upload(temp_txt_file.name, object_name, overwrite=True)
120165
assert get_file_extension(o.name) == "txt"
121166

122-
def test_storage_upload_with_prefix(self, storage):
123-
object_name = "my-txt-hello-hello"
167+
def test_storage_upload_with_prefix(self, storage, temp_txt_file):
168+
object_name = os.path.splitext(os.path.basename(temp_txt_file.name))[0]
124169
prefix = "dir1/dir2/dir3/"
125170
full_name = "%s%s.%s" % (prefix, object_name, "txt")
126-
o = storage.upload(CWD + "/data/hello.txt", name=object_name, prefix=prefix, overwrite=True)
171+
o = storage.upload(temp_txt_file.name,
172+
name=object_name,
173+
prefix=prefix,
174+
overwrite=True)
127175
assert full_name in storage
128176
assert o.name == full_name
129177

130-
def test_save_to(self, storage):
131-
object_name = "my-txt-hello-to-save.txt"
132-
o = storage.upload(CWD + "/data/hello.txt", name=object_name)
133-
file = o.save_to(CWD + "/data", overwrite=True)
134-
file2 = o.save_to(CWD + "/data", name="my_new_file", overwrite=True)
178+
def test_save_to(self, storage, temp_dir, temp_txt_file):
179+
object_name = "test_save_to.txt"
180+
o = storage.upload(temp_txt_file.name, name=object_name)
181+
file = o.save_to(temp_dir, overwrite=True)
182+
file2 = o.save_to(
183+
temp_dir,
184+
name="my_new_file",
185+
overwrite=True)
186+
print(o, o.name, file, temp_dir, file2)
135187
assert os.path.isfile(file)
136-
assert file2 == CWD + "/data/my_new_file.txt"
188+
assert file2 == join(temp_dir, "my_new_file.txt")
137189

138-
def test_werkzeug_upload(self, storage):
139-
try:
140-
import werkzeug
141-
except ImportError:
142-
return
143-
object_name = "my-txt-hello.txt"
144-
filepath = CWD + "/data/hello.txt"
145-
file = None
146-
with open(filepath, 'rb') as fp:
147-
file = werkzeug.datastructures.FileStorage(fp)
148-
file.filename = object_name
149-
o = storage.upload(file, overwrite=True)
150-
assert isinstance(o, Object)
151-
assert o.name == object_name
152-
153-
def test_local_server(self, storage):
190+
def test_local_server(self, storage, temp_txt_file):
154191
"""Test the local server function."""
155192
object_name = "test_local_server.txt"
156-
storage.upload(CWD + "/data/hello.txt", name=object_name, overwrite=True)
193+
storage.upload(temp_txt_file.name, name=object_name, overwrite=True)
194+
print(storage.app.view_functions)
157195
file_server = storage.app.view_functions['FLASK_CLOUDY_SERVER']
158196
response = file_server(object_name)
159197
assert response.status_code == 200
198+
199+
@pytest.mark.timeout(30)
200+
def test_werkzeug_upload(self, storage, temp_png_file):
201+
try:
202+
import werkzeug
203+
except ImportError:
204+
return
205+
object_name = "test-werkzeug-upload.txt"
206+
file = werkzeug.datastructures.FileStorage(temp_png_file)
207+
file.filename = object_name
208+
o = storage.upload(file, overwrite=True)
209+
assert isinstance(o, Object)
210+
assert o.name == object_name

0 commit comments

Comments
 (0)