Exemple #1
0
    def test_create_file(self):
        logging.info('test_create_file')
        f1 = Folder(name='test_folder')
        f1.save()
        file1 = File(name='file1', parent=f1)
        file1.file.save('file1.txt', ContentFile('Content'.encode('utf-8')))

        self.assertEqual(self.get_bucket_contents(), ['media/test_folder/', 'media/test_folder/file1.txt'])
Exemple #2
0
    def test_delete_file(self):
        """
        When a File Object is deleted, its associated S3 Object should also be deleted
        """
        logging.info('test_delete_file')
        f1 = Folder(name='test_folder')
        f1.save()
        file1 = File(name='file1', parent=f1)
        file1.file.save('file.txt', ContentFile('Content'.encode('utf-8')))
        file1.delete()

        self.assertEqual(self.get_bucket_contents(), ['media/test_folder/'])
Exemple #3
0
    def test_update_file(self):
        """
        Update a File Object with a new file (eg a new version). Old file should be deleted on S3
        """
        logging.info('test_update_file')
        f1 = Folder(name='test_folder')
        f1.save()
        myFile = File(name='my-file', parent=f1)
        myFile.file.save('file-v1.txt', ContentFile('Content'.encode('utf-8')))
        myFile.file.save('file-v2.txt', ContentFile('Content2'.encode('utf-8')))

        self.assertEqual(self.get_bucket_contents(), ['media/test_folder/', 'media/test_folder/file-v2.txt'])
Exemple #4
0
    def test_update_file_name(self):
        """
        Changing the File Object's name property should have no effect on file stored on S3
        """
        logging.info('test_update_file_name')
        f1 = Folder(name='test_folder')
        f1.save()
        file1 = File(name='file1', parent=f1)
        file1.file.save('file1.txt', ContentFile('Content'.encode('utf-8')))
        file1.name = 'file1-edit'
        file1.save()

        self.assertEqual(self.get_bucket_contents(), ['media/test_folder/', 'media/test_folder/file1.txt'])
Exemple #5
0
    def test_update_folder_with_files(self):
        """
        When a Folder name is updated, should update the S3 keys of it and all its Files
        """
        logging.info('test_update_folder_with_files')
        f1 = Folder(name='my-folder')
        f1.save()
        file1 = File(name='file1', parent=f1)
        file1.file.save('file1.txt', ContentFile('Content'.encode('utf-8')))
        file2 = File(name='file2', parent=f1)
        file2.file.save('file2.txt', ContentFile('Content'.encode('utf-8')))
        file3 = File(name='file3', parent=f1)
        file3.file.save('file3.txt', ContentFile('Content'.encode('utf-8')))
        file4 = File(name='file4', parent=f1)
        file4.file.save('file4.txt', ContentFile('Content'.encode('utf-8')))
        file5 = File(name='file5', parent=f1)
        file5.file.save('file5.txt', ContentFile('Content'.encode('utf-8')))
        file6 = File(name='file6', parent=f1)
        file6.file.save('file6.txt', ContentFile('Content'.encode('utf-8')))

        f1.name='my-folder-edit'
        f1.save()

        self.assertEqual(self.get_bucket_contents(), [
            'media/my-folder-edit/',
            'media/my-folder-edit/file1.txt',
            'media/my-folder-edit/file2.txt',
            'media/my-folder-edit/file3.txt',
            'media/my-folder-edit/file4.txt',
            'media/my-folder-edit/file5.txt',
            'media/my-folder-edit/file6.txt',
        ])
Exemple #6
0
 def test_get_descendants_if_none_exist_inc_self_false(self):
     """
     get_all_descendants() should return empty, if it
     has no children and include_self is set to false
     """
     f1 = Folder(name='folder1')
     f1.save()
     self.assertEqual(f1.get_all_descendants(), [])
     f1.delete()
Exemple #7
0
 def test_get_descendants_if_none_exist_inc_self_true(self):
     """
     get_all_descendants() should return the current_folder only, if it
     has no children and include_self is set to true
     """
     f1 = Folder(name='folder1')
     f1.save()
     self.assertEqual(f1.get_all_descendants(True), [f1])
     f1.delete()
Exemple #8
0
    def test_delete_folder_with_subfolders(self):
        logging.info('test_delete_folder_with_subfolders...')
        f1 = Folder(name='test_folder')
        f1.save()
        f2 = Folder(name='subfolder', parent=f1)
        f2.save()
        f3 = Folder(name='second_folder')
        f3.save()
        f1.delete()

        bucket_contents = []
        for obj in s3_utils.s3.list_objects(Bucket = s3_utils.bucket)['Contents']:
            bucket_contents.append(obj['Key'])

        self.assertEqual(bucket_contents, ['media/second_folder/'])
