From 11f8a078874cc8b7c7fe89c7a1420a6b01f8e4a5 Mon Sep 17 00:00:00 2001 From: Tris Date: Fri, 17 Sep 2021 14:55:32 +1000 Subject: [PATCH] Moved tests to package --- byostorage/tests/__init__.py | 0 .../{tests.py => tests/test_byo_storage.py} | 86 ++++++------------- byostorage/tests/test_http_storage.py | 28 ++++++ byostorage/tests/test_multi_storage.py | 36 ++++++++ 4 files changed, 92 insertions(+), 58 deletions(-) create mode 100644 byostorage/tests/__init__.py rename byostorage/{tests.py => tests/test_byo_storage.py} (53%) create mode 100644 byostorage/tests/test_http_storage.py create mode 100644 byostorage/tests/test_multi_storage.py diff --git a/byostorage/tests/__init__.py b/byostorage/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/byostorage/tests.py b/byostorage/tests/test_byo_storage.py similarity index 53% rename from byostorage/tests.py rename to byostorage/tests/test_byo_storage.py index 4dbedf6..93b5eb0 100644 --- a/byostorage/tests.py +++ b/byostorage/tests/test_byo_storage.py @@ -2,46 +2,17 @@ from django.test import TestCase from django.core.files.storage import FileSystemStorage from django.core.exceptions import ValidationError -from .multi import MultiStorage -from .models import UserStorage -from .user import BYOStorage -from .http import HTTPStorage +from byostorage.models import UserStorage +from byostorage.user import BYOStorage + +from example.models import LinkedStorageTestModel + +import tempfile +import os.path #import logging #logging.basicConfig(level=logging.DEBUG) -class MultiStorageTestCase(TestCase): - - def test_storage_selection(self): - ms = MultiStorage({'one': {'location': 'storage/one'}, 'two': {}}) - self.assertEqual(str(ms), "") - - # use the default if nothing set - default = ms.get_storage('foo') - self.assertIsInstance(default, FileSystemStorage) - self.assertEqual(default.base_location, 'media') - - # get first instance - one = ms.get_storage('one') - self.assertEqual(one.base_location, 'storage/one') - - def test_storage_proxies(self): - ms = MultiStorage({ - 'one': {'location': 'storage/one', 'base_url': 'http://one'}, - 'two': {'location': 'storage/two', 'base_url': 'http://two'} - }) - - - self.assertEqual(ms.url("one:foo.txt"), 'http://one/foo.txt') - self.assertEqual(ms.url("two:foo.txt"), 'http://two/foo.txt') - - def test_with_http(self): - ms = MultiStorage({ - 'https': {'storage': 'byostorage.http.HTTPStorage', 'protocol': 'https'} - }) - - self.assertEqual(ms.url('https://google.com'), 'https://google.com') - class UserStorageTestCase(TestCase): def test_construction(self): @@ -63,10 +34,10 @@ class UserStorageTestCase(TestCase): instance = UserStorage.objects.create(name='one', storage='storages.backends.s3boto3.S3Boto3Storage', settings_data=''' { - "access_key": "polyphonic_test_key", - "secret_key": "polyphonic_secret", + "access_key": "minioadmin", + "secret_key": "minioadmin", "endpoint_url": "http://localhost:9000", - "bucket_name": "personal" + "bucket_name": "byostorage_test" } ''') @@ -76,10 +47,10 @@ class UserStorageTestCase(TestCase): UserStorage.objects.create(name='one', storage='storages.backends.s3boto3.S3Boto3Storage', settings_data=''' { - "access_key": "polyphonic_test_key", + "access_key": "minioadmin", "secret_key": "the_wrong_secret", "endpoint_url": "http://localhost:9000", - "bucket_name": "missing" + "bucket_name": "byostorage_test" } ''') self.fail("Should have raised an exception") @@ -113,28 +84,27 @@ class BYOStorageTestCase(TestCase): one = storage.get_storage('one') self.assertEqual(one.base_location, 'other/one') +class BYOStorageFieldTestCase(TestCase): -class HTTPStorageTestCase(TestCase): + def setUp(self): + self.test_dir = tempfile.TemporaryDirectory() + self.one = UserStorage.objects.create(name='one', storage='django.core.files.storage.FileSystemStorage', + settings_data = '{"location": "%s/one"}' % self.test_dir.name) - def test_url(self): - s = HTTPStorage() + def tearDown(self): + self.test_dir.cleanup() - self.assertEqual(s.url('//google.com'), 'https://google.com') + def test_linked_storage(self): + + from django.core.files.uploadedfile import UploadedFile - def test_exists(self): - s = HTTPStorage() + with open(__file__, 'rb') as f: + result = LinkedStorageTestModel.objects.create(name="first", my_storage_id=self.one.pk, my_file=UploadedFile(f)) - self.assertTrue(s.exists('//gitea.tfconsulting.com.au/tris')) - self.assertFalse(s.exists('//gitea.tfconsulting.com.au/foo.txt')) + expected = os.path.basename(__file__) - def test_save(self): - s = HTTPStorage() - with self.assertRaisesMessage(NotImplementedError, "Unable to save to web locations"): - s.save('//gitea.tfconsulting.com.au/foo', 'Some content') + self.assertEqual(result.my_file.name, expected) + self.assertGreater(result.my_file.size, 0) - def test_open(self): - s = HTTPStorage() - with s.open('//gitea.tfconsulting.com.au', 'rb') as f: - data = f.read() + self.assertEqual(os.listdir(os.path.join(self.test_dir.name, 'one')), [expected]) - self.assertTrue(len(data) > 4000) \ No newline at end of file diff --git a/byostorage/tests/test_http_storage.py b/byostorage/tests/test_http_storage.py new file mode 100644 index 0000000..f3f1548 --- /dev/null +++ b/byostorage/tests/test_http_storage.py @@ -0,0 +1,28 @@ +from django.test import TestCase + +from byostorage.http import HTTPStorage + +class HTTPStorageTestCase(TestCase): + + def test_url(self): + s = HTTPStorage() + + self.assertEqual(s.url('//google.com'), 'https://google.com') + + def test_exists(self): + s = HTTPStorage() + + self.assertTrue(s.exists('//gitea.tfconsulting.com.au/tris')) + self.assertFalse(s.exists('//gitea.tfconsulting.com.au/foo.txt')) + + def test_save(self): + s = HTTPStorage() + with self.assertRaisesMessage(NotImplementedError, "Unable to save to web locations"): + s.save('//gitea.tfconsulting.com.au/foo', 'Some content') + + def test_open(self): + s = HTTPStorage() + with s.open('//gitea.tfconsulting.com.au', 'rb') as f: + data = f.read() + + self.assertTrue(len(data) > 4000) \ No newline at end of file diff --git a/byostorage/tests/test_multi_storage.py b/byostorage/tests/test_multi_storage.py new file mode 100644 index 0000000..315ffed --- /dev/null +++ b/byostorage/tests/test_multi_storage.py @@ -0,0 +1,36 @@ +from django.test import TestCase +from byostorage.multi import MultiStorage +from django.core.files.storage import FileSystemStorage + + +class MultiStorageTestCase(TestCase): + + def test_storage_selection(self): + ms = MultiStorage({'one': {'location': 'storage/one'}, 'two': {}}) + self.assertEqual(str(ms), "") + + # use the default if nothing set + default = ms.get_storage('foo') + self.assertIsInstance(default, FileSystemStorage) + self.assertEqual(default.base_location, 'media') + + # get first instance + one = ms.get_storage('one') + self.assertEqual(one.base_location, 'storage/one') + + def test_storage_proxies(self): + ms = MultiStorage({ + 'one': {'location': 'storage/one', 'base_url': 'http://one'}, + 'two': {'location': 'storage/two', 'base_url': 'http://two'} + }) + + + self.assertEqual(ms.url("one:foo.txt"), 'http://one/foo.txt') + self.assertEqual(ms.url("two:foo.txt"), 'http://two/foo.txt') + + def test_with_http(self): + ms = MultiStorage({ + 'https': {'storage': 'byostorage.http.HTTPStorage', 'protocol': 'https'} + }) + + self.assertEqual(ms.url('https://google.com'), 'https://google.com')