From 4fa1caafa388f09c76f5ee4a0c0543be4f134eb1 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 8 Dec 2016 15:30:39 -0500 Subject: [PATCH] More tests (#9) * add pip install command * more tests * add more docstrings, and add a view test --- dbfilestorage/urls.py | 7 +++---- setup.cfg | 5 ++--- tests/tests.py | 47 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/dbfilestorage/urls.py b/dbfilestorage/urls.py index 1fe30a0..35b7353 100644 --- a/dbfilestorage/urls.py +++ b/dbfilestorage/urls.py @@ -1,7 +1,6 @@ -from django.conf.urls import patterns, url +from django.conf.urls import url import views -urlpatterns = patterns( - '', +urlpatterns = [ url(r'^(?P.*)$', views.show_file, name="dbstorage_file"), -) +] diff --git a/setup.cfg b/setup.cfg index 1e9c139..1e39ce9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,6 @@ verbosity=1 detailed-errors=1 with-coverage=1 cover-package=dbfilestorage -debug=nose.loader -pdb=1 -pdb-failures=1 +#pdb=1 +#pdb-failures=1 diff --git a/tests/tests.py b/tests/tests.py index 170b37f..60ff34f 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -4,12 +4,14 @@ import os from dbfilestorage.models import DBFile 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 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): def setUp(self): self.filename = "kris.jpg" @@ -20,37 +22,64 @@ class DBFileTest(TestCase): with open(self.filepath, 'rb') as f: return default_storage.save(self.filepath, f) - @override_settings( - DEFAULT_FILE_STORAGE="dbfilestorage.storage.DBStorage") def test_upload(self): """ Test that the file storage uploads and puts in DB Properly """ name = self._upload() self.assertEqual(name, self.md5) self.assertTrue(DBFile.objects.filter(name=name).exists()) - @override_settings( - DEFAULT_FILE_STORAGE="dbfilestorage.storage.DBStorage") def test_equality(self): """ Test that the DB entry matches what is expected from the file """ name = self._upload() + with open(self.filepath, 'rb') as f: dbf = DBFile.objects.get(name=name) self.assertEqual(dbf.b64.decode("base64"), f.read()) self.assertEqual(dbf.content_type, 'image/jpeg') - @override_settings( - DEFAULT_FILE_STORAGE="dbfilestorage.storage.DBStorage") def test_open(self): """ Test that the storage mechanism can upload """ name = self._upload() + dbf = default_storage.open(name) with open(self.filepath, 'rb') as f: self.assertEqual(dbf.read(), f.read()) - @override_settings( - DEFAULT_FILE_STORAGE="dbfilestorage.storage.DBStorage") def test_exists(self): """ Test that the storage mechanism can check existance """ name = self._upload() + 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)