Exemple #9
0
    def test_delete_folder_with_subfolders_and_files(self):
        """
        When a Folder is deleted, all its subfolders and Files should also be deleted
        """
        logging.info('test_delete_folder_with_subfolders_and_files')
        f1 = Folder(name='f1')
        f1.save()
        f2 = Folder(name='f2', parent=f1)
        f2.save()
        f3 = Folder(name='f3', parent=f1)
        f3.save()
        f4 = Folder(name='f4', parent=f1)
        f4.save()
        f5 = Folder(name='f5', parent=f2)
        f5.save()
        f6 = Folder(name='f6', parent=f2)
        f6.save()
        f7 = Folder(name='f7', parent=f4)
        f7.save()
        f8 = Folder(name='f8', parent=f6)
        f8.save()

        file1 = File(name='file1', parent=f1)
        file1.file.save('file1.txt', ContentFile('Content'.encode('utf-8')))
        file2 = File(name='file2', parent=f1)
        file2.file.save('file2.txt', ContentFile('Content'.encode('utf-8')))
        file3 = File(name='file3', parent=f2)
        file3.file.save('file3.txt', ContentFile('Content'.encode('utf-8')))
        file4 = File(name='file4', parent=f2)
        file4.file.save('file4.txt', ContentFile('Content'.encode('utf-8')))
        file5 = File(name='file5', parent=f6)
        file5.file.save('file5.txt', ContentFile('Content'.encode('utf-8')))
        file6 = File(name='file6', parent=f6)
        file6.file.save('file6.txt', ContentFile('Content'.encode('utf-8')))
        file7 = File(name='file7', parent=f6)
        file7.file.save('file7.txt', ContentFile('Content'.encode('utf-8')))
        file8 = File(name='file8', parent=f7)
        file8.file.save('file8.txt', ContentFile('Content'.encode('utf-8')))
        file9 = File(name='file9', parent=f7)
        file9.file.save('file9.txt', ContentFile('Content'.encode('utf-8')))

        f2.delete()

        self.assertEqual(set(self.get_bucket_contents()), set([
            'media/f1/',
            'media/f1/file1.txt',
            'media/f1/file2.txt',
            'media/f1/f3/',
            'media/f1/f4/',
            'media/f1/f4/f7/',
            'media/f1/f4/f7/file8.txt',
            'media/f1/f4/f7/file9.txt',
        ]))
Exemple #10
0
    def test_delete_folder_with_files(self):
        """
        When a Folder is deleted, all its Files should also be deleted
        """
        logging.info('test_delete_folder_with_files')
        f1 = Folder(name='f1')
        f1.save()
        f2 = Folder(name='f2', parent=f1)
        f2.save()
        f3 = Folder(name='f3', parent=f1)
        f3.save()

        file1 = File(name='file1', parent=f2)
        file1.file.save('file1.txt', ContentFile('Content'.encode('utf-8')))
        file2 = File(name='file2', parent=f2)
        file2.file.save('file2.txt', ContentFile('Content'.encode('utf-8')))
        file3 = File(name='file3', parent=f2)

        f2.delete()

        self.assertEqual(set(self.get_bucket_contents()), set([
            'media/f1/',
            'media/f1/f3/'
        ]))
