PK! README.md# pytest-serverless Mock local resources for serverless framework. ## Installation ```sh pip install pytest-serverless ``` ## What problem it tries to solve? When building your project with [serverless](https://serverless.com/) most likely you will create [resources](https://serverless.com/framework/docs/providers/aws/guide/resources/) like dynamodb tables, sqs queues, sns topics. During writing tests you will have to mock those in [moto](https://github.com/spulec/moto). This pytest plugin tries to automate this process by reading `serverless.yml` file and create mocks of resources for you. ## Usage Mark your test with `@pytest.mark.usefixtures("serverless")`, and rest will be done by plugin. ```python import boto3 import pytest @pytest.mark.usefixtures("serverless") def test(): table = boto3.resource("dynamodb").Table("my-microservice.my-table") count_of_items = len(table.scan()["Items"]) assert count_of_items == 0 ``` ## Supported resources ### AWS::DynamoDB::Table ## Issues? Plugin is in early stage of development, so you might find some bugs or missing functionality. If possible create pull request (with tests) that fixes particular problem. PK!$pytest_serverless.pyimport os import re import boto3 from box import Box import pytest import yaml @pytest.fixture() def serverless(): is_serverless = os.path.isfile("serverless.yml") if not is_serverless: raise Exception("No serverless.yml file found!") with open(os.path.join(os.getcwd(), "serverless.yml")) as f: serverless_yml_content = f.read() serverless_yml_dict = yaml.safe_load(serverless_yml_content) resources = yaml.safe_dump(serverless_yml_dict.get("resources")) my_box = Box.from_yaml(serverless_yml_content) variables_to_replace = find_self_variables_to_replace(resources) for variable in variables_to_replace: resources = resources.replace(variable[0], eval(f"my_box.{variable[1]}")) serverless_yml_dict["resources"] = yaml.safe_load(resources) dynamodb_tables = [] for resource_name, resource_definition in ( serverless_yml_dict.get("resources", {}).get("Resources", {}).items() ): if resource_definition.get("Type") == "AWS::DynamoDB::Table": dynamodb_tables.append(resource_definition["Properties"]) if dynamodb_tables: from moto import mock_dynamodb2 dynamodb = mock_dynamodb2() dynamodb.start() for table_definition in dynamodb_tables: boto3.resource("dynamodb").create_table(**table_definition) yield dynamodb.stop() def find_self_variables_to_replace(content): return re.findall(r"(\${self:([a-zA-Z.]+)})", content) PK!H08)2pytest_serverless-0.3.0.dist-info/entry_points.txt.,I-.14*N-*K-I-.#DPK!!O..)pytest_serverless-0.3.0.dist-info/LICENSEMIT License Copyright (c) 2019 Daniel Ancuta Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!Hu)GTU'pytest_serverless-0.3.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/R(O-)$qzd&Y)r$UV&UrPK!Hsܑ*pytest_serverless-0.3.0.dist-info/METADATAU[OH}_)$R<XmaE\=If{\I(JK99/"&S?`WՍ'NڥZ:6}>6MFgRx *MJgͥ5%m4SZr)eR9-]sasSrZK _W_*XXTJj:ſOY 3Z<1A}<!sjd +R[/|pe4t?a[QӅEt6{k>v|c|FS͈OÓ|QFrȏ'G!(!ÖF@hqendm6wo,]nUa䋩-t>5~lRpar*~(/KZV].w}Xu=vJ܆:(xnl8#::~bם: *J5K5_x*ވp[<>PfʣV3r* Aqq{h>D륀`PK!HX^F(pytest_serverless-0.3.0.dist-info/RECORDϻ0~%!"6"En(