test_s3fs.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from __future__ import unicode_literals
  2. import unittest
  3. from nose.plugins.attrib import attr
  4. from fs.test import FSTestCases
  5. from fs_s3fs import S3FS
  6. import boto3
  7. class TestS3FS(FSTestCases, unittest.TestCase):
  8. """Test S3FS implementation from dir_path."""
  9. bucket_name = "fsexample"
  10. s3 = boto3.resource("s3")
  11. client = boto3.client("s3")
  12. def make_fs(self):
  13. self._delete_bucket_contents()
  14. return S3FS(self.bucket_name)
  15. def _delete_bucket_contents(self):
  16. response = self.client.list_objects(Bucket=self.bucket_name)
  17. contents = response.get("Contents", ())
  18. for obj in contents:
  19. self.client.delete_object(Bucket=self.bucket_name, Key=obj["Key"])
  20. @attr("slow")
  21. class TestS3FSSubDir(FSTestCases, unittest.TestCase):
  22. """Test S3FS implementation from dir_path."""
  23. bucket_name = "fsexample"
  24. s3 = boto3.resource("s3")
  25. client = boto3.client("s3")
  26. def make_fs(self):
  27. self._delete_bucket_contents()
  28. self.s3.Object(self.bucket_name, "subdirectory").put()
  29. return S3FS(self.bucket_name, dir_path="subdirectory")
  30. def _delete_bucket_contents(self):
  31. response = self.client.list_objects(Bucket=self.bucket_name)
  32. contents = response.get("Contents", ())
  33. for obj in contents:
  34. self.client.delete_object(Bucket=self.bucket_name, Key=obj["Key"])
  35. class TestS3FSHelpers(unittest.TestCase):
  36. def test_path_to_key(self):
  37. s3 = S3FS("foo")
  38. self.assertEqual(s3._path_to_key("foo.bar"), "foo.bar")
  39. self.assertEqual(s3._path_to_key("foo/bar"), "foo/bar")
  40. def test_path_to_key_subdir(self):
  41. s3 = S3FS("foo", "/dir")
  42. self.assertEqual(s3._path_to_key("foo.bar"), "dir/foo.bar")
  43. self.assertEqual(s3._path_to_key("foo/bar"), "dir/foo/bar")
  44. def test_upload_args(self):
  45. s3 = S3FS("foo", acl="acl", cache_control="cc")
  46. self.assertDictEqual(
  47. s3._get_upload_args("test.jpg"),
  48. {"ACL": "acl", "CacheControl": "cc", "ContentType": "image/jpeg"},
  49. )
  50. self.assertDictEqual(
  51. s3._get_upload_args("test.mp3"),
  52. {"ACL": "acl", "CacheControl": "cc", "ContentType": "audio/mpeg"},
  53. )
  54. self.assertDictEqual(
  55. s3._get_upload_args("test.json"),
  56. {"ACL": "acl", "CacheControl": "cc", "ContentType": "application/json"},
  57. )
  58. self.assertDictEqual(
  59. s3._get_upload_args("unknown.unknown"),
  60. {"ACL": "acl", "CacheControl": "cc", "ContentType": "binary/octet-stream"},
  61. )