Exemple #11
0
    def test_update_folder_with_subfolders_and_files(self):
        """
        When a Folder name is updated, its own S3 key, along with the S3 keys of its subfolders and files, should be updated
        """
        logging.info('test_update_folder_with_subfolders_and_files')
        f1 = Folder(name='f1')
        f1.save()
        f2 = Folder(name='f2', parent=f1)
        f2.save()
        f3 = Folder(name='f3', parent=f1)
        f3.save()
        f4 = Folder(name='f4', parent=f1)
        f4.save()
        f5 = Folder(name='f5', parent=f2)
        f5.save()
        f6 = Folder(name='f6', parent=f2)
        f6.save()
        f7 = Folder(name='f7', parent=f4)
        f7.save()

        file1 = File(name='file1', parent=f1)
        file1.file.save('file1.txt', ContentFile('Content'.encode('utf-8')))
        file2 = File(name='file2', parent=f1)
        file2.file.save('file2.txt', ContentFile('Content'.encode('utf-8')))
        file3 = File(name='file3', parent=f2)
        file3.file.save('file3.txt', ContentFile('Content'.encode('utf-8')))
        file4 = File(name='file4', parent=f2)
        file4.file.save('file4.txt', ContentFile('Content'.encode('utf-8')))
        file5 = File(name='file5', parent=f6)
        file5.file.save('file5.txt', ContentFile('Content'.encode('utf-8')))
        file6 = File(name='file6', parent=f6)
        file6.file.save('file6.txt', ContentFile('Content'.encode('utf-8')))
        file7 = File(name='file7', parent=f6)
        file7.file.save('file7.txt', ContentFile('Content'.encode('utf-8')))
        file8 = File(name='file8', parent=f7)
        file8.file.save('file8.txt', ContentFile('Content'.encode('utf-8')))
        file9 = File(name='file9', parent=f7)
        file9.file.save('file9.txt', ContentFile('Content'.encode('utf-8')))

        f2.name = 'f2-edit'
        f2.save()

        self.assertEqual(set(self.get_bucket_contents()), set([
            'media/f1/',
            'media/f1/file1.txt',
            'media/f1/file2.txt',
            'media/f1/f2-edit/',
            'media/f1/f2-edit/file3.txt',
            'media/f1/f2-edit/file4.txt',
            'media/f1/f2-edit/f5/',
            'media/f1/f2-edit/f6/',
            'media/f1/f2-edit/f6/file5.txt',
            'media/f1/f2-edit/f6/file6.txt',
            'media/f1/f2-edit/f6/file7.txt',
            'media/f1/f3/',
            'media/f1/f4/',
            'media/f1/f4/f7/',
            'media/f1/f4/f7/file8.txt',
            'media/f1/f4/f7/file9.txt',
        ]))
Exemple #12
0
 def test_get_descendants_single_branch_inc_self_false(self):
     """
     get_all_descendants() should return the correct descendants for a single branch tree,
     excluding the current_folder if include_self is set to false
     """
     f1 = Folder(name='folder1')
     f1.save()
     f2 = Folder(name='folder2', parent=f1)
     f2.save()
     f3 = Folder(name='folder3', parent=f2)
     f3.save()
     f4 = Folder(name='folder4', parent=f3)
     f4.save()
     self.assertEqual(set(f1.get_all_descendants()), set([f2, f3, f4]))
     f1.delete()
Exemple #13
0
 def test_get_descendants_many_branches_inc_self_true(self):
     """
     get_all_descendants() should return the correct descendants for a multiple branch tree,
     including the current_folder if include_self is set to true
     """
     f1 = Folder(name='folder1')
     f1.save()
     f2 = Folder(name='folder2', parent=f1)
     f2.save()
     f3 = Folder(name='folder3', parent=f1)
     f3.save()
     f4 = Folder(name='folder4', parent=f1)
     f4.save()
     f5 = Folder(name='folder5', parent=f2)
     f5.save()
     f6 = Folder(name='folder6', parent=f2)
     f6.save()
     f7 = Folder(name='folder7', parent=f3)
     f7.save()
     f8 = Folder(name='folder8', parent=f6)
     f8.save()
     f9 = Folder(name='folder9', parent=f7)
     f9.save()
     f10 = Folder(name='folder10', parent=f7)
     f10.save()
     f11 = Folder(name='folder11', parent=f7)
     f11.save()
     f12 = Folder(name='folder12', parent=f10)
     f12.save()
     self.assertEqual(
         set(f1.get_all_descendants(True)),
         set([f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12]))
     f1.delete()
Exemple #14
0
 def test_get_descendants_many_branch_including_files_inc_self_true(self):
     """
     get_all_descendants should return the correct descendants for a multiple branch tree, including files,
     including the current_folder if include_self is set to true
     """
     f1 = Folder(name='folder1')
     f1.save()
     f2 = Folder(name='folder2', parent=f1)
     f2.save()
     f3 = Folder(name='folder3', parent=f2)
     f3.save()
     f4 = Folder(name='folder4', parent=f1)
     f4.save()
     file1 = File(name='file1', parent=f3)
     file1.save()
     file2 = File(name='file2', parent=f3)
     file2.save()
     file3 = File(name='file3', parent=f4)
     file3.save()
     file4 = File(name='file4', parent=f4)
     file4.save()
     self.assertEqual(set(f1.get_all_descendants(True)),
                      set([f1, f2, f3, f4, file1, file2, file3, file4]))
     f1.delete()