我正在使用模拟 dynamodb 来测试在我的单元测试中记录是否已保存到 dynamoDB,但出现错误:
'_patch' object has no attribute 'is_local'
这是我的代码:
@mock_dynamodb2
class TestDatabaseFunctions(unittest.TestCase):
def setUp(self):
"""Create the mock database and table"""
os.environ["TableName"] = table_name
self.dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
self.table = create_table(self.dynamodb)
self.is_local = True
self.temp_original = None
def tearDown(self):
"""Delete mock database and table after test is run"""
self.table.delete()
self.dynamodb = None
def test_table_exists(self):
self.assertTrue(self.table) # check if we got a result
self.assertIn('MOCK_DATA_TABLE', self.table.name) # check if the table name is 'Movies'
def test_save_records(self):
from src.handler import save_records_to_dynamodb
try:
mocked_stream_records = create_mock_records()
save_records_to_dynamodb(mocked_stream_records)
result = self.table.get_item(Key={'xx': '2021-11-28', 'unique_id': 'xxx'})
print(result)
self.assertEqual('2021-11-28', result['Item']['xx'])
except AttributeError as e:
print(e)