Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Websites UFRPE
Django Private Storage
Commits
9b1a73f2
Commit
9b1a73f2
authored
Feb 06, 2018
by
Diederik van der Boor
Browse files
Added test for generate_filename()
parent
53efdf43
Changes
5
Show whitespace changes
Inline
Side-by-side
private_storage/fields.py
View file @
9b1a73f2
...
...
@@ -34,7 +34,7 @@ class PrivateFileField(models.FileField):
}
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
_
upload
=
kwargs
.
pop
(
'upload_subfolder'
,
None
)
self
.
upload
_subfolder
=
kwargs
.
pop
(
'upload_subfolder'
,
None
)
self
.
content_types
=
kwargs
.
pop
(
"content_types"
,
None
)
or
()
self
.
max_file_size
=
kwargs
.
pop
(
"max_file_size"
,
None
)
...
...
@@ -74,9 +74,9 @@ class PrivateFileField(models.FileField):
path_parts
.
append
(
dirname
)
# Add our custom subdir function.
subdir_func
=
self
.
_
upload
if
subdir_func
:
extra_dirs
=
subdir_func
(
instance
)
upload_subfolder
=
self
.
upload
_subfolder
if
upload_subfolder
:
extra_dirs
=
upload_subfolder
(
instance
)
if
isinstance
(
extra_dirs
,
string_types
):
# Avoid mistakes by developers, no "s/u/b/p/a/t/h/"
path_parts
.
append
(
self
.
storage
.
get_valid_name
(
extra_dirs
))
...
...
private_storage/tests/models.py
0 → 100644
View file @
9b1a73f2
from
django.db
import
models
from
django.utils.text
import
slugify
from
private_storage.fields
import
PrivateFileField
class
SimpleDossier
(
models
.
Model
):
file
=
PrivateFileField
()
class
UploadToDossier
(
models
.
Model
):
file
=
PrivateFileField
(
upload_to
=
'dossier2'
)
class
CustomerDossier
(
models
.
Model
):
def
upload_subfolder
(
self
):
# self.pk is still None here!
return
[
slugify
(
self
.
customer
)]
customer
=
models
.
CharField
(
max_length
=
100
)
file
=
PrivateFileField
(
upload_to
=
'dossier3'
,
upload_subfolder
=
upload_subfolder
)
private_storage/tests/test_imports.py
View file @
9b1a73f2
# Most pathetic test case ever, only see if all files are importable.
# TODO: write real tests.
# Most pathetic test case ever, see if all files are importable.
import
private_storage
import
private_storage.appconfig
import
private_storage.fields
...
...
private_storage/tests/test_models.py
0 → 100644
View file @
9b1a73f2
import
os
import
shutil
from
django.conf
import
settings
from
django.core.files.uploadedfile
import
SimpleUploadedFile
from
django.test
import
TestCase
from
private_storage.tests.models
import
CustomerDossier
,
SimpleDossier
,
UploadToDossier
class
PrivateFileTestCase
(
TestCase
):
def
tearDown
(
self
):
"""
Empty the test folder after each test case.
"""
super
(
PrivateFileTestCase
,
self
).
tearDown
()
shutil
.
rmtree
(
settings
.
PRIVATE_STORAGE_ROOT
)
def
assertExists
(
self
,
*
parts
):
"""
Extra assert, check whether a path exists.
"""
path
=
os
.
path
.
join
(
settings
.
PRIVATE_STORAGE_ROOT
,
*
parts
)
if
not
os
.
path
.
exists
(
path
):
raise
self
.
failureException
(
"Path {} does not exist"
.
format
(
path
))
class
ModelTests
(
PrivateFileTestCase
):
def
test_simple
(
self
):
SimpleDossier
.
objects
.
create
(
file
=
SimpleUploadedFile
(
'test1.txt'
,
b
'test1'
))
self
.
assertExists
(
'test1.txt'
)
def
test_upload_to
(
self
):
UploadToDossier
.
objects
.
create
(
file
=
SimpleUploadedFile
(
'test2.txt'
,
b
'test2'
))
self
.
assertExists
(
'dossier2'
,
'test2.txt'
)
def
test_upload_subfolder
(
self
):
obj
=
CustomerDossier
.
objects
.
create
(
customer
=
'cust1'
,
file
=
SimpleUploadedFile
(
'test3.txt'
,
b
'test3'
))
self
.
assertExists
(
'dossier3'
,
'cust1'
,
'test3.txt'
)
runtests.py
View file @
9b1a73f2
...
...
@@ -7,8 +7,6 @@ from os import path
if
not
settings
.
configured
:
module_root
=
path
.
dirname
(
path
.
realpath
(
__file__
))
sys
.
path
.
insert
(
0
,
path
.
join
(
module_root
,
'example'
))
settings
.
configure
(
DEBUG
=
False
,
# will be False anyway by DjangoTestRunner.
DATABASES
=
{
...
...
@@ -42,6 +40,7 @@ if not settings.configured:
},
],
AWS_PRIVATE_STORAGE_BUCKET_NAME
=
'foobar'
,
PRIVATE_STORAGE_ROOT
=
path
.
join
(
module_root
,
'test-media-root'
),
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment