from django.test import TestCase
from django.core.files.base import ContentFile
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from uuid import uuid4
import os
from storages.backends.s3boto import S3BotoStorage


class S3BotoStorageTests(TestCase):
    def setUp(self):
        self.storage = S3BotoStorage()
        
        # use a unique folder namespace for tests
        path_prefix = "test-subfolder/"
        dirs, files = self.storage.listdir(path_prefix)
        while dirs or files:
            path_prefix = "test-subfolder-%s/" % uuid4()
            dirs, files = self.storage.listdir(path_prefix)
        self.path_prefix = path_prefix
        
    def prefix_path(self, path):
        return "%s%s" % (self.path_prefix, path)
    
    def test_storage_save(self):
        name = self.prefix_path('test_storage_save.txt')
        content = 'new content'
        self.storage.save(name, ContentFile(content))
        self.assertEqual(self.storage.open(name).read(), content)
    
    def test_storage_open_for_writing(self):
        name = self.prefix_path('test_open_for_writing.txt')
        content = 'new content'
        file = self.storage.open(name, 'w')
        file.write(content)
        file.close()
        self.assertEqual(self.storage.open(name).read(), content)
        
@@ -255,18 +255,16 @@
 rage%0D%0A%0D%0A
-%0D%0A
 class S3
@@ -702,16 +702,376 @@
 prefix%0D%0A
+    %0D%0A    def tearDown(self):%0D%0A        # delete all files created during each test%0D%0A        name = self.storage._normalize_name(self.storage._clean_name(self.path_prefix))%0D%0A        dirlist = self.storage.bucket.list(self.storage._encode_name(name))%0D%0A        names = %5Bx.name for x in dirlist%5D%0D%0A        for name in names:%0D%0A            self.storage.delete(name)%0D%0A
         

delete all files created during each test