如何将 Google Cloud Firestore 本地模拟器用于 python 和测试目的

Posted

技术标签:

【中文标题】如何将 Google Cloud Firestore 本地模拟器用于 python 和测试目的【英文标题】:How to use Google Cloud Firestore local emulator for python and for testing purpose 【发布时间】:2019-07-18 23:07:44 【问题描述】:

我试图了解如何将 firestore 本地模拟器用于 python 和测试目的。但我找不到操作方法文档。

有人可以帮我吗?

【问题讨论】:

【参考方案1】:

使用firebase_admin 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 实例。

npx firebase setup:emulators:firestore
npx firebase --token $FIREBASE_TOKEN emulators:start --only firestore --project $PROJECT_KEY

最后,在已实例化的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 Carterfiguring this out on the gcloud internal api

【讨论】:

注意:grpc 在多处理环境中不起作用。见github.com/googleapis/google-cloud-python/issues/… 哈,我刚刚偶然发现了这个答案:) 对于任何希望绕过 grpc 多处理限制的人,请考虑将 multiprocessing.managers.BaseManager 作为外观扩展到进程安全的单例 grpc 实例。【参考方案2】:

欢迎来到 SO :)

Cloud Firestore 模拟器(目前)的主要目的似乎是测试安全规则,如 here 所记录的那样。 This section 声明:“目前唯一支持模拟器的 SDK 是 Node.js SDK。”

令人困惑的是,还有用于 Google Cloud 客户端库的 these Ruby 文档。 Python 中似乎还没有同样的功能。

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


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

使用Choosing between Native Mode and Datastore Mode 页面来决定您要采取的方向。如果您觉得需要额外的“本机模式”功能,那么直接连接到云中的真实 Firestore 实例可能是最简单的方法。

【讨论】:

“Datastore 模式下的 Cloud Firestore”听起来一点也不像 Firestore——它不提供实时更新或离线持久性。 “数据存储模式”优于“本机模式”的唯一优势似乎是它每秒可以处理更多的写入。 @aldel 当您说 Datastore 模式下的 Cloud Firestore“不提供离线持久性”时,不确定您的意思。 Datastore 模式下的 Cloud Firestore(以前称为 Cloud Datastore)早在 Firestore 之前就已经存在,因此您应该将其视为已融入 Firestore 的遗留产品。其优势在于,为与 Datastore 一起使用而构建的旧应用程序无需修改即可在 Datastore 模式下切换到 Cloud Firestore,并利用 Firestore 的大部分优势(实时功能除外)。 我指的是上面链接的“在本机模式和数据存储模式之间选择”页面中的功能比较表。它说在数据存储模式下“离线数据持久性”是“不支持”。我相信它指的是移动和网络客户端在与网络断开连接时继续允许本地更新的能力。 我认为将新版本的 Datastore 重命名为“Datastore 模式下的 Cloud Firestore”是一个品牌错误。自从 Firestore 发布以来,我一直认为它基本上是一个实时数据库(它被宣传为 Firebase RTDB 的更复杂的替代品,而且 Firebase 品牌从一开始就与实时更新密切相关)。我花了很长时间才明白“数据存储模式”不做实时更新。 我什至不清楚是否有 是原生模式下的 Firestore 模拟器(我认为有,但不知何故它只适用于 javascript 客户端库)。 【参考方案3】:

目前,只需设置这两个环境变量,即可将firebase_admin 的任何SDK 连接到firebase 模拟器。我已经在 Python SDK 上亲自测试过它,它就像一个魅力。

export FIRESTORE_EMULATOR_HOST="localhost:8080"
export GCLOUD_PROJECT="any-valid-name"

Documentation Link

【讨论】:

我只是在尝试编写文档时对此进行了测试,但没有运气。当我设置 FIRESTORE_EMULATOR_HOST="localhost:8080" 时,set-promise 永远不会解决,导致错误“503 无法连接到所有地址”。 您在运行测试之前是否启动了模拟器? 是的,它正在运行,我检查了 firebase.json 中设置的端口是否正确 任何指向最小可重现示例的链接?我想深入了解一下。 不,抱歉。更多详情请看这里:***.com/questions/68272114/…

以上是关于如何将 Google Cloud Firestore 本地模拟器用于 python 和测试目的的主要内容,如果未能解决你的问题,请参考以下文章

具有 Google Cloud Functions 的 Google Cloud Endpoints [关闭]

如何将公共数据集导入 Google Cloud Bucket

如何使用 OpenTelemetry 将 Prometheus 导出到 Google Cloud Monitoring?

如何更改 Google Compute Engine 服务帐户的范围以将数据写入 Google Cloud Storage 存储桶?

我如何将变量传递给Google Cloud函数

如何从 google.cloud.monitoring_v3 将参数传递给 list_time_series 方法?