Skip to content

Commit

Permalink
Mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis-chambers committed Sep 26, 2024
1 parent e8c467f commit fb068cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ object_bytes = reader.read(bucket="my-bucket", key="Path/to/file")
```

#### Writing
The `S3Writer` operates in the same way as the reader but supplies a `write` method instead of `read`
The `S3Writer` operates in the same way as the reader but supplies a `write` method instead of `read`. The `body` argument is expected by AWS to be a `bytes` object, which is left to the user to provide.

```python
from driutils.io.aws import S3Writer
Expand All @@ -111,17 +111,24 @@ client = boto3.client("s3")
body = b"I'm a byte encoded document"
writer = S3Writer(client)

# Submit a file from AWS S3
# Submit a file to AWS S3

object_bytes = reader.read(bucket="my-bucket", key="Path/to/file", body=body)
```
### DuckDB Reader

#### Reading/Writing Conbo Class
The `S3ReaderWriter` behaves the same as the prior classes but supplies both commands in one class

### DuckDB

#### Readers

The DuckDB classes use the duckdb python interface to read files from local documents or S3 object storage - this comes with the capacity to use custom s3 endpoints.

To read a local file:
```python

from driutils.read import DuckDBFileReader
from driutils.io.duckdb import DuckDBFileReader

reader = DuckDBFileReader()
query = "SELECT * FROM READ_PARQUET('myfile.parquet');"
Expand Down Expand Up @@ -154,7 +161,7 @@ To read from an S3 storage location there is a more configuration available and

The reader is instantiated like this:
```python
from driutils.read import import DuckDBS3Reader
from driutils.duckdb import DuckDBS3Reader

# Automatic authentication from your environment
auto_auth_reader = DuckDBS3Reader("auto")
Expand Down
11 changes: 9 additions & 2 deletions tests/io/test_aws.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock, Mock
from driutils.io.aws import S3Writer, S3Reader
import boto3
from botocore.client import BaseClient
Expand Down Expand Up @@ -60,10 +60,17 @@ def setUpClass(cls) -> None:
cls.s3_client: S3Client = boto3.client("s3") #type: ignore
cls.bucket = "my-bucket"
cls.key = "my-key"
def test_error_if_read_fails(self) -> None:
def test_error_caught_if_read_fails(self) -> None:
"""Tests that a ClientError is raised if read fails"""

reader = S3Reader(self.s3_client)
fake_error = ClientError(operation_name='InvalidKeyPair.Duplicate', error_response={
'Error': {
'Code': 'Duplicate',
'Message': 'This is a custom message'
}
})
reader._connection.get_object = MagicMock(side_effect=fake_error)

with self.assertRaises((RuntimeError, ClientError)):
reader.read(self.bucket, self.key)
Expand Down

0 comments on commit fb068cf

Please sign in to comment.