From cebe70d471d222fa3296424d0dfdd63b6bd171af Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 8 Dec 2016 14:54:53 -0500 Subject: [PATCH 1/5] Setup for Coverage and Codcov.io (#6) * coverage * coveragerc * fix placeholder --- .coveragerc | 13 +++++++++++++ requirements.txt | 1 + setup.cfg | 9 +++++++++ 3 files changed, 23 insertions(+) create mode 100644 .coveragerc create mode 100644 setup.cfg diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..e227db0 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,13 @@ +[run] +branch = True +source = dbfilestorage + +[report] +exclude_lines = + if self.debug: + pragma: no cover + raise NotImplementedError + if __name__ == .__main__.: +ignore_errors = True +omit = + tests/* diff --git a/requirements.txt b/requirements.txt index edde82d..d09cb1e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Django==1.10.4 Sphinx==1.5 +coverage==4.2 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..1e9c139 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,9 @@ +[nosetests] +verbosity=1 +detailed-errors=1 +with-coverage=1 +cover-package=dbfilestorage +debug=nose.loader +pdb=1 +pdb-failures=1 + From b73889bd67676f60da7e3f952b44fa935f4d32b5 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 8 Dec 2016 14:58:22 -0500 Subject: [PATCH 2/5] add pip install command (#8) --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b80d5b..9ce0e44 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,11 @@ Intended to be used in tests, never in production. ## INSTALLATION -In your project's `settings.py` file, add `'dbfilestorage'` to your `INSTALLED_APPS`: +``` +pip install django-dbfilestorage +``` + +Then in your project's `settings.py` file, add `'dbfilestorage'` to your `INSTALLED_APPS`: ```python INSTALLED_APPS = ( From 4fa1caafa388f09c76f5ee4a0c0543be4f134eb1 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 8 Dec 2016 15:30:39 -0500 Subject: [PATCH 3/5] 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) From 62a5118a7e5ee62f50f4b64fb28dd87604d0b61b Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 8 Dec 2016 15:31:46 -0500 Subject: [PATCH 4/5] Update Version number --- README.md | 1 - docs/conf.py | 4 ++-- setup.py | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9ce0e44..faefb51 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # Django-dbfilestorage - [![CircleCI](https://circleci.com/gh/tyrelsouza/django-dbfilestorage.svg?style=svg)](https://circleci.com/gh/tyrelsouza/django-dbfilestorage) [![codecov](https://codecov.io/gh/tyrelsouza/django-dbfilestorage/branch/master/graph/badge.svg)](https://codecov.io/gh/tyrelsouza/django-dbfilestorage) Custom file storage for Django that stores file data and content type in the database. diff --git a/docs/conf.py b/docs/conf.py index f56ad0a..febbc98 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -54,9 +54,9 @@ author = u'Tyrel Souza' # built documents. # # The short X.Y version. -version = u'0.0.3' +version = u'0.0.4' # The full version, including alpha/beta/rc tags. -release = u'0.0.3' +release = u'0.0.4' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/setup.py b/setup.py index 47d4510..2a10b26 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ except ImportError: setup( name="django-dbfilestorage", - version="0.0.3", + version="0.0.4", description="Database backed file storage for testing.", long_description="Database backed file storage for testing. Stores files as base64 encoded textfields.", author="Tyrel Souza", From 3f7d333d98547c4425cde43d963ff7e479400f02 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 8 Dec 2016 15:43:34 -0500 Subject: [PATCH 5/5] Rename Storage Class (#10) * Rename Storage Class * bump version to 0.1.0 --- dbfilestorage/storage.py | 2 +- docs/Makefile | 4 ++-- docs/conf.py | 4 ++-- docs/make.bat | 2 +- setup.py | 2 +- tests/tests.py | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/dbfilestorage/storage.py b/dbfilestorage/storage.py index 133d919..f6f30d4 100644 --- a/dbfilestorage/storage.py +++ b/dbfilestorage/storage.py @@ -12,7 +12,7 @@ from .models import DBFile L = logging.getLogger(__name__) -class DBStorage(Storage): +class DBFileStorage(Storage): """ This is the Test Database file upload storage backend. This is used so that in our test database we always have uploaded diff --git a/docs/Makefile b/docs/Makefile index 68477f0..cee472b 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -4,7 +4,7 @@ # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build -SPHINXPROJ = DjangoDBStorage +SPHINXPROJ = DjangoDBFileStorage SOURCEDIR = . BUILDDIR = _build @@ -17,4 +17,4 @@ help: # Catch-all target: route all unknown targets to Sphinx using the new # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). %: Makefile - @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) \ No newline at end of file + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/conf.py b/docs/conf.py index febbc98..ab8fa01 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -54,9 +54,9 @@ author = u'Tyrel Souza' # built documents. # # The short X.Y version. -version = u'0.0.4' +version = u'0.1.0' # The full version, including alpha/beta/rc tags. -release = u'0.0.4' +release = u'0.1.0' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/make.bat b/docs/make.bat index 3bea410..68b51be 100644 --- a/docs/make.bat +++ b/docs/make.bat @@ -9,7 +9,7 @@ if "%SPHINXBUILD%" == "" ( ) set SOURCEDIR=. set BUILDDIR=_build -set SPHINXPROJ=DjangoDBStorage +set SPHINXPROJ=DjangoDBFileStorage if "%1" == "" goto help diff --git a/setup.py b/setup.py index 2a10b26..e368c89 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ except ImportError: setup( name="django-dbfilestorage", - version="0.0.4", + version="0.1.0", description="Database backed file storage for testing.", long_description="Database backed file storage for testing. Stores files as base64 encoded textfields.", author="Tyrel Souza", diff --git a/tests/tests.py b/tests/tests.py index 60ff34f..25d4cc9 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -8,7 +8,7 @@ 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" +DEFAULT_FILE_STORAGE = "dbfilestorage.storage.DBFileStorage" @override_settings(DEFAULT_FILE_STORAGE=DEFAULT_FILE_STORAGE)