本文整理汇总了Python中microcosm.api.create_object_graph函数的典型用法代码示例。如果您正苦于以下问题:Python create_object_graph函数的具体用法?Python create_object_graph怎么用?Python create_object_graph使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了create_object_graph函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_deferred_production_decorator
def test_deferred_production_decorator():
"""
Deferred production can be used to decorate a function
"""
def loader(metadata):
return dict(
sns_topic_arns=dict(
default="topic",
)
)
class Foo:
def __init__(self, graph):
self.graph = graph
self.sns_producer = graph.sns_producer
def bar(self):
assert isinstance(self.sns_producer, DeferredProducer)
self.sns_producer.produce(DerivedSchema.MEDIA_TYPE, data="data")
assert_that(graph.sns_producer.sns_client.publish.call_count, is_(equal_to(0)))
graph = create_object_graph("example", testing=True, loader=loader)
foo = Foo(graph)
func = deferred(foo)(foo.bar)
func()
assert_that(graph.sns_producer.sns_client.publish.call_count, is_(equal_to(1)))
示例2: test_publish_batch_with_no_topic_fails
def test_publish_batch_with_no_topic_fails():
"""
Require explicit configuration of a topic for batch messages.
"""
def loader(metadata):
return dict(
sns_topic_arns=dict(
default="topic",
)
)
graph = create_object_graph("example", testing=True, loader=loader)
graph.use("opaque")
# set up response
graph.sns_producer.sns_client.publish.return_value = dict(MessageId=MESSAGE_ID)
assert_that(
calling(graph.sns_producer.produce).with_args(
MessageBatchSchema.MEDIA_TYPE,
messages=[]
),
raises(TopicNotDefinedError)
)
示例3: test_basic_auth_default_realm
def test_basic_auth_default_realm():
"""
Basic auth uses the convention that the metadata's name is the realm.
"""
graph = create_object_graph(name="example", testing=True)
@graph.app.route("/unauthorized")
@graph.audit
@graph.basic_auth.required
def unauthorized():
raise Exception("Should not be raised!")
client = graph.app.test_client()
response = client.get("/unauthorized")
assert_that(response.status_code, is_(equal_to(401)))
data = loads(response.get_data().decode("utf-8"))
assert_that(data, is_(equal_to({
"code": 401,
"message": "The server could not verify that you are authorized to access the URL requested. "
"You either supplied the wrong credentials (e.g. a bad password), or your browser "
"doesn't understand how to supply the credentials required.",
"retryable": False,
"context": {"errors": []},
})))
assert_that(response.headers["WWW-Authenticate"], is_(equal_to('Basic realm="example"')))
示例4: test_basic_auth_custom_credentials
def test_basic_auth_custom_credentials():
"""
Basic auth default credentials work.
"""
config = dict(
basic_auth=dict(
credentials=dict(
username="password",
)
)
)
graph = create_object_graph(name="example", testing=True, loader=lambda metadata: config)
@graph.app.route("/ok")
@graph.audit
@graph.basic_auth.required
def unauthorized():
return "OK"
client = graph.app.test_client()
response = client.get("/ok", headers={
"Authorization": encode_basic_auth("username", "password"),
})
assert_that(response.status_code, is_(equal_to(200)))
示例5: setup
def setup(self):
self.graph = create_object_graph(name="example", testing=True, import_name="microcosm_postgres")
self.company_store = self.graph.company_store
self.context = SessionContext(self.graph)
self.context.recreate_all()
self.context.open()
示例6: test_ack
def test_ack():
"""
Consumer delegates to SQS client.
"""
def loader(metadata):
return dict(
sqs_consumer=dict(
sqs_queue_url=FOO_QUEUE_URL,
),
pubsub_message_codecs=dict(
default=FooSchema,
),
)
graph = create_object_graph("example", testing=True, loader=loader)
message = SQSMessage(
consumer=graph.sqs_consumer,
message_id=MESSAGE_ID,
receipt_handle=RECEIPT_HANDLE,
content=None,
)
message.ack()
graph.sqs_consumer.sqs_client.delete_message.assert_called_with(
QueueUrl='foo-queue-url',
ReceiptHandle=RECEIPT_HANDLE,
)
示例7: test_codec_sqs_envelope
def test_codec_sqs_envelope():
graph = create_object_graph("example", testing=True)
consumer = None
message_id = "message_id"
receipt_handle = "receipt_handle"
envelope = CodecSQSEnvelope(graph)
media_type = created("foo")
uri = "http://foo/id"
sqs_message = envelope.parse_raw_message(consumer, dict(
MessageId=message_id,
ReceiptHandle=receipt_handle,
Body=dumps(dict(
Message=dumps(dict(
mediaType=media_type,
foo="bar",
uri=uri,
)),
)),
))
assert_that(sqs_message.content, is_(equal_to(dict(
# NB: no foo key here because it's not part of the schema
media_type=media_type,
uri=uri,
))))
assert_that(sqs_message.media_type, is_(equal_to(media_type)))
assert_that(sqs_message.message_id, is_(equal_to(message_id)))
assert_that(sqs_message.receipt_handle, is_(equal_to(receipt_handle)))
示例8: test_configure_engine
def test_configure_engine():
"""
Engine factory should work with zero configuration.
"""
graph = create_object_graph(name="example", testing=True)
engine = graph.postgres
assert_that(engine, is_(instance_of(Engine)))
# engine has expected configuration
assert_that(
str(engine.url),
starts_with("postgresql://example:@"),
)
assert_that(
str(engine.url),
ends_with(":5432/example_test_db"),
)
# engine supports connections
with engine.connect() as connection:
row = connection.execute("SELECT 1;").fetchone()
assert_that(row[0], is_(equal_to(1)))
示例9: produce
def produce():
"""
Produce test messages.
"""
parser = ArgumentParser()
parser.add_argument("--count", default=1, type=int)
parser.add_argument("--message")
parser.add_argument("--message-type", default="test")
parser.add_argument("--topic-arn", required=True)
args = parser.parse_args()
def load_config(metadata):
return dict(
pubsub_message_codecs=dict(
default=SimpleSchema,
),
sns_topic_arns=dict(
default=args.topic_arn,
),
)
graph = create_object_graph("example", loader=load_config)
for _ in range(args.count):
message_id = graph.sns_producer.produce(
args.message_type,
message=args.message or uuid4().hex,
timestamp=time(),
)
print message_id # noqa
示例10: test_produce_custom_topic
def test_produce_custom_topic():
"""
Producer delegates to SNS client.
"""
def loader(metadata):
return dict(
pubsub_message_codecs=dict(
default=FooSchema,
),
sns_topic_arns=dict(
default=None,
mappings={
FOO_MEDIA_TYPE: FOO_TOPIC,
},
)
)
graph = create_object_graph("example", testing=True, loader=loader)
# set up response
graph.sns_producer.sns_client.publish.return_value = dict(MessageId=MESSAGE_ID)
message_id = graph.sns_producer.produce(FOO_MEDIA_TYPE, bar="baz")
assert_that(graph.sns_producer.sns_client.publish.call_count, is_(equal_to(1)))
assert_that(graph.sns_producer.sns_client.publish.call_args[1]["TopicArn"], is_(equal_to(FOO_TOPIC)))
assert_that(loads(graph.sns_producer.sns_client.publish.call_args[1]["Message"]), is_(equal_to({
"bar": "baz",
"mediaType": "application/vnd.globality.pubsub.foo",
})))
assert_that(message_id, is_(equal_to(MESSAGE_ID)))
示例11: test_configure_sessionmaker
def test_configure_sessionmaker():
"""
Should create the `SQLAlchemy` sessionmaker
"""
graph = create_object_graph(name="example", testing=True)
assert_that(graph.sessionmaker, is_(instance_of(sessionmaker)))
示例12: test_override_default_limit_from_request_header
def test_override_default_limit_from_request_header():
graph = create_object_graph(name="example", testing=True)
with graph.flask.test_request_context(headers={"X-Request-Limit": "2"}):
page = OffsetLimitPage.from_dict(dict(foo="bar"))
assert_that(page.offset, is_(equal_to(0)))
assert_that(page.limit, is_(equal_to(2)))
assert_that(page.kwargs, has_entry("foo", "bar"))
示例13: test_configure_flywheel_engine
def test_configure_flywheel_engine():
"""
Should create the `flywheel` engine
"""
graph = create_object_graph(name="example", testing=True, import_name="microcosm_dynamodb")
assert_that(graph.dynamodb, is_(instance_of(Engine)))
示例14: test_configure_flask
def test_configure_flask():
"""
Should create the `Flask` application.
"""
graph = create_object_graph(name="example", testing=True)
assert_that(graph.app, is_(instance_of(Flask)))
示例15: setup
def setup(self):
self.graph = create_object_graph(
name="example",
testing=True,
import_name="microcosm_postgres",
)
self.company_store = self.graph.company_store
self.companies = [
Company(
name="name1",
type=CompanyType.private,
),
Company(
name="name2",
type=CompanyType.private,
),
Company(
name="name3",
type=CompanyType.private,
),
]
with SessionContext(self.graph) as context:
context.recreate_all()