Skip to content

Commit

Permalink
Merge pull request #23 from barseghyanartur/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
barseghyanartur authored Jun 16, 2023
2 parents 6f4605b + 54ab6e8 commit 40ad6f0
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
11 changes: 10 additions & 1 deletion .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@
"line_number": 84
}
],
"src/faker_file/tests/test_sftp_storage.py": [
{
"type": "Secret Keyword",
"filename": "src/faker_file/tests/test_sftp_storage.py",
"hashed_secret": "a4b48a81cdab1e1a5dd37907d6c85ca1c61ddc7c",
"is_verified": true,
"line_number": 272
}
],
"src/faker_file/tests/test_storages.py": [
{
"type": "Secret Keyword",
Expand All @@ -167,5 +176,5 @@
}
]
},
"generated_at": "2023-06-16T13:52:28Z"
"generated_at": "2023-06-16T21:12:08Z"
}
42 changes: 41 additions & 1 deletion docs/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,46 @@ an HTML file is generated from a template.
extension="html",
)
Working with storages
~~~~~~~~~~~~~~~~~~~~~
AWS S3 storage
^^^^^^^^^^^^^^
.. code-block:: python
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
from faker_file.storages.aws_s3 import AWSS3Storage
FAKER = Faker()
AWS_S3_STORAGE = AWSS3Storage(
bucket_name="your-bucket-name",
root_path="",
rel_path="",
)
FAKER.add_provider(TxtFileProvider)
txt_file = FAKER.txt_file(storage=AWS_S3_STORAGE)
SFTP storage
^^^^^^^^^^^^
.. code-block:: python
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
from faker_file.storages.sftp import SFTPStorage
FAKER = Faker()
SFTP_STORAGE = SFTPStorage(
host="your-sftp-host.domain",
port: 22,
username: "your-sftp-username",
password: "your-sftp-password,
root_path: "/dir-name",
)
FAKER.add_provider(TxtFileProvider)
txt_file = FAKER.txt_file(storage=SFTP_STORAGE)
When using with ``Django`` (and ``factory_boy``)
------------------------------------------------
When used with Django (to generate fake data with ``factory_boy`` factories),
Expand Down Expand Up @@ -1296,7 +1336,7 @@ Other Django usage examples
)
FAKER.add_provider(PdfFileProvider)
file = PdfFileProvider(FAKER).pdf_file(storage=STORAGE)
file = FAKER.pdf_file(storage=STORAGE)
**factory-boy example with AWS S3 storage**

Expand Down
3 changes: 3 additions & 0 deletions src/faker_file/storages/sftp_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class SFTPStorage(BaseStorage):
txt_file = FAKER.txt_file(storage=STORAGE_SUB_DIR)
"""

sftp: Optional[paramiko.SFTPClient] = None
transport: Optional[paramiko.Transport] = None

def __init__(
self: "SFTPStorage",
host: str,
Expand Down
72 changes: 72 additions & 0 deletions src/faker_file/tests/test_sftp_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,75 @@ def test_integration_sub_dir(self: "TestSFTPStorageTestCase") -> None:
os.path.join(self.sftp_root_path, "sub")
)
)

@parametrize(
"kwargs",
[
# Wrong username, key
(
{
"host": sftp_host,
"port": sftp_port,
"username": "wrong",
"key": "wrong",
},
),
# Wrong username, password
(
{
"host": sftp_host,
"port": sftp_port,
"username": "wrong",
"password": "wrong",
},
),
],
)
def test_storage_initialization_exceptions(
self: "TestSFTPStorageTestCase",
kwargs: Dict[str, Any],
) -> None:
with self.assertRaises(Exception):
# Initialize the storage
SFTPStorage(**kwargs)

def test_storage_write_text_exceptions(self) -> None:
storage = SFTPStorage(
host=self.sftp_host,
port=self.sftp_port,
username=self.sftp_user,
password=self.sftp_pass,
root_path=self.sftp_root_path,
)
val = storage.write_text(
filename=FAKER.file_name(),
data=1, # type: ignore
)
self.assertEqual(val, -1)

def test_storage_write_bytes_exceptions(self) -> None:
storage = SFTPStorage(
host=self.sftp_host,
port=self.sftp_port,
username=self.sftp_user,
password=self.sftp_pass,
root_path=self.sftp_root_path,
)
val = storage.write_bytes(
filename=FAKER.file_name(),
data=1, # type: ignore
)
self.assertEqual(val, -1)

def test_storage_exists_exceptions(self) -> None:
storage = SFTPStorage(
host=self.sftp_host,
port=self.sftp_port,
username=self.sftp_user,
password=self.sftp_pass,
root_path=self.sftp_root_path,
)
val = storage.exists(
filename=FAKER.file_name(),
)
self.assertFalse(val)

0 comments on commit 40ad6f0

Please sign in to comment.