More tests (#9)

* add pip install command

* more tests

* add more docstrings, and add a view test
This commit is contained in:
Tyrel Souza 2016-12-08 15:30:39 -05:00 committed by GitHub
parent b73889bd67
commit 4fa1caafa3
3 changed files with 43 additions and 16 deletions

View File

@ -1,7 +1,6 @@
from django.conf.urls import patterns, url from django.conf.urls import url
import views import views
urlpatterns = patterns( urlpatterns = [
'',
url(r'^(?P<name>.*)$', views.show_file, name="dbstorage_file"), url(r'^(?P<name>.*)$', views.show_file, name="dbstorage_file"),
) ]

View File

@ -3,7 +3,6 @@ verbosity=1
detailed-errors=1 detailed-errors=1
with-coverage=1 with-coverage=1
cover-package=dbfilestorage cover-package=dbfilestorage
debug=nose.loader #pdb=1
pdb=1 #pdb-failures=1
pdb-failures=1

View File

@ -4,12 +4,14 @@ import os
from dbfilestorage.models import DBFile from dbfilestorage.models import DBFile
from django.core.files.storage import default_storage from django.core.files.storage import default_storage
from django.test import TestCase 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.DBStorage"
@override_settings(DEFAULT_FILE_STORAGE=DEFAULT_FILE_STORAGE)
class DBFileTest(TestCase): class DBFileTest(TestCase):
def setUp(self): def setUp(self):
self.filename = "kris.jpg" self.filename = "kris.jpg"
@ -20,37 +22,64 @@ class DBFileTest(TestCase):
with open(self.filepath, 'rb') as f: with open(self.filepath, 'rb') as f:
return default_storage.save(self.filepath, f) return default_storage.save(self.filepath, f)
@override_settings(
DEFAULT_FILE_STORAGE="dbfilestorage.storage.DBStorage")
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() name = self._upload()
self.assertEqual(name, self.md5) self.assertEqual(name, self.md5)
self.assertTrue(DBFile.objects.filter(name=name).exists()) self.assertTrue(DBFile.objects.filter(name=name).exists())
@override_settings(
DEFAULT_FILE_STORAGE="dbfilestorage.storage.DBStorage")
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() 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=name)
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')
@override_settings(
DEFAULT_FILE_STORAGE="dbfilestorage.storage.DBStorage")
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() name = self._upload()
dbf = default_storage.open(name) 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())
@override_settings(
DEFAULT_FILE_STORAGE="dbfilestorage.storage.DBStorage")
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() name = self._upload()
self.assertTrue(default_storage.exists(name)) self.assertTrue(default_storage.exists(name))
def test_delete(self):
""" Test Deletion """
name = self._upload()
self.assertTrue(DBFile.objects.filter(name=name).exists())
default_storage.delete(name)
self.assertFalse(DBFile.objects.filter(name=name).exists())
def test_path(self):
""" Test the path is just the md5 name """
name = self._upload()
path = default_storage.path(name)
self.assertEqual(name, path)
self.assertNotIn(self.filename, path)
def test_size(self):
""" Ensure we can get the proper size """
name = self._upload()
size = default_storage.size(name)
self.assertGreater(size, 0)
def test_url(self):
""" Test that the url returned is the md5 path not the filename """
name = self._upload()
self.assertIn(self.md5, default_storage.url(name))
def test_view(self):
client = Client()
name = self._upload()
url = default_storage.url(name)
resp = client.get(url)
self.assertEqual(resp.status_code, 200)