Category : mocking

Given a folder structure like such: dags/ **/ code.py tests/ dags/ **/ test_code.py conftest.py Where dags serves as the root of the src files, with ‘dags/a/b/c.py’ imported as ‘a.b.c’. I want to test the following function in code.py: from dag_common.connections import get_conn from utils.database import dbtypes def select_records( conn_id: str, sql: str, bindings, ): conn: ..

Read more

I’m writing a simple function to write out a dict to disk via some transformation and yaml. The easiest way to test what’s going on would be to capture the output of Path(file_name).write_text(text_to_write). But I’m having trouble understanding exactly what I need to do to capture this content. def test_path_write(): with mock.patch(‘Path.write_text’, return_value=None): function_that_uses_path_write_text() assert ..

Read more

I’m trying to test an api mock call using from unittest.mock import patch. I keep getting an AttributError when I include .json() in my function’s response return (i.e. return response.json()). When I exclude the .json() in my return statement, the test passes. I’m pretty new to testing and I can’t figure out how to get ..

Read more

I am trying to mock an AWS SQS with moto, below is my code from myClass import get_msg_from_sqs from moto import mock_sqs #from moto.sqs import mock_sqs @mock_sqs def test_get_all_msg_from_queue(): #Tried this #from myClass import get_msg_from_sqs conn = boto3.client(‘sqs’, region_name=’us-east-1′) queue = conn.create_queue(QueueName=’Test’) os.environ["SQS_URL"] = queue["QueueUrl"] queue.send_message( MessageBody=json.dumps({‘a’: ‘1’, ‘b’: ‘2’, ‘c’: ‘3’})) #Tried this #conn.send_message(QueueUrl=queue["QueueUrl"], ..

Read more

There is a django manager command that handles the csv file. app/my_app/my_command.py class Command(BaseCommand): def handle(self, *args, **options): path = (os.path.join(os.path.abspath(os.path.dirname(__name__)), ‘data.csv’)) # other logic with file I am writing a test for it in pytest, the problem is that I can not understand how to mock the variable path to test accessed not the ..

Read more

I am trying to mock an AWS lambda function, below is my sample code def get_lambda_resp(arn: str, input: str) -> str: lambda_client = boto3.client("lambda") response = lambda_client.invoke( FunctionName=arn, LogType="None", Payload=json.dumps({"param": input}).encode("utf-8") ) output = json.loads(response["Payload"].read().decode("utf-8")) return output["value"] and below is my test case import io import zipfile import boto3 from moto import mock_lambda @mock_lambda def ..

Read more