|
| 1 | +import gzip |
| 2 | +import sys |
| 3 | +from http import HTTPStatus |
| 4 | +from io import BytesIO |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from aiohttp_s3_client import S3Client |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize("object_name", ("test/test", "/test/test")) |
| 13 | +async def test_put_str(s3_client: S3Client, object_name): |
| 14 | + data = "hello, world" |
| 15 | + resp = await s3_client.put(object_name, data) |
| 16 | + assert resp.status == HTTPStatus.OK |
| 17 | + |
| 18 | + resp = await s3_client.get(object_name) |
| 19 | + result = await resp.text() |
| 20 | + assert result == data |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize("object_name", ("test/test", "/test/test")) |
| 24 | +async def test_put_bytes(s3_client: S3Client, s3_read, object_name): |
| 25 | + data = b"hello, world" |
| 26 | + resp = await s3_client.put(object_name, data) |
| 27 | + assert resp.status == HTTPStatus.OK |
| 28 | + assert (await s3_read()) == data |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("object_name", ("test/test", "/test/test")) |
| 32 | +async def test_put_async_iterable(s3_client: S3Client, s3_read, object_name): |
| 33 | + async def async_iterable(iterable: bytes): |
| 34 | + for i in iterable: |
| 35 | + yield i.to_bytes(1, sys.byteorder) |
| 36 | + |
| 37 | + data = b"hello, world" |
| 38 | + resp = await s3_client.put(object_name, async_iterable(data)) |
| 39 | + assert resp.status == HTTPStatus.OK |
| 40 | + |
| 41 | + assert (await s3_read()) == data |
| 42 | + |
| 43 | + |
| 44 | +async def test_put_file(s3_client: S3Client, s3_read, tmp_path): |
| 45 | + data = b"hello, world" |
| 46 | + |
| 47 | + with (tmp_path / "hello.txt").open("wb") as f: |
| 48 | + f.write(data) |
| 49 | + f.flush() |
| 50 | + |
| 51 | + # Test upload by file str path |
| 52 | + resp = await s3_client.put_file("/test/test", f.name) |
| 53 | + assert resp.status == HTTPStatus.OK |
| 54 | + |
| 55 | + assert (await s3_read("/test/test")) == data |
| 56 | + |
| 57 | + # Test upload by file Path |
| 58 | + resp = await s3_client.put_file("/test/test2", Path(f.name)) |
| 59 | + assert resp.status == HTTPStatus.OK |
| 60 | + |
| 61 | + assert (await s3_read("/test/test2")) == data |
| 62 | + |
| 63 | + |
| 64 | +async def test_list_objects_v2(s3_client: S3Client, s3_read, tmp_path): |
| 65 | + data = b"hello, world" |
| 66 | + |
| 67 | + with (tmp_path / "hello.txt").open("wb") as f: |
| 68 | + f.write(data) |
| 69 | + f.flush() |
| 70 | + |
| 71 | + resp = await s3_client.put_file("/test/list/test1", f.name) |
| 72 | + assert resp.status == HTTPStatus.OK |
| 73 | + |
| 74 | + resp = await s3_client.put_file("/test/list/test2", f.name) |
| 75 | + assert resp.status == HTTPStatus.OK |
| 76 | + |
| 77 | + # Test list file |
| 78 | + batch = 0 |
| 79 | + async for result, prefixes in s3_client.list_objects_v2( |
| 80 | + prefix="test/list/", |
| 81 | + delimiter="/", |
| 82 | + max_keys=1, |
| 83 | + ): |
| 84 | + batch += 1 |
| 85 | + assert result[0].key == f"test/list/test{batch}" |
| 86 | + assert result[0].size == len(data) |
| 87 | + |
| 88 | + |
| 89 | +async def test_list_objects_v2_prefix(s3_client: S3Client, s3_read, tmp_path): |
| 90 | + data = b"hello, world" |
| 91 | + |
| 92 | + with (tmp_path / "hello.txt").open("wb") as f: |
| 93 | + f.write(data) |
| 94 | + f.flush() |
| 95 | + |
| 96 | + resp = await s3_client.put_file("/test2/list1/test1", f.name) |
| 97 | + assert resp.status == HTTPStatus.OK |
| 98 | + |
| 99 | + resp = await s3_client.put_file("/test2/list2/test2", f.name) |
| 100 | + assert resp.status == HTTPStatus.OK |
| 101 | + |
| 102 | + # Test list file |
| 103 | + batch = 0 |
| 104 | + |
| 105 | + async for result, prefixes in s3_client.list_objects_v2( |
| 106 | + prefix="test2/", |
| 107 | + delimiter="/", |
| 108 | + ): |
| 109 | + batch += 1 |
| 110 | + assert len(result) == 0 |
| 111 | + assert prefixes[0] == "test2/list1/" |
| 112 | + assert prefixes[1] == "test2/list2/" |
| 113 | + |
| 114 | + |
| 115 | +async def test_url_path_with_colon(s3_client: S3Client, s3_read): |
| 116 | + data = b"hello, world" |
| 117 | + key = "/some-path:with-colon.txt" |
| 118 | + resp = await s3_client.put(key, data) |
| 119 | + assert resp.status == HTTPStatus.OK |
| 120 | + |
| 121 | + assert (await s3_read(key)) == data |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.parametrize("object_name", ("test/test", "/test/test")) |
| 125 | +async def test_put_compression(s3_client: S3Client, s3_read, object_name): |
| 126 | + async def async_iterable(iterable: bytes): |
| 127 | + for i in iterable: |
| 128 | + yield i.to_bytes(1, sys.byteorder) |
| 129 | + |
| 130 | + data = b"hello, world" |
| 131 | + resp = await s3_client.put( |
| 132 | + object_name, async_iterable(data), compress="gzip", |
| 133 | + ) |
| 134 | + assert resp.status == HTTPStatus.OK |
| 135 | + |
| 136 | + result = await s3_read() |
| 137 | + # assert resp.headers[hdrs.CONTENT_ENCODING] == "gzip" |
| 138 | + # FIXME: uncomment after update fakes3 image |
| 139 | + actual = gzip.GzipFile(fileobj=BytesIO(result)).read() |
| 140 | + assert actual == data |
0 commit comments