Moved tests to package

This commit is contained in:
Tris 2021-09-17 14:55:32 +10:00
parent c67d636d24
commit 11f8a07887
4 changed files with 92 additions and 58 deletions

View File

View File

@ -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), "<MultiStorage: 2 storages>")
# 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)

View File

@ -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)

View File

@ -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), "<MultiStorage: 2 storages>")
# 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')