我正在使用 moto 和 Python 3.7 来模拟一些 S3 交互。如果所有模拟代码都包含在测试方法中,那么 moto 似乎可以正常工作。当我将一些初步代码移动到 时setUp(),测试失败了,好像setUp()从未运行过一样。
import unittest
import boto3
from moto import mock_s3
class BucketFacadeTests(unittest.TestCase):
@mock_s3
def setUp(self):
print('setUp called')
s3 = boto3.resource('s3', region_name='us-east-1')
s3.create_bucket(Bucket='bucket')
key = 'a/b/c/d.txt'
object = s3.Object('bucket', key)
object.put(Body='my dog has fleas')
def do_test(self):
s3 = boto3.resource('s3', region_name='us-east-1')
the_object = s3.Object('bucket', 'a/b/c/d.txt')
string_data = the_object.get()['Body'].read().decode('utf-8')
self.assertEqual('my dog has fleas', string_data)
@mock_s3
def test_bucket_can_be_accessed_with_setup(self):
self.do_test()
@mock_s3
def test_bucket_can_be_accessed_without_setup(self):
# This does what setUp() should
s3 = boto3.resource('s3', …Run Code Online (Sandbox Code Playgroud) 我正在尝试git replace并犯了一个错误。现在我已经无法回到正常的状态了。任何重要的 git 命令都会产生错误fatal: replace depth too high for object b769532341677b7c34b5adeb85a173daa0ced852。 git reset --hard并git checkout <another_branch>产生这个错误。
我不在乎结果replace,我只想恢复正常。我该怎么做呢?
我创建了一个新的Grails 3.1.4角度项目以及一些域对象和控制器扩展RestfulController.我在下面创建了集成测试.当我跑步时,grails test-app -integration我得到了错误
java.lang.IllegalStateException: No transactionManager was specified. Using @Transactional or @Rollback requires a valid configured transaction manager. If you are running in a unit test ensure the test has been properly configured and that you run the test suite not an individual test method.
at grails.transaction.GrailsTransactionTemplate.<init>(GrailsTransactionTemplate.groovy:60)
at com.waldoware.invoicer.BillingEntityRestControllerIntegrationSpec.$tt__$spock_feature_0_0(BillingEntityRestControllerIntegrationSpec.groovy:29)
at com.waldoware.invoicer.BillingEntityRestControllerIntegrationSpec.test all entities_closure2(BillingEntityRestControllerIntegrationSpec.groovy)
at groovy.lang.Closure.call(Closure.java:426)
at groovy.lang.Closure.call(Closure.java:442)
at grails.transaction.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:70)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:67)
at com.waldoware.invoicer.BillingEntityRestControllerIntegrationSpec.test all entities(BillingEntityRestControllerIntegrationSpec.groovy)
Run Code Online (Sandbox Code Playgroud)
测试类:
package com.waldoware.invoicer
import grails.test.mixin.integration.Integration
import grails.transaction.*
import …Run Code Online (Sandbox Code Playgroud) 我正在尝试对一些导入模块的 Python 3 代码进行单元测试。不幸的是,模块的编写方式,简单地导入它会产生令人不快的副作用,这对测试来说并不重要。我试图用unitest.mock.patch它来解决它,但运气不佳。
这是说明性示例的结构:
.
??? work
??? __init__.py
??? test_work.py
??? work.py
??? work_caller.py
Run Code Online (Sandbox Code Playgroud)
__init__.py 是一个空文件
.
??? work
??? __init__.py
??? test_work.py
??? work.py
??? work_caller.py
Run Code Online (Sandbox Code Playgroud)
import os
def work_on():
path = os.getcwd()
print(f"Working on {path}")
return path
def unpleasant_side_effect():
print("I am an unpleasant side effect of importing this module")
# Note that this is called simply by importing this file
unpleasant_side_effect()
Run Code Online (Sandbox Code Playgroud)
from work.work import work_on
class WorkCaller:
def call_work(self):
# Do …Run Code Online (Sandbox Code Playgroud) 我有一些创建 AWS Aurora RDS 集群的 Terraform 代码:
resource "aws_rds_cluster" "default" {
provider = aws.customer
cluster_identifier = "my_id"
engine = "aurora-mysql"
engine_version = "5.7.mysql_aurora.2.03.2"
database_name = var.db_name
port = var.db_port
master_username = var.db_master_username
master_password = random_password.sqlpassword.result
backup_retention_period = 5
preferred_backup_window = "07:00-09:00"
skip_final_snapshot = true
db_subnet_group_name = aws_db_subnet_group.default.name
vpc_security_group_ids = [aws_security_group.rds.id]
deletion_protection = true
}
Run Code Online (Sandbox Code Playgroud)
这段代码已经工作正常有一段时间了,直到最近因此terraform apply错误而失败Error: Failed to modify RDS Cluster (my_id): InvalidParameterCombination: Cannot upgrade aurora-mysql from 5.7.mysql_aurora.2.07.2 to 5.7.mysql_aurora.2.03.2
长话短说,AWS 在维护时段升级了次要版本号,并拒绝允许 Terraform 降级数据库。我同意 AWS 这样做,但我不想每次发生这种情况时都必须提交新的 Terraform …
amazon-web-services terraform amazon-aurora terraform-provider-aws
unit-testing ×2
boto3 ×1
eclipse ×1
git ×1
grails ×1
grails-3.0 ×1
grails-3.1 ×1
java ×1
macos ×1
moto ×1
python ×1
spock ×1
terraform ×1