Release 0.1.1 - More testing and cleanup things (#15)

* add more tests (#14)

* add more tests

* add more tests, add admin class, update gitignore

* Add a clean command to setup (#13)
This commit is contained in:
Tyrel Souza 2016-12-09 10:10:13 -05:00 committed by GitHub
parent 49b21be77b
commit 784b66d111
4 changed files with 57 additions and 27 deletions

1
.gitignore vendored
View File

@ -26,3 +26,4 @@ coverage.xml
# Sphinx documentation # Sphinx documentation
docs/_build/ docs/_build/
db.sqlite3

View File

@ -1,4 +1,8 @@
from django.contrib import admin from django.contrib import admin
from .models import DBFile from .models import DBFile
admin.site.register(DBFile)
class DBFileAdmin(admin.ModelAdmin):
list_display = ('name', 'content_type')
admin.site.register(DBFile, DBFileAdmin)

View File

@ -8,6 +8,20 @@ except ImportError:
use_setuptools() use_setuptools()
from setuptools import setup from setuptools import setup
from setuptools import setup, Command
class CleanCommand(Command):
"""Custom clean command to tidy up the project root."""
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
os.system('rm -vrf ./build ./dist ./*.pyc ./*.tgz ./*.egg-info')
os.system('rm -vrf htmlcov .coverage')
os.system('rm -vrf .DS_Store')
setup( setup(
name="django-dbfilestorage", name="django-dbfilestorage",
version="0.1.0", version="0.1.0",
@ -41,5 +55,8 @@ setup(
"Programming Language :: Python :: 2", "Programming Language :: Python :: 2",
"Programming Language :: Python :: 3", "Programming Language :: Python :: 3",
"Topic :: Software Development :: Libraries :: Python Modules", "Topic :: Software Development :: Libraries :: Python Modules",
] ],
cmdclass={
'clean': CleanCommand,
}
) )

View File

@ -3,10 +3,13 @@ import os
from dbfilestorage.models import DBFile from dbfilestorage.models import DBFile
from django.contrib.auth.models import User
from django.core.files.storage import default_storage from django.core.files.storage import default_storage
from django.core.urlresolvers import reverse
from django.test import TestCase, Client from django.test import TestCase, Client
from django.test.utils import override_settings from django.test.utils import override_settings
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__)) PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
DEFAULT_FILE_STORAGE = "dbfilestorage.storage.DBFileStorage" DEFAULT_FILE_STORAGE = "dbfilestorage.storage.DBFileStorage"
@ -17,6 +20,7 @@ class DBFileTest(TestCase):
self.filename = "kris.jpg" self.filename = "kris.jpg"
self.filepath = os.path.join(PROJECT_ROOT, "test_files", self.filename) self.filepath = os.path.join(PROJECT_ROOT, "test_files", self.filename)
self.md5 = hashlib.md5(open(self.filepath, 'rb').read()).hexdigest() self.md5 = hashlib.md5(open(self.filepath, 'rb').read()).hexdigest()
self._upload()
def _upload(self): def _upload(self):
with open(self.filepath, 'rb') as f: with open(self.filepath, 'rb') as f:
@ -24,62 +28,66 @@ class DBFileTest(TestCase):
def test_upload(self): def test_upload(self):
""" Test that the file storage uploads and puts in DB Properly """ """ Test that the file storage uploads and puts in DB Properly """
name = self._upload() self.assertTrue(DBFile.objects.filter(name=self.md5).exists())
self.assertEqual(name, self.md5)
self.assertTrue(DBFile.objects.filter(name=name).exists()) def test_no_duplicate_upload(self):
""" Test that it won't make a new file if it already exists """
# uploads once in setup already
name2 = self._upload()
self.assertEqual(self.md5, name2)
def test_equality(self): def test_equality(self):
""" Test that the DB entry matches what is expected from the file """ """ Test that the DB entry matches what is expected from the file """
name = self._upload()
with open(self.filepath, 'rb') as f: with open(self.filepath, 'rb') as f:
dbf = DBFile.objects.get(name=name) dbf = DBFile.objects.get(name=self.md5)
self.assertEqual(dbf.b64.decode("base64"), self.assertEqual(dbf.b64.decode("base64"),
f.read()) f.read())
self.assertEqual(dbf.content_type, 'image/jpeg') self.assertEqual(dbf.content_type, 'image/jpeg')
def test_open(self): def test_open(self):
""" Test that the storage mechanism can upload """ """ Test that the storage mechanism can upload """
name = self._upload() dbf = default_storage.open(self.md5)
dbf = default_storage.open(name)
with open(self.filepath, 'rb') as f: with open(self.filepath, 'rb') as f:
self.assertEqual(dbf.read(), f.read()) self.assertEqual(dbf.read(), f.read())
def test_exists(self): def test_exists(self):
""" Test that the storage mechanism can check existance """ """ Test that the storage mechanism can check existance """
name = self._upload() self.assertTrue(default_storage.exists(self.md5))
self.assertTrue(default_storage.exists(name))
def test_delete(self): def test_delete(self):
""" Test Deletion """ """ Test Deletion """
name = self._upload() self.assertTrue(DBFile.objects.filter(name=self.md5).exists())
self.assertTrue(DBFile.objects.filter(name=name).exists()) default_storage.delete(self.md5)
default_storage.delete(name) self.assertFalse(DBFile.objects.filter(name=self.md5).exists())
self.assertFalse(DBFile.objects.filter(name=name).exists())
def test_path(self): def test_path(self):
""" Test the path is just the md5 name """ """ Test the path is just the md5 name """
name = self._upload() path = default_storage.path(self.md5)
path = default_storage.path(name) self.assertEqual(self.md5, path)
self.assertEqual(name, path)
self.assertNotIn(self.filename, path) self.assertNotIn(self.filename, path)
def test_size(self): def test_size(self):
""" Ensure we can get the proper size """ """ Ensure we can get the proper size """
name = self._upload() size = default_storage.size(self.md5)
size = default_storage.size(name)
self.assertGreater(size, 0) self.assertGreater(size, 0)
def test_url(self): def test_url(self):
""" Test that the url returned is the md5 path not the filename """ """ Test that the url returned is the md5 path not the filename """
name = self._upload() self.assertIn(self.md5, default_storage.url(self.md5))
self.assertIn(self.md5, default_storage.url(name))
def test_view(self): def test_view(self):
client = Client() client = Client()
name = self._upload() url = default_storage.url(self.md5)
url = default_storage.url(name)
resp = client.get(url) resp = client.get(url)
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
def test_admin(self):
my_admin = User.objects.create_superuser(
username='tester',
email='test@test.com',
password='top_secret')
client = Client()
client.login(username=my_admin.username, password='top_secret')
url = reverse("admin:dbfilestorage_dbfile_changelist")
resp = client.get(url)
self.assertContains(resp, self.md5)