如何使用googlecloudfirestore本地模拟器for python和测试purpos

2024-04-26 10:25:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图了解如何使用firestorelocalemulator for python和进行测试。但我找不到如何记录。在

有人能帮帮我吗?在


Tags: for记录帮帮我firestorelocalemulator
4条回答

使用^{}python模块,遵循Cloud Firestore Docs中记录的标准设置

这将涉及使用credentials上下文调用initialize_app,然后使用firestore.client()创建一个传统的Firestore客户机

例如:

from firebase_admin import credentials, firestore, initialize_app

firebase_credentials_file_path = ...
cred = credentials.Certificate(firebase_credentials_file_path)
initialize_app(cred)
db = firestore.client()

接下来,您需要安装并运行Firestore Emulator,它将在localhost:8080上托管本地Firestore实例。在

^{pr2}$

最后,在已经实例化的firestore.client实例中注入重定向,以使用不安全的GRPC通道与本地仿真器主机/端口交互:

import grpc
from google.cloud.firestore_v1.gapic import firestore_client
from google.cloud.firestore_v1.gapic.transports import firestore_grpc_transport

channel = grpc.insecure_channel("localhost:8080")
transport = firestore_grpc_transport.FirestoreGrpcTransport(channel=channel)
db._firestore_api_internal = firestore_client.FirestoreClient(transport=transport)

现在,db对象将与本地仿真器交互,不会出现任何问题。在

John Carter致谢figuring this out on the gcloud internal api

欢迎使用SO:)

cloudfirestore仿真器(目前)的主要目的似乎是测试安全规则,如文档所述hereThis section状态:“当前唯一支持模拟器的SDK是节点.jsSDK。“

令人困惑的是,Google云客户端库还有theseRuby文档。在Python中似乎还没有同样的功能。在

Here是作为googlecloudsdk的一部分运行模拟器的说明。


考虑在数据存储模式下使用Cloud Firestore,它有更好的工具(它可能只是有更多的时间成熟)。您可以在Running the Datastore mode Emulator页上找到运行其仿真器的说明。在

使用Choosing between Native Mode and Datastore Mode页来决定要走哪个方向。如果您觉得需要额外的“本机模式”特性,那么直接连接到云中的一个真正的Firestore实例可能是最容易的。在

相关问题 更多 >