1

I am trying to test the data being written to RDS, but I can't seem to be able to mock the DB. The idea is to mock a DB, then run my code and retrieve the data for testing. Could anyone help, please?

import unittest
import boto3
import mock
from moto import mock_s3, mock_rds
from sqlalchemy import create_engine

@mock_s3
@mock_rds
class TestData(unittest.TestCase):
    def setUp(self):
        """Initial setup."""
        # Setup db

        test_instances = db_conn.create_db_instance(
            DBName='test_db',
            AllocatedStorage=10,
            StorageType='standard',
            DBInstanceIdentifier='instance',
            DBInstanceClass='db.t2.micro',
            Engine='postgres',
            MasterUsername='postgres_user',
            MasterUserPassword='p$ssw$rd',
            AvailabilityZone='us-east-1',
            PubliclyAccessible=True,
            DBSecurityGroups=["my_sg"],
            VpcSecurityGroupIds=["sg-123456"],
            Port=5432
        )
        db_instance = test_instances["DBInstance"]

        user_name = db_instance['MasterUsername']
        host = db_instance['Endpoint']['Address']
        port = db_instance['Endpoint']['Port']
        db_name = db_instance['DBName']
        conn_str = f'postgresql://{user_name}:p$ssw$rd@{host}:{port}/{db_name}'
        print(conn_str)
        engine_con = create_engine(conn_str)
        engine_con.connect()

Error:

>       conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
E       sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "instance.aaaaaaaaaa.eu-west-1.rds.amazonaws.com" to address: nodename nor servname provided, or not known
E       
E       (Background on this error at: https://sqlalche.me/e/14/e3q8)

Miss.Saturn
  • 155
  • 2
  • 13

1 Answers1

1

So, instead of testing the data from my DB, I replicated the execution of the code I had in my lambda on my test, accessing the results locally. So those same tests are working fine on Github now.

Miss.Saturn
  • 155
  • 2
  • 13