From f5fbddec86ecd78fe5c1945c1c9d2d6bf5de44b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Rubin?= Date: Thu, 22 Dec 2022 15:34:17 +0100 Subject: [PATCH] chore(readme): Align code snippets formatting with project's rules. (#5802) --- README.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 34de0cf35..c39c29835 100644 --- a/README.md +++ b/README.md @@ -27,14 +27,15 @@ Imagine you have the following python code that you want to test: ```python import boto3 -class MyModel(object): + +class MyModel: def __init__(self, name, value): self.name = name self.value = value def save(self): - s3 = boto3.client('s3', region_name='us-east-1') - s3.put_object(Bucket='mybucket', Key=self.name, Body=self.value) + s3 = boto3.client("s3", region_name="us-east-1") + s3.put_object(Bucket="mybucket", Key=self.name, Body=self.value) ``` Take a minute to think how you would have tested that in the past. @@ -46,15 +47,16 @@ import boto3 from moto import mock_s3 from mymodule import MyModel + @mock_s3 def test_my_model_save(): - conn = boto3.resource('s3', region_name='us-east-1') + conn = boto3.resource("s3", region_name="us-east-1") # We need to create the bucket since this is all in Moto's 'virtual' AWS account - conn.create_bucket(Bucket='mybucket') - model_instance = MyModel('steve', 'is awesome') + conn.create_bucket(Bucket="mybucket") + model_instance = MyModel("steve", "is awesome") model_instance.save() - body = conn.Object('mybucket', 'steve').get()['Body'].read().decode("utf-8") - assert body == 'is awesome' + body = conn.Object("mybucket", "steve").get()["Body"].read().decode("utf-8") + assert body == "is awesome" ``` With the decorator wrapping the test, all the calls to s3 are automatically mocked out. The mock keeps the state of the buckets and keys.